Skip to content

Commit

Permalink
feat: Detecting orphans file for S3
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 7, 2023
1 parent dd88766 commit b149441
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/ViBiOh/ChatPotte v0.2.29
github.com/ViBiOh/absto v1.2.9
github.com/ViBiOh/absto v1.3.1
github.com/ViBiOh/auth/v2 v2.14.23
github.com/ViBiOh/exas v0.6.0
github.com/ViBiOh/flags v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/ViBiOh/ChatPotte v0.2.29 h1:8cIZDvkTfKOC4bZaQkxyOM47BcXZQR8ykJPl9LsZpMM=
github.com/ViBiOh/ChatPotte v0.2.29/go.mod h1:GXEpgMQkN0R3CHpqVbuMhS8F6U1S/Qb7qQJ2juFqTtc=
github.com/ViBiOh/absto v1.2.9 h1:ZrocMKKfknsjgDSom6IjMU/tGaIgI4XiPzOgRSTR/E0=
github.com/ViBiOh/absto v1.2.9/go.mod h1:RXbNesRB3Q9BWMxLeT3DXX29bhk5Tqifo2gssPtQVnk=
github.com/ViBiOh/absto v1.3.1 h1:9UY2fAgd7GD+zFCoOVIbAOKEzLF8veAoBc6mG/tqQsY=
github.com/ViBiOh/absto v1.3.1/go.mod h1:RXbNesRB3Q9BWMxLeT3DXX29bhk5Tqifo2gssPtQVnk=
github.com/ViBiOh/auth/v2 v2.14.23 h1:ZeQ2/tsh4Z3Jcu/y9R4X05uyhBUJeUC6UN8XyaKJ2s4=
github.com/ViBiOh/auth/v2 v2.14.23/go.mod h1:pIMuBueO3sknvnvfAFJBOxTjoGWuxke6lp1ZSh6478w=
github.com/ViBiOh/exas v0.6.0 h1:UWQuzesHShBSMkQw7mWAj4NBwL1Hb/fDcgzk/UpacYQ=
Expand Down
22 changes: 21 additions & 1 deletion pkg/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ import (
"golang.org/x/crypto/bcrypt"
)

type Items []absto.Item

func (i Items) FindDirectory(name string) (absto.Item, bool) {
for _, item := range i {
if item.IsDir && absto.Dirname(item.Pathname) == name {
return item, true
}
}

return absto.Item{}, false
}

var (
ErrNotAuthorized = errors.New("you're not authorized to do this ⛔")
ErrEmptyName = errors.New("name is empty")
Expand Down Expand Up @@ -117,7 +129,7 @@ func (a App) start(ctx context.Context) {

done := ctx.Done()

var directories []absto.Item
var directories Items

err := a.storageApp.Walk(ctx, "", func(item absto.Item) error {
select {
Expand All @@ -132,6 +144,8 @@ func (a App) start(ctx context.Context) {
directories = append(directories, item)
} else {
a.notify(ctx, provider.NewStartEvent(item))

a.sanitizeOrphan(directories, item)
}

return nil
Expand Down Expand Up @@ -164,6 +178,12 @@ func (a App) sanitizeName(ctx context.Context, item absto.Item) absto.Item {
return a.rename(ctx, item, name)
}

func (a App) sanitizeOrphan(directories Items, item absto.Item) {
if _, ok := directories.FindDirectory(item.Dir()); !ok {
logger.Warn("File with name `%s` doesn't have a parent directory", item.Pathname)
}
}

func (a App) rename(ctx context.Context, item absto.Item, name string) absto.Item {
logger.Info("Renaming `%s` to `%s`", item.Pathname, name)

Expand Down

0 comments on commit b149441

Please sign in to comment.