generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fs.go
114 lines (91 loc) · 2.57 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
package fs
import (
"context"
"io"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/datumforge/datum/pkg/utils/storage"
)
// Storage is a local filesystem storage interface
type Storage struct {
root string
}
// Config is the configuration for Storage
type Config struct {
// Enabled is a flag to enable or disable the storage
Enabled bool `json:"enabled" koanf:"enabled" default:"false"`
// Root is the root directory for the filesystem storage
Root string `json:"root" koanf:"root" default:"./storage"`
}
// NewStorage returns a new filesystem storage with the provided configuration
func NewStorage(cfg Config) *Storage {
return &Storage{root: cfg.Root}
}
// abs returns the absolute path of a given path
func (fs *Storage) abs(path string) string {
return filepath.Join(fs.root, path)
}
// Save saves content to the provided path
func (fs *Storage) Save(ctx context.Context, content io.Reader, path string) error {
abs := fs.abs(path)
if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil { // nolint:gomnd
return errors.WithStack(err)
}
w, err := os.Create(abs)
if err != nil {
return errors.WithStack(err)
}
defer w.Close()
if _, err := io.Copy(w, content); err != nil {
return errors.WithStack(err)
}
return nil
}
// Stat returns path metadata
func (fs *Storage) Stat(ctx context.Context, path string) (*storage.Stat, error) {
fi, err := os.Stat(fs.abs(path))
if err != nil {
if errors.Is(err, storage.ErrNotExist) {
return nil, storage.ErrNotExist
}
return nil, errors.WithStack(err)
}
return &storage.Stat{
ModifiedTime: fi.ModTime(),
Size: fi.Size(),
}, nil
}
// Open opens path for reading
func (fs *Storage) Open(ctx context.Context, path string) (io.ReadCloser, error) {
f, err := os.Open(fs.abs(path))
if err != nil {
if errors.Is(err, storage.ErrNotExist) {
return nil, storage.ErrNotExist
}
return nil, errors.WithStack(err)
}
return f, errors.WithStack(err)
}
// Delete deletes path
func (fs *Storage) Delete(ctx context.Context, path string) error {
return os.Remove(fs.abs(path))
}
// OpenWithStat opens path for reading with file stats
func (fs *Storage) OpenWithStat(ctx context.Context, path string) (io.ReadCloser, *storage.Stat, error) {
f, err := os.Open(fs.abs(path))
if err != nil {
if errors.Is(err, storage.ErrNotExist) {
return nil, nil, storage.ErrNotExist
}
return nil, nil, errors.WithStack(err)
}
stat, err := f.Stat()
if err != nil {
return nil, nil, errors.WithStack(err)
}
return f, &storage.Stat{
ModifiedTime: stat.ModTime(),
Size: stat.Size(),
}, nil
}