aboutsummaryrefslogtreecommitdiff
path: root/internal/gallery
diff options
context:
space:
mode:
authorRanXom <saiyedshizain@gmail.com>2026-07-18 22:40:52 +0530
committerRanXom <saiyedshizain@gmail.com>2026-07-18 22:40:52 +0530
commiteb6a8c0306c44d5317c8d321ae24537bac9434ed (patch)
tree4fa8310e498441e93efd1b23584bee55cef2af78 /internal/gallery
parent8983d196786b86045920c0ee0e03711fbf62dfe8 (diff)
downloadgalleryd-eb6a8c0306c44d5317c8d321ae24537bac9434ed.tar.xz
galleryd-eb6a8c0306c44d5317c8d321ae24537bac9434ed.zip
feat(gallery): support sorted photo queries
Diffstat (limited to 'internal/gallery')
-rw-r--r--internal/gallery/query_photos.go6
-rw-r--r--internal/gallery/query_photos_test.go48
-rw-r--r--internal/gallery/query_sort.go40
3 files changed, 94 insertions, 0 deletions
diff --git a/internal/gallery/query_photos.go b/internal/gallery/query_photos.go
index 12fa877..e024090 100644
--- a/internal/gallery/query_photos.go
+++ b/internal/gallery/query_photos.go
@@ -4,6 +4,12 @@ func QueryPhotos(
photos []Photo,
query Query,
) []Photo {
+ if query.Sort != "" {
+ photos = append([]Photo(nil), photos...)
+
+ sortQueryPhotos(photos, query)
+ }
+
if query.Offset >= len(photos) {
return []Photo{}
}
diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go
index d5a25d0..d2700e5 100644
--- a/internal/gallery/query_photos_test.go
+++ b/internal/gallery/query_photos_test.go
@@ -3,6 +3,8 @@ package gallery
import (
"strconv"
"testing"
+
+ "github.com/RanXom/galleryd/internal/scanner"
)
func makePhotos(n int) []Photo {
@@ -140,3 +142,49 @@ func TestQueryPhotos(t *testing.T) {
)
})
}
+
+func TestQueryPhotosSortByPathAscending(t *testing.T) {
+ photos := []Photo{
+ {ID: "1", File: scanner.File{RelativePath: "c.jpg"}},
+ {ID: "2", File: scanner.File{RelativePath: "a.jpg"}},
+ {ID: "3", File: scanner.File{RelativePath: "b.jpg"}},
+ }
+
+ got := QueryPhotos(photos, Query{
+ Sort: SortByPath,
+ Order: SortAsc,
+ })
+
+ assertIDs(t, got, "2", "3", "1")
+}
+
+func TestQueryPhotosSortByPathDescending(t *testing.T) {
+ photos := []Photo{
+ {ID: "1", File: scanner.File{RelativePath: "a.jpg"}},
+ {ID: "2", File: scanner.File{RelativePath: "b.jpg"}},
+ {ID: "3", File: scanner.File{RelativePath: "c.jpg"}},
+ }
+
+ got := QueryPhotos(photos, Query{
+ Sort: SortByPath,
+ Order: SortDesc,
+ })
+
+ assertIDs(t, got, "3", "2", "1")
+}
+
+func TestQueryPhotosSortThenPaginate(t *testing.T) {
+ photos := []Photo{
+ {ID: "1", File: scanner.File{RelativePath: "c.jpg"}},
+ {ID: "2", File: scanner.File{RelativePath: "a.jpg"}},
+ {ID: "3", File: scanner.File{RelativePath: "b.jpg"}},
+ }
+
+ got := QueryPhotos(photos, Query{
+ Sort: SortByPath,
+ Order: SortAsc,
+ Limit: 1,
+ })
+
+ assertIDs(t, got, "2")
+}
diff --git a/internal/gallery/query_sort.go b/internal/gallery/query_sort.go
new file mode 100644
index 0000000..7ad811b
--- /dev/null
+++ b/internal/gallery/query_sort.go
@@ -0,0 +1,40 @@
+package gallery
+
+import "sort"
+
+func sortQueryPhotos(
+ photos []Photo,
+ query Query,
+) {
+ if query.Sort == "" {
+ return
+ }
+
+ sort.SliceStable(photos, func(i, j int) bool {
+ switch query.Sort {
+ case SortByPath:
+ if query.Order == SortDesc {
+ return photos[i].RelativePath > photos[j].RelativePath
+ }
+
+ return photos[i].RelativePath < photos[j].RelativePath
+ case SortByDateTime:
+ if query.Order == SortAsc {
+ if !photos[i].DateTaken.Equal(photos[j].DateTaken) {
+ return photos[i].DateTaken.Before(photos[j].DateTaken)
+ }
+
+ return photos[i].RelativePath < photos[j].RelativePath
+ }
+
+ if !photos[i].DateTaken.Equal(photos[j].DateTaken) {
+ return photos[i].DateTaken.After(photos[j].DateTaken)
+ }
+
+ return photos[i].RelativePath < photos[j].RelativePath
+
+ default:
+ return false
+ }
+ })
+}