dir2opds/main.go

208 lines
5.3 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"
"fmt"
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"
"path/filepath"
2017-03-24 00:11:58 +00:00
"time"
2018-01-14 03:19:54 +00:00
"github.com/dubyte/dir2opds/opds"
2018-01-14 03:19:54 +00:00
"golang.org/x/tools/blog/atom"
2017-03-04 03:41:48 +00:00
)
2017-03-24 00:11:58 +00:00
var (
port = flag.String("port", "8080", "The server will listen in this port")
host = flag.String("host", "0.0.0.0", "The server will listen in this host")
dirRoot = flag.String("dir", "./books", "A directory with books")
author = flag.String("author", "", "The server Feed author")
authorURI = flag.String("uri", "", "The feed's author uri")
authorEmail = flag.String("email", "", "The feed's author email")
debug = flag.Bool("debug", false, "If it is set it will log the requests")
2017-03-24 00:11:58 +00:00
)
2017-03-04 03:41:48 +00:00
2018-03-03 20:12:41 +00:00
type acquisitionFeed struct {
2017-05-11 23:06:21 +00:00
*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(".cbz", "application/x-cbz")
_ = mime.AddExtensionType(".cbr", "application/x-cbr")
_ = mime.AddExtensionType(".fb2", "text/fb2+xml")
2017-03-04 03:41:48 +00:00
}
2018-03-03 20:12:41 +00:00
func main() {
2018-03-03 20:12:41 +00:00
flag.Parse()
2020-05-15 18:00:27 +00:00
if !*debug {
log.SetOutput(ioutil.Discard)
}
fmt.Println(startValues())
2018-03-03 20:12:41 +00:00
http.HandleFunc("/", errorHandler(handler))
log.Fatal(http.ListenAndServe(*host+":"+*port, nil))
}
func startValues() string {
result := fmt.Sprintf("listening in: %s:%s", *host, *port)
return result
2018-03-03 20:12:41 +00:00
}
2017-05-11 23:06:21 +00:00
func handler(w http.ResponseWriter, req *http.Request) error {
fpath := filepath.Join(*dirRoot, req.URL.Path)
2020-05-15 18:04:52 +00:00
log.Printf("fpath:'%s'", fpath)
2020-05-15 18:04:52 +00:00
2017-05-11 23:06:21 +00:00
fi, err := os.Stat(fpath)
if err != nil {
return err
}
2018-03-03 20:12:41 +00:00
if isFile(fi) {
2017-05-11 23:06:21 +00:00
http.ServeFile(w, req, fpath)
2018-03-03 20:12:41 +00:00
return nil
2017-05-11 23:06:21 +00:00
}
2017-03-24 00:11:58 +00:00
2018-03-03 20:12:41 +00:00
content, err := getContent(req, fpath)
if err != nil {
return err
}
2017-03-24 00:11:58 +00:00
2019-03-10 08:06:37 +00:00
content = append([]byte(xml.Header), content...)
2018-03-03 20:12:41 +00:00
http.ServeContent(w, req, "feed.xml", time.Now(), bytes.NewReader(content))
return nil
2017-03-04 03:41:48 +00:00
}
2018-01-14 03:19:54 +00:00
func getContent(req *http.Request, dirpath string) (result []byte, err error) {
2017-05-11 23:06:21 +00:00
feed := makeFeed(dirpath, req)
2019-03-10 08:06:37 +00:00
if getPathType(dirpath) == pathTypeDirOfFiles {
2018-03-03 20:12:41 +00:00
acFeed := &acquisitionFeed{&feed, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"}
2017-05-11 23:06:21 +00:00
result, err = xml.MarshalIndent(acFeed, " ", " ")
} 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
}
2020-05-15 18:04:52 +00:00
const navigationType = "application/atom+xml;profile=opds-catalog;kind=navigation"
2019-03-10 08:06:37 +00:00
2017-05-11 23:06:21 +00:00
func makeFeed(dirpath string, req *http.Request) atom.Feed {
feedBuilder := opds.FeedBuilder.
2018-03-03 20:12:41 +00:00
ID(req.URL.Path).
2017-05-11 23:06:21 +00:00
Title("Catalog in " + req.URL.Path).
Author(opds.AuthorBuilder.Name(*author).Email(*authorEmail).URI(*authorURI).Build()).
2017-05-11 23:06:21 +00:00
Updated(time.Now()).
2020-05-15 18:04:52 +00:00
AddLink(opds.LinkBuilder.Rel("start").Href("/").Type(navigationType).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 {
2019-03-10 08:06:37 +00:00
pathType := getPathType(filepath.Join(dirpath, fi.Name()))
2017-05-11 23:06:21 +00:00
feedBuilder = feedBuilder.
AddEntry(opds.EntryBuilder.
2018-03-03 20:12:41 +00:00
ID(req.URL.Path + fi.Name()).
2017-05-11 23:06:21 +00:00
Title(fi.Name()).
Updated(time.Now()).
Published(time.Now()).
AddLink(opds.LinkBuilder.
2019-03-10 08:06:37 +00:00
Rel(getRel(fi.Name(), pathType)).
2017-05-11 23:06:21 +00:00
Title(fi.Name()).
Href(getHref(req, fi.Name())).
2019-03-10 08:06:37 +00:00
Type(getType(fi.Name(), pathType)).
2017-05-11 23:06:21 +00:00
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
}
2019-03-10 08:06:37 +00:00
func getRel(name string, pathType int) string {
if pathType == pathTypeDirOfFiles || pathType == pathTypeDirOfDirs {
return "subsection"
2017-03-24 00:11:58 +00:00
}
2019-03-10 08:06:37 +00:00
2017-05-11 23:06:21 +00:00
ext := filepath.Ext(name)
2019-03-10 08:06:37 +00:00
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
return "http://opds-spec.org/image/thumbnail"
2017-03-24 00:11:58 +00:00
}
2019-03-10 08:06:37 +00:00
// mobi, epub, etc
return "http://opds-spec.org/acquisition"
2017-05-11 23:06:21 +00:00
}
2019-03-10 08:06:37 +00:00
func getType(name string, pathType int) string {
if pathType == pathTypeFile {
return mime.TypeByExtension(filepath.Ext(name))
2017-03-04 03:41:48 +00:00
}
2019-03-10 08:06:37 +00:00
return "application/atom+xml;profile=opds-catalog;kind=acquisition"
2017-05-11 23:06:21 +00:00
}
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.RequestURI(), url.PathEscape(name))
2017-03-04 03:41:48 +00:00
}
2018-03-03 20:12:41 +00:00
2019-03-10 08:06:37 +00:00
const (
pathTypeFile = iota
pathTypeDirOfDirs
pathTypeDirOfFiles
)
func getPathType(dirpath string) int {
2017-05-11 23:06:21 +00:00
fi, _ := os.Stat(dirpath)
2018-03-03 20:12:41 +00:00
if isFile(fi) {
2019-03-10 08:06:37 +00:00
return pathTypeFile
2017-05-11 23:06:21 +00:00
}
2017-03-04 03:41:48 +00:00
2017-05-11 23:06:21 +00:00
fis, _ := ioutil.ReadDir(dirpath)
for _, fi := range fis {
2018-03-03 20:12:41 +00:00
if isFile(fi) {
2019-03-10 08:06:37 +00:00
return pathTypeDirOfFiles
2017-05-11 23:06:21 +00:00
}
2017-03-04 03:41:48 +00:00
}
2019-03-10 08:06:37 +00:00
// Directory of directories
return pathTypeDirOfDirs
2017-03-04 03:41:48 +00:00
}
2018-03-03 20:12:41 +00:00
func isFile(fi os.FileInfo) bool {
return !fi.IsDir()
}
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
}
}
}