Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single fileops use DoMultiOperation #506

Merged
merged 19 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -40,7 +41,13 @@ var copyCmd = &cobra.Command{
remotePath := cmd.Flag("remotepath").Value.String()
destPath := cmd.Flag("destpath").Value.String()

err = allocationObj.CopyObject(remotePath, destPath)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationCopy,
RemotePath: remotePath,
DestPath: destPath,
},
})
if err != nil {
PrintError("Error performing CopyObject", err)
os.Exit(1)
Expand Down
12 changes: 7 additions & 5 deletions cmd/createdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -32,11 +33,12 @@ var createDirCmd = &cobra.Command{
}
dirname := cmd.Flag("dirname").Value.String()

if err != nil {
PrintError("CreateDir failed: ", err)
os.Exit(1)
}
err = allocationObj.CreateDir(dirname)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationCreateDir,
RemotePath: dirname,
},
})

if err != nil {
PrintError("CreateDir failed: ", err)
Expand Down
14 changes: 10 additions & 4 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
Expand All @@ -15,12 +16,12 @@ var deleteCmd = &cobra.Command{
Long: `delete file from blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if fflags.Changed("allocation") == false { // check if the flag "path" is set
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if !fflags.Changed("allocation") { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and return
}
if fflags.Changed("remotepath") == false {
if !fflags.Changed("remotepath") {
PrintError("Error: remotepath flag is missing")
os.Exit(1)
}
Expand All @@ -33,7 +34,12 @@ var deleteCmd = &cobra.Command{
}
remotePath := cmd.Flag("remotepath").Value.String()

err = allocationObj.DeleteFile(remotePath)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationDelete,
RemotePath: remotePath,
},
})
if err != nil {
PrintError("Delete failed.", err.Error())
os.Exit(1)
Expand Down
9 changes: 8 additions & 1 deletion cmd/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -38,7 +39,13 @@ var moveCmd = &cobra.Command{
remotePath := cmd.Flag("remotepath").Value.String()
destPath := cmd.Flag("destpath").Value.String()

err = allocationObj.MoveObject(remotePath, destPath)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationMove,
RemotePath: remotePath,
DestPath: destPath,
},
})
if err != nil {
PrintError("Error performing CopyObject", err)
os.Exit(1)
Expand Down
17 changes: 12 additions & 5 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/pathutil"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
Expand All @@ -16,17 +17,17 @@ var renameCmd = &cobra.Command{
Long: `rename an object on blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if fflags.Changed("allocation") == false { // check if the flag "path" is set
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if !fflags.Changed("allocation") { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and os.Exit(1)
}
if fflags.Changed("remotepath") == false {
if !fflags.Changed("remotepath") {
PrintError("Error: remotepath flag is missing")
os.Exit(1)
}

if fflags.Changed("destname") == false {
if !fflags.Changed("destname") {
PrintError("Error: destname flag is missing")
os.Exit(1)
}
Expand All @@ -44,7 +45,13 @@ var renameCmd = &cobra.Command{
return
}

err = allocationObj.RenameObject(remotePath, destName)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationRename,
RemotePath: remotePath,
DestName: destName,
},
})
if err != nil {
PrintError(err.Error())
os.Exit(1)
Expand Down
12 changes: 1 addition & 11 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,7 @@ var updateCmd = &cobra.Command{

wg := &sync.WaitGroup{}
statusBar := &StatusBar{wg: wg}
wg.Add(1)

err = startChunkedUpload(cmd, allocationObj, chunkedUploadArgs{
localPath: localPath,
remotePath: remotePath,
thumbnailPath: thumbnailPath,
encrypt: encrypt,
chunkNumber: updateChunkNumber,
isUpdate: true,
// isRepair: false,
}, statusBar)
err = singleUpload(allocationObj, localPath, remotePath, thumbnailPath, encrypt, false, true, updateChunkNumber, statusBar)

if err != nil {
PrintError("Update failed.", err)
Expand Down
39 changes: 27 additions & 12 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,7 @@ var uploadCmd = &cobra.Command{
if multiuploadJSON != "" {
err = multiUpload(allocationObj, localPath, multiuploadJSON, statusBar)
} else {
wg.Add(1)
err = startChunkedUpload(cmd, allocationObj,
chunkedUploadArgs{
localPath: localPath,
thumbnailPath: thumbnailPath,
remotePath: remotePath,
encrypt: encrypt,
webStreaming: webStreaming,
chunkNumber: uploadChunkNumber,
// isUpdate: false,
// isRepair: false,
}, statusBar)
err = singleUpload(allocationObj, localPath, remotePath, thumbnailPath, encrypt, webStreaming, false, uploadChunkNumber, statusBar)
}
if err != nil {
PrintError("Upload failed.", err.Error())
Expand Down Expand Up @@ -154,6 +143,7 @@ type MultiUploadOption struct {
ThumbnailPath string `json:"thumbnailPath,omitempty"`
Encrypt bool `json:"encrypt,omitempty"`
ChunkNumber int `json:"chunkNumber,omitempty"`
IsUpdate bool `json:"isUpdate,omitempty"`
}

func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions string, statusBar *StatusBar) error {
Expand All @@ -172,6 +162,31 @@ func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions
return err
}

return multiUploadWithOptions(allocationObj, workdir, options, statusBar)
}

func singleUpload(allocationObj *sdk.Allocation, localPath, remotePath, thumbnailPath string, encrypt, webStreaming, isUpdate bool, chunkNumber int, statusBar *StatusBar) error {
remotePath, fileName, err := fullPathAndFileNameForUpload(localPath, remotePath)
if err != nil {
return err
}
options := []MultiUploadOption{
{
FilePath: localPath,
FileName: fileName,
RemotePath: remotePath,
ThumbnailPath: thumbnailPath,
Encrypt: encrypt,
ChunkNumber: chunkNumber,
},
}

workdir := util.GetHomeDir()

return multiUploadWithOptions(allocationObj, workdir, options, statusBar)
}

func multiUploadWithOptions(allocationObj *sdk.Allocation, workdir string, options []MultiUploadOption, statusBar *StatusBar) error {
totalUploads := len(options)
filePaths := make([]string, totalUploads)
fileNames := make([]string, totalUploads)
Expand Down
Loading