feat: check parent dir before upload

refactor/fs
Noah Hsu 2022-06-15 19:20:36 +08:00
parent 083395ee53
commit d9eb188b7a
2 changed files with 22 additions and 7 deletions

View File

@ -130,5 +130,22 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
}
func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {
f, err := Get(ctx, account, parentPath)
if err != nil {
// if parent dir not exists, create it
if driver.IsErrObjectNotFound(err) {
err = MakeDir(ctx, account, parentPath)
if err != nil {
return errors.WithMessagef(err, "failed to make parent dir [%s]", parentPath)
}
} else {
return errors.WithMessage(err, "failed to get parent dir")
}
} else {
// object exists, check if it is a dir
if !f.IsDir() {
return errors.Errorf("object [%s] is not a dir", parentPath)
}
}
return account.Put(ctx, parentPath, file)
}

View File

@ -1,12 +1,10 @@
// manage task, such as file upload, file copy between accounts, offline download, etc.
package task
import "context"
type Task struct {
Name string
Func func(context.Context) error
Status string
Error error
Finish bool
Name string
Status string
Error error
Finish bool
Children []*Task
}