This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipfs.go
90 lines (76 loc) · 2 KB
/
ipfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package wbipfs
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
shell "github.com/ipfs/go-ipfs-api"
"github.com/wabarc/ipfs-pinner/pkg/infura"
"github.com/wabarc/ipfs-pinner/pkg/pinata"
)
func Publish(s string) (string, error) {
return "", fmt.Errorf("%s", "embed mode removed.")
}
// Publish to IPFS use local daemon
type Daemon struct {
Host string
Port uint
}
func NewDaemon(host string, port uint) *Daemon {
dm := &Daemon{
Host: "localhost",
Port: 5001,
}
if len(host) > 0 {
dm.Host = host
}
if port < 65536 || port > 0 {
dm.Port = port
}
return dm
}
func (dm *Daemon) Transfer(source string) (string, error) {
if len(dm.Host) == 0 || dm.Port > 65535 || dm.Port < 1 {
return "", fmt.Errorf("IPFS host or port is not valid")
}
content, err := ioutil.ReadFile(source)
if err != nil {
return "", fmt.Errorf("Read file failed: %w", err)
}
sh := shell.NewShell(fmt.Sprintf("%s:%d", dm.Host, dm.Port))
cid, err := sh.Add(strings.NewReader(string(content)))
if err != nil {
return "", fmt.Errorf("Add file to IPFS failed: %w", err)
}
return cid, nil
}
// Pinner is the handle of put the file to IPFS by pinning service.
// It supports two slots which infura and pinata, you can apply a specific slot via WAYBACK_SLOT env.
// If you use the pinata slot, it requires WAYBACK_APIKEY and WAYBACK_SECRET environment variable.
func Pinner(source string) (string, error) {
var err error
var cid string
var slot string = os.Getenv("WAYBACK_SLOT")
switch slot {
default:
cid, err = infura.PinFile(source)
case "infura":
cid, err = infura.PinFile(source)
case "pinata":
apikey := os.Getenv("WAYBACK_APIKEY")
secret := os.Getenv("WAYBACK_SECRET")
if apikey == "" || secret == "" {
log.Println("Please set WAYBACK_APIKEY or WAYBACK_SECRET env.")
err = fmt.Errorf("Missing WAYBACK_APIKEY or WAYBACK_SECRET env.")
break
}
pnt := &pinata.Pinata{Apikey: apikey, Secret: secret}
cid, err = pnt.PinFile(source)
}
if err != nil {
return "", err
} else {
return cid, nil
}
}