diff --git a/files/file.go b/files/file.go index 5abf0792..d58b2a34 100644 --- a/files/file.go +++ b/files/file.go @@ -135,6 +135,10 @@ func (i *FileInfo) Checksum(algo string) error { //nolint:goconst //TODO: use constants func (i *FileInfo) detectType(modify, saveContent bool) error { + if IsNamedPipe(i.Mode) { + i.Type = "blob" + return nil + } // 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 @@ -232,9 +236,9 @@ func (i *FileInfo) readListing(checker rules.Checker) error { continue } - if strings.HasPrefix(f.Mode().String(), "L") { + if IsSymlink(f.Mode()) { // It's a symbolic link. We try to follow it. If it doesn't work, - // we stay with the link information instead if the target's. + // we stay with the link information instead of the target's. info, err := i.Fs.Stat(fPath) if err == nil { f = info diff --git a/files/utils.go b/files/utils.go index 519a5326..bcaa13f1 100644 --- a/files/utils.go +++ b/files/utils.go @@ -1,6 +1,7 @@ package files import ( + "os" "unicode/utf8" ) @@ -48,3 +49,11 @@ func isBinary(content []byte, _ int) bool { } return false } + +func IsNamedPipe(mode os.FileMode) bool { + return mode&os.ModeNamedPipe != 0 +} + +func IsSymlink(mode os.FileMode) bool { + return mode&os.ModeSymlink != 0 +} diff --git a/http/raw.go b/http/raw.go index 12b51536..1f3c19ef 100644 --- a/http/raw.go +++ b/http/raw.go @@ -1,7 +1,9 @@ package http import ( + "bytes" "errors" + "io/ioutil" "net/http" "net/url" gopath "path" @@ -9,6 +11,7 @@ import ( "strings" "github.com/mholt/archiver" + "github.com/spf13/afero" "github.com/filebrowser/filebrowser/v2/files" "github.com/filebrowser/filebrowser/v2/fileutils" @@ -91,6 +94,11 @@ var rawHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) return errToStatus(err), err } + if files.IsNamedPipe(file.Mode) { + setContentDisposition(w, r, file) + return 0, nil + } + if !file.IsDir { return rawFileHandler(w, r, file) } @@ -110,11 +118,18 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error { return err } - file, err := d.user.Fs.Open(path) - if err != nil { - return err + var ( + file afero.File + arcReadCloser = ioutil.NopCloser(&bytes.Buffer{}) + ) + if !files.IsNamedPipe(info.Mode()) { + file, err = d.user.Fs.Open(path) + if err != nil { + return err + } + defer file.Close() + arcReadCloser = file } - defer file.Close() if path != commonPath { filename := strings.TrimPrefix(path, commonPath) @@ -124,7 +139,7 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error { FileInfo: info, CustomName: filename, }, - ReadCloser: file, + ReadCloser: arcReadCloser, }) if err != nil { return err