2024-04-09 18:39:14 -04:00
|
|
|
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)
|
|
|
|
}
|
2024-08-17 18:29:33 -04:00
|
|
|
|
|
|
|
func (f *FilesystemAdapter) FileExists(path string) bool {
|
|
|
|
_, err := os.Stat(f.BasePath + path)
|
|
|
|
return err == nil
|
|
|
|
}
|