feat: rename then delete if storage doesn't support overwrite upload (close #3643)

pull/3720/head
Andy Hsu 2023-03-05 15:36:12 +08:00
parent 12a095a1d6
commit c915313ec9
3 changed files with 44 additions and 23 deletions

View File

@ -16,14 +16,15 @@ type Addition struct {
} }
var config = driver.Config{ var config = driver.Config{
Name: "AliyundriveOpen", Name: "AliyundriveOpen",
LocalSort: false, LocalSort: false,
OnlyLocal: false, OnlyLocal: false,
OnlyProxy: false, OnlyProxy: false,
NoCache: false, NoCache: false,
NoUpload: false, NoUpload: false,
NeedMs: false, NeedMs: false,
DefaultRoot: "root", DefaultRoot: "root",
NoOverwriteUpload: true,
} }
func init() { func init() {

View File

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

View File

@ -459,6 +459,7 @@ func Remove(ctx context.Context, storage driver.Driver, path string) error {
if err != nil { if err != nil {
// if object not found, it's ok // if object not found, it's ok
if errs.IsObjectNotFound(err) { if errs.IsObjectNotFound(err) {
log.Debugf("%s have been removed", path)
return nil return nil
} }
return errors.WithMessage(err, "failed to get object") return errors.WithMessage(err, "failed to get object")
@ -497,6 +498,8 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file *mo
// if file exist and size = 0, delete it // if file exist and size = 0, delete it
dstDirPath = utils.FixAndCleanPath(dstDirPath) dstDirPath = utils.FixAndCleanPath(dstDirPath)
dstPath := stdpath.Join(dstDirPath, file.GetName()) dstPath := stdpath.Join(dstDirPath, file.GetName())
tempName := file.GetName() + ".alist_to_delete"
tempPath := stdpath.Join(dstDirPath, tempName)
fi, err := GetUnwrap(ctx, storage, dstPath) fi, err := GetUnwrap(ctx, storage, dstPath)
if err == nil { if err == nil {
if fi.GetSize() == 0 { if fi.GetSize() == 0 {
@ -504,6 +507,12 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file *mo
if err != nil { if err != nil {
return errors.WithMessagef(err, "failed remove file that exist and have size 0") 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 { } else {
file.Old = fi file.Old = fi
} }
@ -542,10 +551,20 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file *mo
return errs.NotImplement return errs.NotImplement
} }
log.Debugf("put file [%s] done", file.GetName()) log.Debugf("put file [%s] done", file.GetName())
//if err == nil { if storage.Config().NoOverwriteUpload && fi != nil && fi.GetSize() > 0 {
// //clear cache if err != nil {
// key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath) // upload failed, recover old obj
// listCache.Del(key) 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) return errors.WithStack(err)
} }