-
Notifications
You must be signed in to change notification settings - Fork 0
/
localfile.go
52 lines (44 loc) · 1.13 KB
/
localfile.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
package objectstore
import (
"fmt"
"os"
"path/filepath"
)
type localFile struct {
basepath string
}
func newLocalFile(basepath string) *localFile {
return &localFile{basepath: basepath}
}
func (lf *localFile) Read(name string) ([]byte, error) {
object := lf.joinPath(lf.basepath, name)
return os.ReadFile(object)
}
func (lf *localFile) Write(name string, data []byte) error {
object := lf.joinPath(lf.basepath, name)
dir := filepath.Dir(object)
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return fmt.Errorf("'%s' does not exist or is not a directory", dir)
}
return os.WriteFile(object, data, 0640)
}
func (lf *localFile) List() ([]string, error) {
files := []string{}
err := filepath.Walk(lf.basepath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, info.Name())
}
return nil
})
return files, err
}
func (lf *localFile) Delete(name string) error {
object := lf.joinPath(lf.basepath, name)
return os.Remove(object)
}
func (lf *localFile) joinPath(basepath, name string) string {
return filepath.Join(basepath, name)
}