43 lines
843 B
Go
43 lines
843 B
Go
package storage
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
type Storage interface {
|
|
Exists(p string) bool
|
|
Open(p string) (fs.File, error)
|
|
Write(p string, b []byte) (int, error)
|
|
WriteFrom(p string, r io.Reader) (int64, error)
|
|
}
|
|
|
|
type withRoot struct {
|
|
root string
|
|
Storage
|
|
}
|
|
|
|
func WithRoot(rootDir string, s Storage) Storage {
|
|
return &withRoot{root: rootDir, Storage: s}
|
|
}
|
|
|
|
func (f *withRoot) Exists(p string) bool {
|
|
return f.Storage.Exists(path.Join(f.root, p))
|
|
}
|
|
|
|
func (f *withRoot) Open(p string) (fs.File, error) {
|
|
return f.Storage.Open(path.Join(f.root, p))
|
|
}
|
|
|
|
func (f *withRoot) Write(p string, b []byte) (int, error) {
|
|
return f.Storage.Write(path.Join(f.root, p), b)
|
|
}
|
|
|
|
func (f *withRoot) WriteFrom(p string, r io.Reader) (int64, error) {
|
|
return f.Storage.WriteFrom(path.Join(f.root, p), r)
|
|
}
|
|
|
|
var ErrNotExists = os.ErrNotExist
|