From 9f076caca7e5680bbf0ed9530d6158b9cbab1d01 Mon Sep 17 00:00:00 2001 From: Sinuhe Tellez Date: Sun, 10 Mar 2019 04:06:37 -0400 Subject: [PATCH] fixing aquisition links --- .gitignore | 1 + CHANGELOG.md | 1 + main.go | 57 ++++++++++++++++++++++++++++------------------------ 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 0c9e329..563e212 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ _obj _test .idea +.vscode # Architecture specific extensions/prefixes *.[568vq] diff --git a/CHANGELOG.md b/CHANGELOG.md index e6eca0c..46f1083 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added a message when the server started in the stadin ### Changed +- fix rel and type for aquisition - In the code change where the parameters are defined. - Changed serveFeedauthor parameter for author. - Adding host parameter. diff --git a/main.go b/main.go index 86c50f4..b92de3c 100644 --- a/main.go +++ b/main.go @@ -44,9 +44,6 @@ var ( authorEmail = flag.String("email", "", "The feed's author email") ) -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"` @@ -93,13 +90,14 @@ func handler(w http.ResponseWriter, req *http.Request) error { return err } + content = append([]byte(xml.Header), content...) http.ServeContent(w, req, "feed.xml", time.Now(), bytes.NewReader(content)) return nil } func getContent(req *http.Request, dirpath string) (result []byte, err error) { feed := makeFeed(dirpath, req) - if isAcquisition(dirpath) { + if getPathType(dirpath) == pathTypeDirOfFiles { acFeed := &acquisitionFeed{&feed, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"} result, err = xml.MarshalIndent(acFeed, " ", " ") } else { @@ -108,6 +106,8 @@ func getContent(req *http.Request, dirpath string) (result []byte, err error) { return } +const navegationType = "application/atom+xml;profile=opds-catalog;kind=navigation" + func makeFeed(dirpath string, req *http.Request) atom.Feed { feedBuilder := opds.FeedBuilder. ID(req.URL.Path). @@ -118,7 +118,7 @@ func makeFeed(dirpath string, req *http.Request) atom.Feed { fis, _ := ioutil.ReadDir(dirpath) for _, fi := range fis { - linkIsAcquisition := isAcquisition(filepath.Join(dirpath, fi.Name())) + pathType := getPathType(filepath.Join(dirpath, fi.Name())) feedBuilder = feedBuilder. AddEntry(opds.EntryBuilder. ID(req.URL.Path + fi.Name()). @@ -126,56 +126,61 @@ func makeFeed(dirpath string, req *http.Request) atom.Feed { Updated(time.Now()). Published(time.Now()). AddLink(opds.LinkBuilder. - Rel(getRel(fi.Name(), linkIsAcquisition)). + Rel(getRel(fi.Name(), pathType)). Title(fi.Name()). Href(getHref(req, fi.Name())). - Type(getType(fi.Name(), linkIsAcquisition)). + Type(getType(fi.Name(), pathType)). Build()). Build()) } return feedBuilder.Build() } -func getRel(name string, acquisition bool) (rel string) { - rel = "subsection" - if !acquisition { - return +func getRel(name string, pathType int) string { + if pathType == pathTypeDirOfFiles || pathType == pathTypeDirOfDirs { + return "subsection" } + ext := filepath.Ext(name) - if rel = "http://opds-spec.org/acquisition"; ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" { - rel = "http://opds-spec.org/image/thumbnail" + if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" { + return "http://opds-spec.org/image/thumbnail" } - return + + // mobi, epub, etc + return "http://opds-spec.org/acquisition" } -func getType(name string, acquisition bool) (linkType string) { - linkType = acquisitionType - if !acquisition { - return +func getType(name string, pathType int) string { + if pathType == pathTypeFile { + return mime.TypeByExtension(filepath.Ext(name)) } - ext := filepath.Ext(name) - linkType = mime.TypeByExtension(ext) - return + return "application/atom+xml;profile=opds-catalog;kind=acquisition" } func getHref(req *http.Request, name string) string { return filepath.Join(req.URL.EscapedPath(), url.PathEscape(name)) } -func isAcquisition(dirpath string) bool { +const ( + pathTypeFile = iota + pathTypeDirOfDirs + pathTypeDirOfFiles +) + +func getPathType(dirpath string) int { fi, _ := os.Stat(dirpath) if isFile(fi) { - return false + return pathTypeFile } fis, _ := ioutil.ReadDir(dirpath) - for _, fi := range fis { if isFile(fi) { - return true + return pathTypeDirOfFiles } } - return false + // Directory of directories + return pathTypeDirOfDirs } func isFile(fi os.FileInfo) bool {