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 {
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
}

View File

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

View File

@ -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(`<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,
generateFilePath(filePath),
dimensions.width,
dimensions.height,
mime,
fileName))
htmlBody.WriteString(`</body></html>`)
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,
generateFilePath(filePath),
mime,
fileName))
htmlBody.WriteString(`</body></html>`)
}