Skip to content

Commit 46d3112

Browse files
Merge pull request #513 from 0chain/sprint-1.10
Sprint 1.10
2 parents 04d9fdc + 8f2b18e commit 46d3112

File tree

10 files changed

+101
-59
lines changed

10 files changed

+101
-59
lines changed

cmd/copy.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/0chain/gosdk/constants"
78
"github.com/0chain/gosdk/zboxcore/sdk"
89
"github.com/spf13/cobra"
910
)
@@ -40,9 +41,15 @@ var copyCmd = &cobra.Command{
4041
remotePath := cmd.Flag("remotepath").Value.String()
4142
destPath := cmd.Flag("destpath").Value.String()
4243

43-
err = allocationObj.CopyObject(remotePath, destPath)
44+
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
45+
{
46+
OperationType: constants.FileOperationCopy,
47+
RemotePath: remotePath,
48+
DestPath: destPath,
49+
},
50+
})
4451
if err != nil {
45-
PrintError("Error performing CopyObject", err)
52+
PrintError("Copy failed.", err)
4653
os.Exit(1)
4754
}
4855

cmd/createdir.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/0chain/gosdk/constants"
78
"github.com/0chain/gosdk/zboxcore/sdk"
89
"github.com/spf13/cobra"
910
)
@@ -32,14 +33,15 @@ var createDirCmd = &cobra.Command{
3233
}
3334
dirname := cmd.Flag("dirname").Value.String()
3435

35-
if err != nil {
36-
PrintError("CreateDir failed: ", err)
37-
os.Exit(1)
38-
}
39-
err = allocationObj.CreateDir(dirname)
36+
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
37+
{
38+
OperationType: constants.FileOperationCreateDir,
39+
RemotePath: dirname,
40+
},
41+
})
4042

4143
if err != nil {
42-
PrintError("CreateDir failed: ", err)
44+
PrintError("Directory creation failed.", err)
4345
os.Exit(1)
4446
}
4547

cmd/delete.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/0chain/gosdk/constants"
78
"github.com/0chain/gosdk/zboxcore/sdk"
89
"github.com/spf13/cobra"
910
)
@@ -15,12 +16,12 @@ var deleteCmd = &cobra.Command{
1516
Long: `delete file from blobbers`,
1617
Args: cobra.MinimumNArgs(0),
1718
Run: func(cmd *cobra.Command, args []string) {
18-
fflags := cmd.Flags() // fflags is a *flag.FlagSet
19-
if fflags.Changed("allocation") == false { // check if the flag "path" is set
19+
fflags := cmd.Flags() // fflags is a *flag.FlagSet
20+
if !fflags.Changed("allocation") { // check if the flag "path" is set
2021
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
2122
os.Exit(1) // and return
2223
}
23-
if fflags.Changed("remotepath") == false {
24+
if !fflags.Changed("remotepath") {
2425
PrintError("Error: remotepath flag is missing")
2526
os.Exit(1)
2627
}
@@ -33,7 +34,12 @@ var deleteCmd = &cobra.Command{
3334
}
3435
remotePath := cmd.Flag("remotepath").Value.String()
3536

36-
err = allocationObj.DeleteFile(remotePath)
37+
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
38+
{
39+
OperationType: constants.FileOperationDelete,
40+
RemotePath: remotePath,
41+
},
42+
})
3743
if err != nil {
3844
PrintError("Delete failed.", err.Error())
3945
os.Exit(1)

cmd/download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ var downloadCmd = &cobra.Command{
139139
}
140140
}
141141
} else if len(remotePath) > 0 {
142-
if fflags.Changed("allocation") == false { // check if the flag "path" is set
142+
if !fflags.Changed("allocation") { // check if the flag "path" is set
143143
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
144144
os.Exit(1) // and return
145145
}
@@ -168,7 +168,7 @@ var downloadCmd = &cobra.Command{
168168
}
169169
}
170170
} else if len(multidownloadJSON) > 0 {
171-
if fflags.Changed("allocation") == false { // check if the flag "path" is set
171+
if !fflags.Changed("allocation") { // check if the flag "path" is set
172172
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
173173
os.Exit(1) // and return
174174
}

cmd/move.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/0chain/gosdk/constants"
78
"github.com/0chain/gosdk/zboxcore/sdk"
89
"github.com/spf13/cobra"
910
)
@@ -38,9 +39,15 @@ var moveCmd = &cobra.Command{
3839
remotePath := cmd.Flag("remotepath").Value.String()
3940
destPath := cmd.Flag("destpath").Value.String()
4041

41-
err = allocationObj.MoveObject(remotePath, destPath)
42+
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
43+
{
44+
OperationType: constants.FileOperationMove,
45+
RemotePath: remotePath,
46+
DestPath: destPath,
47+
},
48+
})
4249
if err != nil {
43-
PrintError("Error performing CopyObject", err)
50+
PrintError("Move failed.", err)
4451
os.Exit(1)
4552
}
4653

cmd/rename.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/0chain/gosdk/constants"
78
"github.com/0chain/gosdk/core/pathutil"
89
"github.com/0chain/gosdk/zboxcore/sdk"
910
"github.com/spf13/cobra"
@@ -16,17 +17,17 @@ var renameCmd = &cobra.Command{
1617
Long: `rename an object on blobbers`,
1718
Args: cobra.MinimumNArgs(0),
1819
Run: func(cmd *cobra.Command, args []string) {
19-
fflags := cmd.Flags() // fflags is a *flag.FlagSet
20-
if fflags.Changed("allocation") == false { // check if the flag "path" is set
20+
fflags := cmd.Flags() // fflags is a *flag.FlagSet
21+
if !fflags.Changed("allocation") { // check if the flag "path" is set
2122
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
2223
os.Exit(1) // and os.Exit(1)
2324
}
24-
if fflags.Changed("remotepath") == false {
25+
if !fflags.Changed("remotepath") {
2526
PrintError("Error: remotepath flag is missing")
2627
os.Exit(1)
2728
}
2829

29-
if fflags.Changed("destname") == false {
30+
if !fflags.Changed("destname") {
3031
PrintError("Error: destname flag is missing")
3132
os.Exit(1)
3233
}
@@ -44,9 +45,15 @@ var renameCmd = &cobra.Command{
4445
return
4546
}
4647

47-
err = allocationObj.RenameObject(remotePath, destName)
48+
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
49+
{
50+
OperationType: constants.FileOperationRename,
51+
RemotePath: remotePath,
52+
DestName: destName,
53+
},
54+
})
4855
if err != nil {
49-
PrintError(err.Error())
56+
PrintError("Rename failed.", err)
5057
os.Exit(1)
5158
}
5259
fmt.Println(remotePath + " renamed")

cmd/update.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ var updateCmd = &cobra.Command{
1616
Args: cobra.MinimumNArgs(0),
1717
Run: func(cmd *cobra.Command, args []string) {
1818
fflags := cmd.Flags()
19-
if fflags.Changed("allocation") == false {
19+
if !fflags.Changed("allocation") {
2020
PrintError("Error: allocation flag is missing")
2121
os.Exit(1)
2222
}
23-
if fflags.Changed("remotepath") == false {
23+
if !fflags.Changed("remotepath") {
2424
PrintError("Error: remotepath flag is missing")
2525
os.Exit(1)
2626
}
@@ -47,17 +47,7 @@ var updateCmd = &cobra.Command{
4747

4848
wg := &sync.WaitGroup{}
4949
statusBar := &StatusBar{wg: wg}
50-
wg.Add(1)
51-
52-
err = startChunkedUpload(cmd, allocationObj, chunkedUploadArgs{
53-
localPath: localPath,
54-
remotePath: remotePath,
55-
thumbnailPath: thumbnailPath,
56-
encrypt: encrypt,
57-
chunkNumber: updateChunkNumber,
58-
isUpdate: true,
59-
// isRepair: false,
60-
}, statusBar)
50+
err = singleUpload(allocationObj, localPath, remotePath, thumbnailPath, encrypt, false, true, updateChunkNumber, statusBar)
6151

6252
if err != nil {
6353
PrintError("Update failed.", err)

cmd/upload.go

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,10 @@ var uploadCmd = &cobra.Command{
6161
if multiuploadJSON != "" {
6262
err = multiUpload(allocationObj, localPath, multiuploadJSON, statusBar)
6363
} else {
64-
wg.Add(1)
65-
err = startChunkedUpload(cmd, allocationObj,
66-
chunkedUploadArgs{
67-
localPath: localPath,
68-
thumbnailPath: thumbnailPath,
69-
remotePath: remotePath,
70-
encrypt: encrypt,
71-
webStreaming: webStreaming,
72-
chunkNumber: uploadChunkNumber,
73-
// isUpdate: false,
74-
// isRepair: false,
75-
}, statusBar)
64+
err = singleUpload(allocationObj, localPath, remotePath, thumbnailPath, encrypt, webStreaming, false, uploadChunkNumber, statusBar)
7665
}
7766
if err != nil {
78-
PrintError("Upload failed.", err.Error())
67+
PrintError("Upload failed.", err)
7968
os.Exit(1)
8069
}
8170
wg.Wait()
@@ -97,7 +86,7 @@ type chunkedUploadArgs struct {
9786
isRepair bool
9887
}
9988

100-
func startChunkedUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, args chunkedUploadArgs, statusBar sdk.StatusCallback) error {
89+
func startChunkedUpload(allocationObj *sdk.Allocation, args chunkedUploadArgs, statusBar sdk.StatusCallback) error {
10190
fileReader, err := os.Open(args.localPath)
10291
if err != nil {
10392
return err
@@ -148,12 +137,14 @@ func startChunkedUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, args
148137
}
149138

150139
type MultiUploadOption struct {
151-
FilePath string `json:"filePath,omitempty"`
152-
FileName string `json:"fileName,omitempty"`
153-
RemotePath string `json:"remotePath,omitempty"`
154-
ThumbnailPath string `json:"thumbnailPath,omitempty"`
155-
Encrypt bool `json:"encrypt,omitempty"`
156-
ChunkNumber int `json:"chunkNumber,omitempty"`
140+
FilePath string `json:"filePath,omitempty"`
141+
FileName string `json:"fileName,omitempty"`
142+
RemotePath string `json:"remotePath,omitempty"`
143+
ThumbnailPath string `json:"thumbnailPath,omitempty"`
144+
Encrypt bool `json:"encrypt,omitempty"`
145+
ChunkNumber int `json:"chunkNumber,omitempty"`
146+
IsUpdate bool `json:"isUpdate,omitempty"`
147+
IsWebstreaming bool `json:"isWebstreaming,omitempty"`
157148
}
158149

159150
func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions string, statusBar *StatusBar) error {
@@ -172,13 +163,43 @@ func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions
172163
return err
173164
}
174165

166+
return multiUploadWithOptions(allocationObj, workdir, options, statusBar)
167+
}
168+
169+
func singleUpload(allocationObj *sdk.Allocation, localPath, remotePath, thumbnailPath string, encrypt, isWebstreaming, isUpdate bool, chunkNumber int, statusBar *StatusBar) error {
170+
fullRemotePath, fileName, err := fullPathAndFileNameForUpload(localPath, remotePath)
171+
if err != nil {
172+
return err
173+
}
174+
remotePath = pathutil.Dir(fullRemotePath) + "/"
175+
options := []MultiUploadOption{
176+
{
177+
FilePath: localPath,
178+
FileName: fileName,
179+
RemotePath: remotePath,
180+
ThumbnailPath: thumbnailPath,
181+
Encrypt: encrypt,
182+
ChunkNumber: chunkNumber,
183+
IsUpdate: isUpdate,
184+
IsWebstreaming: isWebstreaming,
185+
},
186+
}
187+
188+
workdir := util.GetHomeDir()
189+
190+
return multiUploadWithOptions(allocationObj, workdir, options, statusBar)
191+
}
192+
193+
func multiUploadWithOptions(allocationObj *sdk.Allocation, workdir string, options []MultiUploadOption, statusBar *StatusBar) error {
175194
totalUploads := len(options)
176195
filePaths := make([]string, totalUploads)
177196
fileNames := make([]string, totalUploads)
178197
remotePaths := make([]string, totalUploads)
179198
thumbnailPaths := make([]string, totalUploads)
180199
chunkNumbers := make([]int, totalUploads)
181200
encrypts := make([]bool, totalUploads)
201+
isUpdates := make([]bool, totalUploads)
202+
isWebstreaming := make([]bool, totalUploads)
182203
for idx, option := range options {
183204
statusBar.wg.Add(1)
184205
filePaths[idx] = option.FilePath
@@ -187,9 +208,11 @@ func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions
187208
remotePaths[idx] = option.RemotePath
188209
chunkNumbers[idx] = option.ChunkNumber
189210
encrypts[idx] = option.Encrypt
211+
isUpdates[idx] = option.IsUpdate
212+
isWebstreaming[idx] = option.IsWebstreaming
190213
}
191214

192-
return allocationObj.StartMultiUpload(workdir, filePaths, fileNames, thumbnailPaths, encrypts, chunkNumbers, remotePaths, false, statusBar)
215+
return allocationObj.StartMultiUpload(workdir, filePaths, fileNames, thumbnailPaths, encrypts, chunkNumbers, remotePaths, isUpdates, isWebstreaming, statusBar)
193216
}
194217

195218
func init() {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/0chain/errors v1.0.3
7-
github.com/0chain/gosdk v1.8.17-0.20230809212922-e71a28baf114
7+
github.com/0chain/gosdk v1.8.18-0.20230901213317-53d640a9b7f9
88
github.com/icza/bitio v1.1.0
99
github.com/olekukonko/tablewriter v0.0.5
1010
github.com/spf13/cobra v1.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 h1:z+DtCR8mBsjPnEs
4040
github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565/go.mod h1:UyDC8Qyl5z9lGkCnf9RHJPMektnFX8XtCJZHXCCVj8E=
4141
github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM=
4242
github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc=
43-
github.com/0chain/gosdk v1.8.17-0.20230809212922-e71a28baf114 h1:fgaUQSUpAqhjhD3ONmiY+3yWn56qHADEd0TCoRcDSZ0=
44-
github.com/0chain/gosdk v1.8.17-0.20230809212922-e71a28baf114/go.mod h1:3NKNYzmnMIYqZwwwOgZwMmTW1DT1ZUAmKyVPmYQOiT4=
43+
github.com/0chain/gosdk v1.8.18-0.20230901213317-53d640a9b7f9 h1:GHTdYTmhNY9genBkNWLXdn3Z1yCtcbSNkcIFaKrqBRU=
44+
github.com/0chain/gosdk v1.8.18-0.20230901213317-53d640a9b7f9/go.mod h1:3NKNYzmnMIYqZwwwOgZwMmTW1DT1ZUAmKyVPmYQOiT4=
4545
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4646
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
4747
github.com/Luzifer/go-openssl/v3 v3.1.0 h1:QqKqo6kYXGGUsvtUoCpRZm8lHw+jDfhbzr36gVj+/gw=

0 commit comments

Comments
 (0)