-
Notifications
You must be signed in to change notification settings - Fork 42
/
public.go
137 lines (109 loc) · 3.67 KB
/
public.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
package textile
import (
"context"
"io"
"github.com/FleekHQ/space-daemon/config"
"github.com/FleekHQ/space-daemon/core/ipfs"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"
api_buckets_pb "github.com/textileio/textile/api/buckets/pb"
"github.com/FleekHQ/space-daemon/core/textile/bucket"
"github.com/FleekHQ/space-daemon/core/textile/utils"
"github.com/FleekHQ/space-daemon/log"
"github.com/textileio/go-threads/core/thread"
bc "github.com/textileio/textile/api/buckets/client"
)
// Get a public bucket on hub. Public bucket has no encryption and its content should be accessible directly via ipfs/ipns
// Only use this bucket for items that is okay to be publicly shared
func (tc *textileClient) GetPublicShareBucket(ctx context.Context) (Bucket, error) {
if err := tc.requiresRunning(); err != nil {
return nil, err
}
return tc.getOrCreatePublicBucket(ctx, defaultPublicShareBucketSlug)
}
func (tc *textileClient) getOrCreatePublicBucket(ctx context.Context, bucketSlug string) (Bucket, error) {
ctx, dbId, err := tc.getPublicShareBucketContext(ctx, bucketSlug)
if err != nil {
return nil, err
}
// find if bucket exists
buckets, err := tc.hb.List(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get public bucket")
}
if buckets != nil {
for _, bucketRoot := range buckets.Roots {
if bucketRoot.Name == bucketSlug {
return bucket.New(
bucketRoot,
tc.getPublicShareBucketContext,
tc.hb,
), nil
}
}
}
// else create bucketRoot
bucketRoot, err := tc.createPublicBucket(ctx, *dbId, bucketSlug)
if err != nil {
return nil, err
}
newB := bucket.New(
bucketRoot,
tc.getPublicShareBucketContext,
tc.hb,
)
return newB, nil
}
func (tc *textileClient) getPublicShareBucketContext(ctx context.Context, bucketSlug string) (context.Context, *thread.ID, error) {
dbId, err := tc.getPublicShareThread(ctx)
if err != nil {
return nil, nil, err
}
ctx, err = utils.GetThreadContext(ctx, bucketSlug, dbId, true, tc.kc, tc.hubAuth, nil)
if err != nil {
return nil, nil, err
}
return ctx, &dbId, nil
}
// Creates a public bucket for current user.
func (tc *textileClient) createPublicBucket(ctx context.Context, dbId thread.ID, bucketSlug string) (*api_buckets_pb.Root, error) {
log.Debug("Creating a new public bucket")
hubCtx, _, err := tc.getBucketContext(ctx, utils.CastDbIDToString(dbId), bucketSlug, true, nil)
if err != nil {
return nil, err
}
b, err := tc.hb.Create(hubCtx, bc.WithName(bucketSlug), bc.WithPrivate(false))
if err != nil {
return nil, err
}
return b.Root, nil
}
const publicShareThreadStoreKey = "publicSharedThreadKey"
// Creates a remote hub thread for the public sharing bucket
func (tc *textileClient) getPublicShareThread(ctx context.Context) (thread.ID, error) {
// check if db id already exists
storedDbId, err := tc.store.Get([]byte(publicShareThreadStoreKey))
if err == nil {
return thread.Cast(storedDbId)
}
// else create new db
ctx, err = tc.getHubCtx(ctx)
if err != nil {
return thread.Undef, err
}
dbId := thread.NewIDV1(thread.Raw, 32)
if err := tc.ht.NewDB(ctx, dbId); err != nil {
return thread.Undef, err
}
log.Debug("Public share thread created")
err = tc.store.Set([]byte(publicShareThreadStoreKey), []byte(dbId))
if err != nil {
return thread.Undef, errors.Wrap(err, "failed to persist public share thread")
}
return dbId, nil
}
// DownloadPublicGatewayItem download a cid content from the hubs public gateway
func (tc *textileClient) DownloadPublicGatewayItem(ctx context.Context, cid cid.Cid) (io.ReadCloser, error) {
gatewayUrl := tc.cfg.GetString(config.TextileHubGatewayUrl, "https://hub-dev.space.storage")
return ipfs.DownloadIpfsItem(ctx, gatewayUrl, cid)
}