-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs.go
218 lines (177 loc) · 4.35 KB
/
fs.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package storage
import (
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/picosh/send/utils"
)
// https://stackoverflow.com/a/32482941
func dirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
type StorageFS struct {
Dir string
Logger *slog.Logger
}
var _ ObjectStorage = &StorageFS{}
var _ ObjectStorage = (*StorageFS)(nil)
func NewStorageFS(logger *slog.Logger, dir string) (*StorageFS, error) {
return &StorageFS{Logger: logger, Dir: dir}, nil
}
func (s *StorageFS) GetBucket(name string) (Bucket, error) {
dirPath := filepath.Join(s.Dir, name)
bucket := Bucket{
Name: name,
Path: dirPath,
}
s.Logger.Info("get bucket", "dir", dirPath)
info, err := os.Stat(dirPath)
if os.IsNotExist(err) {
return bucket, fmt.Errorf("directory does not exist: %v %w", dirPath, err)
}
if err != nil {
return bucket, fmt.Errorf("directory error: %v %w", dirPath, err)
}
if !info.IsDir() {
return bucket, fmt.Errorf("directory is a file, not a directory: %#v", dirPath)
}
return bucket, nil
}
func (s *StorageFS) UpsertBucket(name string) (Bucket, error) {
s.Logger.Info("upsert bucket", "name", name)
bucket, err := s.GetBucket(name)
if err == nil {
return bucket, nil
}
dir := filepath.Join(s.Dir, bucket.Path)
s.Logger.Info("bucket not found, creating", "dir", dir, "err", err)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return bucket, err
}
return bucket, nil
}
func (s *StorageFS) GetBucketQuota(bucket Bucket) (uint64, error) {
dsize, err := dirSize(bucket.Path)
return uint64(dsize), err
}
func (s *StorageFS) DeleteBucket(bucket Bucket) error {
return os.RemoveAll(bucket.Path)
}
func (s *StorageFS) GetObject(bucket Bucket, fpath string) (utils.ReaderAtCloser, *ObjectInfo, error) {
objInfo := &ObjectInfo{
LastModified: time.Time{},
Metadata: nil,
UserMetadata: map[string]string{},
}
dat, err := os.Open(filepath.Join(bucket.Path, fpath))
if err != nil {
return nil, objInfo, err
}
info, err := dat.Stat()
if err != nil {
return nil, objInfo, err
}
objInfo.Size = info.Size()
objInfo.LastModified = info.ModTime()
return dat, objInfo, nil
}
func (s *StorageFS) PutObject(bucket Bucket, fpath string, contents io.Reader, entry *utils.FileEntry) (string, int64, error) {
loc := filepath.Join(bucket.Path, fpath)
err := os.MkdirAll(filepath.Dir(loc), os.ModePerm)
if err != nil {
return "", 0, err
}
f, err := os.OpenFile(loc, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return "", 0, err
}
size, err := io.Copy(f, contents)
if err != nil {
return "", 0, err
}
f.Close()
if entry.Mtime > 0 {
uTime := time.Unix(entry.Mtime, 0)
_ = os.Chtimes(loc, uTime, uTime)
}
return loc, size, nil
}
func (s *StorageFS) DeleteObject(bucket Bucket, fpath string) error {
loc := filepath.Join(bucket.Path, fpath)
err := os.Remove(loc)
if err != nil {
return err
}
return nil
}
func (s *StorageFS) ListBuckets() ([]string, error) {
return []string{}, fmt.Errorf("not implemented")
}
func (s *StorageFS) ListObjects(bucket Bucket, dir string, recursive bool) ([]os.FileInfo, error) {
var fileList []os.FileInfo
fpath := path.Join(bucket.Path, dir)
info, err := os.Stat(fpath)
if err != nil {
return fileList, err
}
if info.IsDir() && !strings.HasSuffix(dir, "/") {
fileList = append(fileList, &utils.VirtualFile{
FName: "",
FIsDir: info.IsDir(),
FSize: info.Size(),
FModTime: info.ModTime(),
})
return fileList, err
}
var files []fs.DirEntry
if recursive {
err = filepath.WalkDir(fpath, func(s string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
files = append(files, d)
return nil
})
if err != nil {
fileList = append(fileList, info)
return fileList, nil
}
} else {
files, err = os.ReadDir(fpath)
if err != nil {
fileList = append(fileList, info)
return fileList, nil
}
}
for _, f := range files {
info, err := f.Info()
if err != nil {
return fileList, err
}
i := &utils.VirtualFile{
FName: f.Name(),
FIsDir: f.IsDir(),
FSize: info.Size(),
FModTime: info.ModTime(),
}
fileList = append(fileList, i)
}
return fileList, err
}