-
Notifications
You must be signed in to change notification settings - Fork 16
/
pinner.go
124 lines (113 loc) · 3.46 KB
/
pinner.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package pinner // import "github.com/wabarc/ipfs-pinner"
import (
"fmt"
"io"
"net/http"
"os"
"github.com/wabarc/ipfs-pinner/pkg/infura"
"github.com/wabarc/ipfs-pinner/pkg/nftstorage"
"github.com/wabarc/ipfs-pinner/pkg/pinata"
"github.com/wabarc/ipfs-pinner/pkg/web3storage"
)
var ErrPinner = fmt.Errorf("unsupported pinner")
const (
Infura = "infura"
Pinata = "pinata"
NFTStorage = "nftstorage"
Web3Storage = "web3storage"
)
// Config represents pinner's configuration. Pinner is the identifier of
// the target IPFS service.
type Config struct {
*http.Client
Pinner string
Apikey string
Secret string
}
// Pin pins a file to a network and returns a content id and an error. The file
// is an interface to access the file. It's contents may be either stored in
// memory or on disk. If stored on disk, it's underlying concrete type should
// be a file path. If it is in memory, it should be an *io.Reader or byte slice.
//
//nolint:gocyclo
func (cfg *Config) Pin(path interface{}) (cid string, err error) {
// TODO using generics
err = ErrPinner
switch v := path.(type) {
case string:
_, err = os.Lstat(v)
if err != nil {
return
}
switch cfg.Pinner {
case Infura:
inf := &infura.Infura{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
cid, err = inf.PinFile(v)
case Pinata:
pnt := &pinata.Pinata{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
cid, err = pnt.PinFile(v)
case NFTStorage:
nft := &nftstorage.NFTStorage{Apikey: cfg.Apikey, Client: cfg.Client}
cid, err = nft.PinFile(v)
case Web3Storage:
web3 := &web3storage.Web3Storage{Apikey: cfg.Apikey, Client: cfg.Client}
cid, err = web3.PinFile(v)
}
case io.Reader:
switch cfg.Pinner {
case Infura:
inf := &infura.Infura{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
cid, err = inf.PinWithReader(v)
case Pinata:
pnt := &pinata.Pinata{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
cid, err = pnt.PinWithReader(v)
case NFTStorage:
nft := &nftstorage.NFTStorage{Apikey: cfg.Apikey, Client: cfg.Client}
cid, err = nft.PinWithReader(v)
case Web3Storage:
web3 := &web3storage.Web3Storage{Apikey: cfg.Apikey, Client: cfg.Client}
cid, err = web3.PinWithReader(v)
}
case []byte:
switch cfg.Pinner {
case Infura:
inf := &infura.Infura{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
cid, err = inf.PinWithBytes(v)
case Pinata:
pnt := &pinata.Pinata{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
cid, err = pnt.PinWithBytes(v)
case NFTStorage:
nft := &nftstorage.NFTStorage{Apikey: cfg.Apikey, Client: cfg.Client}
cid, err = nft.PinWithBytes(v)
case Web3Storage:
web3 := &web3storage.Web3Storage{Apikey: cfg.Apikey, Client: cfg.Client}
cid, err = web3.PinWithBytes(v)
}
}
if err != nil {
err = fmt.Errorf("%s: %w", cfg.Pinner, err)
}
return cid, err
}
// PinHash pins from any IPFS node, returns the original cid and an error.
func (cfg *Config) PinHash(cid string) (string, error) {
ok := false
err := ErrPinner
switch cfg.Pinner {
case Infura:
inf := &infura.Infura{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
ok, err = inf.PinHash(cid)
case Pinata:
pnt := &pinata.Pinata{Apikey: cfg.Apikey, Secret: cfg.Secret, Client: cfg.Client}
ok, err = pnt.PinHash(cid)
}
if ok {
return cid, nil
}
return "", err
}
// WithClient attach http.Client
func (cfg *Config) WithClient(c *http.Client) *Config {
cfg.Client = c
return cfg
}