From 7578221f92f9a3c3cc1ac71735e7ddb170dde939 Mon Sep 17 00:00:00 2001 From: RanXom Date: Wed, 22 Jul 2026 18:04:22 +0530 Subject: feat(gallery): add extension filter to queries --- internal/gallery/query.go | 2 ++ internal/gallery/query_photos_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/internal/gallery/query.go b/internal/gallery/query.go index e2003d5..b66f99a 100644 --- a/internal/gallery/query.go +++ b/internal/gallery/query.go @@ -24,4 +24,6 @@ type Query struct { Sort SortField Order SortOrder + + Extension string } diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index c9bb986..05dab67 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -250,3 +250,13 @@ func TestQueryPhotosSortByDateDescending(t *testing.T) { assertIDs(t, got, "new", "mid", "old") } + +func TestQueryPhotosIgnoresExtensionFilteringForNow(t *testing.T) { + photos := makePhotos(3) + + got := QueryPhotos(photos, Query{ + Extension: "jpg", + }) + + assertIDs(t, got, "0", "1", "2") +} -- cgit v1.2.3 From 132e580b982bf28a75f83310317bedf0c4c00e55 Mon Sep 17 00:00:00 2001 From: RanXom Date: Wed, 22 Jul 2026 18:07:10 +0530 Subject: feat(api): parse extension query parameter --- internal/api/query_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/api/query_test.go b/internal/api/query_test.go index cbfe89a..0c3c1ae 100644 --- a/internal/api/query_test.go +++ b/internal/api/query_test.go @@ -1,6 +1,7 @@ package api import ( + "net/http" "net/http/httptest" "testing" @@ -209,3 +210,20 @@ func TestParseQueryOrderWithoutSort(t *testing.T) { ) } } + +func TestParseQueryExtension(t *testing.T) { + req := httptest.NewRequest( + http.MethodGet, + "/api/photos?extension=jpg", + nil, + ) + + got, err := parseQuery(req) + if err != nil { + t.Fatalf("parseQuery() error = %v", err) + } + + if got.Extension != "jpg" { + t.Fatalf("expected extension %q, got %q", "jpg", got.Extension) + } +} -- cgit v1.2.3 From a9150facac14799f4d188062c01b6ed353d82588 Mon Sep 17 00:00:00 2001 From: RanXom Date: Wed, 22 Jul 2026 18:08:34 +0530 Subject: chore(api): push remaining changes of previous commit --- internal/api/query.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/api/query.go b/internal/api/query.go index 3468163..d33027f 100644 --- a/internal/api/query.go +++ b/internal/api/query.go @@ -62,5 +62,7 @@ func parseQuery(r *http.Request) (gallery.Query, error) { } } + query.Extension = values.Get("extension") + return query, nil } -- cgit v1.2.3 From 8fb30a66a81a1f7150ebe86ed091e7c1651233a1 Mon Sep 17 00:00:00 2001 From: RanXom Date: Wed, 22 Jul 2026 18:20:15 +0530 Subject: feat(gallery): filter photos by extension --- internal/gallery/query_filter.go | 27 +++++++++++++++++ internal/gallery/query_photos.go | 2 ++ internal/gallery/query_photos_test.go | 57 +++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 internal/gallery/query_filter.go 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") } -- cgit v1.2.3 From 816c1506b2ccf081a5792ce676bcad7a7d6841fc Mon Sep 17 00:00:00 2001 From: RanXom Date: Wed, 22 Jul 2026 18:33:40 +0530 Subject: refactor(api): add flexible filtering options --- internal/gallery/query_filter.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/gallery/query_filter.go b/internal/gallery/query_filter.go index 9e10758..dda73c4 100644 --- a/internal/gallery/query_filter.go +++ b/internal/gallery/query_filter.go @@ -18,7 +18,10 @@ func filterQueryPhotos( for _, photo := range photos { ext := strings.TrimPrefix(filepath.Ext(photo.RelativePath), ".") - if strings.EqualFold(ext, query.Extension) { + if strings.EqualFold( + strings.TrimPrefix(ext, "."), + strings.TrimPrefix(query.Extension, "."), + ) { filtered = append(filtered, photo) } } -- cgit v1.2.3 From 2ca59223f20029a3a4a351b22706b2b4d47ad6e3 Mon Sep 17 00:00:00 2001 From: Saiyed Shizain Date: Tue, 25 Aug 2026 19:34:09 +0530 Subject: feat(gallery): add search query to gallery queries --- internal/gallery/query.go | 1 + internal/gallery/query_photos_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/internal/gallery/query.go b/internal/gallery/query.go index b66f99a..346c25f 100644 --- a/internal/gallery/query.go +++ b/internal/gallery/query.go @@ -26,4 +26,5 @@ type Query struct { Order SortOrder Extension string + Search string } diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index b72b6ec..b60cfea 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -311,3 +311,13 @@ func TestQueryPhotosFilterThenSortThenPaginate(t *testing.T) { assertIDs(t, got, "3") } + +func TestQueryPhotosIgnoreSearchForNow(t *testing.T) { + photos := makePhotos(3) + + got := QueryPhotos(photos, Query{ + Search: "vacation", + }) + + assertIDs(t, got, "0", "1", "2") +} -- cgit v1.2.3 From cce66ffa45779bc8752c0237a1dfad34083479bf Mon Sep 17 00:00:00 2001 From: Saiyed Shizain Date: Tue, 25 Aug 2026 19:40:36 +0530 Subject: feat(api): parse search query parameter --- internal/api/query.go | 1 + internal/api/query_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/internal/api/query.go b/internal/api/query.go index d33027f..d5428f0 100644 --- a/internal/api/query.go +++ b/internal/api/query.go @@ -63,6 +63,7 @@ func parseQuery(r *http.Request) (gallery.Query, error) { } query.Extension = values.Get("extension") + query.Search = values.Get("q") return query, nil } diff --git a/internal/api/query_test.go b/internal/api/query_test.go index 0c3c1ae..a1effe7 100644 --- a/internal/api/query_test.go +++ b/internal/api/query_test.go @@ -227,3 +227,24 @@ func TestParseQueryExtension(t *testing.T) { t.Fatalf("expected extension %q, got %q", "jpg", got.Extension) } } + +func TestParseQuerySearch(t *testing.T) { + req := httptest.NewRequest( + "http.MethodGet", + "/api/photos?q=vacation", + nil, + ) + + got, err := parseQuery(req) + if err != nil { + t.Fatalf("parseQuery() error = %v", err) + } + + if got.Search != "vacation" { + t.Fatalf( + "expected search %q, got %q", + "vacation", + got.Search, + ) + } +} -- cgit v1.2.3 From 6792c6f697db94012adca1c56d6b032d71d00665 Mon Sep 17 00:00:00 2001 From: Saiyed Shizain Date: Wed, 26 Aug 2026 16:46:29 +0530 Subject: feat(gallery): search photos by relative path --- internal/gallery/query_filter.go | 52 ++++++++++++++++++++++++++++------- internal/gallery/query_photos_test.go | 48 ++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 13 deletions(-) diff --git a/internal/gallery/query_filter.go b/internal/gallery/query_filter.go index dda73c4..14d9d59 100644 --- a/internal/gallery/query_filter.go +++ b/internal/gallery/query_filter.go @@ -9,22 +9,54 @@ 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 !matchesExtension(photo, query.Extension) { + continue + } - if strings.EqualFold( - strings.TrimPrefix(ext, "."), - strings.TrimPrefix(query.Extension, "."), - ) { - filtered = append(filtered, photo) + if !matchesSearch(photo, query.Search) { + continue } + + filtered = append(filtered, photo) } return filtered } + +func matchesExtension( + photo Photo, + extension string, +) bool { + if extension == "" { + return true + } + + ext := strings.TrimPrefix( + filepath.Ext(photo.RelativePath), + ".", + ) + + extension = strings.TrimPrefix( + extension, + ".", + ) + + return strings.EqualFold(ext, extension) +} + +func matchesSearch( + photo Photo, + search string, +) bool { + if search == "" { + return true + } + + return strings.Contains( + strings.ToLower(photo.RelativePath), + strings.ToLower(search), + ) +} diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index b60cfea..c5d9376 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -312,12 +312,54 @@ func TestQueryPhotosFilterThenSortThenPaginate(t *testing.T) { assertIDs(t, got, "3") } -func TestQueryPhotosIgnoreSearchForNow(t *testing.T) { - photos := makePhotos(3) +func TestQueryPhotosSearchByRelativePath(t *testing.T) { + photos := []Photo{ + { + ID: "vacation", + File: scanner.File{ + RelativePath: "photos/vacation/beach.jpg", + }, + }, + { + ID: "work", + File: scanner.File{ + RelativePath: "photos/work/meeting.jpg", + }, + }, + { + ID: "vacation2", + File: scanner.File{ + RelativePath: "photos/vacation/mountains.jpg", + }, + }, + } got := QueryPhotos(photos, Query{ Search: "vacation", }) - assertIDs(t, got, "0", "1", "2") + assertIDs(t, got, "vacation", "vacation2") +} + +func TestQueryPhotosSearchIsCaseInsensitive(t *testing.T) { + photos := []Photo{ + { + ID: "1", + File: scanner.File{ + RelativePath: "Photos/Vacation/Beach.jpg", + }, + }, + { + ID: "2", + File: scanner.File{ + RelativePath: "photos/work/meeting.jpg", + }, + }, + } + + got := QueryPhotos(photos, Query{ + Search: "VACATION", + }) + + assertIDs(t, got, "1") } -- cgit v1.2.3 From bd8fab9ace7444d1b97a6df7b2d81ef981808a05 Mon Sep 17 00:00:00 2001 From: Saiyed Shizain Date: Wed, 26 Aug 2026 16:53:33 +0530 Subject: test(gallery): cover combined query pipeline --- internal/gallery/query_photos_test.go | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index c5d9376..f38c237 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -363,3 +363,49 @@ func TestQueryPhotosSearchIsCaseInsensitive(t *testing.T) { assertIDs(t, got, "1") } + +func TestQueryPhotosFilterSearchSortThenPaginate(t *testing.T) { + photos := []Photo{ + { + ID: "1", + File: scanner.File{ + RelativePath: "photos/work/report.jpg", + }, + }, + { + ID: "2", + File: scanner.File{ + RelativePath: "photos/vacation/zebra.jpg", + }, + }, + { + ID: "3", + File: scanner.File{ + RelativePath: "photos/vacation/apple.jpg", + }, + }, + { + ID: "4", + File: scanner.File{ + RelativePath: "photos/vacation/beach.png", + }, + }, + { + ID: "5", + File: scanner.File{ + RelativePath: "photos/vacation/mountains.jpg", + }, + }, + } + + got := QueryPhotos(photos, Query{ + Extension: "jpg", + Search: "vacation", + Sort: SortByPath, + Order: SortAsc, + Offset: 1, + Limit: 1, + }) + + assertIDs(t, got, "5") +} -- cgit v1.2.3