-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestorage.go
188 lines (159 loc) · 5.38 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
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
// Package fsstorage is an implementation of filestorage using the filesystem
package fsstorage
import (
"context"
"io"
"io/ioutil"
"os"
"path"
"github.com/Nivl/go-filestorage"
"github.com/Nivl/go-filestorage/implementations"
)
// New returns a new instance of a File System Storage
func New() (*FSStorage, error) {
dir, err := ioutil.TempDir("", "storage")
if err != nil {
return nil, err
}
return NewWithContext(context.Background(), dir)
}
// NewWithDir returns a new instance of a File System Storage with
func NewWithDir(path string) (*FSStorage, error) {
return NewWithContext(context.Background(), path)
}
// NewWithContext returns a new GCStorage instance using a new Google Cloud
// Storage client attached to the provided context
func NewWithContext(ctx context.Context, path string) (*FSStorage, error) {
return &FSStorage{
path: path,
ctx: ctx,
}, nil
}
// FSStorage is an implementation of the FileStorage interface for the file system
type FSStorage struct {
path string
bucket string
ctx context.Context
}
// ID returns the unique identifier of the storage provider
func (s *FSStorage) ID() string {
return "file_system"
}
// SetBucket is used to set the bucket
func (s *FSStorage) SetBucket(name string) error {
s.bucket = name
return nil
}
// Read fetches a file a returns a reader
// Will use the defaut context
func (s *FSStorage) Read(filepath string) (io.ReadCloser, error) {
return s.ReadCtx(s.ctx, filepath)
}
// ReadCtx fetches a file a returns a reader
func (s *FSStorage) ReadCtx(ctx context.Context, filepath string) (io.ReadCloser, error) {
return os.Open(s.fullPath(filepath))
}
// Exists check if a file exists
// Will use the defaut context
func (s *FSStorage) Exists(filepath string) (bool, error) {
return s.ExistsCtx(s.ctx, filepath)
}
// ExistsCtx check if a file exists
func (s *FSStorage) ExistsCtx(ctx context.Context, filepath string) (bool, error) {
fi, err := os.Stat(s.fullPath(filepath))
if err == nil {
return !fi.IsDir(), err
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Write copy the provided os.File to dest
// Will use the defaut context
func (s *FSStorage) Write(src io.Reader, destPath string) error {
return s.WriteCtx(s.ctx, src, destPath)
}
// WriteCtx copy the provided os.File to dest
func (s *FSStorage) WriteCtx(ctx context.Context, src io.Reader, destPath string) error {
fullPath := s.fullPath(destPath)
// make sure the path exists
if err := os.MkdirAll(path.Dir(fullPath), os.ModePerm); err != nil {
return err
}
dest, err := os.Create(fullPath)
if err != nil {
return err
}
defer dest.Close()
// Copy the file
_, err = io.Copy(dest, src)
if err != nil {
return err
}
return nil
}
// Delete removes a file, ignores files that do not exist
// Will use the defaut context
func (s *FSStorage) Delete(filepath string) error {
return s.DeleteCtx(s.ctx, filepath)
}
// DeleteCtx removes a file, ignores files that do not exist
func (s *FSStorage) DeleteCtx(ctx context.Context, filepath string) error {
err := os.Remove(s.fullPath(filepath))
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// URL returns the URL of the file
// Will use the defaut context
func (s *FSStorage) URL(filepath string) (string, error) {
return s.URLCtx(s.ctx, filepath)
}
// URLCtx returns the URL of the file
func (s *FSStorage) URLCtx(ctx context.Context, filepath string) (string, error) {
return s.fullPath(filepath), nil
}
// SetAttributes sets the attributes of the file
// Will use the defaut context
func (s *FSStorage) SetAttributes(filepath string, attrs *filestorage.UpdatableFileAttributes) (*filestorage.FileAttributes, error) {
return s.SetAttributesCtx(s.ctx, filepath, attrs)
}
// SetAttributesCtx sets the attributes of the file
func (s *FSStorage) SetAttributesCtx(ctx context.Context, filepath string, attrs *filestorage.UpdatableFileAttributes) (*filestorage.FileAttributes, error) {
return filestorage.NewFileAttributesFromUpdatable(attrs), nil
}
// Attributes returns the attributes of the file
// Will use the defaut context
func (s *FSStorage) Attributes(filepath string) (*filestorage.FileAttributes, error) {
return s.AttributesCtx(s.ctx, filepath)
}
// AttributesCtx returns the attributes of the file
func (s *FSStorage) AttributesCtx(ctx context.Context, filepath string) (*filestorage.FileAttributes, error) {
return &filestorage.FileAttributes{}, nil
}
func (s *FSStorage) fullPath(filepath string) string {
return path.Join(s.path, s.bucket, filepath)
}
// 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
func (s *FSStorage) WriteIfNotExist(src io.Reader, destPath string) (new bool, url string, err error) {
return s.WriteIfNotExistCtx(s.ctx, src, destPath)
}
// 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
func (s *FSStorage) WriteIfNotExistCtx(ctx context.Context, src io.Reader, destPath string) (new bool, url string, err error) {
return implementations.WriteIfNotExist(ctx, s, src, destPath)
}