summaryrefslogtreecommitdiff
path: root/adapters/files/filesystem/filesystem.go
blob: 726a58845b20cc9f2fc8b1e71fe0a6cb08b8d7ca (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
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)
}