diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-22 18:20:15 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-22 18:20:15 +0530 |
| commit | 8fb30a66a81a1f7150ebe86ed091e7c1651233a1 (patch) | |
| tree | f23eb6ebdb05a0bfe6e998df5803dd687c8eaa85 | |
| parent | a9150facac14799f4d188062c01b6ed353d82588 (diff) | |
| download | galleryd-8fb30a66a81a1f7150ebe86ed091e7c1651233a1.tar.xz galleryd-8fb30a66a81a1f7150ebe86ed091e7c1651233a1.zip | |
feat(gallery): filter photos by extension
| -rw-r--r-- | internal/gallery/query_filter.go | 27 | ||||
| -rw-r--r-- | internal/gallery/query_photos.go | 2 | ||||
| -rw-r--r-- | internal/gallery/query_photos_test.go | 57 |
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") } |