Skip to content

Commit 90c5c04

Browse files
committed
chore: Updated uploads
1 parent 391de6e commit 90c5c04

4 files changed

Lines changed: 42 additions & 18 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pinata
55
.vscode
66
gon.hcl
77
.DS_Store
8+
Makefile

cmd/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,22 @@ func main() {
5656
Name: "verbose",
5757
Usage: "Show upload progress",
5858
},
59+
&cli.StringFlag{
60+
Name: "network",
61+
Aliases: []string{"net"},
62+
Usage: "Specify the network (public or private). Uses default if not specified",
63+
},
5964
},
6065
Action: func(ctx *cli.Context) error {
6166
filePath := ctx.Args().First()
6267
groupId := ctx.String("group")
6368
name := ctx.String("name")
6469
verbose := ctx.Bool("verbose")
70+
network := ctx.String("network")
6571
if filePath == "" {
6672
return errors.New("no file path provided")
6773
}
68-
_, err := uploads.Upload(filePath, groupId, name, verbose)
74+
_, err := uploads.Upload(filePath, groupId, name, verbose, network)
6975
return err
7076
},
7177
},

internal/types/types.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package types
22

33
type UploadResponse struct {
44
Data struct {
5-
Id string `json:"id"`
6-
Name string `json:"name"`
7-
Cid string `json:"cid"`
8-
Size int `json:"size"`
9-
NumberOfFiles int `json:"number_of_files"`
10-
MimeType string `json:"mime_type"`
11-
CreatedAt string `json:"created_at"`
12-
GroupId string `json:"group_id,omitempty"`
13-
IsDuplicate bool `json:"is_duplicate,omitempty"`
5+
Id string `json:"id"`
6+
Name string `json:"name"`
7+
Cid string `json:"cid"`
8+
Size int `json:"size"`
9+
CreatedAt string `json:"created_at"`
10+
NumberOfFiles int `json:"number_of_files"`
11+
MimeType string `json:"mime_type"`
12+
GroupId *string `json:"group_id"`
13+
KeyValues map[string]string `json:"keyvalues"`
14+
Vectorized bool `json:"vectorized"`
15+
Network string `json:"network"`
16+
IsDuplicate bool `json:"is_duplicate,omitempty"`
1417
} `json:"data"`
1518
}
1619

internal/upload/uploads.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"pinata/internal/common"
15+
"pinata/internal/config"
1516
cliConfig "pinata/internal/config"
1617
"pinata/internal/types"
1718
"strings"
@@ -26,26 +27,26 @@ const (
2627
CHUNK_SIZE = 10 * 1024 * 1024 // Chunk size
2728
)
2829

29-
func Upload(filePath string, groupId string, name string, verbose bool) (types.UploadResponse, error) {
30+
func Upload(filePath string, groupId string, name string, verbose bool, network string) (types.UploadResponse, error) {
3031

3132
stats, err := os.Stat(filePath)
3233
if err != nil {
3334
return types.UploadResponse{}, err
3435
}
3536

3637
if stats.Size() > MAX_SIZE_REGULAR_UPLOAD {
37-
return uploadWithTUS(filePath, groupId, name, verbose, stats)
38+
return uploadWithTUS(filePath, groupId, name, verbose, stats, network)
3839
}
3940

40-
return regularUpload(filePath, groupId, name, verbose)
41+
return regularUpload(filePath, groupId, name, verbose, network)
4142
}
4243

4344
type progressReader struct {
4445
r io.Reader
4546
bar *progressbar.ProgressBar
4647
}
4748

48-
func regularUpload(filePath string, groupId string, name string, verbose bool) (types.UploadResponse, error) {
49+
func regularUpload(filePath string, groupId string, name string, verbose bool, network string) (types.UploadResponse, error) {
4950

5051
jwt, err := common.FindToken()
5152
if err != nil {
@@ -61,7 +62,7 @@ func regularUpload(filePath string, groupId string, name string, verbose bool) (
6162
return types.UploadResponse{}, err
6263
}
6364
body := &bytes.Buffer{}
64-
contentType, err := createMultipartRequest(filePath, files, body, stats, groupId, name)
65+
contentType, err := createMultipartRequest(filePath, files, body, stats, groupId, name, network)
6566
if err != nil {
6667
return types.UploadResponse{}, err
6768
}
@@ -166,7 +167,7 @@ func formatSize(bytes int) string {
166167
return formattedSize
167168
}
168169

169-
func uploadWithTUS(filePath string, groupId string, name string, verbose bool, stats os.FileInfo) (types.UploadResponse, error) {
170+
func uploadWithTUS(filePath string, groupId string, name string, verbose bool, stats os.FileInfo, network string) (types.UploadResponse, error) {
170171
jwt, err := common.FindToken()
171172
if err != nil {
172173
return types.UploadResponse{}, err
@@ -194,9 +195,15 @@ func uploadWithTUS(filePath string, groupId string, name string, verbose bool, s
194195
}
195196
defer f.Close()
196197

198+
networkParam, err := cliConfig.GetNetworkParam(network)
199+
if err != nil {
200+
return types.UploadResponse{}, err
201+
}
202+
197203
// Create metadata
198204
metadata := map[string]string{
199205
"filename": filepath.Base(filePath),
206+
"network": networkParam,
200207
}
201208
if groupId != "" {
202209
metadata["group_id"] = groupId
@@ -289,7 +296,7 @@ func uploadWithTUS(filePath string, groupId string, name string, verbose bool, s
289296
return response, nil
290297
}
291298

292-
func createMultipartRequest(filePath string, files []string, body io.Writer, stats os.FileInfo, groupId string, name string) (string, error) {
299+
func createMultipartRequest(filePath string, files []string, body io.Writer, stats os.FileInfo, groupId string, name string, network string) (string, error) {
293300
contentType := ""
294301
writer := multipart.NewWriter(body)
295302

@@ -322,6 +329,13 @@ func createMultipartRequest(filePath string, files []string, body io.Writer, sta
322329
}
323330
}
324331

332+
networkParam, err := config.GetNetworkParam(network)
333+
if err != nil {
334+
return "", err
335+
}
336+
337+
err = writer.WriteField("network", networkParam)
338+
325339
if groupId != "" {
326340
err := writer.WriteField("group_id", groupId)
327341
if err != nil {
@@ -333,7 +347,7 @@ func createMultipartRequest(filePath string, files []string, body io.Writer, sta
333347
if name != "nil" {
334348
nameToUse = name
335349
}
336-
err := writer.WriteField("name", nameToUse)
350+
err = writer.WriteField("name", nameToUse)
337351
if err != nil {
338352
return contentType, err
339353
}

0 commit comments

Comments
 (0)