diff --git a/cmd/files.go b/cmd/files.go index fd5a9e5..f350fa5 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -144,7 +144,7 @@ func preparePath(path string) string { func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCache bool) error { if shouldCache { - supported, _, err := isSupportedFileType(path) + supported, _, _, err := isSupportedFileType(path) if err != nil { return err } @@ -358,26 +358,30 @@ func pathIsValid(filePath string, paths []string) bool { } } -func isSupportedFileType(path string) (bool, string, error) { +func isSupportedFileType(path string) (bool, string, string, error) { file, err := os.Open(path) switch { case errors.Is(err, os.ErrNotExist): - return false, "", nil + return false, "", "", nil case err != nil: - return false, "", err + return false, "", "", err } defer file.Close() head := make([]byte, 261) file.Read(head) + extension := strings.TrimPrefix(filepath.Ext(path), ".") + + fileType := filetype.GetType(extension) + switch { case filetype.IsImage(head): - return true, "image", nil + return true, "image", fileType.MIME.Value, nil case filetype.IsVideo(head): - return true, "video", nil + return true, "video", fileType.MIME.Value, nil default: - return false, "", nil + return false, "", "", nil } } @@ -393,7 +397,7 @@ func pathHasSupportedFiles(path string) (bool, error) { case !recursive && info.IsDir() && p != path: return filepath.SkipDir case !info.IsDir(): - supported, _, err := isSupportedFileType(p) + supported, _, _, err := isSupportedFileType(p) if err != nil { return err } @@ -641,7 +645,7 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin filePath := fileList[val] if !fromCache { - supported, _, err := isSupportedFileType(filePath) + supported, _, _, err := isSupportedFileType(filePath) if err != nil { return "", err } diff --git a/cmd/root.go b/cmd/root.go index f1b7790..8a6f911 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ var ( ) const ( - Version string = "0.61.1" + Version string = "0.61.2" ) var ( diff --git a/cmd/web.go b/cmd/web.go index 7865a2c..ae9d1eb 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -974,7 +974,7 @@ func serveMedia(paths []string, Regexes *Regexes, index *Index) httprouter.Handl return } - supported, fileType, err := isSupportedFileType(filePath) + supported, fileType, mime, err := isSupportedFileType(filePath) if err != nil { fmt.Println(err) @@ -1034,17 +1034,19 @@ func serveMedia(paths []string, Regexes *Regexes, index *Index) httprouter.Handl switch fileType { case "image": - htmlBody.WriteString(fmt.Sprintf(`Roulette selected: %s`, + htmlBody.WriteString(fmt.Sprintf(`Roulette selected: %s`, queryParams, generateFilePath(filePath), dimensions.width, dimensions.height, + mime, fileName)) htmlBody.WriteString(``) case "video": - htmlBody.WriteString(fmt.Sprintf(``, + htmlBody.WriteString(fmt.Sprintf(``, queryParams, generateFilePath(filePath), + mime, fileName)) htmlBody.WriteString(``) }