aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/gallery/query_filter.go27
-rw-r--r--internal/gallery/query_photos.go2
-rw-r--r--internal/gallery/query_photos_test.go57
3 files changed, 83 insertions, 3 deletions
diff --git a/internal/gallery/query_filter.go b/internal/gallery/query_filter.go
new file mode 100644
index 0000000..9e10758
--- /dev/null
+++ b/internal/gallery/query_filter.go
@@ -0,0 +1,27 @@
+package gallery
+
+import (
+ "path/filepath"
+ "strings"
+)
+
+func filterQueryPhotos(
+ photos []Photo,
+ query Query,
+) []Photo {
+ if query.Extension == "" {
+ return photos
+ }
+
+ filtered := make([]Photo, 0, len(photos))
+
+ for _, photo := range photos {
+ ext := strings.TrimPrefix(filepath.Ext(photo.RelativePath), ".")
+
+ if strings.EqualFold(ext, query.Extension) {
+ filtered = append(filtered, photo)
+ }
+ }
+
+ return filtered
+}
diff --git a/internal/gallery/query_photos.go b/internal/gallery/query_photos.go
index e024090..7b25250 100644
--- a/internal/gallery/query_photos.go
+++ b/internal/gallery/query_photos.go
@@ -4,6 +4,8 @@ func QueryPhotos(
photos []Photo,
query Query,
) []Photo {
+ photos = filterQueryPhotos(photos, query)
+
if query.Sort != "" {
photos = append([]Photo(nil), photos...)
diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go
index 05dab67..b72b6ec 100644
--- a/internal/gallery/query_photos_test.go
+++ b/internal/gallery/query_photos_test.go
@@ -251,12 +251,63 @@ func TestQueryPhotosSortByDateDescending(t *testing.T) {
assertIDs(t, got, "new", "mid", "old")
}
-func TestQueryPhotosIgnoresExtensionFilteringForNow(t *testing.T) {
- photos := makePhotos(3)
+func TestQueryPhotosFilterByExtension(t *testing.T) {
+ photos := []Photo{
+ {
+ ID: "jpg",
+ File: scanner.File{
+ RelativePath: "photo.jpg",
+ },
+ },
+ {
+ ID: "png",
+ File: scanner.File{
+ RelativePath: "photo.png",
+ },
+ },
+ {
+ ID: "jpg2",
+ File: scanner.File{
+ RelativePath: "anotherphoto.jpg",
+ },
+ },
+ }
+
+ got := QueryPhotos(photos, Query{
+ Extension: "jpg",
+ })
+
+ assertIDs(t, got, "jpg", "jpg2")
+}
+
+func TestQueryPhotosFilterThenSortThenPaginate(t *testing.T) {
+ photos := []Photo{
+ {
+ ID: "1",
+ File: scanner.File{
+ RelativePath: "a.png",
+ },
+ },
+ {
+ ID: "2",
+ File: scanner.File{
+ RelativePath: "c.jpg",
+ },
+ },
+ {
+ ID: "3",
+ File: scanner.File{
+ RelativePath: "a.jpg",
+ },
+ },
+ }
got := QueryPhotos(photos, Query{
Extension: "jpg",
+ Sort: SortByPath,
+ Order: SortAsc,
+ Limit: 1,
})
- assertIDs(t, got, "0", "1", "2")
+ assertIDs(t, got, "3")
}