Skip to content

Commit

Permalink
chore: Adding id conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Nov 5, 2022
1 parent 671cfe6 commit cba7989
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/exif/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func (a App) EventConsumer(ctx context.Context, e provider.Event) {

switch e.Type {
case provider.StartEvent:
if err := provider.RenamePreviousID(ctx, a.storageApp, e.Item, ".json", a.Rename); err != nil {
logger.Error("rename from previous ID: %s", err)
}
if err = a.handleStartEvent(ctx, e); err != nil {
getEventLogger(e.Item).Error("start: %s", err)
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package provider

import (
"context"
"crypto/sha256"
"encoding/hex"
"path"
"path/filepath"

absto "github.com/ViBiOh/absto/pkg/model"
Expand Down Expand Up @@ -37,3 +41,24 @@ func MetadataDirectory(item absto.Item) string {

return Dirname(MetadataDirectoryName + pathname)
}

func itemPreviousID(item absto.Item) string {
hasher := sha256.New()
_, _ = hasher.Write([]byte(item.Pathname))
return hex.EncodeToString(hasher.Sum(nil))
}

func RenamePreviousID(ctx context.Context, storageApp absto.Storage, item absto.Item, suffix string, renameFunc func(context.Context, absto.Item, absto.Item) error) error {
if _, err := storageApp.Info(ctx, path.Join(MetadataDirectory(item), itemPreviousID(item)+suffix)); err != nil {
if absto.IsNotExist(err) {
return nil
}

return err
}

previous := item
previous.ID = itemPreviousID(previous)

return renameFunc(ctx, previous, item)
}
3 changes: 3 additions & 0 deletions pkg/thumbnail/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func (a App) EventConsumer(ctx context.Context, e provider.Event) {

switch e.Type {
case provider.StartEvent:
if err := provider.RenamePreviousID(ctx, a.storageApp, e.Item, ".webp", a.Rename); err != nil {
logger.Error("rename from previous ID: %s", err)
}
fallthrough
case provider.UploadEvent:
a.generateItem(ctx, e)
Expand Down

0 comments on commit cba7989

Please sign in to comment.