code refactoring
This commit is contained in:
@@ -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() {
|
||||
<ListItemIcon>
|
||||
{file.isDirectory
|
||||
? <FolderIcon />
|
||||
: <VideoFileIcon />
|
||||
: file.isVideo
|
||||
? <VideoFileIcon />
|
||||
: <InsertDriveFileIcon />
|
||||
}
|
||||
</ListItemIcon>
|
||||
<ListItemText primary={file.name} />
|
||||
|
||||
9
frontend/src/types/index.d.ts
vendored
9
frontend/src/types/index.d.ts
vendored
@@ -66,10 +66,15 @@ export type DirectoryEntry = {
|
||||
name: string
|
||||
path: string
|
||||
shaSum: string
|
||||
isVideo: boolean,
|
||||
isDirectory: boolean
|
||||
}
|
||||
|
||||
export type DeleteRequest = Omit<DirectoryEntry, 'name' | 'isDirectory'>
|
||||
export type DeleteRequest = Omit<
|
||||
DirectoryEntry, 'name' | 'isDirectory' | 'isVideo'
|
||||
>
|
||||
|
||||
export type PlayRequest = Omit<DirectoryEntry, 'shaSum' | 'name' | 'isDirectory'>
|
||||
export type PlayRequest = Omit<
|
||||
DirectoryEntry, 'shaSum' | 'name' | 'isDirectory' | 'isVideo'
|
||||
>
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
29
server/utils/file.go
Normal file
29
server/utils/file.go
Normal file
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user