refactor service handler

This commit is contained in:
Sinuhe Tellez 2021-06-10 02:18:53 -04:00
parent 3d8dd95c80
commit 622703cf5c
1 changed files with 25 additions and 29 deletions

View File

@ -27,6 +27,12 @@ func init() {
_ = mime.AddExtensionType(".fb2", "text/fb2+xml") _ = mime.AddExtensionType(".fb2", "text/fb2+xml")
} }
const (
pathTypeFile = iota
pathTypeDirOfDirs
pathTypeDirOfFiles
)
type OPDS struct { type OPDS struct {
DirRoot string DirRoot string
Author string Author string
@ -36,40 +42,40 @@ type OPDS struct {
var TimeNow = timeNowFunc() var TimeNow = timeNowFunc()
// Handler serve the content of a book file or
// returns an Acquisition Feed when the entries are documents or
// returns an Navegation Feed when the entries are other folders
func (s OPDS) Handler(w http.ResponseWriter, req *http.Request) error { func (s OPDS) Handler(w http.ResponseWriter, req *http.Request) error {
fPath := filepath.Join(s.DirRoot, req.URL.Path) fPath := filepath.Join(s.DirRoot, req.URL.Path)
log.Printf("fPath:'%s'", fPath) log.Printf("fPath:'%s'", fPath)
fi, err := os.Stat(fPath) if getPathType(fPath) == pathTypeFile {
if err != nil {
return err
}
if isFile(fi) {
http.ServeFile(w, req, fPath) http.ServeFile(w, req, fPath)
return nil return nil
} }
content, err := s.getContent(req, fPath) navFeed := s.makeFeed(fPath, req)
var content []byte
var err error
if getPathType(fPath) == pathTypeDirOfFiles {
// if path is a directory of files it is an aquisition feed
acFeed := &opds.AcquisitionFeed{Feed: &navFeed, Dc: "http://purl.org/dc/terms/", Opds: "http://opds-spec.org/2010/catalog"}
content, err = xml.MarshalIndent(acFeed, " ", " ")
} else {
// if path is a directory of directories it is an aquisition feed
content, err = xml.MarshalIndent(navFeed, " ", " ")
}
if err != nil { if err != nil {
log.Printf("error while serving '%s': %s", fPath, err)
return err return err
} }
content = append([]byte(xml.Header), content...) content = append([]byte(xml.Header), content...)
http.ServeContent(w, req, "feed.xml", TimeNow(), bytes.NewReader(content)) http.ServeContent(w, req, "feed.xml", TimeNow(), bytes.NewReader(content))
return nil
}
func (s OPDS) getContent(req *http.Request, dirpath string) (result []byte, err error) { return nil
feed := s.makeFeed(dirpath, req)
if getPathType(dirpath) == pathTypeDirOfFiles {
acFeed := &opds.AcquisitionFeed{Feed: &feed, Dc: "http://purl.org/dc/terms/", Opds: "http://opds-spec.org/2010/catalog"}
result, err = xml.MarshalIndent(acFeed, " ", " ")
} else {
result, err = xml.MarshalIndent(feed, " ", " ")
}
return
} }
const navigationType = "application/atom+xml;profile=opds-catalog;kind=navigation" const navigationType = "application/atom+xml;profile=opds-catalog;kind=navigation"
@ -94,7 +100,7 @@ func (s OPDS) makeFeed(dirpath string, req *http.Request) atom.Feed {
AddLink(opds.LinkBuilder. AddLink(opds.LinkBuilder.
Rel(getRel(fi.Name(), pathType)). Rel(getRel(fi.Name(), pathType)).
Title(fi.Name()). Title(fi.Name()).
Href(getHref(req, fi.Name())). Href(filepath.Join(req.URL.RequestURI(), url.PathEscape(fi.Name()))).
Type(getType(fi.Name(), pathType)). Type(getType(fi.Name(), pathType)).
Build()). Build()).
Build()) Build())
@ -123,16 +129,6 @@ func getType(name string, pathType int) string {
return "application/atom+xml;profile=opds-catalog;kind=acquisition" return "application/atom+xml;profile=opds-catalog;kind=acquisition"
} }
func getHref(req *http.Request, name string) string {
return filepath.Join(req.URL.RequestURI(), url.PathEscape(name))
}
const (
pathTypeFile = iota
pathTypeDirOfDirs
pathTypeDirOfFiles
)
func getPathType(dirpath string) int { func getPathType(dirpath string) int {
fi, _ := os.Stat(dirpath) fi, _ := os.Stat(dirpath)
if isFile(fi) { if isFile(fi) {