Skip to content

Commit

Permalink
feat(search): Adding filter by tags in the ui
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jan 26, 2023
1 parent 9407aec commit 8e4af75
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 41 deletions.
5 changes: 5 additions & 0 deletions cmd/fibr/templates/search-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ <h2 class="flex flex-center header no-margin">Search files</h2>
</select>
</p>

<p class="padding no-margin">
<label for="tags" class="block">Tags</label>
<input id="tags" name="tags" type="text" value="{{ if .Search.tags }}{{ join .Search.tags " " }}{{ end }}" placeholder="Tags..." class="full">
</p>

<p class="padding no-margin">
<label for="after" class="block">After</label>
<input id="after" name="after" type="date" placeholder="2020-01-31" value="{{ with .Search.after }}{{ index . 0 }}{{ end }}">
Expand Down
10 changes: 10 additions & 0 deletions cmd/fibr/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@
<img class="icon" src="{{ url "/svg/download?fill=aliceblue" }}" alt="download">
</a>
</a>

{{ if .Tags }}
{{- if eq $root.Request.Display "grid" -}}
<img class="icon tags" src="{{ url "/svg/tag" }}?fill=aliceblue" alt="tag" title="#{{ join .Tags " #" }}">
{{ else }}
<em class="tags ellipsis padding-left">
#{{ join .Tags " #" }}
</em>
{{ end}}
{{ end }}
</li>
{{ end }}

Expand Down
14 changes: 1 addition & 13 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,7 @@ func (a App) handleDir(w http.ResponseWriter, r *http.Request, request provider.
go a.notify(tracer.CopyToBackground(r.Context()), provider.NewAccessEvent(item, r))

if query.GetBool(r, "search") {
files, hasMap, err := a.searchApp.Search(r, request, items)
if err != nil {
return errorReturn(request, err)
}

return renderer.NewPage("search", http.StatusOK, map[string]any{
"Paths": getPathParts(request),
"Files": files,
"Cover": a.getCover(r.Context(), request, items),
"Search": r.URL.Query(),
"Request": request,
"HasMap": hasMap,
}), nil
return a.search(r, request, item, items)
}

provider.SetPrefsCookie(w, request)
Expand Down
52 changes: 52 additions & 0 deletions pkg/crud/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package crud

import (
"net/http"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/thumbnail"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

func (a App) search(r *http.Request, request provider.Request, item absto.Item, files []absto.Item) (renderer.Page, error) {
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "search")
defer end()

metadatas, err := a.metadataApp.GetAllMetadataFor(ctx, files...)
if err != nil {
listLogger(item.Pathname).Error("list metadatas: %s", err)
}

items := make([]provider.RenderItem, len(files))
var hasMap bool

renderWithThumbnail := request.Display == provider.GridDisplay

for i, item := range files {
renderItem := provider.StorageToRender(item, request)

metadata := metadatas[item.ID]
renderItem.Tags = metadata.Tags

if renderWithThumbnail && a.thumbnailApp.CanHaveThumbnail(item) && a.thumbnailApp.HasThumbnail(ctx, item, thumbnail.SmallSize) {
renderItem.HasThumbnail = true
}

items[i] = renderItem

if !hasMap && metadata.Geocode.Longitude != 0 && metadata.Geocode.Latitude != 0 {
hasMap = true
}
}

return renderer.NewPage("search", http.StatusOK, map[string]any{
"Paths": getPathParts(request),
"Files": items,
"Cover": a.getCover(ctx, request, files),
"Search": r.URL.Query(),
"Request": request,
"HasMap": hasMap,
}), nil
}
4 changes: 4 additions & 0 deletions pkg/search/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func parseSearch(params url.Values, now time.Time) (output search, err error) {
}
}

if tags := strings.TrimSpace(params.Get("tags")); len(tags) > 0 {
output.tags = strings.Split(tags, " ")
}

output.before, err = parseDate(strings.TrimSpace(params.Get("before")))
if err != nil {
return
Expand Down
28 changes: 0 additions & 28 deletions pkg/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,3 @@ func (a App) Files(r *http.Request, request provider.Request) (items []absto.Ite

return
}

func (a App) Search(r *http.Request, request provider.Request, files []absto.Item) ([]provider.RenderItem, bool, error) {
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "search")
defer end()

items := make([]provider.RenderItem, len(files))
var hasMap bool

renderWithThumbnail := request.Display == provider.GridDisplay

for i, item := range files {
renderItem := provider.StorageToRender(item, request)

if renderWithThumbnail && a.thumbnailApp.CanHaveThumbnail(item) && a.thumbnailApp.HasThumbnail(ctx, item, thumbnail.SmallSize) {
renderItem.HasThumbnail = true
}

items[i] = renderItem

if !hasMap {
if exif, err := a.exifApp.GetMetadataFor(ctx, item); err == nil && exif.Geocode.Longitude != 0 && exif.Geocode.Latitude != 0 {
hasMap = true
}
}
}

return items, hasMap, nil
}

0 comments on commit 8e4af75

Please sign in to comment.