Skip to content

Commit

Permalink
refactor: Moving description out of exas and starting to rename it me…
Browse files Browse the repository at this point in the history
…tadata

Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Dec 30, 2022
1 parent 4084d48 commit 8accb94
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 90 deletions.
24 changes: 15 additions & 9 deletions cmd/fibr/static/scripts/async-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ async function fetchThumbnail() {
}
}

document.addEventListener('readystatechange', async (event) => {
if (event.target.readyState !== 'complete') {
return;
}

let dateTimeFormatter = new Intl.DateTimeFormat(navigator.language, {
dateStyle: 'full',
timeStyle: 'long',
});

document.querySelectorAll('.date').forEach((item) => {
item.innerHTML = dateTimeFormatter.format(new Date(item.innerHTML));
});
});

document.addEventListener(
'readystatechange',
async (event) => {
Expand All @@ -183,15 +198,6 @@ document.addEventListener(
return;
}

let dateTimeFormatter = new Intl.DateTimeFormat(navigator.language, {
dateStyle: 'full',
timeStyle: 'long',
});

document.querySelectorAll('.date').forEach((item) => {
item.innerHTML = dateTimeFormatter.format(new Date(item.innerHTML));
});

const thumbnailsElem = document.querySelectorAll('[data-thumbnail]');
if (!thumbnailsElem) {
return;
Expand Down
2 changes: 1 addition & 1 deletion cmd/fibr/static/scripts/index.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/fibr/templates/story.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@

{{ if not .Exif.Date.IsZero }}
<img class="icon margin-right-half{{ if .Exif.Geocode.HasAddress }} margin-left-half{{ end }}" src="{{ url "/svg/calendar?fill=%23272727" }}" alt="Location">
<span class="date">{{ .Exif.Date.Format "Monday _2 January 15:04:05 2006 MST" }}</span>
<span class="date">{{ .Exif.Date.Format "2006-01-02T15:04:05Z07:00" }}</span>
{{ end }}
</p>
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ require (
github.com/ViBiOh/ChatPotte v0.2.26
github.com/ViBiOh/absto v1.1.2
github.com/ViBiOh/auth/v2 v2.14.20
github.com/ViBiOh/exas v0.5.11
github.com/ViBiOh/exas v0.6.0
github.com/ViBiOh/flags v1.2.0
github.com/ViBiOh/httputils/v4 v4.51.0
github.com/ViBiOh/httputils/v4 v4.51.1
github.com/ViBiOh/vith v0.5.10
github.com/golang/mock v1.6.0
github.com/prometheus/client_golang v1.14.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ github.com/ViBiOh/absto v1.1.2 h1:Gyvgka4mKIa0CC83+0VIZPiWRJUJZUTwpuvVQ0O21rI=
github.com/ViBiOh/absto v1.1.2/go.mod h1:1XJVGq7s1nbZxq/iehIyOzXzDq9FLAZ+7HfHdrXyLhA=
github.com/ViBiOh/auth/v2 v2.14.20 h1:xCTQrnTOFLiyFLIV0CMXlCPrHEbimganZsbTbdHUh50=
github.com/ViBiOh/auth/v2 v2.14.20/go.mod h1:lL4rXpvS0Gtui4CSy0J/AK1pq59XVXaR4Qz1ELsQ3eY=
github.com/ViBiOh/exas v0.5.11 h1:KpW9mmt+Fe3I2l11UXovye8wWZ+zoV8LVVyCcvH6j+w=
github.com/ViBiOh/exas v0.5.11/go.mod h1:A1dbq+ZR5yBo4RkDWhg4M2hze9MLTBUDMk5gRliWEEk=
github.com/ViBiOh/exas v0.6.0 h1:UWQuzesHShBSMkQw7mWAj4NBwL1Hb/fDcgzk/UpacYQ=
github.com/ViBiOh/exas v0.6.0/go.mod h1:w6aibKNptsqVwIIIeuifr/1JM551/idiiKhHgDzf7sk=
github.com/ViBiOh/flags v1.2.0 h1:DaujjNXzD29KxKyp4eZdn7c9+uBN5DokWgDAe7DcUmc=
github.com/ViBiOh/flags v1.2.0/go.mod h1:UyMB5zeD/aId7Xw3x7577ZNU298JmukzOcV8p/H2W1s=
github.com/ViBiOh/httputils/v4 v4.51.0 h1:3TxNmMTebOkY6ife7j2BPWvIybEO73lR/eeJ8htS/ws=
github.com/ViBiOh/httputils/v4 v4.51.0/go.mod h1:wFfDWFpelX/bwli5s1VLGOOFiT1sgIg9/D/ACyy7Lg8=
github.com/ViBiOh/httputils/v4 v4.51.1 h1:kTklOFl5zgzweh6CNxtwRJhYC4/GNZ8oYHxQkhHGmxQ=
github.com/ViBiOh/httputils/v4 v4.51.1/go.mod h1:wFfDWFpelX/bwli5s1VLGOOFiT1sgIg9/D/ACyy7Lg8=
github.com/ViBiOh/vith v0.5.10 h1:BriyDGDFmBRpJpbQhsFxIlq2bT9HmuJ/q903Y+0w0S0=
github.com/ViBiOh/vith v0.5.10/go.mod h1:JcGIWVjvXA0zYHz/picn+xMIdk7ljbYjDp7d16BufzM=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
7 changes: 3 additions & 4 deletions pkg/crud/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sort"

absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/thumbnail"
"github.com/ViBiOh/httputils/v4/pkg/concurrent"
Expand All @@ -24,7 +23,7 @@ func (a App) browse(ctx context.Context, request provider.Request, item absto.It
previous provider.RenderItem
next provider.RenderItem
files []absto.Item
exif exas.Exif
exif provider.Metadata
)

wg := concurrent.NewSimple()
Expand All @@ -39,8 +38,8 @@ func (a App) browse(ctx context.Context, request provider.Request, item absto.It

wg.Go(func() {
var err error
exif, err = a.exifApp.GetExifFor(ctx, item)
if err != nil {
exif, err = a.exifApp.GetMetadataFor(ctx, item)
if err != nil && !absto.IsNotExist(err) {
logger.WithField("item", item.Pathname).Error("load exif: %s", err)
}
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/exif"
"github.com/ViBiOh/fibr/pkg/geo"
"github.com/ViBiOh/fibr/pkg/provider"
Expand Down Expand Up @@ -223,7 +222,7 @@ func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provid
a.generateGeoJSON(ctx, w, request, items, exifs)
}

func (a App) generateGeoJSON(ctx context.Context, w io.Writer, request provider.Request, items []absto.Item, exifs map[string]exas.Exif) {
func (a App) generateGeoJSON(ctx context.Context, w io.Writer, request provider.Request, items []absto.Item, exifs map[string]provider.Metadata) {
done := ctx.Done()
isDone := func() bool {
select {
Expand Down
32 changes: 19 additions & 13 deletions pkg/crud/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,33 @@ func BenchmarkServeGeoJSON(b *testing.B) {

mockExif := mocks.NewExifManager(ctrl)

mockExif.EXPECT().ListExifFor(gomock.Any(), gomock.Any()).Return(map[string]exas.Exif{
mockExif.EXPECT().ListExifFor(gomock.Any(), gomock.Any()).Return(map[string]provider.Metadata{
"9012": {
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
Exif: exas.Exif{
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
"5678": {
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
Exif: exas.Exif{
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
"1234": {
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
Exif: exas.Exif{
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
}, nil).AnyTimes()

Expand Down
2 changes: 1 addition & 1 deletion pkg/crud/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (a App) handlePostDescription(w http.ResponseWriter, r *http.Request, reque
return
}

exif, err := a.exifApp.GetExifFor(ctx, item)
exif, err := a.exifApp.GetMetadataFor(ctx, item)
if err != nil && !absto.IsNotExist(err) {
a.error(w, r, request, err)
return
Expand Down
3 changes: 1 addition & 2 deletions pkg/crud/story.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/thumbnail"
"github.com/ViBiOh/httputils/v4/pkg/concurrent"
Expand Down Expand Up @@ -35,7 +34,7 @@ func (a App) story(r *http.Request, request provider.Request, item absto.Item, f
}
})

var exifs map[string]exas.Exif
var exifs map[string]provider.Metadata
wg.Go(func() {
var err error

Expand Down
13 changes: 6 additions & 7 deletions pkg/exif/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/version"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
Expand All @@ -23,9 +22,9 @@ func redisKey(item absto.Item) string {
return version.Redis("exif:" + item.ID)
}

func (a App) GetExifFor(ctx context.Context, item absto.Item) (exas.Exif, error) {
func (a App) GetMetadataFor(ctx context.Context, item absto.Item) (provider.Metadata, error) {
if item.IsDir {
return exas.Exif{}, nil
return provider.Metadata{}, nil
}

ctx, end := tracer.StartSpan(ctx, a.tracer, "get_exif")
Expand All @@ -34,7 +33,7 @@ func (a App) GetExifFor(ctx context.Context, item absto.Item) (exas.Exif, error)
return a.exifCacheApp.Get(ctx, item)
}

func (a App) ListExifFor(ctx context.Context, items ...absto.Item) (map[string]exas.Exif, error) {
func (a App) ListExifFor(ctx context.Context, items ...absto.Item) (map[string]provider.Metadata, error) {
ctx, end := tracer.StartSpan(ctx, a.tracer, "list_exif")
defer end()

Expand All @@ -43,7 +42,7 @@ func (a App) ListExifFor(ctx context.Context, items ...absto.Item) (map[string]e
return nil, fmt.Errorf("list: %w", err)
}

output := make(map[string]exas.Exif, len(items))
output := make(map[string]provider.Metadata, len(items))
exifsLen := len(exifs)

for index, item := range items {
Expand Down Expand Up @@ -87,7 +86,7 @@ func (a App) ListAggregateFor(ctx context.Context, items ...absto.Item) (map[str
return output, nil
}

func (a App) SaveExifFor(ctx context.Context, item absto.Item, exif exas.Exif) error {
func (a App) SaveExifFor(ctx context.Context, item absto.Item, exif provider.Metadata) error {
return a.exifCacheApp.EvictOnSuccess(ctx, item, a.saveMetadata(ctx, item, exif))
}

Expand Down Expand Up @@ -123,7 +122,7 @@ func (a App) computeAndSaveAggregate(ctx context.Context, dir absto.Item) error
return nil
}

exifData, err := a.GetExifFor(ctx, item)
exifData, err := a.GetMetadataFor(ctx, item)
if err != nil {
if absto.IsNotExist(err) {
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/exif/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ func (a App) AMQPHandler(ctx context.Context, message amqp.Delivery) error {
return nil
}

exif, err := a.GetExifFor(ctx, resp.Item)
exif, err := a.GetMetadataFor(ctx, resp.Item)
if err != nil && !absto.IsNotExist(err) {
logger.WithField("item", resp.Item.Pathname).Error("load exif: %s", err)
}

resp.Exif.Description = exif.Description
exif.Exif = resp.Exif

if err := a.SaveExifFor(ctx, resp.Item, resp.Exif); err != nil {
if err := a.SaveExifFor(ctx, resp.Item, exif); err != nil {
return fmt.Errorf("save: %w", err)
}

return a.processExif(ctx, resp.Item, resp.Exif, true)
return a.processExif(ctx, resp.Item, exif, true)
}
4 changes: 2 additions & 2 deletions pkg/exif/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"

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

func (a App) updateDate(ctx context.Context, item absto.Item, data exas.Exif) error {
func (a App) updateDate(ctx context.Context, item absto.Item, data provider.Metadata) error {
if data.Date.IsZero() || item.Date.Equal(data.Date) {
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/exif/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/logger"
)
Expand Down Expand Up @@ -112,7 +111,7 @@ func (a App) handleUploadEvent(ctx context.Context, item absto.Item, aggregate b
return a.processExif(ctx, item, exif, aggregate)
}

func (a App) processExif(ctx context.Context, item absto.Item, exif exas.Exif, aggregate bool) error {
func (a App) processExif(ctx context.Context, item absto.Item, exif provider.Metadata, aggregate bool) error {
if err := a.updateDate(ctx, item, exif); err != nil {
return fmt.Errorf("update date: %w", err)
}
Expand Down
25 changes: 12 additions & 13 deletions pkg/exif/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type App struct {
listStorageApp absto.Storage
exifMetric *prometheus.CounterVec
aggregateMetric *prometheus.CounterVec
exifCacheApp cache.App[absto.Item, exas.Exif]
exifCacheApp cache.App[absto.Item, provider.Metadata]
aggregateCacheApp cache.App[absto.Item, provider.Aggregate]

redisClient redis.App
Expand Down Expand Up @@ -104,9 +104,9 @@ func New(config Config, storageApp absto.Storage, prometheusRegisterer prometheu
aggregateMetric: prom.CounterVec(prometheusRegisterer, "fibr", "aggregate", "item", "state"),
}

app.exifCacheApp = cache.New(redisClient, redisKey, func(ctx context.Context, item absto.Item) (exas.Exif, error) {
app.exifCacheApp = cache.New(redisClient, redisKey, func(ctx context.Context, item absto.Item) (provider.Metadata, error) {
if item.IsDir {
return exas.Exif{}, errInvalidItemType
return provider.Metadata{}, errInvalidItemType
}

return app.loadExif(ctx, item)
Expand Down Expand Up @@ -139,30 +139,29 @@ func (a App) enabled() bool {
return !a.exifRequest.IsZero()
}

func (a App) extractAndSaveExif(ctx context.Context, item absto.Item) (exif exas.Exif, err error) {
exif, err = a.extractExif(ctx, item)
func (a App) extractAndSaveExif(ctx context.Context, item absto.Item) (provider.Metadata, error) {
exif, err := a.extractExif(ctx, item)
if err != nil {
err = fmt.Errorf("extract exif: %w", err)
return
return provider.Metadata{}, fmt.Errorf("extract exif: %w", err)
}

previousExif, err := a.GetExifFor(ctx, item)
metadata, err := a.GetMetadataFor(ctx, item)
if err != nil && !absto.IsNotExist(err) {
logger.WithField("item", item.Pathname).Error("load exif: %s", err)
}

exif.Description = previousExif.Description
metadata.Exif = exif

if exif.IsZero() {
logger.WithField("item", item.Pathname).Debug("no exif")
return
return metadata, nil
}

if err = a.SaveExifFor(ctx, item, exif); err != nil {
err = fmt.Errorf("save exif: %w", err)
if err = a.SaveExifFor(ctx, item, metadata); err != nil {
return metadata, fmt.Errorf("save exif: %w", err)
}

return
return metadata, nil
}

func (a App) extractExif(ctx context.Context, item absto.Item) (exif exas.Exif, err error) {
Expand Down
7 changes: 3 additions & 4 deletions pkg/exif/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"

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

Expand All @@ -28,16 +27,16 @@ func (a App) hasMetadata(ctx context.Context, item absto.Item) bool {
return err == nil
}

data, err := a.GetExifFor(ctx, item)
data, err := a.GetMetadataFor(ctx, item)
if err != nil {
return false
}

return data.HasData()
}

func (a App) loadExif(ctx context.Context, item absto.Item) (exas.Exif, error) {
return loadMetadata[exas.Exif](ctx, a.storageApp, item)
func (a App) loadExif(ctx context.Context, item absto.Item) (provider.Metadata, error) {
return loadMetadata[provider.Metadata](ctx, a.storageApp, item)
}

func (a App) loadAggregate(ctx context.Context, item absto.Item) (provider.Aggregate, error) {
Expand Down
Loading

0 comments on commit 8accb94

Please sign in to comment.