@@ -14,24 +14,25 @@ import (
14
14
"github.com/labdao/plex/internal/bacalhau"
15
15
)
16
16
17
- func DeriveIpfsNodeUrl () ( string , error ) {
18
- bacalApiHost := bacalhau . GetBacalhauApiHost ( )
17
+ func DeriveIpfsNodeUrl () string {
18
+ ipfsApiHost , exists := os . LookupEnv ( "IPFS_API_HOST" )
19
19
20
- // If bacalApiHost already starts with http:// or https://, don't prepend it.
21
- if strings .HasPrefix (bacalApiHost , "http://" ) || strings .HasPrefix (bacalApiHost , "https://" ) {
22
- ipfsUrl := fmt .Sprintf ("%s:5001" , bacalApiHost )
23
- return ipfsUrl , nil
20
+ // If IPFS_API_HOST is not set, use BACALHAU_API_HOST
21
+ if ! exists {
22
+ ipfsApiHost = bacalhau .GetBacalhauApiHost ()
24
23
}
25
24
26
- ipfsUrl := fmt .Sprintf ("http://%s:5001" , bacalApiHost )
27
- return ipfsUrl , nil
25
+ // If ipfsApiHost already starts with http:// or https://, don't prepend it.
26
+ if strings .HasPrefix (ipfsApiHost , "http://" ) || strings .HasPrefix (ipfsApiHost , "https://" ) {
27
+ ipfsUrl := fmt .Sprintf ("%s:5001" , ipfsApiHost )
28
+ return ipfsUrl
29
+ }
30
+ ipfsUrl := fmt .Sprintf ("http://%s:5001" , ipfsApiHost )
31
+ return ipfsUrl
28
32
}
29
33
30
34
func PinDir (dirPath string ) (cid string , err error ) {
31
- ipfsNodeUrl , err := DeriveIpfsNodeUrl ()
32
- if err != nil {
33
- return "" , err
34
- }
35
+ ipfsNodeUrl := DeriveIpfsNodeUrl ()
35
36
sh := shell .NewShell (ipfsNodeUrl )
36
37
cid , err = sh .AddDir (dirPath )
37
38
if err != nil {
@@ -42,10 +43,7 @@ func PinDir(dirPath string) (cid string, err error) {
42
43
}
43
44
44
45
func PinFile (filePath string ) (cid string , err error ) {
45
- ipfsNodeUrl , err := DeriveIpfsNodeUrl ()
46
- if err != nil {
47
- return "" , err
48
- }
46
+ ipfsNodeUrl := DeriveIpfsNodeUrl ()
49
47
sh := shell .NewShell (ipfsNodeUrl )
50
48
51
49
// Open the file and ensure it gets closed
@@ -68,10 +66,7 @@ func PinFile(filePath string) (cid string, err error) {
68
66
69
67
// wraps a file in a directory and adds it to IPFS
70
68
func WrapAndPinFile (filePath string ) (cid string , err error ) {
71
- ipfsNodeUrl , err := DeriveIpfsNodeUrl ()
72
- if err != nil {
73
- return "" , err
74
- }
69
+ ipfsNodeUrl := DeriveIpfsNodeUrl ()
75
70
sh := shell .NewShell (ipfsNodeUrl )
76
71
77
72
absPath , err := filepath .Abs (filePath )
@@ -115,17 +110,14 @@ func WrapAndPinFile(filePath string) (cid string, err error) {
115
110
}
116
111
117
112
func DownloadToDirectory (cid , directory string ) error {
118
- ipfsNodeUrl , err := DeriveIpfsNodeUrl ()
119
- if err != nil {
120
- return err
121
- }
113
+ ipfsNodeUrl := DeriveIpfsNodeUrl ()
122
114
sh := shell .NewShell (ipfsNodeUrl )
123
115
124
116
// Construct the full directory path where the CID content will be downloaded
125
117
downloadPath := path .Join (directory , cid )
126
118
127
119
// Use the Get method to download the file or directory with the specified CID
128
- err = sh .Get (cid , downloadPath )
120
+ err : = sh .Get (cid , downloadPath )
129
121
if err != nil {
130
122
return err
131
123
}
@@ -134,10 +126,7 @@ func DownloadToDirectory(cid, directory string) error {
134
126
}
135
127
136
128
func DownloadToTempDir (cid string ) (string , error ) {
137
- ipfsNodeUrl , err := DeriveIpfsNodeUrl ()
138
- if err != nil {
139
- return "" , err
140
- }
129
+ ipfsNodeUrl := DeriveIpfsNodeUrl ()
141
130
sh := shell .NewShell (ipfsNodeUrl )
142
131
143
132
// Get the system's temporary directory
@@ -147,7 +136,7 @@ func DownloadToTempDir(cid string) (string, error) {
147
136
downloadPath := path .Join (tempDir , cid )
148
137
149
138
// Use the Get method to download the file or directory with the specified CID
150
- err = sh .Get (cid , downloadPath )
139
+ err : = sh .Get (cid , downloadPath )
151
140
if err != nil {
152
141
return "" , err
153
142
}
@@ -227,10 +216,7 @@ func onlyOneFile(dirPath string) (bool, string, error) {
227
216
}
228
217
229
218
func DownloadFileContents (cid , filepath string ) error {
230
- ipfsNodeUrl , err := DeriveIpfsNodeUrl ()
231
- if err != nil {
232
- return err
233
- }
219
+ ipfsNodeUrl := DeriveIpfsNodeUrl ()
234
220
sh := shell .NewShell (ipfsNodeUrl )
235
221
236
222
// Use the cat method to get the file with the specified CID
0 commit comments