Skip to content

Commit

Permalink
feat: rename then delete if storage doesn't support overwrite upload (c…
Browse files Browse the repository at this point in the history
…lose #3643)
  • Loading branch information
xhofe committed Mar 5, 2023
1 parent 12a095a commit c915313
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
17 changes: 9 additions & 8 deletions drivers/aliyundrive_open/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ type Addition struct {
}

var config = driver.Config{
Name: "AliyundriveOpen",
LocalSort: false,
OnlyLocal: false,
OnlyProxy: false,
NoCache: false,
NoUpload: false,
NeedMs: false,
DefaultRoot: "root",
Name: "AliyundriveOpen",
LocalSort: false,
OnlyLocal: false,
OnlyProxy: false,
NoCache: false,
NoUpload: false,
NeedMs: false,
DefaultRoot: "root",
NoOverwriteUpload: true,
}

func init() {
Expand Down
21 changes: 11 additions & 10 deletions internal/driver/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package driver

type Config struct {
Name string `json:"name"`
LocalSort bool `json:"local_sort"`
OnlyLocal bool `json:"only_local"`
OnlyProxy bool `json:"only_proxy"`
NoCache bool `json:"no_cache"`
NoUpload bool `json:"no_upload"`
NeedMs bool `json:"need_ms"` // if need get message from user, such as validate code
DefaultRoot string `json:"default_root"`
CheckStatus bool `json:"-"`
Alert string `json:"alert"` //info,success,warning,danger
Name string `json:"name"`
LocalSort bool `json:"local_sort"`
OnlyLocal bool `json:"only_local"`
OnlyProxy bool `json:"only_proxy"`
NoCache bool `json:"no_cache"`
NoUpload bool `json:"no_upload"`
NeedMs bool `json:"need_ms"` // if need get message from user, such as validate code
DefaultRoot string `json:"default_root"`
CheckStatus bool `json:"-"`
Alert string `json:"alert"` //info,success,warning,danger
NoOverwriteUpload bool `json:"-"`
}

func (c Config) MustProxy() bool {
Expand Down
29 changes: 24 additions & 5 deletions internal/op/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ func Remove(ctx context.Context, storage driver.Driver, path string) error {
if err != nil {
// if object not found, it's ok
if errs.IsObjectNotFound(err) {
log.Debugf("%s have been removed", path)
return nil
}
return errors.WithMessage(err, "failed to get object")
Expand Down Expand Up @@ -497,13 +498,21 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file *mo
// if file exist and size = 0, delete it
dstDirPath = utils.FixAndCleanPath(dstDirPath)
dstPath := stdpath.Join(dstDirPath, file.GetName())
tempName := file.GetName() + ".alist_to_delete"
tempPath := stdpath.Join(dstDirPath, tempName)
fi, err := GetUnwrap(ctx, storage, dstPath)
if err == nil {
if fi.GetSize() == 0 {
err = Remove(ctx, storage, dstPath)
if err != nil {
return errors.WithMessagef(err, "failed remove file that exist and have size 0")
}
} else if storage.Config().NoOverwriteUpload {
// try to rename old obj
err = Rename(ctx, storage, dstPath, tempName)
if err != nil {
return err
}
} else {
file.Old = fi
}
Expand Down Expand Up @@ -542,10 +551,20 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file *mo
return errs.NotImplement
}
log.Debugf("put file [%s] done", file.GetName())
//if err == nil {
// //clear cache
// key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath)
// listCache.Del(key)
//}
if storage.Config().NoOverwriteUpload && fi != nil && fi.GetSize() > 0 {
if err != nil {
// upload failed, recover old obj
err := Rename(ctx, storage, tempPath, file.GetName())
if err != nil {
log.Errorf("failed recover old obj: %+v", err)
}
} else {
// upload success, remove old obj
err := Remove(ctx, storage, tempPath)
if err != nil {
return err
}
}
}
return errors.WithStack(err)
}

1 comment on commit c915313

@LisonFan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

夸克也是同样的问题,是不是也可以同样使用这个方式处理

Please sign in to comment.