Adding updated field

This commit is contained in:
Sinuhe Tellez 2017-03-07 20:01:13 -05:00
parent ce288cef9e
commit 0337cff931
1 changed files with 14 additions and 9 deletions

23
main.go
View File

@ -31,6 +31,8 @@ import (
"path/filepath" "path/filepath"
"golang.org/x/tools/blog/atom" "golang.org/x/tools/blog/atom"
"net/url"
"time"
) )
var dirRoot string var dirRoot string
@ -69,17 +71,19 @@ func catalogRoot(w http.ResponseWriter, req *http.Request) error {
} }
if fi.IsDir() { if fi.IsDir() {
return catalogFeed(w, req, dirPath) return catalogFeed(w, req, dirPath, fi.ModTime())
} }
return writeFileTo(w, dirPath) return writeFileTo(w, dirPath)
} }
func catalogFeed(w io.Writer, r *http.Request, dirPath string) error { func catalogFeed(w io.Writer, r *http.Request, dirPath string, updatedTime time.Time) error {
fis, err := ioutil.ReadDir(dirPath) fis, err := ioutil.ReadDir(dirPath)
if err != nil { if err != nil {
return err return err
} }
feed := &atom.Feed{Title: "OPDS Catalog: " + r.URL.Path} feed := &atom.Feed{Title: "OPDS Catalog: " + r.URL.Path}
feed.ID = base64.StdEncoding.EncodeToString([]byte(r.URL.EscapedPath()))
feed.Updated = atom.Time(updatedTime)
if len(fis) < 1 { if len(fis) < 1 {
return writeFeedTo(w, feed) return writeFeedTo(w, feed)
} }
@ -95,9 +99,9 @@ func catalogFeed(w io.Writer, r *http.Request, dirPath string) error {
func FeedEntries(f *atom.Feed, fis []os.FileInfo, r *http.Request) error { func FeedEntries(f *atom.Feed, fis []os.FileInfo, r *http.Request) error {
for _, fi := range fis { for _, fi := range fis {
e := &atom.Entry{Title: fi.Name()} e := &atom.Entry{Title: fi.Name()}
encoded := base64.StdEncoding.EncodeToString([]byte(path.Join(r.URL.Path, fi.Name()))) e.ID = base64.StdEncoding.EncodeToString([]byte(path.Join(r.URL.EscapedPath(), url.PathEscape(fi.Name()))))
e.ID = encoded e.Updated = atom.Time(fi.ModTime())
l := atom.Link{Title: fi.Name(), Href: path.Join(r.URL.EscapedPath(), fi.Name())} l := atom.Link{Title: fi.Name(), Href: path.Join(r.URL.EscapedPath(), url.PathEscape(fi.Name()))}
if !fi.IsDir() { if !fi.IsDir() {
l.Rel = "http://opds-spec.org/acquisition" l.Rel = "http://opds-spec.org/acquisition"
} }
@ -113,7 +117,7 @@ func FeedEntries(f *atom.Feed, fis []os.FileInfo, r *http.Request) error {
} }
func writeFeedTo(w io.Writer, feed *atom.Feed) error { func writeFeedTo(w io.Writer, feed *atom.Feed) error {
io.WriteString(w, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") io.WriteString(w, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
enc := xml.NewEncoder(w) enc := xml.NewEncoder(w)
enc.Indent(" ", " ") enc.Indent(" ", " ")
if err := enc.Encode(feed); err != nil { if err := enc.Encode(feed); err != nil {
@ -144,14 +148,15 @@ func getLinkType(lPath string) (string, error) {
if !fi.IsDir() { if !fi.IsDir() {
return mime.TypeByExtension(filepath.Ext(lPath)), nil return mime.TypeByExtension(filepath.Ext(lPath)), nil
} }
files, err := ioutil.ReadDir(lPath) fis, err := ioutil.ReadDir(lPath)
if err != nil { if err != nil {
return "", err return "", err
} }
for _, file := range files { for _, fi := range fis {
if !file.IsDir() { if !fi.IsDir() {
return "application/atom+xml;profile=opds-catalog;kind=acquisition", nil return "application/atom+xml;profile=opds-catalog;kind=acquisition", nil
} }
} }
return "application/atom+xml;profile=opds-catalog;kind=navigation", nil return "application/atom+xml;profile=opds-catalog;kind=navigation", nil
} }