Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ zbox
zboxcli
sample/zbox
sample/zboxcli
.vscode
__debug_bin__
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func init() {
rootCmd.PersistentFlags().StringVar(&walletClientKey, "wallet_client_key", "", "wallet client_key")
rootCmd.PersistentFlags().StringVar(&cDir, "configDir", "", "configuration directory (default is $HOME/.zcn)")
rootCmd.PersistentFlags().BoolVar(&bSilent, "silent", false, "Do not show interactive sdk logs (shown by default)")

}

func Execute() {

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
79 changes: 70 additions & 9 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"log"
"os"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -45,6 +46,8 @@ var uploadCmd = &cobra.Command{
thumbnailpath := cmd.Flag("thumbnailpath").Value.String()
encrypt, _ := cmd.Flags().GetBool("encrypt")
commit, _ := cmd.Flags().GetBool("commit")
stream, _ := cmd.Flags().GetBool("stream")

wg := &sync.WaitGroup{}
statusBar := &StatusBar{wg: wg}
wg.Add(1)
Expand All @@ -66,19 +69,28 @@ var uploadCmd = &cobra.Command{
attrs.WhoPaysForReads = wp // set given value
}

if len(thumbnailpath) > 0 {
if encrypt {
err = allocationObj.EncryptAndUploadFileWithThumbnail(localpath, remotepath, thumbnailpath, attrs, statusBar)
} else {
err = allocationObj.UploadFileWithThumbnail(localpath, remotepath, thumbnailpath, attrs, statusBar)
}
if stream {

err = startStreamUpload(cmd, allocationObj, localpath, thumbnailpath, remotepath, encrypt, attrs, statusBar)

} else {
if encrypt {
err = allocationObj.EncryptAndUploadFile(localpath, remotepath, attrs, statusBar)

if len(thumbnailpath) > 0 {
if encrypt {
err = allocationObj.EncryptAndUploadFileWithThumbnail(localpath, remotepath, thumbnailpath, attrs, statusBar)
} else {
err = allocationObj.UploadFileWithThumbnail(localpath, remotepath, thumbnailpath, attrs, statusBar)
}
} else {
err = allocationObj.UploadFile(localpath, remotepath, attrs, statusBar)
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)
Expand All @@ -99,6 +111,53 @@ var uploadCmd = &cobra.Command{
},
}

func startStreamUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, localPath, thumbnailPath, remotePath string, encrypt bool, attrs fileref.Attributes, statusBar sdk.StatusCallback) error {
chunkSize, _ := cmd.Flags().GetInt("chunksize")

fileReader, err := os.Open(localPath)
if err != nil {
return err
}
defer fileReader.Close()

fileInfo, err := fileReader.Stat()
if err != nil {
return err
}

mimeType, err := zboxutil.GetFileContentType(fileReader)
if err != nil {
return err
}

remotePath = zboxutil.RemoteClean(remotePath)
isabs := zboxutil.IsRemoteAbs(remotePath)
if !isabs {
err = common.NewError("invalid_path", "Path should be valid and absolute")
return err
}
remotePath = zboxutil.GetFullRemotePath(localPath, remotePath)

_, fileName := filepath.Split(remotePath)

fileMeta := sdk.FileMeta{
Path: localPath,
ActualSize: fileInfo.Size(),
MimeType: mimeType,
RemoteName: fileName,
RemotePath: remotePath,
Attributes: attrs,
}

streamUpload := sdk.CreateStreamUpload(allocationObj, fileMeta, fileReader,
sdk.WithThumbnailFile(thumbnailPath),
sdk.WithChunkSize(chunkSize),
sdk.WithEncrypt(encrypt),
sdk.WithStatusCallback(statusBar))

return streamUpload.Start()
}

func init() {
rootCmd.AddCommand(uploadCmd)
uploadCmd.PersistentFlags().String("allocation", "", "Allocation ID")
Expand All @@ -108,6 +167,8 @@ func init() {
uploadCmd.PersistentFlags().String("attr-who-pays-for-reads", "owner", "Who pays for reads: owner or 3rd_party")
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.Flags().Bool("stream", false, "pass this option to enable stream upload for large file")
uploadCmd.Flags().Int("chunksize", sdk.CHUNK_SIZE, "pass this option to custom chunk size for upload")
uploadCmd.MarkFlagRequired("allocation")
uploadCmd.MarkFlagRequired("localpath")
uploadCmd.MarkFlagRequired("remotepath")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ require (
)

// temporary, for development
// replace github.com/0chain/gosdk => ../gosdk
replace github.com/0chain/gosdk => ../gosdk
5 changes: 5 additions & 0 deletions sp_lock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

./zbox sp-lock --blobber_id f65af5d64000c7cd2883f4910eb69086f9d6e6635c744e62afcfab58b938ee25 --tokens 1 --verbose
./zbox sp-lock --blobber_id 7a90e6790bcd3d78422d7a230390edc102870fe58c15472073922024985b1c7d --tokens 1 --verbose
./zbox sp-lock --blobber_id 2f051ca6447d8712a020213672bece683dbd0d23a81fdf93ff273043a0764d18 --tokens 1 --verbose