-
Notifications
You must be signed in to change notification settings - Fork 19
/
upload.go
142 lines (129 loc) · 4.38 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
package cmd
import (
"os"
"strings"
"sync"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zboxcore/zboxutil"
"github.com/spf13/cobra"
)
var createDirCmd = &cobra.Command{
Use: "createdir",
Short: "Create directory",
Long: `Create directory`,
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("dirname") {
PrintError("Error: dirname 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)
}
dirname := cmd.Flag("dirname").Value.String()
if err != nil {
PrintError("CreateDir failed.", err)
os.Exit(1)
}
err = allocationObj.CreateDir(dirname)
if err != nil {
PrintError("CreateDir failed.", err)
os.Exit(1)
}
return
},
}
// 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") == false { // 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 {
PrintError("Error: remotepath flag is missing")
os.Exit(1)
}
if fflags.Changed("localpath") == false {
PrintError("Error: 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)
}
remotepath := cmd.Flag("remotepath").Value.String()
localpath := cmd.Flag("localpath").Value.String()
thumbnailpath := cmd.Flag("thumbnailpath").Value.String()
encrypt, _ := cmd.Flags().GetBool("encrypt")
commit, _ := cmd.Flags().GetBool("commit")
wg := &sync.WaitGroup{}
statusBar := &StatusBar{wg: wg}
wg.Add(1)
if strings.HasPrefix(remotepath, "/Encrypted") {
encrypt = true
}
var attrs fileref.Attributes // depreciated
if len(thumbnailpath) > 0 {
if encrypt {
err = allocationObj.EncryptAndUploadFileWithThumbnail(localpath, remotepath, thumbnailpath, attrs, statusBar)
} else {
err = allocationObj.UploadFileWithThumbnail(localpath, remotepath, thumbnailpath, attrs, statusBar)
}
} else {
if encrypt {
err = allocationObj.EncryptAndUploadFile(localpath, remotepath, attrs, statusBar)
} else {
err = allocationObj.UploadFile(localpath, remotepath, attrs, statusBar)
}
}
if err != nil {
PrintError("Upload failed.", err)
os.Exit(1)
}
wg.Wait()
if !statusBar.success {
os.Exit(1)
}
if commit {
remotepath = zboxutil.GetFullRemotePath(localpath, remotepath)
statusBar.wg.Add(1)
commitMetaTxn(remotepath, "Upload", "", "", allocationObj, nil, statusBar)
statusBar.wg.Wait()
}
return
},
}
func init() {
rootCmd.AddCommand(uploadCmd)
rootCmd.AddCommand(createDirCmd)
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.Flags().Bool("encrypt", false, "pass this option to encrypt and upload the file")
uploadCmd.Flags().Bool("commit", false, "pass this option to commit the metadata transaction")
uploadCmd.MarkFlagRequired("allocation")
uploadCmd.MarkFlagRequired("localpath")
uploadCmd.MarkFlagRequired("remotepath")
createDirCmd.PersistentFlags().String("allocation", "", "Allocation ID")
createDirCmd.PersistentFlags().String("dirname", "", "New directory name")
createDirCmd.MarkFlagRequired("allocation")
createDirCmd.MarkFlagRequired("dirname")
}