-
Notifications
You must be signed in to change notification settings - Fork 23
/
upload.go
199 lines (168 loc) · 6.58 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cmd
import (
"encoding/json"
"os"
"strings"
"sync"
thrown "github.com/0chain/errors"
"github.com/0chain/gosdk/core/pathutil"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zboxcore/zboxutil"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
var uploadChunkNumber int = 200
// uploadCmd represents upload command
var uploadCmd = &cobra.Command{
Use: "upload",
Short: "upload file to blobbers",
Long: `upload file to blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
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("multiuploadjson") || (fflags.Changed("remotepath") && fflags.Changed("localpath"))) {
PrintError("Error: multiuploadjson or remotepath/localpath flag is missing")
os.Exit(1)
}
allocationID := cmd.Flag("allocation").Value.String()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation.", err)
os.Exit(1)
}
var multiuploadJSON string
if fflags.Changed("multiuploadjson") {
multiuploadJSON = cmd.Flag("multiuploadjson").Value.String()
}
remotePath := cmd.Flag("remotepath").Value.String()
localPath := cmd.Flag("localpath").Value.String()
thumbnailPath := cmd.Flag("thumbnailpath").Value.String()
encrypt, _ := cmd.Flags().GetBool("encrypt")
webStreaming, _ := cmd.Flags().GetBool("web-streaming")
wg := &sync.WaitGroup{}
statusBar := &StatusBar{wg: wg}
if strings.HasPrefix(remotePath, "/Encrypted") {
encrypt = true
}
if multiuploadJSON != "" {
err = multiUpload(allocationObj, localPath, multiuploadJSON, statusBar)
} else {
err = singleUpload(allocationObj, localPath, remotePath, thumbnailPath, encrypt, webStreaming, false, uploadChunkNumber, statusBar)
}
if err != nil {
PrintError("Upload failed.", err)
os.Exit(1)
}
},
}
type chunkedUploadArgs struct {
localPath string
remotePath string
thumbnailPath string
encrypt bool
webStreaming bool
chunkNumber int
isUpdate bool
isRepair bool
}
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"`
IsWebstreaming bool `json:"isWebstreaming,omitempty"`
}
func multiUpload(allocationObj *sdk.Allocation, workdir, jsonMultiUploadOptions string, statusBar *StatusBar) error {
file, err := os.Open(jsonMultiUploadOptions)
if err != nil {
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
var options []MultiUploadOption
err = decoder.Decode(&options)
if err != nil {
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)
remotePaths := make([]string, totalUploads)
thumbnailPaths := make([]string, totalUploads)
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
fileNames[idx] = option.FileName
thumbnailPaths[idx] = option.ThumbnailPath
remotePaths[idx] = option.RemotePath
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, isWebstreaming, statusBar)
}
func init() {
rootCmd.AddCommand(uploadCmd)
uploadCmd.PersistentFlags().String("allocation", "", "Allocation ID")
uploadCmd.PersistentFlags().String("remotepath", "", "Remote path to upload")
uploadCmd.PersistentFlags().String("localpath", "", "Local path of file to upload")
uploadCmd.PersistentFlags().String("thumbnailpath", "", "Local thumbnail path of file to upload")
uploadCmd.PersistentFlags().String("multiuploadjson", "", "A JSON file containing multiupload options")
uploadCmd.PersistentFlags().String("attr-who-pays-for-reads", "owner", "Who pays for reads: owner or 3rd_party")
uploadCmd.Flags().Bool("encrypt", false, "(default false) pass this option to encrypt and upload the file")
uploadCmd.Flags().Bool("web-streaming", false, "(default false) pass this option to enable web streaming support")
uploadCmd.Flags().IntVarP(&uploadChunkNumber, "chunknumber", "", 200, "how many chunks should be uploaded in a http request")
uploadCmd.MarkFlagRequired("allocation")
uploadCmd.MarkFlagRequired("remotepath")
uploadCmd.MarkFlagRequired("localpath")
}
func fullPathAndFileNameForUpload(localPath, remotePath string) (string, string, error) {
isUploadToDir := strings.HasSuffix(remotePath, "/")
remotePath = zboxutil.RemoteClean(remotePath)
if !zboxutil.IsRemoteAbs(remotePath) {
return "", "", thrown.New("invalid_path", "Path should be valid and absolute")
}
// re-add trailing slash to indicate intending to upload to directory
if isUploadToDir && !strings.HasSuffix(remotePath, "/") {
remotePath += "/"
}
fullRemotePath := zboxutil.GetFullRemotePath(localPath, remotePath)
_, fileName := pathutil.Split(fullRemotePath)
return fullRemotePath, fileName, nil
}