diff --git a/frontend/src/Archive.tsx b/frontend/src/Archive.tsx index 7c3f0c2..ad73203 100644 --- a/frontend/src/Archive.tsx +++ b/frontend/src/Archive.tsx @@ -22,8 +22,10 @@ import { } from '@mui/material' import DeleteForeverIcon from '@mui/icons-material/DeleteForever' -import VideoFileIcon from '@mui/icons-material/VideoFile' import FolderIcon from '@mui/icons-material/Folder' +import InsertDriveFileIcon from '@mui/icons-material/InsertDriveFile' +import VideoFileIcon from '@mui/icons-material/VideoFile' + import { Buffer } from 'buffer' import { useEffect, useMemo, useState, useTransition } from 'react' import { useSelector } from 'react-redux' @@ -174,7 +176,9 @@ export default function Downloaded() { {file.isDirectory ? - : + : file.isVideo + ? + : } diff --git a/frontend/src/types/index.d.ts b/frontend/src/types/index.d.ts index 9d5c3a5..15f6221 100644 --- a/frontend/src/types/index.d.ts +++ b/frontend/src/types/index.d.ts @@ -66,10 +66,15 @@ export type DirectoryEntry = { name: string path: string shaSum: string + isVideo: boolean, isDirectory: boolean } -export type DeleteRequest = Omit +export type DeleteRequest = Omit< + DirectoryEntry, 'name' | 'isDirectory' | 'isVideo' +> -export type PlayRequest = Omit +export type PlayRequest = Omit< + DirectoryEntry, 'shaSum' | 'name' | 'isDirectory' | 'isVideo' +> diff --git a/server/rest/handlers.go b/server/rest/handlers.go index e203117..f230ca1 100644 --- a/server/rest/handlers.go +++ b/server/rest/handlers.go @@ -1,10 +1,8 @@ package rest import ( - "crypto/sha256" "encoding/hex" "errors" - "io/fs" "net/http" "os" "path/filepath" @@ -12,27 +10,17 @@ import ( "github.com/gofiber/fiber/v2" "github.com/marcopeocchi/yt-dlp-web-ui/server/config" + "github.com/marcopeocchi/yt-dlp-web-ui/server/utils" ) type DirectoryEntry struct { Name string `json:"name"` Path string `json:"path"` SHASum string `json:"shaSum"` + IsVideo bool `json:"isVideo"` IsDirectory bool `json:"isDirectory"` } -func isValidEntry(d fs.DirEntry) bool { - return !strings.HasPrefix(d.Name(), ".") && - !strings.HasSuffix(d.Name(), ".part") && - !strings.HasSuffix(d.Name(), ".ytdl") -} - -func shaSumString(path string) string { - h := sha256.New() - h.Write([]byte(path)) - return hex.EncodeToString(h.Sum(nil)) -} - func walkDir(root string) (*[]DirectoryEntry, error) { files := []DirectoryEntry{} @@ -42,7 +30,7 @@ func walkDir(root string) (*[]DirectoryEntry, error) { } for _, d := range dirs { - if !isValidEntry(d) { + if !utils.IsValidEntry(d) { continue } @@ -51,7 +39,8 @@ func walkDir(root string) (*[]DirectoryEntry, error) { files = append(files, DirectoryEntry{ Path: path, Name: d.Name(), - SHASum: shaSumString(path), + SHASum: utils.ShaSumString(path), + IsVideo: utils.IsVideo(d), IsDirectory: d.IsDir(), }) } @@ -91,7 +80,7 @@ func DeleteFile(ctx *fiber.Ctx) error { return err } - sum := shaSumString(req.Path) + sum := utils.ShaSumString(req.Path) if sum != req.SHASum { return errors.New("shasum mismatch") } diff --git a/server/utils/file.go b/server/utils/file.go new file mode 100644 index 0000000..bf9f9f2 --- /dev/null +++ b/server/utils/file.go @@ -0,0 +1,29 @@ +package utils + +import ( + "crypto/sha256" + "encoding/hex" + "io/fs" + "regexp" + "strings" +) + +var ( + videoRe = regexp.MustCompile(`(?i)/\.mov|\.mp4|\.webm|\.mvk|/gmi`) +) + +func IsVideo(d fs.DirEntry) bool { + return videoRe.MatchString(d.Name()) +} + +func IsValidEntry(d fs.DirEntry) bool { + return !strings.HasPrefix(d.Name(), ".") && + !strings.HasSuffix(d.Name(), ".part") && + !strings.HasSuffix(d.Name(), ".ytdl") +} + +func ShaSumString(path string) string { + h := sha256.New() + h.Write([]byte(path)) + return hex.EncodeToString(h.Sum(nil)) +}