Skip to content

Commit

Permalink
Merge pull request #8 from paradeum-team/yuan
Browse files Browse the repository at this point in the history
fix some bugs.
  • Loading branch information
freedomiris authored Jun 9, 2023
2 parents 367dd93 + ea1ebfa commit 8b84ba2
Show file tree
Hide file tree
Showing 19 changed files with 392 additions and 213 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ testdata
logs/
/uploading/*
/testserver/*
/testserver
/testserver
myfolder
myfolder_download
xyz
32 changes: 13 additions & 19 deletions MANUAL.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Manual
# Chainstorage-cli Manual

## Introduction
- chainstorage-cli is the command line application to interact with rest api SDK of *chain strorage*.
Expand Down Expand Up @@ -400,8 +400,7 @@ Here is an example of a configuration file (`config.toml`) with available option
```
[cli]
ipfsGateway = "test-ipfs-gateway.netwarps.com/ipfs/"
ggcscmdPath = '/path/to/ggcscmd'
useHttpsProtocol = "https"
useHttpsProtocol = true
bucketPrefix = 'cs://'
listOffset = 20
cleanTmpData = true
Expand All @@ -411,17 +410,12 @@ retryDelay = 3
[sdk]
defaultRegion = 'hk-1'
timeZone = 'UTC +08:00'
#链存服务API地址
chainStorageApiEndpoint = 'http://127.0.0.1:8821'
#CAR文件工作目录
useHttpsProtocol = true
carFileWorkPath = './tmp/carfile'
#CAR文件分片阈值()
carFileShardingThreshold = 1048576
#链存服务API token
carFileShardingThreshold = 46137344
chainStorageApiToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
#HTTP request user agent (K2请求需要)
httpRequestUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
#HTTP request overtime
httpRequestOvertime = 30
carVersion = 1
Expand All @@ -448,17 +442,11 @@ The CLI configuration allows you to specify various settings related to the tool
- Type: String
- Default Value: `test-ipfs-gateway.netwarps.com/ipfs/`

#### ggcscmdPath

- Description: The path to the `ggcscmd` executable.
- Type: String
- Default Value: `/path/to/ggcscmd`

#### useHttpsProtocol

- Description: Indicates whether to use HTTPS protocol.
- Type: String
- Default Value: `https`
- Type: Boolean
- Default Value: `true`

#### bucketPrefix

Expand Down Expand Up @@ -514,6 +502,12 @@ The SDK configuration allows you to specify various settings related to the SDK'
- Type: String
- Default Value: `http://127.0.0.1:8821`

#### useHttpsProtocol

- Description: Indicates whether to use HTTPS protocol.
- Type: Boolean
- Default Value: `true`

#### carFileWorkPath

- Description: The working directory for CAR files.
Expand All @@ -524,7 +518,7 @@ The SDK configuration allows you to specify various settings related to the SDK'

- Description: The threshold (in bytes) for CAR file sharding.
- Type: Integer
- Default Value: `1048576`
- Default Value: `46137344`

#### chainStorageApiToken

Expand Down
38 changes: 24 additions & 14 deletions cmd/bucket.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cmd

import (
"fmt"
chainstoragesdk "github.com/paradeum-team/chainstorage-sdk/sdk"
sdkcode "github.com/paradeum-team/chainstorage-sdk/sdk/code"
"github.com/paradeum-team/chainstorage-sdk/sdk/consts"
"github.com/paradeum-team/chainstorage-sdk/sdk/model"
chainstoragesdk "github.com/paradeum-team/chainstorage-sdk"
sdkcode "github.com/paradeum-team/chainstorage-sdk/code"
"github.com/paradeum-team/chainstorage-sdk/consts"
"github.com/paradeum-team/chainstorage-sdk/model"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/ulule/deepcopier"
Expand Down Expand Up @@ -102,11 +101,18 @@ func bucketListRunOutput(cmd *cobra.Command, args []string, resp model.BucketPag
// 存储网络
bucketOutput.StorageNetwork = consts.StorageNetworkCodeMapping[bucketOutput.StorageNetworkCode]

// 桶策略
bucketOutput.BucketPrinciple = consts.BucketPrincipleCodeMapping[bucketOutput.BucketPrincipleCode]
//// 桶策略
//bucketOutput.BucketPrinciple = consts.BucketPrincipleCodeMapping[bucketOutput.BucketPrincipleCode]

// 桶策略(英文)
bucketOutput.BucketPrinciple = consts.BucketPrincipleCodeMappingEn[bucketOutput.BucketPrincipleCode]

// 创建时间
bucketOutput.CreatedDate = bucketOutput.CreatedAt.Format("2006-01-02")

// 已使用空间
bucketOutput.FormatUsedSpace = convertSizeUnit(bucketOutput.UsedSpace)

bucketListOutput.List = append(bucketListOutput.List, bucketOutput)
}
}
Expand All @@ -117,7 +123,7 @@ total {{.Count}}
Status: {{.Code}}
{{- else}}
{{- range .List}}
{{.ObjectAmount}} {{.StorageNetwork}} {{.BucketPrinciple}} {{.UsedSpace}} {{.CreatedDate}} {{.BucketName}}
{{.StorageNetwork}} {{.BucketPrinciple}} {{.FormatUsedSpace}} {{.ObjectAmount}} {{.CreatedDate}} {{.BucketName}}
{{- end}}
{{- end}}
`
Expand Down Expand Up @@ -155,6 +161,7 @@ type BucketOutput struct {
StorageNetwork string `json:"storageNetwork" comment:"存储网络(10001-IPFS)"`
BucketPrinciple string `json:"bucketPrinciple" comment:"桶策略(10001-公开,10000-私有)"`
CreatedDate string `json:"createdDate" comment:"创建日期"`
FormatUsedSpace string `json:"formatUsedSpace" comment:"格式化已使用空间"`
}

// endregion Bucket List
Expand Down Expand Up @@ -245,8 +252,11 @@ func bucketCreateRunOutput(cmd *cobra.Command, args []string, resp model.BucketC
// 存储网络
bucketOutput.StorageNetwork = consts.StorageNetworkCodeMapping[bucketOutput.StorageNetworkCode]

// 桶策略
bucketOutput.BucketPrinciple = consts.BucketPrincipleCodeMapping[bucketOutput.BucketPrincipleCode]
//// 桶策略
//bucketOutput.BucketPrinciple = consts.BucketPrincipleCodeMapping[bucketOutput.BucketPrincipleCode]

// 桶策略(英文)
bucketOutput.BucketPrinciple = consts.BucketPrincipleCodeMappingEn[bucketOutput.BucketPrincipleCode]

// 创建时间 todo: timezone
//bucketOutput.CreatedDate = bucketOutput.CreatedAt.Format("2006-01-02")
Expand Down Expand Up @@ -406,10 +416,10 @@ func bucketEmptyRun(cmd *cobra.Command, args []string) {
}

// todo: remove it
fmt.Sprint(force)
//if !force {
// Error(cmd, args, errors.New("empty bucket operation, add --force to confirm emptying"))
//}
//fmt.Sprint(force)
if !force {
Error(cmd, args, errors.New("empty bucket operation, add --force to confirm emptying"))
}

sdk, err := chainstoragesdk.New(&appConfig)
if err != nil {
Expand Down
60 changes: 9 additions & 51 deletions cmd/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"github.com/alanshaw/go-carbites"
"github.com/cheggaaa/pb/v3"
chainstoragesdk "github.com/paradeum-team/chainstorage-sdk/sdk"
sdkcode "github.com/paradeum-team/chainstorage-sdk/sdk/code"
"github.com/paradeum-team/chainstorage-sdk/sdk/consts"
"github.com/paradeum-team/chainstorage-sdk/sdk/model"
"github.com/paradeum-team/chainstorage-sdk/sdk/utils"
chainstoragesdk "github.com/paradeum-team/chainstorage-sdk"
sdkcode "github.com/paradeum-team/chainstorage-sdk/code"
"github.com/paradeum-team/chainstorage-sdk/consts"
"github.com/paradeum-team/chainstorage-sdk/model"
"github.com/paradeum-team/chainstorage-sdk/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -117,49 +117,6 @@ func carUploadRunOutput(cmd *cobra.Command, args []string, resp model.ObjectCrea
Error(cmd, args, err)
}

//对象上传
//通过命令向固定桶内上传对象,包括文件、目录
//
//模版
//
//gcscmd put FILE[/DIR...] cs://BUCKET
//BUCKET
//
//桶名称
//
//命令行例子
//
//上传文件
//
//当前目录
//
//gcscmd put ./aaa.mp4 cs://bbb
//绝对路径
//
//gcscmd put /home/pz/aaa.mp4 cs://bbb
//相对路径
//
//gcscmd put ../pz/aaa.mp4 cs://bbb
//上传目录
//
//gcscmd put ./aaaa cs://bbb
//上传 carfile
//
//gcscmd put ./aaa.car cs://bbb --carfile
//响应
//
//过程
//
//################ 15%
//Tarkov.mp4
//完成
//
//CID: QmWgnG7pPjG31w328hZyALQ2BgW5aQrZyKpT47jVpn8CNo
//Name:Tarkov.mp4
//报错
//
//Error: This file is a car file, add --carfile to confirm uploading car

templateContent := `
CID: {{.Data.ObjectCid}}
Name: {{.Data.ObjectName}}
Expand Down Expand Up @@ -507,7 +464,7 @@ func UploadBigCarFile(sdk *chainstoragesdk.CssClient, req *model.CarFileUploadRe
"index": i,
"retry": j,
}).Info("upload sharding car file")
fmt.Printf("UploadBigCarFile => UploadShardingCarFileExt, index:%d, parameter uploadingReq:%+v\n", i, uploadingReq)
//fmt.Printf("UploadBigCarFile => UploadShardingCarFileExt, index:%d, parameter uploadingReq:%+v\n", i, uploadingReq)
uploadResp, err := sdk.Car.UploadShardingCarFileExt(&uploadingReq, extReader)
if err == nil && uploadResp.Code == http.StatusOK {
uploadRespList = append(uploadRespList, uploadResp)
Expand Down Expand Up @@ -597,8 +554,9 @@ func carImportRun(cmd *cobra.Command, args []string) {

// CAR文件类型检查
if !strings.HasSuffix(strings.ToLower(dataPath), ".car") {
err := sdkcode.ErrCarUploadFileInvalidDataPath
Error(cmd, args, err)
//err := sdkcode.ErrCarUploadFileInvalidDataPath
//Error(cmd, args, err)
Error(cmd, args, errors.New("please specify car format file with .car suffix"))
}

//// CAR文件标识
Expand Down
65 changes: 2 additions & 63 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type CliConfig struct {
IpfsGateway string `toml:"ipfsGateway"`
GgcscmdPath string `toml:"ggcscmdPath"`
UseHTTPSProtocol string `toml:"useHttpsProtocol"`
UseHTTPSProtocol bool `toml:"useHttpsProtocol"`
BucketPrefix string `toml:"bucketPrefix"`
ListOffset int `toml:"listOffset"`
CleanTmpData bool `toml:"cleanTmpData"`
Expand All @@ -28,6 +28,7 @@ type SdkConfig struct {
HTTPRequestUserAgent string `toml:"httpRequestUserAgent"`
HTTPRequestOvertime int `toml:"httpRequestOvertime"`
CarVersion int `toml:"carVersion"`
UseHTTPSProtocol bool `toml:"useHttpsProtocol"`
}

type LoggerConfig struct {
Expand All @@ -49,75 +50,13 @@ type CscConfig struct {

// region Config show

//var bucketListCmd = &cobra.Command{
// Use: "ls",
// Short: "ls",
// Long: "List links from object or bucket",
// Example: "gcscmd ls [--Offset=<Offset>]",
//
// Run: func(cmd *cobra.Command, args []string) {
// //cmd.Help()
// //fmt.Printf("%s %s\n", cmd.Name(), strconv.Itoa(offset))
// bucketListRun(cmd, args)
// },
//}

func configShowRun(cmd *cobra.Command, args []string) {
configFileUsed := viper.ConfigFileUsed()
fmt.Fprintln(os.Stderr, "Using config file:", configFileUsed)
err := printFileContent(configFileUsed)
if err != nil {
Error(cmd, args, err)
}

configShowRunOutput(cmd, args)
}

func configShowRunOutput(cmd *cobra.Command, args []string) {
// templateContent := `
//total {{.Count}}
//{{- if eq (len .List) 0}}
//Status: {{.Code}}
//{{- else}}
//{{- range .List}}
//{{.ObjectAmount}} {{.StorageNetwork}} {{.BucketPrinciple}} {{.UsedSpace}} {{.CreatedDate}} {{.BucketName}}
//{{- end}}
//{{- end}}
//`
//
// t, err := template.New("configShowTemplate").Parse(templateContent)
// if err != nil {
// Error(cmd, args, err)
// }
//
// err = t.Execute(os.Stdout, bucketListOutput)
// if err != nil {
// Error(cmd, args, err)
// }
}

//type BucketListOutput struct {
// RequestId string `json:"requestId,omitempty"`
// Code int32 `json:"code,omitempty"`
// Msg string `json:"msg,omitempty"`
// Status string `json:"status,omitempty"`
// Count int `json:"count,omitempty"`
// PageIndex int `json:"pageIndex,omitempty"`
// PageSize int `json:"pageSize,omitempty"`
// List []BucketOutput `json:"list,omitempty"`
//}
//
//type BucketOutput struct {
// Id int `json:"id" comment:"桶ID"`
// BucketName string `json:"bucketName" comment:"桶名称(3-63字长度限制)"`
// StorageNetworkCode int `json:"storageNetworkCode" comment:"存储网络编码(10001-IPFS)"`
// BucketPrincipleCode int `json:"bucketPrincipleCode" comment:"桶策略编码(10001-公开,10000-私有)"`
// UsedSpace int64 `json:"usedSpace" comment:"已使用空间(字节)"`
// ObjectAmount int `json:"objectAmount" comment:"对象数量"`
// CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
// StorageNetwork string `json:"storageNetwork" comment:"存储网络(10001-IPFS)"`
// BucketPrinciple string `json:"bucketPrinciple" comment:"桶策略(10001-公开,10000-私有)"`
// CreatedDate string `json:"createdDate" comment:"创建日期"`
//}

// endregion Config show
3 changes: 3 additions & 0 deletions cmd/getCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ func init() {

// 对象CID
getCmd.Flags().StringP("cid", "c", "", "cid of object")

// 用户自定义目录
getCmd.Flags().StringP("downloadFolder", "d", "", "download folder")
}
21 changes: 20 additions & 1 deletion cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"bufio"
"fmt"
sdkcode "github.com/paradeum-team/chainstorage-sdk/sdk/code"
sdkcode "github.com/paradeum-team/chainstorage-sdk/code"
"github.com/spf13/cobra"
"io"
"os"
Expand Down Expand Up @@ -189,3 +189,22 @@ func printFileContent(filename string) error {

return nil
}

func convertSizeUnit(size int64) string {
// 2^10 = 1024
const unit = 1024
if size < unit {
return fmt.Sprintf("%dB", size)
}

div, exp := int64(unit), 0
for size >= div && exp < 8 {
div *= unit
exp++
}

convertedSize := float64(size) / float64(div/unit)
//fmt.Printf("size:%d\n", size)
//fmt.Printf("div:%d\n", div)
return fmt.Sprintf("%.1f%cB", convertedSize, "KMGTPEZY"[exp-1])
}
Loading

0 comments on commit 8b84ba2

Please sign in to comment.