Add local file abstraction

This commit is contained in:
9seconds
2021-11-29 07:26:27 +03:00
parent 3540408adf
commit c14a2329c5
6 changed files with 147 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
package files
import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
type localFile struct {
root fs.FS
name string
}
func (l localFile) Open(ctx context.Context) (io.ReadCloser, error) {
return l.root.Open(l.name)
}
func NewLocal(path string) (File, error) {
if stat, err := os.Stat(path); os.IsNotExist(err) || stat.IsDir() || stat.Mode().Perm()&0o400 == 0 {
return nil, fmt.Errorf("%s is not a readable file", path)
}
return localFile{
root: os.DirFS(filepath.Dir(path)),
name: filepath.Base(path),
}, nil
}