-
Notifications
You must be signed in to change notification settings - Fork 5
/
buckets.go
357 lines (315 loc) · 9.51 KB
/
buckets.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package buckets
// @todo: Clean up error messages
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
c "github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/textileio/go-buckets/collection"
"github.com/textileio/go-buckets/dag"
"github.com/textileio/go-buckets/dns"
"github.com/textileio/go-buckets/ipns"
dbc "github.com/textileio/go-threads/api/client"
"github.com/textileio/go-threads/core/did"
core "github.com/textileio/go-threads/core/thread"
"github.com/textileio/go-threads/db"
nc "github.com/textileio/go-threads/net/api/client"
"github.com/textileio/go-threads/net/util"
nutil "github.com/textileio/go-threads/net/util"
)
var (
log = logging.Logger("buckets")
// GatewayURL is used to construct externally facing bucket links.
GatewayURL string
// ThreadsGatewayURL is used to construct externally facing bucket links.
ThreadsGatewayURL string
// WWWDomain can be set to specify the domain to use for bucket website hosting, e.g.,
// if this is set to mydomain.com, buckets can be rendered as a website at the following URL:
// https://<bucket_key>.mydomain.com
WWWDomain string
// ErrNonFastForward is returned when an update in non-fast-forward.
ErrNonFastForward = errors.New("update is non-fast-forward")
movePathRegexp = regexp.MustCompile("/ipfs/([^/]+)/")
)
// IsPathNotFoundErr returns whether or not an error indicates a bucket path was not found.
// This is needed because public and private (encrypted) buckets can return different
// errors for the same operation.
func IsPathNotFoundErr(err error) bool {
if err == nil {
return false
}
msg := err.Error()
return strings.Contains(msg, "could not resolve path") ||
strings.Contains(msg, "no link named")
}
// Bucket adds thread ID to collection.Bucket.
type Bucket struct {
Thread core.ID `json:"thread"`
collection.Bucket
}
// PathItem describes a file or directory in a bucket.
type PathItem struct {
Cid string `json:"cid"`
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
IsDir bool `json:"is_dir"`
Items []PathItem `json:"items"`
ItemsCount int32 `json:"items_count"`
Metadata collection.Metadata `json:"metadata"`
}
// Links wraps links for resolving a bucket with various protocols.
type Links struct {
// URL is the thread URL, which maps to a ThreadDB collection instance.
URL string `json:"url"`
// WWW is the URL at which the bucket will be rendered as a website (requires remote DNS configuration).
WWW string `json:"www"`
// IPNS is the bucket IPNS address.
IPNS string `json:"ipns"`
// BPS is the bucket pinning service URL.
BPS string `json:"bps"`
}
// Seed describes a bucket seed file.
type Seed struct {
Cid c.Cid
Data []byte
}
// Buckets is an object storage library built on Threads, IPFS, and IPNS.
type Buckets struct {
net *nc.Client
db *dbc.Client
c *collection.Buckets
ipfs iface.CoreAPI
ipns *ipns.Manager
dns *dns.Manager
locks *nutil.SemaphorePool
}
var _ nutil.SemaphoreKey = (*lock)(nil)
type lock string
func (l lock) Key() string {
return string(l)
}
// Txn allows for holding a bucket lock while performing multiple write operations.
type Txn struct {
b *Buckets
thread core.ID
key string
identity did.Token
lock *util.Semaphore
}
// Close the Txn, releasing the bucket for additional writes.
func (t *Txn) Close() error {
t.lock.Release()
return nil
}
// NewBuckets returns a new buckets library.
func NewBuckets(
net *nc.Client,
db *dbc.Client,
ipfs iface.CoreAPI,
ipns *ipns.Manager,
dns *dns.Manager,
) (*Buckets, error) {
bc, err := collection.NewBuckets(db)
if err != nil {
return nil, fmt.Errorf("getting buckets collection: %v", err)
}
return &Buckets{
net: net,
db: db,
c: bc,
ipfs: ipfs,
ipns: ipns,
dns: dns,
locks: nutil.NewSemaphorePool(1),
}, nil
}
// Close it down.
func (b *Buckets) Close() error {
b.locks.Stop()
return nil
}
// Net returns the underlying thread net client.
func (b *Buckets) Net() *nc.Client {
return b.net
}
// DB returns the underlying thread db client.
func (b *Buckets) DB() *dbc.Client {
return b.db
}
// Ipfs returns the underlying IPFS client.
func (b *Buckets) Ipfs() iface.CoreAPI {
return b.ipfs
}
// NewTxn returns a new Txn for bucket key.
func (b *Buckets) NewTxn(thread core.ID, key string, identity did.Token) (*Txn, error) {
if err := thread.Validate(); err != nil {
return nil, fmt.Errorf("invalid thread id: %v", err)
}
lk := b.locks.Get(lock(key))
lk.Acquire()
return &Txn{
b: b,
thread: thread,
key: key,
identity: identity,
lock: lk,
}, nil
}
// Get returns a bucket by thread id and key.
func (b *Buckets) Get(ctx context.Context, thread core.ID, key string, identity did.Token) (*Bucket, error) {
if err := thread.Validate(); err != nil {
return nil, fmt.Errorf("invalid thread id: %v", err)
}
instance, err := b.c.GetSafe(ctx, thread, key, collection.WithIdentity(identity))
if err != nil {
return nil, err
}
log.Debugf("got %s", key)
return instanceToBucket(thread, instance), nil
}
// GetLinks returns a Links object containing the bucket thread, IPNS, and WWW links by thread and bucket key.
func (b *Buckets) GetLinks(
ctx context.Context,
thread core.ID,
key string,
identity did.Token,
pth string,
) (links Links, err error) {
if err := thread.Validate(); err != nil {
return links, fmt.Errorf("invalid thread id: %v", err)
}
instance, err := b.c.GetSafe(ctx, thread, key, collection.WithIdentity(identity))
if err != nil {
return links, err
}
log.Debugf("got %s links", key)
return b.GetLinksForBucket(ctx, instanceToBucket(thread, instance), identity, pth)
}
// GetLinksForBucket returns a Links object containing the bucket thread, IPNS, and WWW links.
func (b *Buckets) GetLinksForBucket(
ctx context.Context,
bucket *Bucket,
identity did.Token,
pth string,
) (links Links, err error) {
links.URL = fmt.Sprintf("%s/thread/%s/%s/%s", ThreadsGatewayURL, bucket.Thread, collection.Name, bucket.Key)
if len(WWWDomain) != 0 {
parts := strings.Split(GatewayURL, "://")
if len(parts) < 2 {
return links, fmt.Errorf("failed to parse gateway URL: %s", GatewayURL)
}
links.WWW = fmt.Sprintf("%s://%s.%s", parts[0], bucket.Key, WWWDomain)
}
links.IPNS = fmt.Sprintf("%s/ipns/%s", GatewayURL, bucket.Key)
pth = trimSlash(pth)
if _, _, ok := bucket.GetMetadataForPath(pth, false); !ok {
return links, fmt.Errorf("could not resolve path: %s", pth)
}
if len(pth) != 0 {
npth, err := getBucketPath(&bucket.Bucket, pth)
if err != nil {
return links, err
}
linkKey := bucket.GetLinkEncryptionKey()
if _, err := dag.GetNodeAtPath(ctx, b.ipfs, npth, linkKey); err != nil {
return links, fmt.Errorf("could not resolve path: %s", pth)
}
pth = "/" + pth
links.URL += pth
if len(links.WWW) != 0 {
links.WWW += pth
}
links.IPNS += pth
} else {
links.BPS = fmt.Sprintf("%s/bps/%s", GatewayURL, bucket.Key)
}
query := "?token=" + string(identity)
links.URL += query
if len(links.WWW) != 0 {
links.WWW += query
}
links.IPNS += query
return links, nil
}
// List returns all buckets under a thread.
func (b *Buckets) List(ctx context.Context, thread core.ID, identity did.Token) ([]Bucket, error) {
if err := thread.Validate(); err != nil {
return nil, fmt.Errorf("invalid thread id: %v", err)
}
list, err := b.c.List(ctx, thread, &db.Query{}, &collection.Bucket{}, collection.WithIdentity(identity))
if err != nil {
return nil, fmt.Errorf("listing buckets: %v", err)
}
instances := list.([]*collection.Bucket)
bucks := make([]Bucket, len(instances))
for i, in := range instances {
bucket := instanceToBucket(thread, in)
bucks[i] = *bucket
}
log.Debugf("listed all in %s", thread)
return bucks, nil
}
// Remove removes an entire bucket, unpinning all data.
func (b *Buckets) Remove(ctx context.Context, thread core.ID, key string, identity did.Token) (int64, error) {
txn, err := b.NewTxn(thread, key, identity)
if err != nil {
return 0, err
}
defer txn.Close()
return txn.Remove(ctx)
}
// Remove is Txn based Remove.
func (t *Txn) Remove(ctx context.Context) (int64, error) {
instance, err := t.b.c.GetSafe(ctx, t.thread, t.key, collection.WithIdentity(t.identity))
if err != nil {
return 0, err
}
if err := t.b.c.Delete(ctx, t.thread, t.key, collection.WithIdentity(t.identity)); err != nil {
return 0, fmt.Errorf("deleting bucket: %v", err)
}
buckPath, err := dag.NewResolvedPath(instance.Path)
if err != nil {
return 0, fmt.Errorf("resolving path: %v", err)
}
linkKey := instance.GetLinkEncryptionKey()
if linkKey != nil {
ctx, err = dag.UnpinNodeAndBranch(ctx, t.b.ipfs, buckPath, linkKey)
if err != nil {
return 0, err
}
} else {
ctx, err = dag.UnpinPath(ctx, t.b.ipfs, buckPath)
if err != nil {
return 0, err
}
}
if err := t.b.ipns.RemoveKey(ctx, t.key); err != nil {
return 0, err
}
log.Debugf("removed %s", t.key)
return dag.GetPinnedBytes(ctx), nil
}
func (b *Buckets) saveAndPublish(
ctx context.Context,
thread core.ID,
identity did.Token,
instance *collection.Bucket,
) error {
if err := b.c.Save(ctx, thread, instance, collection.WithIdentity(identity)); err != nil {
return fmt.Errorf("saving bucket: %v", err)
}
go b.ipns.Publish(path.New(instance.Path), instance.Key)
return nil
}
func instanceToBucket(thread core.ID, instance *collection.Bucket) *Bucket {
return &Bucket{
Thread: thread,
Bucket: *instance,
}
}