dir2opds/main.go

185 lines
4.6 KiB
Go
Raw Normal View History

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-05-11 23:06:21 +00:00
"bytes"
2017-03-04 03:41:48 +00:00
"encoding/xml"
"flag"
2017-05-11 23:06:21 +00:00
"golang.org/x/tools/blog/atom"
2017-03-04 03:41:48 +00:00
"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"
2017-05-11 23:06:21 +00:00
2017-03-04 03:41:48 +00:00
"path/filepath"
2017-03-24 00:11:58 +00:00
"time"
2017-03-04 03:41:48 +00:00
)
2017-03-24 00:11:58 +00:00
var (
port,
dirRoot,
author,
authorUri,
authorEmail string
)
2017-03-04 03:41:48 +00:00
2017-05-11 23:06:21 +00:00
const acquisitionType = "application/atom+xml;profile=opds-catalog;kind=acquisition"
const navegationType = "application/atom+xml;profile=opds-catalog;kind=navigation"
type AcquisitionFeed struct {
*atom.Feed
Dc string `xml:"xmlns:dc,attr"`
Opds string `xml:"xmlns:opds,attr"`
}
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-05-11 23:06:21 +00:00
func handler(w http.ResponseWriter, req *http.Request) error {
fpath := filepath.Join(dirRoot, req.URL.Path)
fi, err := os.Stat(fpath)
if err != nil {
return err
}
if fi.IsDir() {
content, err := getContent(w, req, fpath)
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-05-11 23:06:21 +00:00
http.ServeContent(w, req, "feed.xml", time.Now(), bytes.NewReader(content))
} else {
http.ServeFile(w, req, fpath)
}
2017-03-24 00:11:58 +00:00
2017-05-11 23:06:21 +00:00
return nil
}
func main() {
flag.StringVar(&port, "port", "8080", "The server will listen in this port")
flag.StringVar(&dirRoot, "dir", "./books", "A directory with books")
flag.StringVar(&author, "serveFeedauthor", "", "The feed's author")
flag.StringVar(&authorUri, "uri", "", "The feed's author uri")
flag.StringVar(&authorEmail, "email", "", "The feed's author email")
flag.Parse()
2017-03-24 00:11:58 +00:00
2017-05-11 23:06:21 +00:00
http.HandleFunc("/", errorHandler(handler))
2017-03-24 00:11:58 +00:00
log.Fatal(http.ListenAndServe(":"+port, nil))
2017-03-04 03:41:48 +00:00
}
2017-05-11 23:06:21 +00:00
func getContent(w http.ResponseWriter, req *http.Request, dirpath string) (result []byte, err error) {
feed := makeFeed(dirpath, req)
if isAcquisition(dirpath) {
acFeed := &AcquisitionFeed{&feed, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"}
result, err = xml.MarshalIndent(acFeed, " ", " ")
2017-03-04 03:41:48 +00:00
2017-05-11 23:06:21 +00:00
} else {
result, err = xml.MarshalIndent(feed, " ", " ")
2017-03-04 03:41:48 +00:00
}
2017-05-11 23:06:21 +00:00
return
2017-03-24 00:11:58 +00:00
}
2017-05-11 23:06:21 +00:00
func makeFeed(dirpath string, req *http.Request) atom.Feed {
feedBuilder := FeedBuilder.
Id(req.URL.Path).
Title("Catalog in " + req.URL.Path).
Author(AuthorBuilder.Name(author).Email(authorEmail).URI(authorUri).Build()).
Updated(time.Now()).
AddLink(LinkBuilder.Rel("start").Href("/").Type(navegationType).Build())
2017-03-24 04:30:57 +00:00
2017-05-11 23:06:21 +00:00
fis, _ := ioutil.ReadDir(dirpath)
2017-03-24 00:11:58 +00:00
for _, fi := range fis {
2017-05-11 23:06:21 +00:00
linkIsAcquisition := isAcquisition(filepath.Join(dirpath, fi.Name()))
feedBuilder = feedBuilder.
AddEntry(EntryBuilder.
Id(req.URL.Path + fi.Name()).
Title(fi.Name()).
Updated(time.Now()).
Published(time.Now()).
AddLink(LinkBuilder.
Rel(getRel(fi.Name(), linkIsAcquisition)).
Title(fi.Name()).
Href(getHref(req, fi.Name())).
Type(getType(fi.Name(), linkIsAcquisition)).
Build()).
Build())
2017-03-24 00:11:58 +00:00
}
2017-05-11 23:06:21 +00:00
return feedBuilder.Build()
2017-03-04 03:41:48 +00:00
}
2017-05-11 23:06:21 +00:00
func getRel(name string, acquisition bool) (rel string) {
rel = "subsection"
if !acquisition {
return
2017-03-24 00:11:58 +00:00
}
2017-05-11 23:06:21 +00:00
ext := filepath.Ext(name)
if rel = "http://opds-spec.org/acquisition"; ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gift" {
rel = "http://opds-spec.org/image/thumbnail"
2017-03-24 00:11:58 +00:00
}
2017-05-11 23:06:21 +00:00
return
}
func getType(name string, acquisition bool) (linkType string) {
linkType = acquisitionType
if !acquisition {
return
2017-03-04 03:41:48 +00:00
}
2017-05-11 23:06:21 +00:00
ext := filepath.Ext(name)
linkType = mime.TypeByExtension(ext)
return
}
2017-03-04 03:41:48 +00:00
2017-05-11 23:06:21 +00:00
func getHref(req *http.Request, name string) string {
return filepath.Join(req.URL.EscapedPath(), url.PathEscape(name))
2017-03-04 03:41:48 +00:00
}
2017-05-11 23:06:21 +00:00
func isAcquisition(dirpath string) bool {
fi, _ := os.Stat(dirpath)
if !fi.IsDir() {
return false
}
2017-03-04 03:41:48 +00:00
2017-05-11 23:06:21 +00:00
fis, _ := ioutil.ReadDir(dirpath)
for _, fi := range fis {
if !fi.IsDir() {
return true
}
2017-03-04 03:41:48 +00:00
}
2017-05-11 23:06:21 +00:00
return false
2017-03-04 03:41:48 +00:00
}
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
}
}
}