2019-01-05 22:44:33 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2020-05-31 23:12:36 +00:00
|
|
|
"crypto/md5" //nolint:gosec
|
|
|
|
"crypto/sha1" //nolint:gosec
|
2019-01-05 22:44:33 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/hex"
|
|
|
|
"hash"
|
2023-11-25 12:35:00 +00:00
|
|
|
"image"
|
2019-01-05 22:44:33 +00:00
|
|
|
"io"
|
2019-01-06 12:59:53 +00:00
|
|
|
"log"
|
2019-01-05 22:44:33 +00:00
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/rules"
|
|
|
|
)
|
|
|
|
|
2024-01-30 09:12:38 +00:00
|
|
|
const PermFile = 0644
|
2023-07-31 10:51:00 +00:00
|
|
|
const PermDir = 0755
|
2023-07-28 16:15:44 +00:00
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
// FileInfo describes a file.
|
|
|
|
type FileInfo struct {
|
|
|
|
*Listing
|
2023-08-26 12:53:18 +00:00
|
|
|
Fs afero.Fs `json:"-"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Extension string `json:"extension"`
|
|
|
|
ModTime time.Time `json:"modified"`
|
|
|
|
Mode os.FileMode `json:"mode"`
|
|
|
|
IsDir bool `json:"isDir"`
|
|
|
|
IsSymlink bool `json:"isSymlink"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Subtitles []string `json:"subtitles,omitempty"`
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
Checksums map[string]string `json:"checksums,omitempty"`
|
|
|
|
Token string `json:"token,omitempty"`
|
|
|
|
currentDir []os.FileInfo `json:"-"`
|
2023-11-25 12:35:00 +00:00
|
|
|
Resolution *ImageResolution `json:"resolution,omitempty"`
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileOptions are the options when getting a file info.
|
|
|
|
type FileOptions struct {
|
2021-01-07 10:30:17 +00:00
|
|
|
Fs afero.Fs
|
|
|
|
Path string
|
|
|
|
Modify bool
|
|
|
|
Expand bool
|
|
|
|
ReadHeader bool
|
2021-03-02 11:00:18 +00:00
|
|
|
Token string
|
2021-01-07 10:30:17 +00:00
|
|
|
Checker rules.Checker
|
2021-04-23 12:04:02 +00:00
|
|
|
Content bool
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 12:35:00 +00:00
|
|
|
type ImageResolution struct {
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
// NewFileInfo creates a File object from a path and a given user. This File
|
|
|
|
// object will be automatically filled depending on if it is a directory
|
|
|
|
// or a file. If it's a video file, it will also detect any subtitles.
|
|
|
|
func NewFileInfo(opts FileOptions) (*FileInfo, error) {
|
|
|
|
if !opts.Checker.Check(opts.Path) {
|
|
|
|
return nil, os.ErrPermission
|
|
|
|
}
|
|
|
|
|
2021-07-26 10:59:09 +00:00
|
|
|
file, err := stat(opts)
|
2019-01-05 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Expand {
|
|
|
|
if file.IsDir {
|
2021-03-03 23:10:08 +00:00
|
|
|
if err := file.readListing(opts.Checker, opts.ReadHeader); err != nil { //nolint:govet
|
2020-05-31 23:12:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return file, nil
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:04:02 +00:00
|
|
|
err = file.detectType(opts.Modify, opts.Content, true)
|
2019-01-05 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
2021-07-26 10:59:09 +00:00
|
|
|
func stat(opts FileOptions) (*FileInfo, error) {
|
|
|
|
var file *FileInfo
|
|
|
|
|
|
|
|
if lstaterFs, ok := opts.Fs.(afero.Lstater); ok {
|
|
|
|
info, _, err := lstaterFs.LstatIfPossible(opts.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
file = &FileInfo{
|
|
|
|
Fs: opts.Fs,
|
|
|
|
Path: opts.Path,
|
|
|
|
Name: info.Name(),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
Mode: info.Mode(),
|
|
|
|
IsDir: info.IsDir(),
|
|
|
|
IsSymlink: IsSymlink(info.Mode()),
|
|
|
|
Size: info.Size(),
|
|
|
|
Extension: filepath.Ext(info.Name()),
|
|
|
|
Token: opts.Token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// regular file
|
|
|
|
if file != nil && !file.IsSymlink {
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fs doesn't support afero.Lstater interface or the file is a symlink
|
|
|
|
info, err := opts.Fs.Stat(opts.Path)
|
|
|
|
if err != nil {
|
|
|
|
// can't follow symlink
|
|
|
|
if file != nil && file.IsSymlink {
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set correct file size in case of symlink
|
|
|
|
if file != nil && file.IsSymlink {
|
|
|
|
file.Size = info.Size()
|
2021-08-04 09:36:25 +00:00
|
|
|
file.IsDir = info.IsDir()
|
2021-07-26 10:59:09 +00:00
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
file = &FileInfo{
|
|
|
|
Fs: opts.Fs,
|
|
|
|
Path: opts.Path,
|
|
|
|
Name: info.Name(),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
Mode: info.Mode(),
|
|
|
|
IsDir: info.IsDir(),
|
|
|
|
Size: info.Size(),
|
|
|
|
Extension: filepath.Ext(info.Name()),
|
|
|
|
Token: opts.Token,
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
// Checksum checksums a given File for a given User, using a specific
|
|
|
|
// algorithm. The checksums data is saved on File object.
|
|
|
|
func (i *FileInfo) Checksum(algo string) error {
|
|
|
|
if i.IsDir {
|
|
|
|
return errors.ErrIsDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.Checksums == nil {
|
|
|
|
i.Checksums = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := i.Fs.Open(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
var h hash.Hash
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
//nolint:gosec
|
2019-01-05 22:44:33 +00:00
|
|
|
switch algo {
|
|
|
|
case "md5":
|
|
|
|
h = md5.New()
|
|
|
|
case "sha1":
|
|
|
|
h = sha1.New()
|
|
|
|
case "sha256":
|
|
|
|
h = sha256.New()
|
|
|
|
case "sha512":
|
|
|
|
h = sha512.New()
|
|
|
|
default:
|
|
|
|
return errors.ErrInvalidOption
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(h, reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Checksums[algo] = hex.EncodeToString(h.Sum(nil))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:59:22 +00:00
|
|
|
func (i *FileInfo) RealPath() string {
|
|
|
|
if realPathFs, ok := i.Fs.(interface {
|
|
|
|
RealPath(name string) (fPath string, err error)
|
|
|
|
}); ok {
|
|
|
|
realPath, err := realPathFs.RealPath(i.Path)
|
|
|
|
if err == nil {
|
|
|
|
return realPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.Path
|
|
|
|
}
|
|
|
|
|
2023-02-16 08:11:12 +00:00
|
|
|
// TODO: use constants
|
|
|
|
//
|
2020-05-31 23:12:36 +00:00
|
|
|
//nolint:goconst
|
2021-01-07 10:30:17 +00:00
|
|
|
func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error {
|
2020-11-24 10:32:23 +00:00
|
|
|
if IsNamedPipe(i.Mode) {
|
|
|
|
i.Type = "blob"
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-06 12:59:53 +00:00
|
|
|
// failing to detect the type should not return error.
|
|
|
|
// imagine the situation where a file in a dir with thousands
|
|
|
|
// of files couldn't be opened: we'd have immediately
|
|
|
|
// a 500 even though it doesn't matter. So we just log it.
|
2019-01-05 22:44:33 +00:00
|
|
|
|
|
|
|
mimetype := mime.TypeByExtension(i.Extension)
|
2021-03-17 18:06:56 +00:00
|
|
|
|
|
|
|
var buffer []byte
|
|
|
|
if readHeader {
|
2021-01-07 10:30:17 +00:00
|
|
|
buffer = i.readFirstBytes()
|
2021-03-17 18:06:56 +00:00
|
|
|
|
|
|
|
if mimetype == "" {
|
|
|
|
mimetype = http.DetectContentType(buffer)
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(mimetype, "video"):
|
|
|
|
i.Type = "video"
|
|
|
|
i.detectSubtitles()
|
|
|
|
return nil
|
|
|
|
case strings.HasPrefix(mimetype, "audio"):
|
|
|
|
i.Type = "audio"
|
|
|
|
return nil
|
|
|
|
case strings.HasPrefix(mimetype, "image"):
|
|
|
|
i.Type = "image"
|
2023-11-25 12:35:00 +00:00
|
|
|
resolution, err := calculateImageResolution(i.Fs, i.Path)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error calculating image resolution: %v", err)
|
|
|
|
} else {
|
|
|
|
i.Resolution = resolution
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
return nil
|
2022-01-10 16:01:21 +00:00
|
|
|
case strings.HasSuffix(mimetype, "pdf"):
|
|
|
|
i.Type = "pdf"
|
|
|
|
return nil
|
2021-03-23 18:18:02 +00:00
|
|
|
case (strings.HasPrefix(mimetype, "text") || !isBinary(buffer)) && i.Size <= 10*1024*1024: // 10 MB
|
2019-01-05 22:44:33 +00:00
|
|
|
i.Type = "text"
|
|
|
|
|
|
|
|
if !modify {
|
|
|
|
i.Type = "textImmutable"
|
|
|
|
}
|
|
|
|
|
2019-01-06 09:24:09 +00:00
|
|
|
if saveContent {
|
|
|
|
afs := &afero.Afero{Fs: i.Fs}
|
|
|
|
content, err := afs.ReadFile(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Content = string(content)
|
|
|
|
}
|
2021-01-07 10:30:17 +00:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
i.Type = "blob"
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-25 12:35:00 +00:00
|
|
|
func calculateImageResolution(fs afero.Fs, filePath string) (*ImageResolution, error) {
|
|
|
|
file, err := fs.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if cErr := file.Close(); cErr != nil {
|
|
|
|
log.Printf("Failed to close file: %v", cErr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
config, _, err := image.DecodeConfig(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ImageResolution{
|
|
|
|
Width: config.Width,
|
|
|
|
Height: config.Height,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 10:30:17 +00:00
|
|
|
func (i *FileInfo) readFirstBytes() []byte {
|
|
|
|
reader, err := i.Fs.Open(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
i.Type = "blob"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
2021-07-26 10:00:05 +00:00
|
|
|
buffer := make([]byte, 512) //nolint:gomnd
|
2021-01-07 10:30:17 +00:00
|
|
|
n, err := reader.Read(buffer)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
log.Print(err)
|
|
|
|
i.Type = "blob"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer[:n]
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
func (i *FileInfo) detectSubtitles() {
|
|
|
|
if i.Type != "video" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Subtitles = []string{}
|
|
|
|
ext := filepath.Ext(i.Path)
|
|
|
|
|
2021-12-20 23:07:43 +00:00
|
|
|
// detect multiple languages. Base*.vtt
|
|
|
|
// TODO: give subtitles descriptive names (lang) and track attributes
|
|
|
|
parentDir := strings.TrimRight(i.Path, i.Name)
|
2023-08-26 12:53:18 +00:00
|
|
|
var dir []os.FileInfo
|
|
|
|
if len(i.currentDir) > 0 {
|
|
|
|
dir = i.currentDir
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
dir, err = afero.ReadDir(i.Fs, parentDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base := strings.TrimSuffix(i.Name, ext)
|
|
|
|
for _, f := range dir {
|
|
|
|
if !f.IsDir() && strings.HasPrefix(f.Name(), base) && strings.HasSuffix(f.Name(), ".vtt") {
|
|
|
|
i.Subtitles = append(i.Subtitles, path.Join(parentDir, f.Name()))
|
2021-12-20 23:07:43 +00:00
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 10:30:17 +00:00
|
|
|
func (i *FileInfo) readListing(checker rules.Checker, readHeader bool) error {
|
2019-01-05 22:44:33 +00:00
|
|
|
afs := &afero.Afero{Fs: i.Fs}
|
|
|
|
dir, err := afs.ReadDir(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
listing := &Listing{
|
|
|
|
Items: []*FileInfo{},
|
|
|
|
NumDirs: 0,
|
|
|
|
NumFiles: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range dir {
|
|
|
|
name := f.Name()
|
2020-05-31 23:12:36 +00:00
|
|
|
fPath := path.Join(i.Path, name)
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
if !checker.Check(fPath) {
|
2019-01-05 22:44:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:14:40 +00:00
|
|
|
isSymlink, isInvalidLink := false, false
|
2020-11-24 10:32:23 +00:00
|
|
|
if IsSymlink(f.Mode()) {
|
2021-07-26 10:59:09 +00:00
|
|
|
isSymlink = true
|
2019-01-05 22:44:33 +00:00
|
|
|
// It's a symbolic link. We try to follow it. If it doesn't work,
|
2020-11-24 10:32:23 +00:00
|
|
|
// we stay with the link information instead of the target's.
|
2020-05-31 23:12:36 +00:00
|
|
|
info, err := i.Fs.Stat(fPath)
|
2019-01-05 22:44:33 +00:00
|
|
|
if err == nil {
|
|
|
|
f = info
|
2022-05-05 15:14:40 +00:00
|
|
|
} else {
|
|
|
|
isInvalidLink = true
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file := &FileInfo{
|
2023-08-26 12:53:18 +00:00
|
|
|
Fs: i.Fs,
|
|
|
|
Name: name,
|
|
|
|
Size: f.Size(),
|
|
|
|
ModTime: f.ModTime(),
|
|
|
|
Mode: f.Mode(),
|
|
|
|
IsDir: f.IsDir(),
|
|
|
|
IsSymlink: isSymlink,
|
|
|
|
Extension: filepath.Ext(name),
|
|
|
|
Path: fPath,
|
|
|
|
currentDir: dir,
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 12:35:00 +00:00
|
|
|
if !file.IsDir && strings.HasPrefix(mime.TypeByExtension(file.Extension), "image/") {
|
|
|
|
resolution, err := calculateImageResolution(file.Fs, file.Path)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error calculating resolution for image %s: %v", file.Path, err)
|
|
|
|
} else {
|
|
|
|
file.Resolution = resolution
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
if file.IsDir {
|
|
|
|
listing.NumDirs++
|
|
|
|
} else {
|
|
|
|
listing.NumFiles++
|
|
|
|
|
2022-05-05 15:14:40 +00:00
|
|
|
if isInvalidLink {
|
|
|
|
file.Type = "invalid_link"
|
|
|
|
} else {
|
|
|
|
err := file.detectType(true, false, readHeader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listing.Items = append(listing.Items, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Listing = listing
|
|
|
|
return nil
|
|
|
|
}
|