-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestore.go
66 lines (55 loc) · 1.97 KB
/
filestore.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
package filestore
import (
"context"
"errors"
"io"
)
// A Storer stores the content of the given reader (e.g. a file) and returns a consistent hash for later retrieval.
type Storer interface {
Store(ctx context.Context, r io.Reader) (hash string, err error)
}
// A HashedStorer stores the content of the given reader (e.g. a file) with a pre-calculated hash.
// The hash can be chosen freely and is not checked against the reader content.
type HashedStorer interface {
StoreHashed(ctx context.Context, r io.Reader, hash string) error
}
// A Fetcher fetches the content of the given hash in the form of an io.Reader.
type Fetcher interface {
Fetch(ctx context.Context, hash string) (io.ReadCloser, error)
}
// An Exister checks if the given hash exists in the store.
type Exister interface {
Exists(ctx context.Context, hash string) (bool, error)
}
// An Iterator iterates over all stored files and returns their hashes in batches.
type Iterator interface {
// Iterate calls callback with a maxBatch number of asset hashes.
// If callback returns an error, iteration stops and the error is returned.
Iterate(ctx context.Context, maxBatch int, callback func(hashes []string) error) error
}
// A Remover can remove a file with the given hash.
type Remover interface {
Remove(ctx context.Context, hash string) error
}
// A Sizer can return the size of a file with the given hash.
type Sizer interface {
Size(ctx context.Context, hash string) (int64, error)
}
// An ImgproxyURLSourcer can return the source URL to original file for imgproxy.
type ImgproxyURLSourcer interface {
// ImgproxyURLSource gets the source URL to original file (e.g. for use with imgproxy).
ImgproxyURLSource(hash string) (string, error)
}
// A FileStore bundles all the interfaces above.
type FileStore interface {
Storer
HashedStorer
Exister
Fetcher
Iterator
Remover
Sizer
ImgproxyURLSourcer
}
// ErrNotExist is returned when a stored file does not exist.
var ErrNotExist = errors.New("file does not exist")