Skip to content

Commit bdffa94

Browse files
authoredMar 24, 2022
Feat/available after (#194)
1 parent d121d08 commit bdffa94

File tree

9 files changed

+313
-231
lines changed

9 files changed

+313
-231
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ sp_lock.sh
2727
rp_lock.sh
2828
.idea/
2929
.vscode
30+
go.work

‎Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ gomod-clean:
2828

2929
$(ZBOX): gomod-download
3030
$(eval VERSION=$(shell git describe --tags --dirty --always))
31-
go build -x -v -tags bn256 -ldflags "-X main.VersionStr=$(VERSION)" -o $(ZBOX) main.go
31+
CGO_ENABLED=1 go build -x -v -tags bn256 -ldflags "-X main.VersionStr=$(VERSION)" -o $(ZBOX) main.go
3232

3333
zboxcli-test:
34-
go test -tags bn256 ./...
34+
CGO_ENABLED=1 go test -tags bn256 ./...
3535

3636
install: $(ZBOX) zboxcli-test
3737

‎README.md

+216-210
Large diffs are not rendered by default.

‎cmd/share.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"time"
78

9+
"github.com/0chain/gosdk/core/common"
810
"github.com/0chain/gosdk/zboxcore/fileref"
911
"github.com/0chain/gosdk/zboxcore/sdk"
1012
"github.com/spf13/cobra"
@@ -51,8 +53,10 @@ var shareCmd = &cobra.Command{
5153
}
5254

5355
var fileName string
56+
5457
_, fileName = filepath.Split(remotepath)
5558
refereeClientID := cmd.Flag("clientid").Value.String()
59+
5660
revoke, _ := cmd.Flags().GetBool("revoke")
5761
if revoke {
5862
err := allocationObj.RevokeShare(remotepath, refereeClientID)
@@ -62,9 +66,29 @@ var shareCmd = &cobra.Command{
6266
}
6367
fmt.Println("Share revoked for client " + refereeClientID)
6468
} else {
65-
expiration, _ := cmd.Flags().GetInt64("expiration-seconds")
69+
expiration, err := cmd.Flags().GetInt64("expiration-seconds")
70+
if err != nil {
71+
PrintError(err.Error())
72+
os.Exit(1)
73+
}
74+
75+
availableAfter := time.Now()
76+
availableAfterInput, err := cmd.Flags().GetString("available-after")
77+
if err != nil {
78+
PrintError(err.Error())
79+
os.Exit(1)
80+
}
81+
if len(availableAfterInput) > 0 {
82+
aa, err := common.ParseTime(availableAfter, availableAfterInput)
83+
if err != nil {
84+
PrintError(err.Error())
85+
os.Exit(1)
86+
}
87+
availableAfter = *aa
88+
}
89+
6690
encryptionpublickey := cmd.Flag("encryptionpublickey").Value.String()
67-
ref, err := allocationObj.GetAuthTicket(remotepath, fileName, refType, refereeClientID, encryptionpublickey, expiration)
91+
ref, err := allocationObj.GetAuthTicket(remotepath, fileName, refType, refereeClientID, encryptionpublickey, expiration, &availableAfter)
6892
if err != nil {
6993
PrintError(err.Error())
7094
os.Exit(1)
@@ -81,6 +105,7 @@ func init() {
81105
shareCmd.PersistentFlags().String("clientid", "", "ClientID of the user to share with. Leave blank for public share")
82106
shareCmd.PersistentFlags().String("encryptionpublickey", "", "Encryption public key of the client you want to share with. Can be retrieved by the getwallet command")
83107
shareCmd.PersistentFlags().Int64("expiration-seconds", 0, "Authticket will expire when the seconds specified have elapsed after the instant of its creation")
108+
shareCmd.PersistentFlags().String("available-after", "", "Timelock for private file that makes the file available for download at certain time. 4 inputs are supported: +1h30m, +30, 1647858200 and 2022-03-21 10:21:38")
84109
shareCmd.PersistentFlags().Bool("revoke", false, "Revoke share for remotepath")
85110
shareCmd.MarkFlagRequired("allocation")
86111
shareCmd.MarkFlagRequired("remotepath")

‎cmd/sync.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,16 @@ var syncCmd = &cobra.Command{
156156

157157
encrypt := len(encryptpath) != 0 && strings.Contains(lPath, encryptpath)
158158

159-
err = startChunkedUpload(cmd, allocationObj, lPath, "", f.Path, encrypt, chunkSize, attrs, statusBar, false)
159+
err = startChunkedUpload(cmd, allocationObj, chunkedUploadArgs{
160+
localPath: lPath,
161+
thumbnailPath: "",
162+
remotePath: f.Path,
163+
encrypt: encrypt,
164+
chunkSize: chunkSize,
165+
attrs: attrs,
166+
// isUpdate: false,
167+
// isRepair: false,
168+
}, statusBar)
160169

161170
// if len(encryptpath) != 0 && strings.Contains(lPath, encryptpath) {
162171
// err = allocationObj.EncryptAndUploadFile(lPath, f.Path, attrs, statusBar)
@@ -168,7 +177,17 @@ var syncCmd = &cobra.Command{
168177

169178
encrypt := len(encryptpath) != 0 && strings.Contains(lPath, encryptpath)
170179

171-
err = startChunkedUpload(cmd, allocationObj, lPath, "", f.Path, encrypt, chunkSize, f.Attributes, statusBar, true)
180+
err = startChunkedUpload(cmd, allocationObj,
181+
chunkedUploadArgs{
182+
localPath: lPath,
183+
thumbnailPath: "",
184+
remotePath: f.Path,
185+
encrypt: encrypt,
186+
chunkSize: chunkSize,
187+
attrs: f.Attributes,
188+
isUpdate: true,
189+
// isRepair: false,
190+
}, statusBar)
172191

173192
// if len(encryptpath) != 0 && strings.Contains(lPath, encryptpath) {
174193
// err = allocationObj.EncryptAndUpdateFile(lPath, f.Path,

‎cmd/update.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ var updateCmd = &cobra.Command{
6565
statusBar := &StatusBar{wg: wg}
6666
wg.Add(1)
6767

68-
err = startChunkedUpload(cmd, allocationObj, localpath, thumbnailpath, remotepath, encrypt, chunkSize, attrs, statusBar, true)
68+
err = startChunkedUpload(cmd, allocationObj, chunkedUploadArgs{
69+
localPath: localpath,
70+
remotePath: remotepath,
71+
thumbnailPath: thumbnailpath,
72+
encrypt: encrypt,
73+
chunkSize: chunkSize,
74+
attrs: attrs,
75+
isUpdate: true,
76+
// isRepair: false,
77+
}, statusBar)
6978

7079
if err != nil {
7180
PrintError("Update failed.", err)

‎cmd/upload.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,17 @@ var uploadCmd = &cobra.Command{
115115

116116
chunkSize, _ := cmd.Flags().GetInt("chunksize")
117117

118-
if err := startChunkedUpload(cmd, allocationObj, localpath, thumbnailpath, remotepath, encrypt, chunkSize, attrs, statusBar, false); err != nil {
118+
if err := startChunkedUpload(cmd, allocationObj,
119+
chunkedUploadArgs{
120+
localPath: localpath,
121+
thumbnailPath: thumbnailpath,
122+
remotePath: remotepath,
123+
encrypt: encrypt,
124+
chunkSize: chunkSize,
125+
attrs: attrs,
126+
// isUpdate: false,
127+
// isRepair: false,
128+
}, statusBar); err != nil {
119129
PrintError("Upload failed.", err.Error())
120130
os.Exit(1)
121131
}
@@ -285,9 +295,21 @@ var streamCmd = &cobra.Command{
285295
},
286296
}
287297

288-
func startChunkedUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, localPath, thumbnailPath, remotePath string, encrypt bool, chunkSize int, attrs fileref.Attributes, statusBar sdk.StatusCallback, isUpdate bool) error {
298+
type chunkedUploadArgs struct {
299+
localPath string
300+
remotePath string
301+
thumbnailPath string
289302

290-
fileReader, err := os.Open(localPath)
303+
encrypt bool
304+
chunkSize int
305+
isUpdate bool
306+
isRepair bool
307+
attrs fileref.Attributes
308+
}
309+
310+
func startChunkedUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, args chunkedUploadArgs, statusBar sdk.StatusCallback) error {
311+
312+
fileReader, err := os.Open(args.localPath)
291313
if err != nil {
292314
return err
293315
}
@@ -303,29 +325,29 @@ func startChunkedUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, local
303325
return err
304326
}
305327

306-
remotePath = zboxutil.RemoteClean(remotePath)
328+
remotePath := zboxutil.RemoteClean(args.remotePath)
307329
isabs := zboxutil.IsRemoteAbs(remotePath)
308330
if !isabs {
309331
err = thrown.New("invalid_path", "Path should be valid and absolute")
310332
return err
311333
}
312-
remotePath = zboxutil.GetFullRemotePath(localPath, remotePath)
334+
remotePath = zboxutil.GetFullRemotePath(args.localPath, remotePath)
313335

314336
_, fileName := filepath.Split(remotePath)
315337

316338
fileMeta := sdk.FileMeta{
317-
Path: localPath,
339+
Path: args.localPath,
318340
ActualSize: fileInfo.Size(),
319341
MimeType: mimeType,
320342
RemoteName: fileName,
321343
RemotePath: remotePath,
322-
Attributes: attrs,
344+
Attributes: args.attrs,
323345
}
324346

325-
ChunkedUpload, err := sdk.CreateChunkedUpload(util.GetHomeDir(), allocationObj, fileMeta, fileReader, isUpdate, false,
326-
sdk.WithThumbnailFile(thumbnailPath),
327-
sdk.WithChunkSize(int64(chunkSize)),
328-
sdk.WithEncrypt(encrypt),
347+
ChunkedUpload, err := sdk.CreateChunkedUpload(util.GetHomeDir(), allocationObj, fileMeta, fileReader, args.isUpdate, args.isRepair,
348+
sdk.WithThumbnailFile(args.thumbnailPath),
349+
sdk.WithChunkSize(int64(args.chunkSize)),
350+
sdk.WithEncrypt(args.encrypt),
329351
sdk.WithStatusCallback(statusBar))
330352
if err != nil {
331353
return err

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.17
44

55
require (
66
github.com/0chain/errors v1.0.3
7-
github.com/0chain/gosdk v1.7.7-0.20220318082026-d86de83b3f08
7+
github.com/0chain/gosdk v1.7.7-0.20220323125839-f117ab4bbf72
88
github.com/icza/bitio v1.1.0
99
github.com/olekukonko/tablewriter v0.0.5
1010
github.com/spf13/cobra v1.1.1

‎go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
4949
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
5050
github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM=
5151
github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc=
52-
github.com/0chain/gosdk v1.7.7-0.20220318082026-d86de83b3f08 h1:9MBNKx1kdocfNOoaT4ES/VJ+4QTEZuGmgcRL1kbm+Tk=
53-
github.com/0chain/gosdk v1.7.7-0.20220318082026-d86de83b3f08/go.mod h1:Cch/iwYKdBMnwcsFm2x9hG4CLYTVChCRFqA1TKRmRXU=
52+
github.com/0chain/gosdk v1.7.7-0.20220323125839-f117ab4bbf72 h1:P4oSjHnFaKTmWp5FQXD+p2EM77pUslECv7oja+tSzmw=
53+
github.com/0chain/gosdk v1.7.7-0.20220323125839-f117ab4bbf72/go.mod h1:Cch/iwYKdBMnwcsFm2x9hG4CLYTVChCRFqA1TKRmRXU=
5454
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
5555
github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc=
5656
github.com/Azure/azure-storage-blob-go v0.7.0/go.mod h1:f9YQKtsG1nMisotuTPpO0tjNuEjKRYAcJU8/ydDI++4=

0 commit comments

Comments
 (0)
Please sign in to comment.