-
Notifications
You must be signed in to change notification settings - Fork 42
/
bucket.go
156 lines (137 loc) · 4.15 KB
/
bucket.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
package bucket
import (
"context"
"io"
"sync"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/textileio/go-threads/core/thread"
bucketsClient "github.com/textileio/textile/v2/api/bucketsd/client"
bucketsproto "github.com/textileio/textile/v2/api/bucketsd/pb"
"github.com/textileio/textile/v2/buckets"
)
type BucketData struct {
Key string `json:"_id"`
Name string `json:"name"`
Path string `json:"path"`
DNSRecord string `json:"dns_record,omitempty"`
//Archives Archives `json:"archives"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type DirEntries bucketsproto.ListPathResponse
type BucketsClient interface {
PushPath(ctx context.Context, key, pth string, reader io.Reader, opts ...bucketsClient.Option) (result path.Resolved, root path.Resolved, err error)
PullPath(ctx context.Context, key, pth string, writer io.Writer, opts ...bucketsClient.Option) error
ListPath(ctx context.Context, key, pth string) (*bucketsproto.ListPathResponse, error)
RemovePath(ctx context.Context, key, pth string, opts ...bucketsClient.Option) (path.Resolved, error)
ListIpfsPath(ctx context.Context, ipfsPath path.Path) (*bucketsproto.ListIpfsPathResponse, error)
PushPathAccessRoles(ctx context.Context, key, path string, roles map[string]buckets.Role) error
}
type EachFunc = func(ctx context.Context, b *Bucket, path string) error
type BucketInterface interface {
Slug() string
Key() string
GetData() BucketData
GetContext(ctx context.Context) (context.Context, *thread.ID, error)
GetClient() BucketsClient
GetThreadID(ctx context.Context) (*thread.ID, error)
DirExists(ctx context.Context, path string) (bool, error)
FileExists(ctx context.Context, path string) (bool, error)
UpdatedAt(ctx context.Context, path string) (int64, error)
UploadFile(
ctx context.Context,
path string,
reader io.Reader,
) (result path.Resolved, root path.Path, err error)
DownloadFile(
ctx context.Context,
path string,
reader io.Reader,
) (result path.Resolved, root path.Path, err error)
GetFile(
ctx context.Context,
path string,
w io.Writer,
) error
CreateDirectory(
ctx context.Context,
path string,
) (result path.Resolved, root path.Path, err error)
ListDirectory(
ctx context.Context,
path string,
) (*DirEntries, error)
DeleteDirOrFile(
ctx context.Context,
path string,
) (path.Resolved, error)
ItemsCount(
ctx context.Context,
path string,
withRecursive bool,
) (int32, error)
Each(
ctx context.Context,
path string,
iterator EachFunc,
withRecursive bool,
) (int, error)
}
type Notifier interface {
OnUploadFile(bucketSlug string, bucketPath string, result path.Resolved, root path.Path)
}
// NOTE: all write operations should use the lock for the bucket to keep consistency
// TODO: Maybe read operations dont need a lock, needs testing
// struct for implementing bucket interface
type Bucket struct {
lock sync.RWMutex
root *bucketsproto.Root
bucketsClient BucketsClient
getBucketContext GetBucketContextFn
notifier Notifier
}
func (b *Bucket) Slug() string {
return b.GetData().Name
}
type GetBucketContextFn func(context.Context, string) (context.Context, *thread.ID, error)
func New(
root *bucketsproto.Root,
getBucketContext GetBucketContextFn,
bucketsClient BucketsClient,
) *Bucket {
return &Bucket{
root: root,
bucketsClient: bucketsClient,
getBucketContext: getBucketContext,
notifier: nil,
}
}
func (b *Bucket) Key() string {
return b.GetData().Key
}
func (b *Bucket) GetData() BucketData {
return BucketData{
Key: b.root.Key,
Name: b.root.Name,
Path: b.root.Path,
DNSRecord: "",
CreatedAt: b.root.CreatedAt,
UpdatedAt: b.root.UpdatedAt,
}
}
func (b *Bucket) GetContext(ctx context.Context) (context.Context, *thread.ID, error) {
return b.getBucketContext(ctx, b.root.Name)
}
func (b *Bucket) GetClient() BucketsClient {
return b.bucketsClient
}
func (b *Bucket) GetThreadID(ctx context.Context) (*thread.ID, error) {
_, threadID, err := b.GetContext(ctx)
if err != nil {
return nil, err
}
return threadID, nil
}
func (b *Bucket) AttachNotifier(n Notifier) {
b.notifier = n
}