-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
69 lines (60 loc) · 1.42 KB
/
service.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"slices"
)
type FilevaultService interface {
CreateFile(path string, file io.Reader) error
RemoveFile(path string) error
ReadFile(path string) ([]byte, error)
}
var _ FilevaultService = (*filevaultService)(nil)
type filevaultService struct {
cfg Config
}
func NewFilevaultService(cfg Config) FilevaultService {
return filevaultService{cfg}
}
func (f filevaultService) filePathJoin(paths ...string) string {
p := slices.Concat([]string{f.cfg.Dir}, paths)
return filepath.Join(p...)
}
func (f filevaultService) CreateFile(path string, r io.Reader) error {
path = f.filePathJoin(path)
dir, filename := filepath.Split(path)
mode := os.FileMode(0755)
if err := os.MkdirAll(dir, mode); err != nil {
return err
}
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("file with the name of %s is already existing in %s", filename, dir)
}
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
_, err = file.ReadFrom(r)
return err
}
func (f filevaultService) RemoveFile(path string) error {
path = f.filePathJoin(path)
if err := os.Remove(path); err != nil {
return err
}
dir := filepath.Dir(path)
elems, err := os.ReadDir(dir)
if err != nil {
return err
}
if len(elems) == 0 {
return os.Remove(dir)
}
return nil
}
func (f filevaultService) ReadFile(path string) ([]byte, error) {
return os.ReadFile(f.filePathJoin(path))
}