This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
backend/pkg/filestorage/filestorage.go

47 lines
818 B
Go
Raw Normal View History

2021-02-28 14:01:30 +00:00
package filestorage
import (
"io"
"os"
"path"
"github.com/pkg/errors"
)
type fileStorage struct {
basePath string
}
func New(cfg *Config) FileStorage {
if cfg == nil {
cfg = getDefaultConfig()
}
return &fileStorage{
cfg.BasePath,
}
}
func (storage *fileStorage) Put(file io.Reader, filename string) error {
fullPath := path.Join(storage.basePath, filename)
f, err := os.Create(fullPath)
if err != nil {
return errors.Wrap(err, "FileStorage.Put")
}
defer f.Close()
_, err = io.Copy(f, file)
if err != nil {
return errors.Wrap(err, "FileStorage.Put")
}
return nil
}
func (storage *fileStorage) Delete(filename string) error {
fullPath := path.Join(storage.basePath, filename)
err := os.Remove(fullPath)
if err != nil {
return errors.Wrap(err, "FileStorage.Delete")
}
return nil
}