-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilestorage.go
130 lines (108 loc) · 4.35 KB
/
filestorage.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
// Package filestorage describes interfaces to deal with filestorage
package filestorage
import (
"context"
"io"
)
// FileStorage is a interface to store and retrieve files
//go:generate mockgen -destination implementations/mockfilestorage/filestorage.go -package mockfilestorage github.com/Nivl/go-filestorage FileStorage
type FileStorage interface {
// ID returns the unique identifier of the storage provider
ID() string
// SetBucket sets the bucket that will contains the files
SetBucket(bucket string) error
// Read fetches a file a returns a reader
// Returns os.ErrNotExist if the file does not exists
// Will use the defaut context
Read(filepath string) (io.ReadCloser, error)
// ReadCtx fetches a file a returns a reader
// Returns os.ErrNotExist if the file does not exists
ReadCtx(ctx context.Context, filepath string) (io.ReadCloser, error)
// Write copies the provided io.Reader to dest
// Will use the defaut context
Write(src io.Reader, destPath string) error
// WriteCtx copies the provided io.Reader to dest
WriteCtx(ctx context.Context, src io.Reader, destPath string) error
// Delete removes a file, ignores files that do not exist
// Will use the defaut context
Delete(filepath string) error
// DeleteCtx removes a file, ignores files that do not exist
DeleteCtx(ctx context.Context, filepath string) error
// URL returns the URL of the file
// Will use the defaut context
URL(filepath string) (string, error)
// URLCtx returns the URL of the file
URLCtx(ctx context.Context, filepath string) (string, error)
// SetAttributes sets the attributes of the file
// Will use the defaut context
SetAttributes(filepath string, attrs *UpdatableFileAttributes) (*FileAttributes, error)
// SetAttributesCtx sets the attributes of the file
SetAttributesCtx(ctx context.Context, filepath string, attrs *UpdatableFileAttributes) (*FileAttributes, error)
// Attributes returns the attributes of the file
// Will use the defaut context
Attributes(filepath string) (*FileAttributes, error)
// AttributesCtx returns the attributes of the file
AttributesCtx(ctx context.Context, filepath string) (*FileAttributes, error)
// Exists check if a file exists
// Will use the defaut context
Exists(filepath string) (bool, error)
// ExistsCtx check if a file exists
ExistsCtx(ctx context.Context, filepath string) (bool, error)
// WriteIfNotExist copies the provided io.Reader to dest if the file does
// not already exist
// Returns:
// - A boolean specifying if the file got uploaded (true) or if already
// existed (false).
// - A URL to the uploaded file
// - An error if something went wrong
// Will use the defaut context
WriteIfNotExist(src io.Reader, destPath string) (new bool, url string, err error)
// WriteIfNotExistCtx copies the provided io.Reader to dest if the file does
// not already exist
// Returns:
// - A boolean specifying if the file got uploaded (true) or if already
// existed (false).
// - A URL to the uploaded file
// - An error if something went wrong
WriteIfNotExistCtx(ctx context.Context, src io.Reader, destPath string) (new bool, url string, err error)
}
// FileAttributes represents the attributes a file can have
type FileAttributes struct {
ContentType string
ContentLanguage string
ContentEncoding string
ContentDisposition string
CacheControl string
Metadata map[string]string
}
// NewFileAttributesFromUpdatable returns a FileAttributes from a UpdatableFileAttributes
func NewFileAttributesFromUpdatable(attrs *UpdatableFileAttributes) *FileAttributes {
fa := &FileAttributes{
Metadata: attrs.Metadata,
}
if val, ok := attrs.ContentType.(string); ok {
fa.ContentType = val
}
if val, ok := attrs.ContentDisposition.(string); ok {
fa.ContentDisposition = val
}
if val, ok := attrs.ContentLanguage.(string); ok {
fa.ContentLanguage = val
}
if val, ok := attrs.ContentEncoding.(string); ok {
fa.ContentEncoding = val
}
if val, ok := attrs.CacheControl.(string); ok {
fa.CacheControl = val
}
return fa
}
// UpdatableFileAttributes represents the updatable attributes a file can have
type UpdatableFileAttributes struct {
ContentType interface{}
ContentLanguage interface{}
ContentEncoding interface{}
ContentDisposition interface{}
CacheControl interface{}
Metadata map[string]string // set to map[string]string{} to delete
}