-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
76 lines (63 loc) · 1.53 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
package storage
import (
"io/ioutil"
"os"
"strings"
)
var DefaultFilePermissions os.FileMode = 0755
//FileSystemStorage is a file system storage handler
type FileSystemStorage struct {
*BaseStorage
}
//NewFileSystemStorage returns a file system storage engine
func NewFileSystemStorage(location string) Storage {
return &FileSystemStorage{
&BaseStorage{
Location: location,
},
}
}
//Save a file
func (s *FileSystemStorage) Save(path string, f File) error {
return s.SaveWithPermissions(path, f, DefaultFilePermissions)
}
//Save with file permissions
func (s *FileSystemStorage) SaveWithPermissions(path string, f File, perm os.FileMode) error {
_, err := os.Stat(s.Location)
if err != nil {
return err
}
location := s.PathWithPrefix(path)
basename := location[:strings.LastIndex(location, "/")+1]
err = os.MkdirAll(basename, perm)
if err != nil {
return err
}
content, err := f.ReadAll()
if err != nil {
return err
}
err = ioutil.WriteFile(location, content, perm)
return err
}
//Open file and get contents
func (s *FileSystemStorage) Open(path string) (File, error) {
f, err := os.Open(s.PathWithPrefix(path))
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return NewContentFile(b), nil
}
//Delete a file
func (s *FileSystemStorage) Delete(path string) error {
return os.Remove(s.PathWithPrefix(path))
}
//Exists checks if a file exists
func (s *FileSystemStorage) Exists(path string) bool {
_, err := os.Stat(s.PathWithPrefix(path))
return err == nil
}