-
Notifications
You must be signed in to change notification settings - Fork 20
/
share.go
134 lines (120 loc) · 4.15 KB
/
share.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
package cmd
import (
"fmt"
"os"
"time"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/pathutil"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
// shareCmd represents share command
var shareCmd = &cobra.Command{
Use: "share",
Short: "share files from blobbers",
Long: `share files 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") { // 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") {
PrintError("Error: remotepath 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()
refType := fileref.FILE
statsMap, err := allocationObj.GetFileStats(remotepath)
if err != nil {
PrintError("Error fetching the file information", err)
os.Exit(1)
}
var isFile bool
for _, v := range statsMap {
if v != nil {
isFile = true
break
}
}
if !isFile {
refType = fileref.DIRECTORY
}
if isFile {
fileMeta, err := allocationObj.GetFileMeta(remotepath)
if err != nil {
PrintError("Error in getting the file meta of the object." + err.Error())
os.Exit(1)
}
if len(fileMeta.EncryptedKey) > 0 && fflags.Changed("encryptionpublickey") == false {
PrintError("Clientid and/or encryptionpublickey are missing for the encrypted share!")
os.Exit(1)
}
}
var fileName string
_, fileName = pathutil.Split(remotepath)
refereeClientID := cmd.Flag("clientid").Value.String()
revoke, _ := cmd.Flags().GetBool("revoke")
if revoke {
err := allocationObj.RevokeShare(remotepath, refereeClientID)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
fmt.Println("Share revoked for client " + refereeClientID)
} else {
expiration, err := cmd.Flags().GetInt64("expiration-seconds")
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
availableAfter := time.Now()
availableAfterInput, err := cmd.Flags().GetString("available-after")
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
if len(availableAfterInput) > 0 {
aa, err := common.ParseTime(availableAfter, availableAfterInput)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
availableAfter = *aa
}
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
encryptionpublickey := cmd.Flag("encryptionpublickey").Value.String()
ref, err := allocationObj.GetAuthTicket(
remotepath, fileName, refType, refereeClientID, encryptionpublickey,
expiration, &availableAfter)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
fmt.Println("Auth token :" + ref)
}
},
}
func init() {
rootCmd.AddCommand(shareCmd)
shareCmd.PersistentFlags().String("allocation", "", "Allocation ID")
shareCmd.PersistentFlags().String("remotepath", "", "Remote path to share")
shareCmd.PersistentFlags().String("clientid", "", "ClientID of the user to share with. Leave blank for public share")
shareCmd.PersistentFlags().String("encryptionpublickey", "", "Encryption public key of the client you want to share with. Can be retrieved by the getwallet command")
shareCmd.PersistentFlags().Int64("expiration-seconds", 0, "Authticket will expire when the seconds specified have elapsed after the instant of its creation")
shareCmd.PersistentFlags().String("available-after", "", "Timelock for private file that makes the file available for download at certain time. 4 input formats are supported: +1h30m, +30, 1647858200 and 2022-03-21 10:21:38. Default value is current local time.")
shareCmd.PersistentFlags().Bool("revoke", false, "(default false) Revoke share for remotepath")
shareCmd.MarkFlagRequired("allocation")
shareCmd.MarkFlagRequired("remotepath")
}