Skip to content

Commit

Permalink
feat: Adding filter by tags
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jan 23, 2023
1 parent 7912e8a commit f294ba5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
29 changes: 29 additions & 0 deletions pkg/search/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
)

const (
Expand All @@ -22,10 +23,15 @@ type search struct {
before time.Time
after time.Time
mimes []string
tags []string
size int64
greaterThan bool
}

func (s search) hasTags() bool {
return len(s.tags) > 0
}

func parseSearch(params url.Values, now time.Time) (output search, err error) {
if name := strings.TrimSpace(params.Get("name")); len(name) > 0 {
output.pattern, err = regexp.Compile(name)
Expand Down Expand Up @@ -123,3 +129,26 @@ func (s search) matchMimes(item absto.Item) bool {

return false
}

func (s search) matchTags(metadata provider.Metadata) bool {
if len(metadata.Tags) == 0 {
return false
}

for _, tag := range s.tags {
var found bool

for _, itemTag := range metadata.Tags {
if itemTag == tag {
found = true
break
}
}

if !found {
return false
}
}

return true
}
19 changes: 18 additions & 1 deletion pkg/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ViBiOh/fibr/pkg/exclusive"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/thumbnail"
"github.com/ViBiOh/httputils/v4/pkg/logger"
httpModel "github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
"go.opentelemetry.io/otel/trace"
Expand All @@ -34,16 +35,32 @@ func New(storageApp absto.Storage, thumbnailApp thumbnail.App, exifApp provider.
func (a App) Files(r *http.Request, request provider.Request) (items []absto.Item, err error) {
params := r.URL.Query()

ctx, end := tracer.StartSpan(r.Context(), a.tracer, "filter")
defer end()

criterions, err := parseSearch(params, time.Now())
if err != nil {
return nil, httpModel.WrapInvalid(err)
}

err = a.storageApp.Walk(r.Context(), request.Filepath(), func(item absto.Item) error {
hasTags := criterions.hasTags()

err = a.storageApp.Walk(ctx, request.Filepath(), func(item absto.Item) error {
if item.IsDir || !criterions.match(item) {
return nil
}

if hasTags {
metadata, err := a.exifApp.GetMetadataFor(ctx, item)
if err != nil {
logger.WithField("item", item.Pathname).Error("get metadata: %s", err)
}

if !criterions.matchTags(metadata) {
return nil
}
}

items = append(items, item)

return nil
Expand Down

0 comments on commit f294ba5

Please sign in to comment.