Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Sort books on main page by last modified
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Jun 26, 2017
1 parent 84748fb commit 9074be2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
20 changes: 11 additions & 9 deletions bookbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/signal"
"path/filepath"
"syscall"
"time"
)

// Series represents a book series
Expand All @@ -18,15 +19,16 @@ type Series struct {

// Book represents a book
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author,omitempty"`
AuthorID string `json:"authorid"`
Publisher string `json:"publisher,omitempty"`
Description string `json:"description,omitempty"`
Series Series `json:"series,omitempty"`
Filepath string `json:"filepath"`
HasCover bool `json:"hascover"`
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author,omitempty"`
AuthorID string `json:"authorid"`
Publisher string `json:"publisher,omitempty"`
Description string `json:"description,omitempty"`
Series Series `json:"series,omitempty"`
Filepath string `json:"filepath"`
HasCover bool `json:"hascover"`
ModTime time.Time `json:"modtime,omitempty"`
}

var bookdir *string
Expand Down
4 changes: 4 additions & 0 deletions indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func getMetadata(epub string) (*Book, error) {

book := new(Book)

if file, err := os.Stat(epub); err == nil {
book.ModTime = file.ModTime()
}

rrsk, err := zfs.Open("/" + rootfile)
if err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ func BooksHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
var booksHTML bytes.Buffer
booksHTML.WriteString(`<div class="books cards">`)
matched := []Book{}
for _, b := range books {
matched = append(matched, b)
}
sort.Slice(matched, func(i, j int) bool {
return matched[i].ModTime.Unix() > matched[j].ModTime.Unix()
})
for _, b := range matched {
booksHTML.WriteString(bookHTML(&b, true))
}
booksHTML.WriteString(`</div>`)
Expand Down

0 comments on commit 9074be2

Please sign in to comment.