diff options
Diffstat (limited to 'internal/media/util.go')
-rw-r--r-- | internal/media/util.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/internal/media/util.go b/internal/media/util.go index f743e3821..22121a546 100644 --- a/internal/media/util.go +++ b/internal/media/util.go @@ -22,13 +22,60 @@ import ( "errors" "fmt" "io" + "io/fs" "os" + "path" "codeberg.org/gruf/go-bytesize" "codeberg.org/gruf/go-iotools" "codeberg.org/gruf/go-mimetypes" ) +// file represents one file +// with the given flag and perms. +type file struct { + abs string + flag int + perm os.FileMode +} + +// allowFiles implements fs.FS to allow +// access to a specified slice of files. +type allowFiles []file + +// Open implements fs.FS. +func (af allowFiles) Open(name string) (fs.File, error) { + for _, file := range af { + var ( + abs = file.abs + flag = file.flag + perm = file.perm + ) + + // Allowed to open file + // at absolute path. + if name == file.abs { + return os.OpenFile(abs, flag, perm) + } + + // Check for other valid reads. + thisDir, thisFile := path.Split(file.abs) + + // Allowed to read directory itself. + if name == thisDir || name == "." { + return os.OpenFile(thisDir, flag, perm) + } + + // Allowed to read file + // itself (at relative path). + if name == thisFile { + return os.OpenFile(abs, flag, perm) + } + } + + return nil, os.ErrPermission +} + // getExtension splits file extension from path. func getExtension(path string) string { for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- { |