feat: rename file only when storagePath exists

This commit is contained in:
krau
2025-06-11 09:54:08 +08:00
parent 9d3a3a8dcd
commit e85d3c9441
8 changed files with 59 additions and 19 deletions

View File

@@ -147,3 +147,8 @@ func (a *Alist) NotSupportStream() string {
func (a *Alist) JoinStoragePath(task types.Task) string {
return path.Join(a.config.BasePath, task.StoragePath)
}
func (a *Alist) Exists(ctx context.Context, storagePath string) bool {
// TODO: Implement it.
return false
}

View File

@@ -63,3 +63,11 @@ func (l *Local) Save(ctx context.Context, r io.Reader, storagePath string) error
_, err = io.Copy(file, r)
return err
}
func (l *Local) Exists(ctx context.Context, storagePath string) bool {
absPath, err := filepath.Abs(storagePath)
if err != nil {
return false
}
return fileutil.IsExist(absPath)
}

View File

@@ -70,3 +70,17 @@ func (m *Minio) Save(ctx context.Context, r io.Reader, storagePath string) error
return nil
}
func (m *Minio) Exists(ctx context.Context, storagePath string) bool {
common.Log.Debugf("Checking if file exists at %s", storagePath)
// TODO: test it.
_, err := m.client.StatObject(ctx, m.config.BucketName, storagePath, minio.StatObjectOptions{})
if err != nil {
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
return false // File does not exist
}
return false
}
return true
}

View File

@@ -21,6 +21,7 @@ type Storage interface {
Name() string
JoinStoragePath(task types.Task) string
Save(ctx context.Context, reader io.Reader, storagePath string) error
Exists(ctx context.Context, storagePath string) bool
}
type StorageNotSupportStream interface {

View File

@@ -5,4 +5,5 @@ import "errors"
var (
ErrFailedToCreateDirectory = errors.New("webdav: failed to create directory")
ErrFailedToWriteFile = errors.New("webdav: failed to write file")
ErrFailedToCheckFileExists = errors.New("webdav: failed to check if file exists")
)

View File

@@ -57,3 +57,13 @@ func (w *Webdav) Save(ctx context.Context, r io.Reader, storagePath string) erro
}
return nil
}
func (w *Webdav) Exists(ctx context.Context, storagePath string) bool {
common.Log.Debugf("Checking if file exists at %s", storagePath)
exists, err := w.client.Exists(ctx, storagePath)
if err != nil {
common.Log.Errorf("Failed to check if file exists at %s: %v", storagePath, err)
return false
}
return exists
}