1
0
Fork 0
mirror of https://github.com/Luzifer/gallery.git synced 2024-12-23 03:11:23 +00:00
gallery/storage_file.go

44 lines
917 B
Go
Raw Permalink Normal View History

2018-04-28 16:51:55 +00:00
package main
import (
"io"
"os"
"path"
)
type fileStorage struct {
path string
}
// WriteFile takes the content of a file and writes it into the underlying
// storage system.
func (l fileStorage) WriteFile(filepath string, content io.Reader, contentType string) error {
fullPath := path.Join(l.path, filepath)
if err := os.MkdirAll(path.Dir(fullPath), 0755); err != nil {
return err
}
fp, err := os.Create(fullPath)
if err != nil {
return err
}
defer fp.Close()
_, err = io.Copy(fp, content)
return err
}
// ReadFile retrieves a file from the underlying storage, needs to return
// errFileNotFound when file is not present.
func (l fileStorage) ReadFile(filepath string) (io.ReadCloser, error) {
fullPath := path.Join(l.path, filepath)
if _, err := os.Stat(fullPath); err != nil {
if os.IsNotExist(err) {
return nil, errFileNotFound
}
return nil, err
}
return os.Open(fullPath)
}