Added mime type tags to <img> and <video>

This commit is contained in:
Seednode 2023-09-10 20:41:08 -05:00
parent 8fa4a7c652
commit 8d0d271f79
3 changed files with 19 additions and 13 deletions

View File

@ -144,7 +144,7 @@ func preparePath(path string) string {
func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCache bool) error { func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCache bool) error {
if shouldCache { if shouldCache {
supported, _, err := isSupportedFileType(path) supported, _, _, err := isSupportedFileType(path)
if err != nil { if err != nil {
return err 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) file, err := os.Open(path)
switch { switch {
case errors.Is(err, os.ErrNotExist): case errors.Is(err, os.ErrNotExist):
return false, "", nil return false, "", "", nil
case err != nil: case err != nil:
return false, "", err return false, "", "", err
} }
defer file.Close() defer file.Close()
head := make([]byte, 261) head := make([]byte, 261)
file.Read(head) file.Read(head)
extension := strings.TrimPrefix(filepath.Ext(path), ".")
fileType := filetype.GetType(extension)
switch { switch {
case filetype.IsImage(head): case filetype.IsImage(head):
return true, "image", nil return true, "image", fileType.MIME.Value, nil
case filetype.IsVideo(head): case filetype.IsVideo(head):
return true, "video", nil return true, "video", fileType.MIME.Value, nil
default: default:
return false, "", nil return false, "", "", nil
} }
} }
@ -393,7 +397,7 @@ func pathHasSupportedFiles(path string) (bool, error) {
case !recursive && info.IsDir() && p != path: case !recursive && info.IsDir() && p != path:
return filepath.SkipDir return filepath.SkipDir
case !info.IsDir(): case !info.IsDir():
supported, _, err := isSupportedFileType(p) supported, _, _, err := isSupportedFileType(p)
if err != nil { if err != nil {
return err return err
} }
@ -641,7 +645,7 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin
filePath := fileList[val] filePath := fileList[val]
if !fromCache { if !fromCache {
supported, _, err := isSupportedFileType(filePath) supported, _, _, err := isSupportedFileType(filePath)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -17,7 +17,7 @@ var (
) )
const ( const (
Version string = "0.61.1" Version string = "0.61.2"
) )
var ( var (

View File

@ -974,7 +974,7 @@ func serveMedia(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
return return
} }
supported, fileType, err := isSupportedFileType(filePath) supported, fileType, mime, err := isSupportedFileType(filePath)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -1034,17 +1034,19 @@ func serveMedia(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
switch fileType { switch fileType {
case "image": case "image":
htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" alt="Roulette selected: %s"></a>`, htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" type="%s" alt="Roulette selected: %s"></a>`,
queryParams, queryParams,
generateFilePath(filePath), generateFilePath(filePath),
dimensions.width, dimensions.width,
dimensions.height, dimensions.height,
mime,
fileName)) fileName))
htmlBody.WriteString(`</body></html>`) htmlBody.WriteString(`</body></html>`)
case "video": case "video":
htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><video controls autoplay><source src="%s" alt="Roulette selected: %s">Your browser does not support the video tag.</video></a>`, htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><video controls autoplay><source src="%s" type="%s" alt="Roulette selected: %s">Your browser does not support the video tag.</video></a>`,
queryParams, queryParams,
generateFilePath(filePath), generateFilePath(filePath),
mime,
fileName)) fileName))
htmlBody.WriteString(`</body></html>`) htmlBody.WriteString(`</body></html>`)
} }