aboutsummaryrefslogtreecommitdiff
path: root/internal/gallery/query_filter.go
blob: 14d9d59b68b473674639348529c738fbcf5d25f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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),
	)
}