Skip to content

Commit

Permalink
refactor: Don't fail if not exif when creating a description
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Sep 27, 2022
1 parent b512eeb commit 76d3f60
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pkg/crud/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
Expand Down Expand Up @@ -168,7 +169,7 @@ func (a App) handlePostDescription(w http.ResponseWriter, r *http.Request, reque
}

exif, err := a.exifApp.GetExifFor(ctx, item)
if err != nil {
if err != nil && !absto.IsNotExist(err) {
a.error(w, r, request, err)
return
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/exif/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ func (a App) hasMetadata(ctx context.Context, item absto.Item) bool {
}

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

func (a App) loadAggregate(ctx context.Context, item absto.Item) (provider.Aggregate, error) {
var data provider.Aggregate
return data, a.loadMetadata(ctx, item, &data)
return loadMetadata[provider.Aggregate](ctx, a.storageApp, item)
}

func (a App) loadMetadata(ctx context.Context, item absto.Item, content any) error {
return provider.LoadJSON(ctx, a.storageApp, Path(item), content)
func loadMetadata[T any](ctx context.Context, storageApp absto.Storage, item absto.Item) (T, error) {
return provider.LoadJSON[T](ctx, storageApp, Path(item))
}

func (a App) saveMetadata(ctx context.Context, item absto.Item, data any) error {
Expand Down
7 changes: 4 additions & 3 deletions pkg/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,19 @@ func RemoveIndex(arr []string, index int) []string {
return append(arr[:index], arr[index+1:]...)
}

func LoadJSON(ctx context.Context, storageApp absto.Storage, filename string, content any) (err error) {
func LoadJSON[T any](ctx context.Context, storageApp absto.Storage, filename string) (output T, err error) {
var reader io.ReadCloser
reader, err = storageApp.ReadFrom(ctx, filename)
if err != nil {
return fmt.Errorf("read: %w", err)
err = fmt.Errorf("read: %w", err)
return
}

defer func() {
err = HandleClose(reader, err)
}()

if err = json.NewDecoder(reader).Decode(content); err != nil {
if err = json.NewDecoder(reader).Decode(&output); err != nil {
err = fmt.Errorf("decode: %w", storageApp.ConvertError(err))
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func (a *App) loadShares(ctx context.Context) error {
}

func (a *App) refresh(ctx context.Context) error {
if err := provider.LoadJSON(ctx, a.storageApp, shareFilename, &a.shares); err != nil {
var err error
if a.shares, err = provider.LoadJSON[map[string]provider.Share](ctx, a.storageApp, shareFilename); err != nil {
if !absto.IsNotExist(err) {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ func (a *App) Start(ctx context.Context) {
}

func (a *App) loadWebhooks(ctx context.Context) error {
if err := provider.LoadJSON(ctx, a.storageApp, webhookFilename, &a.webhooks); err != nil {
var err error

if a.webhooks, err = provider.LoadJSON[map[string]provider.Webhook](ctx, a.storageApp, webhookFilename); err != nil {
if !absto.IsNotExist(err) {
return err
}
Expand Down

0 comments on commit 76d3f60

Please sign in to comment.