hatecomputers.club/adapters/files/filesystem/filesystem.go

43 lines
790 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)
}
func (f *FilesystemAdapter) FileExists(path string) bool {
_, err := os.Stat(f.BasePath + path)
return err == nil
}