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 ( var (
ErrNoImagesFound = errors.New("no supported image formats found which match all criteria") 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 { type Dimensions struct {
@ -144,12 +144,12 @@ 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 {
image, err := isSupportedFileType(path) supported, _, err := isSupportedFileType(path)
if err != nil { if err != nil {
return err return err
} }
if !image { if !supported {
return nil 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) 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()
@ -373,9 +373,11 @@ func isSupportedFileType(path string) (bool, error) {
switch { switch {
case filetype.IsImage(head): case filetype.IsImage(head):
return true, nil return true, "image", nil
case filetype.IsVideo(head):
return true, "video", nil
default: default:
return false, nil return false, "", nil
} }
} }
@ -391,12 +393,12 @@ 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():
image, err := isSupportedFileType(p) supported, _, err := isSupportedFileType(p)
if err != nil { if err != nil {
return err return err
} }
if image { if supported {
hasSupportedFiles <- true hasSupportedFiles <- true
return filepath.SkipAll return filepath.SkipAll
} }
@ -639,12 +641,12 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin
filePath := fileList[val] filePath := fileList[val]
if !fromCache { if !fromCache {
image, err := isSupportedFileType(filePath) supported, _, err := isSupportedFileType(filePath)
if err != nil { if err != nil {
return "", err return "", err
} }
if image { if supported {
return filePath, nil return filePath, nil
} }

View File

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

View File

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