37 lines
648 B
Go
37 lines
648 B
Go
package storage
|
|
|
|
import "io"
|
|
import "os"
|
|
import "path/filepath"
|
|
|
|
type FileStore struct {
|
|
BaseDir string
|
|
}
|
|
|
|
func (fs FileStore) Ensure() error {
|
|
return os.MkdirAll(fs.BaseDir, 0o755)
|
|
}
|
|
|
|
func (fs FileStore) Save(name string, r io.Reader) (string, int64, error) {
|
|
var err error
|
|
var file *os.File
|
|
var path string
|
|
var n int64
|
|
err = fs.Ensure()
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
path = filepath.Join(fs.BaseDir, name)
|
|
file, err = os.Create(path)
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
defer file.Close()
|
|
n, err = io.Copy(file, r)
|
|
return path, n, err
|
|
}
|
|
|
|
func (fs FileStore) Open(path string) (*os.File, error) {
|
|
return os.Open(path)
|
|
}
|