mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:44:03 +03:00
Add local file abstraction
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user