From 928bbb8c53259d3872b1f510d92ef80c48e15ad3 Mon Sep 17 00:00:00 2001 From: Seednode Date: Sun, 10 Sep 2023 20:23:48 -0500 Subject: [PATCH] Added video support --- cmd/files.go | 26 ++++++++++++++------------ cmd/root.go | 2 +- cmd/web.go | 44 +++++++++++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index 2a170a6..511f0f9 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -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 } diff --git a/cmd/root.go b/cmd/root.go index 6624f75..73f6a83 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ var ( ) const ( - Version string = "0.60.0" + Version string = "0.61.0" ) var ( diff --git a/cmd/web.go b/cmd/web.go index e646ba1..5279024 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -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%);}`) - htmlBody.WriteString(fmt.Sprintf(`%s (%dx%d)`, - fileName, - dimensions.width, - dimensions.height)) + + switch fileType { + case "image": + htmlBody.WriteString(fmt.Sprintf(`%s (%dx%d)`, + fileName, + dimensions.width, + dimensions.height)) + case "video": + htmlBody.WriteString(fmt.Sprintf(`%s`, + fileName)) + } + htmlBody.WriteString(``) if refreshInterval != "0ms" { htmlBody.WriteString(fmt.Sprintf("", queryParams, refreshTimer)) } - htmlBody.WriteString(fmt.Sprintf(`Roulette selected: %s`, - queryParams, - generateFilePath(filePath), - dimensions.width, - dimensions.height, - fileName)) - htmlBody.WriteString(``) + + switch fileType { + case "image": + htmlBody.WriteString(fmt.Sprintf(`Roulette selected: %s`, + queryParams, + generateFilePath(filePath), + dimensions.width, + dimensions.height, + fileName)) + htmlBody.WriteString(``) + case "video": + htmlBody.WriteString(fmt.Sprintf(``, + queryParams, + generateFilePath(filePath), + fileName)) + htmlBody.WriteString(``) + } _, err = io.WriteString(w, gohtml.Format(htmlBody.String())) if err != nil {