Skip to content

Commit

Permalink
fix: fix create folder logic
Browse files Browse the repository at this point in the history
  • Loading branch information
flywukong committed Nov 22, 2023
1 parent d7ab762 commit a1464f8
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions cmd/cmd_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,25 +373,33 @@ func uploadFolder(urlInfo string, ctx *cli.Context,
return errors.New("failed to parse folder path with recursive flag")
}

lastDir := filepath.Base(folderName)
baseDir := filepath.Base(folderName)

fileInfos := make([]os.FileInfo, 0)
filePaths := make([]string, 0)

objectNames := make([]string, 0)
listFolderErr := filepath.Walk(folderName, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
fileInfos = append(fileInfos, info)
// use the base dir to construct object name of the file
index := strings.Index(path, baseDir)
if index == -1 {
return nil
}
objectNames = append(objectNames, path[index:])
filePaths = append(filePaths, path)
} else {
objectname := path + "/"
if path != folderName {
relativePath := strings.TrimPrefix(path, path+"/")
objectname = filepath.Join(lastDir, relativePath)
// use the base dir to construct object name of the sub-folder
index := strings.Index(path, baseDir)
if index == -1 {
return nil
}
fmt.Println("creating folder:", path)
if createFolderErr := uploadFile(bucketName, objectname, path, urlInfo, ctx, gnfdClient, true, false, 0); createFolderErr != nil {
subFolderName := path[index:] + "/"
fmt.Println("creating folder:", subFolderName)
if createFolderErr := uploadFile(bucketName, subFolderName, path, urlInfo, ctx, gnfdClient, true, false, 0); createFolderErr != nil {
return toCmdErr(createFolderErr)
}

}
return nil
})
Expand All @@ -401,7 +409,7 @@ func uploadFolder(urlInfo string, ctx *cli.Context,
}
// upload folder
for id, info := range fileInfos {
if uploadErr := uploadFile(bucketName, filePaths[id], filePaths[id], urlInfo, ctx, gnfdClient, false, false, info.Size()); uploadErr != nil {
if uploadErr := uploadFile(bucketName, objectNames[id], filePaths[id], urlInfo, ctx, gnfdClient, false, false, info.Size()); uploadErr != nil {
fmt.Printf("failed to upload object: %s, error:%v \n", filePaths[id], uploadErr)
}
}
Expand Down

0 comments on commit a1464f8

Please sign in to comment.