Skip to content

Commit 9630537

Browse files
committed
fix: Fixed gateway open command
1 parent e7560d6 commit 9630537

3 files changed

Lines changed: 34 additions & 29 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ require (
1010
github.com/charmbracelet/lipgloss v0.13.0
1111
github.com/eventials/go-tus v0.0.0-20220610120217-05d0564bb571
1212
github.com/schollz/progressbar/v3 v3.13.1
13-
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
1413
github.com/urfave/cli/v2 v2.25.7
1514
)
1615

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,8 +1152,6 @@ github.com/sethgrid/pester v1.2.0/go.mod h1:hEUINb4RqvDxtoCaU0BNT/HV4ig5kfgOasrf
11521152
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
11531153
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
11541154
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
1155-
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
1156-
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
11571155
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
11581156
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
11591157
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=

internal/gateways/gateways.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"fmt"
88
"net/http"
99
"os"
10+
"os/exec"
1011
"path/filepath"
1112
"pinata/internal/common"
1213
"pinata/internal/config"
1314
"pinata/internal/types"
1415
"pinata/internal/utils"
16+
"runtime"
1517
"strings"
1618
"time"
17-
18-
"github.com/skratchdot/open-golang/open"
1919
)
2020

2121
func FindGatewayDomain() ([]byte, error) {
@@ -174,38 +174,46 @@ func GetAccessLink(cid string, expires int, network string) (types.GetSignedURLR
174174
return response, nil
175175
}
176176

177-
func Convert(cid string) (string, error) {
178-
domain, err := FindGatewayDomain()
177+
func OpenCID(cid string, network string) error {
178+
networkParam, err := config.GetNetworkParam(network)
179179
if err != nil {
180-
return "", err
180+
return fmt.Errorf("problem getting network parameter: %w", err)
181181
}
182182

183-
url := fmt.Sprintf("https://%s/ipfs/%s", domain, cid)
184-
fmt.Println(url)
185-
186-
return url, nil
187-
}
188-
189-
func OpenCID(cid string, network string) error {
190-
if network == "public" {
191-
url, err := Convert(cid)
192-
if err != nil {
193-
return fmt.Errorf("problem creating URL: %w", err)
194-
}
195-
err = open.Run(url)
183+
var url string
184+
if networkParam == "public" {
185+
domain, err := FindGatewayDomain()
196186
if err != nil {
197-
return fmt.Errorf("problem opening URL: %w", err)
187+
return fmt.Errorf("problem finding gateway domain: %w", err)
198188
}
199-
return nil
189+
url = fmt.Sprintf("https://%s/ipfs/%s", string(domain), cid)
200190
} else {
201-
data, err := GetAccessLink(cid, 30, network)
191+
resp, err := GetAccessLink(cid, 30, networkParam)
202192
if err != nil {
203193
return fmt.Errorf("problem creating URL: %w", err)
204194
}
205-
err = open.Run(data.Data)
206-
if err != nil {
207-
return fmt.Errorf("problem opening URL: %w", err)
208-
}
209-
return nil
195+
196+
url = resp.Data
197+
url = strings.ReplaceAll(url, "\\u0026", "&")
198+
url = strings.Trim(url, "\"")
199+
}
200+
201+
fmt.Printf("Opening URL: %s\n", url)
202+
203+
switch runtime.GOOS {
204+
case "darwin":
205+
err = exec.Command("open", url).Start()
206+
case "windows":
207+
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
208+
case "linux":
209+
err = exec.Command("xdg-open", url).Start()
210+
default:
211+
err = fmt.Errorf("unsupported platform")
212+
}
213+
214+
if err != nil {
215+
return fmt.Errorf("problem opening URL: %w", err)
210216
}
217+
218+
return nil
211219
}

0 commit comments

Comments
 (0)