FILE / ScuroNeko/mtg

ipblocklist/files/local.go

Исходный файл и его история в репозитории.
FILE e6fa69d28890ebe1ee287f8338b19de54fe9862e
Files
mtg/ipblocklist/files/local.go
T
2021-11-29 07:26:27 +03:00

31 lines
541 B
Go

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
}