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/fstorage/filestorage.go

47 lines
855 B
Go
Raw Normal View History

2021-05-02 07:15:10 +00:00
package fstorage
2021-02-28 14:01:30 +00:00
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 {
2021-05-14 13:34:26 +00:00
return errors.Wrap(err, "couldn't create a file")
2021-02-28 14:01:30 +00:00
}
defer f.Close()
_, err = io.Copy(f, file)
if err != nil {
2021-05-14 13:34:26 +00:00
return errors.Wrap(err, "couldn't write a file")
2021-02-28 14:01:30 +00:00
}
return nil
}
func (storage *fileStorage) Remove(filename string) error {
2021-02-28 14:01:30 +00:00
fullPath := path.Join(storage.basePath, filename)
err := os.Remove(fullPath)
2021-05-14 13:34:26 +00:00
if err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "couldn't remove a file")
2021-02-28 14:01:30 +00:00
}
return nil
}