Skip to content

Commit b45ce06

Browse files
committed
chore: Updated command for access links
1 parent 99954a8 commit b45ce06

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

cmd/main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,19 @@ func main() {
532532
},
533533
},
534534
{
535-
Name: "sign",
536-
Aliases: []string{"s"},
537-
Usage: "Get a signed URL for a file by CID",
535+
Name: "link",
536+
Aliases: []string{"l"},
537+
Usage: "Get either an IPFS link for a public file or a temporary access link for a Private IPFS file",
538538
ArgsUsage: "[cid of the file, seconds the url is valid for]",
539+
Flags: []cli.Flag{
540+
&cli.StringFlag{
541+
Name: "network",
542+
Aliases: []string{"net"},
543+
Usage: "Specify the network (public or private). Uses default if not specified",
544+
},
545+
},
539546
Action: func(ctx *cli.Context) error {
547+
network := ctx.String("network")
540548
cid := ctx.Args().First()
541549
if cid == "" {
542550
return errors.New("No CID provided")
@@ -551,7 +559,7 @@ func main() {
551559
if err != nil {
552560
return errors.New("Invalid expire time")
553561
}
554-
_, err = gateways.GetAccessLink(cid, expiresInt)
562+
_, err = gateways.GetAccessLink(cid, expiresInt, network)
555563
return err
556564
},
557565
},

internal/gateways/gateways.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,29 @@ func SetGateway(domain string) error {
101101
return nil
102102
}
103103

104-
func GetAccessLink(cid string, expires int) (types.GetSignedURLResponse, error) {
104+
func GetAccessLink(cid string, expires int, network string) (types.GetSignedURLResponse, error) {
105105

106106
jwt, err := common.FindToken()
107107
if err != nil {
108108
return types.GetSignedURLResponse{}, err
109109
}
110110

111+
networkParam, err := config.GetNetworkParam(network)
112+
if err != nil {
113+
return types.GetSignedURLResponse{}, err
114+
}
115+
111116
domain, err := FindGatewayDomain()
112117
if err != nil {
113118
return types.GetSignedURLResponse{}, err
114119
}
115120

121+
if networkParam == "public" {
122+
url := fmt.Sprintf("https://%s/ipfs/%s", domain, cid)
123+
fmt.Println(url)
124+
return types.GetSignedURLResponse{}, err
125+
}
126+
116127
domainUrl := fmt.Sprintf("https://%s/files/%s", domain, cid)
117128

118129
currentTime := time.Now().Unix()
@@ -130,7 +141,7 @@ func GetAccessLink(cid string, expires int) (types.GetSignedURLResponse, error)
130141
return types.GetSignedURLResponse{}, errors.Join(err, errors.New("Failed to marshal paylod"))
131142
}
132143

133-
url := fmt.Sprintf("https://%s/v3/files/download_link", config.GetAPIHost())
144+
url := fmt.Sprintf("https://%s/v3/files/private/download_link", config.GetAPIHost())
134145
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
135146
if err != nil {
136147
return types.GetSignedURLResponse{}, errors.Join(err, errors.New("failed to create the request"))
@@ -187,7 +198,7 @@ func OpenCID(cid string, network string) error {
187198
}
188199
return nil
189200
} else {
190-
data, err := GetAccessLink(cid, 30)
201+
data, err := GetAccessLink(cid, 30, network)
191202
if err != nil {
192203
return fmt.Errorf("problem creating URL: %w", err)
193204
}

0 commit comments

Comments
 (0)