diff --git a/storage/minio/client.go b/storage/minio/client.go index a9e903f..3380edf 100644 --- a/storage/minio/client.go +++ b/storage/minio/client.go @@ -13,6 +13,7 @@ import ( storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" + "github.com/rs/xid" ) type Minio struct { @@ -72,6 +73,11 @@ func (m *Minio) Save(ctx context.Context, r io.Reader, storagePath string) error candidate := storagePath for i := 1; m.Exists(ctx, candidate); i++ { candidate = fmt.Sprintf("%s_%d%s", base, i, ext) + if i > 1000 { + m.logger.Errorf("Too many attempts to find a unique filename for %s", storagePath) + candidate = fmt.Sprintf("%s_%s%s", base, xid.New().String(), ext) + break + } } size := int64(-1) if length := ctx.Value(ctxkey.ContentLength); length != nil { diff --git a/storage/webdav/webdav.go b/storage/webdav/webdav.go index e1ba082..a887de8 100644 --- a/storage/webdav/webdav.go +++ b/storage/webdav/webdav.go @@ -12,6 +12,7 @@ import ( "github.com/charmbracelet/log" config "github.com/krau/SaveAny-Bot/config/storage" storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage" + "github.com/rs/xid" ) type Webdav struct { @@ -56,6 +57,11 @@ func (w *Webdav) Save(ctx context.Context, r io.Reader, storagePath string) erro candidate := storagePath for i := 1; w.Exists(ctx, candidate); i++ { candidate = fmt.Sprintf("%s_%d%s", base, i, ext) + if i > 1000 { + w.logger.Errorf("Too many attempts to find a unique filename for %s", storagePath) + candidate = fmt.Sprintf("%s_%s%s", base, xid.New().String(), ext) + break + } } if err := w.client.MkDir(ctx, path.Dir(candidate)); err != nil {