-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
113 lines (93 loc) · 2.9 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
package filestore
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
)
type FileSystemStorage struct {
basePath string
baseURL string
}
func NewFileSystemStorage(basePath string, baseURL string) *FileSystemStorage {
return &FileSystemStorage{
basePath: basePath,
baseURL: baseURL,
}
}
func (s *FileSystemStorage) List(ctx context.Context, prefix string) ([]FileStoreItem, error) {
fullPath := filepath.Join(s.basePath, prefix)
files, err := os.ReadDir(fullPath)
if err != nil {
return nil, fmt.Errorf("error reading directory: %w", err)
}
items := []FileStoreItem{}
for _, f := range files {
path := filepath.Join(prefix, f.Name())
info, err := f.Info()
if err != nil {
return nil, fmt.Errorf("error fetching file info: %w", err)
}
items = append(items, FileStoreItem{
Directory: f.IsDir(),
Name: f.Name(),
Path: path,
URL: fmt.Sprintf("%s/%s", s.baseURL, path),
Created: info.ModTime().Unix(),
Size: info.Size(),
})
}
return items, nil
}
func (s *FileSystemStorage) Get(ctx context.Context, path string) (FileStoreItem, error) {
fullPath := filepath.Join(s.basePath, path)
info, err := os.Stat(fullPath)
if err != nil {
return FileStoreItem{}, fmt.Errorf("error fetching file info: %w", err)
}
return FileStoreItem{
Directory: info.IsDir(),
Name: info.Name(),
Path: path,
URL: fmt.Sprintf("%s/%s", s.baseURL, path),
Created: info.ModTime().Unix(),
Size: info.Size(),
}, nil
}
func (s *FileSystemStorage) Upload(ctx context.Context, path string, r io.Reader) (FileStoreItem, error) {
fullPath := filepath.Join(s.basePath, path)
file, err := os.Create(fullPath)
if err != nil {
return FileStoreItem{}, fmt.Errorf("error creating file: %w", err)
}
defer file.Close()
if _, err := io.Copy(file, r); err != nil {
return FileStoreItem{}, fmt.Errorf("failed to copy content to file: %w", err)
}
return s.Get(ctx, path)
}
func (s *FileSystemStorage) Rename(ctx context.Context, path string, newPath string) (FileStoreItem, error) {
src := filepath.Join(s.basePath, path)
dst := filepath.Join(s.basePath, newPath)
if err := os.Rename(src, dst); err != nil {
return FileStoreItem{}, fmt.Errorf("failed to rename file or directory: %w", err)
}
return s.Get(ctx, newPath)
}
func (s *FileSystemStorage) Delete(ctx context.Context, path string) error {
fullPath := filepath.Join(s.basePath, path)
if err := os.RemoveAll(fullPath); err != nil {
return fmt.Errorf("failed to delete file or directory: %w", err)
}
return nil
}
func (s *FileSystemStorage) CreateFolder(ctx context.Context, path string) (FileStoreItem, error) {
fullPath := filepath.Join(s.basePath, path)
if err := os.MkdirAll(fullPath, os.ModePerm); err != nil {
return FileStoreItem{}, fmt.Errorf("failed to create folder: %w", err)
}
return s.Get(ctx, path)
}
// Compile-time interface check:
var _ FileStore = (*FileSystemStorage)(nil)