fixing aquisition links

This commit is contained in:
Sinuhe Tellez 2019-03-10 04:06:37 -04:00
parent bb8acd750c
commit 9f076caca7
3 changed files with 33 additions and 26 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@
_obj _obj
_test _test
.idea .idea
.vscode
# Architecture specific extensions/prefixes # Architecture specific extensions/prefixes
*.[568vq] *.[568vq]

View File

@ -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 - Added a message when the server started in the stadin
### Changed ### Changed
- fix rel and type for aquisition
- In the code change where the parameters are defined. - In the code change where the parameters are defined.
- Changed serveFeedauthor parameter for author. - Changed serveFeedauthor parameter for author.
- Adding host parameter. - Adding host parameter.

63
main.go
View File

@ -44,9 +44,6 @@ var (
authorEmail = flag.String("email", "", "The feed's author email") 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 { type acquisitionFeed struct {
*atom.Feed *atom.Feed
Dc string `xml:"xmlns:dc,attr"` Dc string `xml:"xmlns:dc,attr"`
@ -93,13 +90,14 @@ func handler(w http.ResponseWriter, req *http.Request) error {
return err return err
} }
content = append([]byte(xml.Header), content...)
http.ServeContent(w, req, "feed.xml", time.Now(), bytes.NewReader(content)) http.ServeContent(w, req, "feed.xml", time.Now(), bytes.NewReader(content))
return nil return nil
} }
func getContent(req *http.Request, dirpath string) (result []byte, err error) { func getContent(req *http.Request, dirpath string) (result []byte, err error) {
feed := makeFeed(dirpath, req) 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"} acFeed := &acquisitionFeed{&feed, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"}
result, err = xml.MarshalIndent(acFeed, " ", " ") result, err = xml.MarshalIndent(acFeed, " ", " ")
} else { } else {
@ -108,6 +106,8 @@ func getContent(req *http.Request, dirpath string) (result []byte, err error) {
return return
} }
const navegationType = "application/atom+xml;profile=opds-catalog;kind=navigation"
func makeFeed(dirpath string, req *http.Request) atom.Feed { func makeFeed(dirpath string, req *http.Request) atom.Feed {
feedBuilder := opds.FeedBuilder. feedBuilder := opds.FeedBuilder.
ID(req.URL.Path). ID(req.URL.Path).
@ -118,7 +118,7 @@ func makeFeed(dirpath string, req *http.Request) atom.Feed {
fis, _ := ioutil.ReadDir(dirpath) fis, _ := ioutil.ReadDir(dirpath)
for _, fi := range fis { for _, fi := range fis {
linkIsAcquisition := isAcquisition(filepath.Join(dirpath, fi.Name())) pathType := getPathType(filepath.Join(dirpath, fi.Name()))
feedBuilder = feedBuilder. feedBuilder = feedBuilder.
AddEntry(opds.EntryBuilder. AddEntry(opds.EntryBuilder.
ID(req.URL.Path + fi.Name()). ID(req.URL.Path + fi.Name()).
@ -126,56 +126,61 @@ func makeFeed(dirpath string, req *http.Request) atom.Feed {
Updated(time.Now()). Updated(time.Now()).
Published(time.Now()). Published(time.Now()).
AddLink(opds.LinkBuilder. AddLink(opds.LinkBuilder.
Rel(getRel(fi.Name(), linkIsAcquisition)). Rel(getRel(fi.Name(), pathType)).
Title(fi.Name()). Title(fi.Name()).
Href(getHref(req, fi.Name())). Href(getHref(req, fi.Name())).
Type(getType(fi.Name(), linkIsAcquisition)). Type(getType(fi.Name(), pathType)).
Build()). Build()).
Build()) Build())
} }
return feedBuilder.Build() return feedBuilder.Build()
} }
func getRel(name string, acquisition bool) (rel string) { func getRel(name string, pathType int) string {
rel = "subsection" if pathType == pathTypeDirOfFiles || pathType == pathTypeDirOfDirs {
if !acquisition { return "subsection"
return
}
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"
}
return
} }
func getType(name string, acquisition bool) (linkType string) {
linkType = acquisitionType
if !acquisition {
return
}
ext := filepath.Ext(name) ext := filepath.Ext(name)
linkType = mime.TypeByExtension(ext) if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
return return "http://opds-spec.org/image/thumbnail"
}
// mobi, epub, etc
return "http://opds-spec.org/acquisition"
}
func getType(name string, pathType int) string {
if pathType == pathTypeFile {
return mime.TypeByExtension(filepath.Ext(name))
}
return "application/atom+xml;profile=opds-catalog;kind=acquisition"
} }
func getHref(req *http.Request, name string) string { func getHref(req *http.Request, name string) string {
return filepath.Join(req.URL.EscapedPath(), url.PathEscape(name)) 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) fi, _ := os.Stat(dirpath)
if isFile(fi) { if isFile(fi) {
return false return pathTypeFile
} }
fis, _ := ioutil.ReadDir(dirpath) fis, _ := ioutil.ReadDir(dirpath)
for _, fi := range fis { for _, fi := range fis {
if isFile(fi) { if isFile(fi) {
return true return pathTypeDirOfFiles
} }
} }
return false // Directory of directories
return pathTypeDirOfDirs
} }
func isFile(fi os.FileInfo) bool { func isFile(fi os.FileInfo) bool {