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/query_filter.go | |
| 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/query_filter.go')
| -rw-r--r-- | internal/gallery/query_filter.go | 62 |
1 files changed, 62 insertions, 0 deletions
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), + ) +} |