diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-08-26 16:59:34 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-26 16:59:34 +0530 |
| commit | 7c346191ca616d5aef20147c46fd45ee856477aa (patch) | |
| tree | 0535b1eae4cf0fa21d24e2ed3e7ab4875c0e70f6 /internal/gallery | |
| parent | 9b541ca2d0a639eebd9610fcd6385e841da68e0a (diff) | |
| parent | bd8fab9ace7444d1b97a6df7b2d81ef981808a05 (diff) | |
| download | galleryd-7c346191ca616d5aef20147c46fd45ee856477aa.tar.xz galleryd-7c346191ca616d5aef20147c46fd45ee856477aa.zip | |
Merge pull request #14 from RanXom/feat/api
Feat/api: add photo search by relative path
Diffstat (limited to 'internal/gallery')
| -rw-r--r-- | internal/gallery/query.go | 3 | ||||
| -rw-r--r-- | internal/gallery/query_filter.go | 62 | ||||
| -rw-r--r-- | internal/gallery/query_photos.go | 2 | ||||
| -rw-r--r-- | internal/gallery/query_photos_test.go | 159 |
4 files changed, 226 insertions, 0 deletions
diff --git a/internal/gallery/query.go b/internal/gallery/query.go index e2003d5..346c25f 100644 --- a/internal/gallery/query.go +++ b/internal/gallery/query.go @@ -24,4 +24,7 @@ type Query struct { Sort SortField Order SortOrder + + Extension string + Search string } diff --git a/internal/gallery/query_filter.go b/internal/gallery/query_filter.go new file mode 100644 index 0000000..14d9d59 --- /dev/null +++ b/internal/gallery/query_filter.go @@ -0,0 +1,62 @@ +package gallery + +import ( + "path/filepath" + "strings" +) + +func filterQueryPhotos( + photos []Photo, + query Query, +) []Photo { + filtered := make([]Photo, 0, len(photos)) + + for _, photo := range photos { + if !matchesExtension(photo, query.Extension) { + continue + } + + 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.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 c9bb986..f38c237 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -250,3 +250,162 @@ func TestQueryPhotosSortByDateDescending(t *testing.T) { assertIDs(t, got, "new", "mid", "old") } + +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, "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, "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") +} + +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") +} |