blob: e7e671f1971e1c232404f00931030fb44fe6759c (
plain)
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
|
package filesystem
import (
"io"
"os"
"path/filepath"
)
type FilesystemAdapter struct {
BasePath string
Permissions os.FileMode
}
func (f *FilesystemAdapter) CreateFile(path string, content io.Reader) (string, error) {
fullPath := f.BasePath + path
dir := filepath.Dir(fullPath)
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, f.Permissions)
}
file, err := os.Create(f.BasePath + path)
if err != nil {
return "", err
}
defer file.Close()
_, err = io.Copy(file, content)
if err != nil {
return "", err
}
return path, nil
}
func (f *FilesystemAdapter) DeleteFile(path string) error {
return os.Remove(f.BasePath + path)
}
func (f *FilesystemAdapter) FileExists(path string) bool {
_, err := os.Stat(f.BasePath + path)
return err == nil
}
|