🎨 Optimize code structure

This commit is contained in:
微凉
2022-01-03 20:06:36 +08:00
parent e7ba289d06
commit 7cf30836bf
18 changed files with 269 additions and 342 deletions

View File

@ -158,7 +158,7 @@ func (driver AliDrive) RefreshToken(account *model.Account) error {
return nil
}
func (driver AliDrive) Rename(fileId, name string, account *model.Account) error {
func (driver AliDrive) rename(fileId, name string, account *model.Account) error {
var resp base.Json
var e AliRespError
_, err := aliClient.R().SetResult(&resp).SetError(&e).
@ -179,7 +179,7 @@ func (driver AliDrive) Rename(fileId, name string, account *model.Account) error
return err
} else {
_ = model.SaveAccount(account)
return driver.Rename(fileId, name, account)
return driver.rename(fileId, name, account)
}
}
return fmt.Errorf("%s", e.Message)
@ -190,7 +190,7 @@ func (driver AliDrive) Rename(fileId, name string, account *model.Account) error
return fmt.Errorf("%+v", resp)
}
func (driver AliDrive) Batch(srcId,dstId string, account *model.Account) error {
func (driver AliDrive) batch(srcId, dstId string, account *model.Account) error {
var e AliRespError
res, err := aliClient.R().SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
@ -200,13 +200,13 @@ func (driver AliDrive) Batch(srcId,dstId string, account *model.Account) error {
"headers": base.Json{
"Content-Type": "application/json",
},
"method":"POST",
"id":srcId,
"body":base.Json{
"drive_id": account.DriveId,
"file_id":srcId,
"to_drive_id":account.DriveId,
"to_parent_file_id":dstId,
"method": "POST",
"id": srcId,
"body": base.Json{
"drive_id": account.DriveId,
"file_id": srcId,
"to_drive_id": account.DriveId,
"to_parent_file_id": dstId,
},
},
},
@ -222,7 +222,7 @@ func (driver AliDrive) Batch(srcId,dstId string, account *model.Account) error {
return err
} else {
_ = model.SaveAccount(account)
return driver.Batch(srcId, dstId, account)
return driver.batch(srcId, dstId, account)
}
}
return fmt.Errorf("%s", e.Message)

View File

@ -286,34 +286,32 @@ func (driver AliDrive) MakeDir(path string, account *model.Account) error {
return fmt.Errorf("%s", e.Message)
}
if resp["file_name"] == name {
_ = base.DeleteCache(dir, account)
return nil
}
return fmt.Errorf("%+v", resp)
}
func (driver AliDrive) Move(src string, dst string, account *model.Account) error {
srcDir, _ := filepath.Split(src)
dstDir, dstName := filepath.Split(dst)
dstDir, _ := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
// rename
if srcDir == dstDir {
err = driver.Rename(srcFile.Id, dstName, account)
} else {
// move
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
}
err = driver.Batch(srcFile.Id, dstDirFile.Id, account)
}
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
_ = base.DeleteCache(srcDir, account)
_ = base.DeleteCache(dstDir, account)
return err
}
err = driver.batch(srcFile.Id, dstDirFile.Id, account)
return err
}
func (driver AliDrive) Rename(src string, dst string, account *model.Account) error {
_, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
err = driver.rename(srcFile.Id, dstName, account)
return err
}
@ -349,7 +347,6 @@ func (driver AliDrive) Delete(path string, account *model.Account) error {
return fmt.Errorf("%s", e.Message)
}
if res.StatusCode() == 204 {
_ = base.DeleteCache(utils.Dir(path), account)
return nil
}
return errors.New(res.String())
@ -471,7 +468,6 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return fmt.Errorf("%s", e.Message)
}
if resp2["file_id"] == resp.FileId {
_ = base.DeleteCache(file.ParentPath, account)
return nil
}
return fmt.Errorf("%+v", resp2)