Added video support

This commit is contained in:
Seednode 2023-09-10 20:23:48 -05:00
parent c5d0eb3b36
commit 928bbb8c53
3 changed files with 46 additions and 26 deletions

View File

@ -44,7 +44,7 @@ type Concurrency struct {
var (
ErrNoImagesFound = errors.New("no supported image formats found which match all criteria")
Extensions = [9]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".mp4", ".ogv", ".webm"}
Extensions = [8]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".mp4", ".webm"}
)
type Dimensions struct {
@ -144,12 +144,12 @@ func preparePath(path string) string {
func appendPath(directory, path string, files *Files, stats *ScanStats, shouldCache bool) error {
if shouldCache {
image, err := isSupportedFileType(path)
supported, _, err := isSupportedFileType(path)
if err != nil {
return err
}
if !image {
if !supported {
return nil
}
}
@ -358,13 +358,13 @@ func pathIsValid(filePath string, paths []string) bool {
}
}
func isSupportedFileType(path string) (bool, error) {
func isSupportedFileType(path string) (bool, 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()
@ -373,9 +373,11 @@ func isSupportedFileType(path string) (bool, error) {
switch {
case filetype.IsImage(head):
return true, nil
return true, "image", nil
case filetype.IsVideo(head):
return true, "video", nil
default:
return false, nil
return false, "", nil
}
}
@ -391,12 +393,12 @@ func pathHasSupportedFiles(path string) (bool, error) {
case !recursive && info.IsDir() && p != path:
return filepath.SkipDir
case !info.IsDir():
image, err := isSupportedFileType(p)
supported, _, err := isSupportedFileType(p)
if err != nil {
return err
}
if image {
if supported {
hasSupportedFiles <- true
return filepath.SkipAll
}
@ -639,12 +641,12 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin
filePath := fileList[val]
if !fromCache {
image, err := isSupportedFileType(filePath)
supported, _, err := isSupportedFileType(filePath)
if err != nil {
return "", err
}
if image {
if supported {
return filePath, nil
}

View File

@ -17,7 +17,7 @@ var (
)
const (
Version string = "0.60.0"
Version string = "0.61.0"
)
var (

View File

@ -974,7 +974,7 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
return
}
image, err := isSupportedFileType(filePath)
supported, fileType, err := isSupportedFileType(filePath)
if err != nil {
fmt.Println(err)
@ -983,7 +983,7 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
return
}
if !image {
if !supported {
notFound(w, r, filePath)
return
@ -1013,23 +1013,41 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
htmlBody.WriteString(`a{display:block;height:100%;width:100%;text-decoration:none;}`)
htmlBody.WriteString(`img{margin:auto;display:block;max-width:97%;max-height:97%;object-fit:scale-down;`)
htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}</style>`)
htmlBody.WriteString(fmt.Sprintf(`<title>%s (%dx%d)</title>`,
fileName,
dimensions.width,
dimensions.height))
switch fileType {
case "image":
htmlBody.WriteString(fmt.Sprintf(`<title>%s (%dx%d)</title>`,
fileName,
dimensions.width,
dimensions.height))
case "video":
htmlBody.WriteString(fmt.Sprintf(`<title>%s</title>`,
fileName))
}
htmlBody.WriteString(`</head><body>`)
if refreshInterval != "0ms" {
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%s';}, %d);};</script>",
queryParams,
refreshTimer))
}
htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" alt="Roulette selected: %s"></a>`,
queryParams,
generateFilePath(filePath),
dimensions.width,
dimensions.height,
fileName))
htmlBody.WriteString(`</body></html>`)
switch fileType {
case "image":
htmlBody.WriteString(fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" alt="Roulette selected: %s"></a>`,
queryParams,
generateFilePath(filePath),
dimensions.width,
dimensions.height,
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>`,
queryParams,
generateFilePath(filePath),
fileName))
htmlBody.WriteString(`</body></html>`)
}
_, err = io.WriteString(w, gohtml.Format(htmlBody.String()))
if err != nil {