diff --git a/cmd/copy.go b/cmd/copy.go index 5f2e5002..045d3098 100644 --- a/cmd/copy.go +++ b/cmd/copy.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/spf13/cobra" ) @@ -40,9 +41,15 @@ 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) + PrintError("Copy failed.", err) os.Exit(1) } diff --git a/cmd/createdir.go b/cmd/createdir.go index 2decc0a9..12b0ec0d 100644 --- a/cmd/createdir.go +++ b/cmd/createdir.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/spf13/cobra" ) @@ -32,14 +33,15 @@ 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) + PrintError("Directory creation failed.", err) os.Exit(1) } diff --git a/cmd/delete.go b/cmd/delete.go index 2569ebeb..9259efcf 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/spf13/cobra" ) @@ -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) } @@ -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) diff --git a/cmd/download.go b/cmd/download.go index eeff0bf0..35dfb9c5 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -139,7 +139,7 @@ var downloadCmd = &cobra.Command{ } } } else if len(remotePath) > 0 { - if fflags.Changed("allocation") == false { // check if the flag "path" is set + 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 } @@ -168,7 +168,7 @@ var downloadCmd = &cobra.Command{ } } } else if len(multidownloadJSON) > 0 { - if fflags.Changed("allocation") == false { // check if the flag "path" is set + 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 } diff --git a/cmd/move.go b/cmd/move.go index b1b47c59..d3909727 100644 --- a/cmd/move.go +++ b/cmd/move.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/spf13/cobra" ) @@ -38,9 +39,15 @@ 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) + PrintError("Move failed.", err) os.Exit(1) } diff --git a/cmd/rename.go b/cmd/rename.go index 59e8877e..3822a4af 100644 --- a/cmd/rename.go +++ b/cmd/rename.go @@ -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" @@ -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) } @@ -44,9 +45,15 @@ 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()) + PrintError("Rename failed.", err) os.Exit(1) } fmt.Println(remotePath + " renamed") diff --git a/cmd/update.go b/cmd/update.go index eaf2f673..a6071799 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -16,11 +16,11 @@ var updateCmd = &cobra.Command{ Args: cobra.MinimumNArgs(0), Run: func(cmd *cobra.Command, args []string) { fflags := cmd.Flags() - if fflags.Changed("allocation") == false { + if !fflags.Changed("allocation") { PrintError("Error: allocation flag is missing") os.Exit(1) } - if fflags.Changed("remotepath") == false { + if !fflags.Changed("remotepath") { PrintError("Error: remotepath flag is missing") os.Exit(1) } @@ -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) diff --git a/cmd/upload.go b/cmd/upload.go index 8edaf7ed..420267e8 100644 --- a/cmd/upload.go +++ b/cmd/upload.go @@ -61,21 +61,10 @@ 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()) + PrintError("Upload failed.", err) os.Exit(1) } wg.Wait() @@ -97,7 +86,7 @@ type chunkedUploadArgs struct { isRepair bool } -func startChunkedUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, args chunkedUploadArgs, statusBar sdk.StatusCallback) error { +func startChunkedUpload(allocationObj *sdk.Allocation, args chunkedUploadArgs, statusBar sdk.StatusCallback) error { fileReader, err := os.Open(args.localPath) if err != nil { return err @@ -148,13 +137,14 @@ func startChunkedUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, args } type MultiUploadOption struct { - FilePath string `json:"filePath,omitempty"` - FileName string `json:"fileName,omitempty"` - RemotePath string `json:"remotePath,omitempty"` - ThumbnailPath string `json:"thumbnailPath,omitempty"` - Encrypt bool `json:"encrypt,omitempty"` - ChunkNumber int `json:"chunkNumber,omitempty"` - IsUpdate bool `json:"isUpdate,omitempty"` + FilePath string `json:"filePath,omitempty"` + FileName string `json:"fileName,omitempty"` + RemotePath string `json:"remotePath,omitempty"` + ThumbnailPath string `json:"thumbnailPath,omitempty"` + Encrypt bool `json:"encrypt,omitempty"` + ChunkNumber int `json:"chunkNumber,omitempty"` + IsUpdate bool `json:"isUpdate,omitempty"` + IsWebstreaming bool `json:"isWebstreaming,omitempty"` } func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions string, statusBar *StatusBar) error { @@ -173,6 +163,34 @@ 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, isWebstreaming, isUpdate bool, chunkNumber int, statusBar *StatusBar) error { + fullRemotePath, fileName, err := fullPathAndFileNameForUpload(localPath, remotePath) + if err != nil { + return err + } + remotePath = pathutil.Dir(fullRemotePath) + "/" + options := []MultiUploadOption{ + { + FilePath: localPath, + FileName: fileName, + RemotePath: remotePath, + ThumbnailPath: thumbnailPath, + Encrypt: encrypt, + ChunkNumber: chunkNumber, + IsUpdate: isUpdate, + IsWebstreaming: isWebstreaming, + }, + } + + 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) @@ -181,6 +199,7 @@ func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions chunkNumbers := make([]int, totalUploads) encrypts := make([]bool, totalUploads) isUpdates := make([]bool, totalUploads) + isWebstreaming := make([]bool, totalUploads) for idx, option := range options { statusBar.wg.Add(1) filePaths[idx] = option.FilePath @@ -190,9 +209,10 @@ func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions chunkNumbers[idx] = option.ChunkNumber encrypts[idx] = option.Encrypt isUpdates[idx] = option.IsUpdate + isWebstreaming[idx] = option.IsWebstreaming } - return allocationObj.StartMultiUpload(workdir, filePaths, fileNames, thumbnailPaths, encrypts, chunkNumbers, remotePaths, isUpdates, statusBar) + return allocationObj.StartMultiUpload(workdir, filePaths, fileNames, thumbnailPaths, encrypts, chunkNumbers, remotePaths, isUpdates, isWebstreaming, statusBar) } func init() { diff --git a/go.mod b/go.mod index c7ed8346..1ea6c06f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/0chain/errors v1.0.3 - github.com/0chain/gosdk v1.8.18-0.20230823172043-ea887d53014a + github.com/0chain/gosdk v1.8.18-0.20230827195410-39deea65eca2 github.com/icza/bitio v1.1.0 github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.6.0 diff --git a/go.sum b/go.sum index 033f8bd1..04ea921d 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 h1:z+DtCR8mBsjPnEs github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565/go.mod h1:UyDC8Qyl5z9lGkCnf9RHJPMektnFX8XtCJZHXCCVj8E= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= -github.com/0chain/gosdk v1.8.18-0.20230823172043-ea887d53014a h1:SObqSMyLF+RAP+gLVN02DelKeAYDYAf3wa8f9RxuVIs= -github.com/0chain/gosdk v1.8.18-0.20230823172043-ea887d53014a/go.mod h1:3NKNYzmnMIYqZwwwOgZwMmTW1DT1ZUAmKyVPmYQOiT4= +github.com/0chain/gosdk v1.8.18-0.20230827195410-39deea65eca2 h1:zfDlOSfXsTBgCF4XC4x01Jo7brQRewppYkpAop+KM28= +github.com/0chain/gosdk v1.8.18-0.20230827195410-39deea65eca2/go.mod h1:3NKNYzmnMIYqZwwwOgZwMmTW1DT1ZUAmKyVPmYQOiT4= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Luzifer/go-openssl/v3 v3.1.0 h1:QqKqo6kYXGGUsvtUoCpRZm8lHw+jDfhbzr36gVj+/gw=