38 lines
671 B
Go
38 lines
671 B
Go
|
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)
|
||
|
}
|