2017-03-04 03:41:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2017 Sinuhé Téllez Rivera
|
|
|
|
|
|
|
|
dir2opds is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
dir2opds is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with dir2opds. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-24 00:11:58 +00:00
|
|
|
"bufio"
|
2017-03-04 03:41:48 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"flag"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
2017-03-24 00:11:58 +00:00
|
|
|
"net/url"
|
2017-03-04 03:41:48 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-24 00:11:58 +00:00
|
|
|
"time"
|
2017-03-04 03:41:48 +00:00
|
|
|
|
|
|
|
"golang.org/x/tools/blog/atom"
|
|
|
|
)
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
type AcquisitionFeed struct {
|
|
|
|
*atom.Feed
|
|
|
|
Dc string `xml:"xmlns:dc,attr"`
|
|
|
|
Opds string `xml:"xmlns:opds,attr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CatalogFeed atom.Feed
|
|
|
|
|
|
|
|
var (
|
|
|
|
port,
|
|
|
|
dirRoot,
|
|
|
|
author,
|
|
|
|
authorUri,
|
|
|
|
authorEmail string
|
|
|
|
updated atom.TimeStr
|
|
|
|
)
|
2017-03-04 03:41:48 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
mime.AddExtensionType(".mobi", "application/x-mobipocket-ebook")
|
|
|
|
mime.AddExtensionType(".epub", "application/epub+zip")
|
|
|
|
mime.AddExtensionType(".fb2", "txt/xml")
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
flag.StringVar(&port, "port", "8080", "The server will listen in this port")
|
|
|
|
flag.StringVar(&dirRoot, "dir", "./books", "A directory with books")
|
|
|
|
flag.StringVar(&author, "author", "", "The author of the feed")
|
|
|
|
flag.StringVar(&authorUri, "uri", "", "The author uri")
|
|
|
|
flag.StringVar(&authorEmail, "email", "", "The author email")
|
2017-03-04 03:41:48 +00:00
|
|
|
flag.Parse()
|
2017-03-24 00:11:58 +00:00
|
|
|
updated = atom.Time(time.Now())
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", errorHandler(func(w http.ResponseWriter, req *http.Request) error {
|
|
|
|
dirPath := filepath.Join(dirRoot, req.URL.Path)
|
|
|
|
fi, err := os.Stat(dirPath)
|
2017-03-04 03:41:48 +00:00
|
|
|
if err != nil {
|
2017-03-24 00:11:58 +00:00
|
|
|
return err
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
w.Write([]byte(xml.Header))
|
|
|
|
return writeFeedTo(w, req.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeFileTo(w, dirPath)
|
|
|
|
}))
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
func writeFeedTo(w io.Writer, u *url.URL) error {
|
|
|
|
isAcquisition, err := isAcquisitionFeed(filepath.Join(dirRoot, u.Path))
|
2017-03-04 03:41:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
if isAcquisition {
|
|
|
|
return writeAcquisitionFeed(w, u)
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
return writeCatalogFeed(w, u)
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
func isAcquisitionFeed(p string) (bool, error) {
|
|
|
|
fis, err := ioutil.ReadDir(p)
|
2017-03-04 03:41:48 +00:00
|
|
|
if err != nil {
|
2017-03-24 00:11:58 +00:00
|
|
|
return false, err
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
for _, fi := range fis {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return true, nil
|
|
|
|
}
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeCatalogFeed(w io.Writer, u *url.URL) error {
|
|
|
|
//TODO: Do I am root
|
|
|
|
feed := &CatalogFeed{ID: u.Path, Title: "Catalog feed in " + u.Path}
|
|
|
|
feed.Author = &atom.Person{Name: author, Email: authorEmail, URI: authorUri}
|
|
|
|
feed.Updated = updated
|
2017-03-24 03:26:04 +00:00
|
|
|
feed.Link = []atom.Link{{
|
|
|
|
Rel: "start",
|
|
|
|
Href: "/",
|
|
|
|
Type: "application/atom+xml;profile=opds-catalog;kind=navigation",
|
|
|
|
}}
|
2017-03-24 00:11:58 +00:00
|
|
|
|
|
|
|
abs_path := filepath.Join(dirRoot, u.Path)
|
|
|
|
fis, err := ioutil.ReadDir(abs_path)
|
2017-03-04 03:41:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
for _, fi := range fis {
|
|
|
|
link := atom.Link{
|
2017-03-24 03:26:04 +00:00
|
|
|
Rel: "subsection",
|
2017-03-24 00:11:58 +00:00
|
|
|
Title: fi.Name(),
|
|
|
|
Href: filepath.Join(u.EscapedPath(), url.PathEscape(fi.Name())),
|
2017-03-24 03:26:04 +00:00
|
|
|
Type: "application/atom+xml;profile=opds-catalog;kind=acquisition",
|
2017-03-24 00:11:58 +00:00
|
|
|
}
|
|
|
|
entry := &atom.Entry{
|
2017-03-24 03:26:04 +00:00
|
|
|
ID: filepath.Join(u.Path, fi.Name()),
|
|
|
|
Title: fi.Name(),
|
|
|
|
Updated: updated,
|
|
|
|
Published: updated,
|
|
|
|
Link: []atom.Link{link},
|
2017-03-24 00:11:58 +00:00
|
|
|
}
|
|
|
|
feed.Entry = append(feed.Entry, entry)
|
2017-03-04 03:41:48 +00:00
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enc := xml.NewEncoder(w)
|
|
|
|
enc.Indent(" ", " ")
|
|
|
|
enc.Encode(feed)
|
|
|
|
return nil
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
func writeAcquisitionFeed(w io.Writer, u *url.URL) error {
|
|
|
|
f := &atom.Feed{}
|
|
|
|
feed := &AcquisitionFeed{f, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"}
|
|
|
|
feed.ID = u.Path
|
|
|
|
feed.Updated = updated
|
|
|
|
feed.Title = filepath.Base(u.Path)
|
|
|
|
feed.Author = &atom.Person{Name: author, Email: authorEmail, URI: authorUri}
|
2017-03-24 03:26:04 +00:00
|
|
|
feed.Link = []atom.Link{{
|
|
|
|
Rel: "start",
|
|
|
|
Href: "/",
|
|
|
|
Type: "application/atom+xml;profile=opds-catalog;kind=navigation",
|
|
|
|
}}
|
2017-03-24 00:11:58 +00:00
|
|
|
|
|
|
|
abs_path := filepath.Join(dirRoot, u.Path)
|
|
|
|
fis, err := ioutil.ReadDir(abs_path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
entry := &atom.Entry{
|
2017-03-24 03:26:04 +00:00
|
|
|
ID: u.Path,
|
|
|
|
Title: filepath.Base(u.Path),
|
|
|
|
Updated: updated,
|
|
|
|
Published: updated,
|
2017-03-24 00:11:58 +00:00
|
|
|
}
|
2017-03-04 03:41:48 +00:00
|
|
|
for _, fi := range fis {
|
2017-03-24 00:11:58 +00:00
|
|
|
ext := filepath.Ext(fi.Name())
|
2017-03-24 03:26:04 +00:00
|
|
|
mime_type := mime.TypeByExtension(ext)
|
2017-03-24 00:11:58 +00:00
|
|
|
var rel string
|
2017-03-24 03:26:04 +00:00
|
|
|
if rel = "http://opds-spec.org/acquisition"; ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gift" {
|
|
|
|
rel = "http://opds-spec.org/image/thumbnail"
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
link := atom.Link{
|
2017-03-24 03:26:04 +00:00
|
|
|
Rel: rel,
|
2017-03-24 00:11:58 +00:00
|
|
|
Type: mime_type,
|
2017-03-24 03:26:04 +00:00
|
|
|
Href: filepath.Join(u.EscapedPath(), url.PathEscape(fi.Name())),
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
entry.Link = append(entry.Link, link)
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
feed.Entry = append(feed.Entry, entry)
|
2017-03-04 03:41:48 +00:00
|
|
|
|
|
|
|
enc := xml.NewEncoder(w)
|
|
|
|
enc.Indent(" ", " ")
|
2017-03-24 00:11:58 +00:00
|
|
|
enc.Encode(feed)
|
|
|
|
return nil
|
2017-03-04 03:41:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFileTo(w io.Writer, filepath string) error {
|
|
|
|
f, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-24 00:11:58 +00:00
|
|
|
defer f.Close()
|
|
|
|
r := bufio.NewReader(f)
|
|
|
|
r.WriteTo(w)
|
2017-03-04 03:41:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-24 00:11:58 +00:00
|
|
|
func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := f(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Printf("handling %q: %v", r.RequestURI, err)
|
2017-03-04 03:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|