Skip to content

Commit

Permalink
chore: Refactored uploads, added list and delete as subcommand to files
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedylandev committed Sep 6, 2024
1 parent 40376e3 commit 8a3d09e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 110 deletions.
23 changes: 13 additions & 10 deletions Upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,21 @@ func Upload(filePath string, groupId string, name string, cidOnly bool) (UploadR
return UploadResponse{}, err
}
if cidOnly {
fmt.Println(response.Cid)
fmt.Println(response.Data.Cid)
} else {
fmt.Println("Success!")
fmt.Println("CID:", response.Cid)
fmt.Println("Name:", response.Name)
fmt.Println("Size:", formatSize(response.Size))
fmt.Println("Number of Files:", response.NumberOfFiles)
fmt.Println("MIME Type:", response.MimeType)
fmt.Println("User ID:", response.UserId)
fmt.Println("Indexed At:", response.IndexedAt)
if response.GroupId != "" {
fmt.Println("Group ID:", response.GroupId)
fmt.Println("CID:", response.Data.Cid)
fmt.Println("Name:", response.Data.Name)
fmt.Println("Size:", formatSize(response.Data.Size))
fmt.Println("Number of Files:", response.Data.NumberOfFiles)
fmt.Println("Mime Type:", response.Data.MimeType)
fmt.Println("User ID:", response.Data.UserId)
fmt.Println("Created At:", response.Data.CreatedAt)
if response.Data.IsDuplicate {
fmt.Println("Duplicate:", response.Data.IsDuplicate)
}
if response.Data.GroupId != "" {
fmt.Println("Group ID:", response.Data.GroupId)
}
}
return response, nil
Expand Down
5 changes: 2 additions & 3 deletions Delete.go → delete_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"net/http"
)

func Delete(cid string) error {
func Delete(id string) error {
jwt, err := findToken()
if err != nil {
return err
}
host := GetHost()
url := fmt.Sprintf("https://%s/pinning/unpin/%s", host, cid)
url := fmt.Sprintf("https://api.pinata.cloud/v3/files/%s", id)

req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions ListFiles.go → list_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ func ListFiles(amount string, cid string, name string, status string, offset str
if err != nil {
return ListResponse{}, err
}
host := GetHost()
url := fmt.Sprintf("https://%s/files", host, amount, status)
url := fmt.Sprintf("https://api.pinata.cloud/v3/files")

if cid != "null" {
url += "&hashContains=" + cid
Expand Down
158 changes: 63 additions & 95 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,6 @@ import (
"github.com/urfave/cli/v2"
)

type UploadResponse struct {
Id string `json:"id"`
Name string `json:"name"`
Cid string `json:"cid"`
Size int `json:"size"`
NumberOfFiles int `json:"number_of_files"`
MimeType string `json:"mime_type"`
UserId string `json:"user_id"`
IndexedAt string `json:"indexed_at"`
GroupId string `json:"group_id,omitempty"`
}

type Options struct {
GroupId string `json:"group_id"`
}

type Metadata struct {
Name string `json:"name"`
}

type File struct {
Id string `json:"id"`
Name string `json:"name"`
Cid string `json:"cid"`
Size int `json:"size"`
NumberOfFiles int `json:"number_of_files"`
MimeType string `json:"mime_type"`
GroupId *string `json:"group_id,omitempty"`
CreatedAt string `json:"created_at"`
}

type ListFilesData struct {
Files []File `json:"files"`
NextPageToken string `json:"next_page_token"`
}

type ListResponse struct {
Data ListFilesData `json:"data"`
}

func main() {
app := &cli.App{
Name: "pinata",
Expand Down Expand Up @@ -103,64 +63,72 @@ func main() {
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a file by CID",
ArgsUsage: "[CID of file]",
Action: func(ctx *cli.Context) error {
cid := ctx.Args().First()
if cid == "" {
return errors.New("no CID provided")
}
err := Delete(cid)
return err
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List most recent files",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cid",
Aliases: []string{"c"},
Value: "null",
Usage: "Search files by CID",
},
&cli.StringFlag{
Name: "amount",
Aliases: []string{"a"},
Value: "10",
Usage: "The number of files you would like to return, default 10 max 1000",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Value: "null",
Usage: "The name of the file",
},
&cli.StringFlag{
Name: "status",
Aliases: []string{"s"},
Value: "pinned",
Usage: "Status of the file. Options are 'pinned', 'unpinned', or 'all'. Default: 'pinned'",
Name: "files",
Aliases: []string{"f"},
Usage: "Interact with your files on Pinata",
Subcommands: []*cli.Command{

{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a file by ID",
ArgsUsage: "[ID of file]",
Action: func(ctx *cli.Context) error {
fileId := ctx.Args().First()
if fileId == "" {
return errors.New("no CID provided")
}
err := Delete(fileId)
return err
},
},
&cli.StringFlag{
Name: "pageOffset",
Aliases: []string{"p"},
Value: "null",
Usage: "Allows you to paginate through files. If your file amount is 10, then you could set the pageOffset to '10' to see the next 10 files.",
{
Name: "list",
Aliases: []string{"l"},
Usage: "List most recent files",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cid",
Aliases: []string{"c"},
Value: "null",
Usage: "Search files by CID",
},
&cli.StringFlag{
Name: "amount",
Aliases: []string{"a"},
Value: "10",
Usage: "The number of files you would like to return, default 10 max 1000",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Value: "null",
Usage: "The name of the file",
},
&cli.StringFlag{
Name: "status",
Aliases: []string{"s"},
Value: "pinned",
Usage: "Status of the file. Options are 'pinned', 'unpinned', or 'all'. Default: 'pinned'",
},
&cli.StringFlag{
Name: "pageOffset",
Aliases: []string{"p"},
Value: "null",
Usage: "Allows you to paginate through files. If your file amount is 10, then you could set the pageOffset to '10' to see the next 10 files.",
},
},
Action: func(ctx *cli.Context) error {
cid := ctx.String("cid")
amount := ctx.String("amount")
name := ctx.String("name")
status := ctx.String("status")
offset := ctx.String("pageOffset")
_, err := ListFiles(amount, cid, name, status, offset)
return err
},
},
},
Action: func(ctx *cli.Context) error {
cid := ctx.String("cid")
amount := ctx.String("amount")
name := ctx.String("name")
status := ctx.String("status")
offset := ctx.String("pageOffset")
_, err := ListFiles(amount, cid, name, status, offset)
return err
},
},
},
}
Expand Down
44 changes: 44 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

type UploadResponse struct {
Data struct {
Id string `json:"id"`
Name string `json:"name"`
Cid string `json:"cid"`
Size int `json:"size"`
NumberOfFiles int `json:"number_of_files"`
MimeType string `json:"mime_type"`
UserId string `json:"user_id"`
CreatedAt string `json:"created_at"`
GroupId string `json:"group_id,omitempty"`
IsDuplicate bool `json:"is_duplicate,omitempty"`
} `json:"data"`
}

type Options struct {
GroupId string `json:"group_id"`
}

type Metadata struct {
Name string `json:"name"`
}

type File struct {
Id string `json:"id"`
Name string `json:"name"`
Cid string `json:"cid"`
Size int `json:"size"`
NumberOfFiles int `json:"number_of_files"`
MimeType string `json:"mime_type"`
GroupId *string `json:"group_id,omitempty"`
CreatedAt string `json:"created_at"`
}

type ListFilesData struct {
Files []File `json:"files"`
NextPageToken string `json:"next_page_token"`
}

type ListResponse struct {
Data ListFilesData `json:"data"`
}

0 comments on commit 8a3d09e

Please sign in to comment.