-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for uploading folders to 0gStorage (#46)
- Loading branch information
Showing
5 changed files
with
348 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/0glabs/0g-storage-client/common/blockchain" | ||
"github.com/0glabs/0g-storage-client/transfer" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
uploadDirArgs uploadArgument | ||
|
||
uploadDirCmd = &cobra.Command{ | ||
Use: "upload-dir", | ||
Short: "Upload directory to ZeroGStorage network", | ||
Run: uploadDir, | ||
} | ||
) | ||
|
||
func init() { | ||
bindUploadFlags(uploadDirCmd, &uploadDirArgs) | ||
uploadDirCmd.Flags().StringVar(&uploadDirArgs.url, "url", "", "Fullnode URL to interact with ZeroGStorage smart contract") | ||
uploadDirCmd.MarkFlagRequired("url") | ||
uploadDirCmd.Flags().StringVar(&uploadDirArgs.key, "key", "", "Private key to interact with smart contract") | ||
uploadDirCmd.MarkFlagRequired("key") | ||
|
||
rootCmd.AddCommand(uploadDirCmd) | ||
} | ||
|
||
func uploadDir(*cobra.Command, []string) { | ||
ctx := context.Background() | ||
var cancel context.CancelFunc | ||
if uploadDirArgs.timeout > 0 { | ||
ctx, cancel = context.WithTimeout(ctx, uploadDirArgs.timeout) | ||
defer cancel() | ||
} | ||
|
||
w3client := blockchain.MustNewWeb3(uploadDirArgs.url, uploadDirArgs.key, providerOption) | ||
defer w3client.Close() | ||
|
||
finalityRequired := transfer.TransactionPacked | ||
if uploadDirArgs.finalityRequired { | ||
finalityRequired = transfer.FileFinalized | ||
} | ||
opt := transfer.UploadOption{ | ||
Tags: hexutil.MustDecode(uploadDirArgs.tags), | ||
FinalityRequired: finalityRequired, | ||
TaskSize: uploadDirArgs.taskSize, | ||
ExpectedReplica: uploadDirArgs.expectedReplica, | ||
SkipTx: uploadDirArgs.skipTx, | ||
} | ||
|
||
uploader, closer, err := newUploader(ctx, uploadDirArgs, w3client, opt) | ||
if err != nil { | ||
logrus.WithError(err).Fatal("Failed to initialize uploader") | ||
} | ||
defer closer() | ||
|
||
txnHash, rootHash, err := uploader.UploadDir(ctx, uploadDirArgs.file, opt) | ||
if err != nil { | ||
logrus.WithError(err).Fatal("Failed to upload directory") | ||
} | ||
|
||
logrus.WithFields(logrus.Fields{ | ||
"txnHash": txnHash, | ||
"rootHash": rootHash, | ||
}).Info("Directory uploaded done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.