From 9bfb8c87179655cd6f3fad1d9c337f6fb4f55a52 Mon Sep 17 00:00:00 2001 From: Felix Geller Date: Tue, 10 Mar 2020 19:48:19 +0100 Subject: [PATCH] adds an option to set image sort order. #1 - defaults to current behavior, by name - option is by directory - add `sort-order` key to `bilder.json` - `ModTime` for newest first, anything else for sorting by name --- readme.md | 1 + watcher.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 0567d73..3ecebab 100644 --- a/readme.md +++ b/readme.md @@ -64,6 +64,7 @@ It currently supports the following options: + `user` *default:* `""`, `pass` *default:* `""`: If both are non-empty strings, bilder will use them as credentials to enable basic authentication for this album. + `title` *default:* `""`: Title that should be set for the album, defaults to the directory name. + `captions` *default:* `null`: Map object from file name to caption string (consider the demo example below). + + `sort-order` *default:* `""`: Identifies sort order for images, supported: `ModTime` (newest first), `Name` (by file name, default). This is the `bilder.json` file in the `kitties` directory of the [demo](https://geller.io/bilder/b/kitties): ``` diff --git a/watcher.go b/watcher.go index f331b30..1939cbb 100644 --- a/watcher.go +++ b/watcher.go @@ -26,6 +26,7 @@ type imgDetails struct { Caption string Path string ThumbPath string + ModTime time.Time } type dirDetails struct { @@ -60,6 +61,7 @@ type dirConfig struct { Title string Captions map[string]string User, Pass string + SortOrder string `json:"sort-order"` } var ( @@ -106,6 +108,12 @@ func (a byImgName) Len() int { return len(a) } func (a byImgName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a byImgName) Less(i, j int) bool { return a[i].Path < a[j].Path } +type byImgModTime []*imgDetails + +func (a byImgModTime) Len() int { return len(a) } +func (a byImgModTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a byImgModTime) Less(i, j int) bool { return a[i].ModTime.Unix() > a[j].ModTime.Unix() } + func (w *watcher) writeIndexes() { tmpl := template.Must(template.New("dirIndex").Parse(dirIndexTempl)) for d, is := range w.images { @@ -114,11 +122,17 @@ func (w *watcher) writeIndexes() { for _, id := range is { ids = append(ids, id) } - sort.Sort(byImgName(ids)) title := d if cfg, exists := w.configs[d]; exists && cfg.Title != "" { title = cfg.Title } + + if cfg, exists := w.configs[d]; exists && cfg.SortOrder == "ModTime" { + sort.Sort(byImgModTime(ids)) + } else { + sort.Sort(byImgName(ids)) + } + dd := dirDetails{ URLPathPrefix: w.urlPathPrefix, Title: title, @@ -307,12 +321,12 @@ func (w *watcher) reloadContents() { if cfgExists { cptn = cfg.Captions[f.Name()] } - details := &imgDetails{ Width: img.Width, Height: img.Height, Caption: cptn, Path: strings.Join([]string{"b", d.Name(), f.Name()}, "/"), + ModTime: f.ModTime(), } if w.images == nil {