/
ipfs.go
145 lines (126 loc) · 2.71 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ipfs
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/gnames/gnfiles/ent/exofs"
"github.com/gnames/gnfiles/ent/metadata"
"github.com/gnames/gnfiles/ent/paths"
"github.com/gnames/gnfmt"
api "github.com/ipfs/go-ipfs-api"
)
type efs struct {
ipfs *api.Shell
}
func NewExoFS(url string) exofs.ExoFS {
return &efs{
ipfs: api.NewShell(url),
}
}
func (e *efs) GetMetaData(
source, metaPath string,
) (metadata.MetaFiles, error) {
var meta metadata.MetaFiles
err := e.Get(source, metaPath)
if err != nil {
return meta, err
}
txtJSON, err := ioutil.ReadFile(metaPath)
if err != nil {
return meta, err
}
enc := gnfmt.GNjson{}
// skipping the error for cases where txtJSON does not contain
// metadata information
err = enc.Decode(txtJSON, &meta)
if err != nil {
log.Printf("Cannod decode _META.json: %v", err)
}
res := make(map[string]*metadata.MetaData)
for k, v := range meta {
if v.ID == "" || strings.HasPrefix(k, "/_META") {
continue
}
res[k] = v
}
mf := metadata.MetaFiles(res)
return mf, err
}
func (e *efs) Add(path string) (id string, err error) {
var f io.Reader
f, err = os.Open(path)
if err != nil {
return id, err
}
return e.ipfs.Add(f)
}
func (e *efs) Get(source, path string) error {
if isFile(source) {
return GetterFile{}.Get(source, path)
}
if isURL(source) {
return GetterHTTP{}.Get(source, path)
}
if isPathIPNS(source) {
return e.ipfs.Get(source, path)
}
if isKeyIPNS(source) {
return e.ipfs.Get(paths.IPNSPath(source), path)
}
if isPathIPFS(source) {
source = strings.TrimLeft(source, "/ipfs/")
}
if isCID(source) {
return e.ipfs.Get(source, path)
}
return fmt.Errorf("unknown source type '%s'", source)
}
func (e *efs) Pin(id string) error {
return e.ipfs.Pin(paths.IPFSPath(id))
}
func (e *efs) Unpin(id string) error {
return e.ipfs.Unpin(paths.IPFSPath(id))
}
func (e *efs) PinExists(id string) (bool, error) {
res, err := e.ipfs.Pins()
if err != nil {
return false, err
}
for k, v := range res {
if k == id && v.Type == "recursive" {
return true, nil
}
}
return false, nil
}
func (e *efs) Keys() ([]*api.Key, error) {
return e.ipfs.KeyList(context.Background())
}
func (e *efs) KeyIPNS(keyName string) (*api.Key, error) {
var res *api.Key
if keyName == "" {
return res, nil
}
keys, err := e.Keys()
if err != nil {
return res, err
}
for i := range keys {
if keys[i].Name == keyName {
return keys[i], nil
}
}
return res, fmt.Errorf("cannot find IPNS key '%s'", keyName)
}
func (e *efs) Publish(name, id string) (key string, err error) {
resp, err := e.ipfs.PublishWithDetails(id, name, 0, 10*time.Second, false)
if err != nil {
return "", err
}
return resp.Name, err
}