code refactoring
This commit is contained in:
@@ -22,8 +22,10 @@ import {
|
|||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
|
|
||||||
import DeleteForeverIcon from '@mui/icons-material/DeleteForever'
|
import DeleteForeverIcon from '@mui/icons-material/DeleteForever'
|
||||||
import VideoFileIcon from '@mui/icons-material/VideoFile'
|
|
||||||
import FolderIcon from '@mui/icons-material/Folder'
|
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 { Buffer } from 'buffer'
|
||||||
import { useEffect, useMemo, useState, useTransition } from 'react'
|
import { useEffect, useMemo, useState, useTransition } from 'react'
|
||||||
import { useSelector } from 'react-redux'
|
import { useSelector } from 'react-redux'
|
||||||
@@ -174,7 +176,9 @@ export default function Downloaded() {
|
|||||||
<ListItemIcon>
|
<ListItemIcon>
|
||||||
{file.isDirectory
|
{file.isDirectory
|
||||||
? <FolderIcon />
|
? <FolderIcon />
|
||||||
: <VideoFileIcon />
|
: file.isVideo
|
||||||
|
? <VideoFileIcon />
|
||||||
|
: <InsertDriveFileIcon />
|
||||||
}
|
}
|
||||||
</ListItemIcon>
|
</ListItemIcon>
|
||||||
<ListItemText primary={file.name} />
|
<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
|
name: string
|
||||||
path: string
|
path: string
|
||||||
shaSum: string
|
shaSum: string
|
||||||
|
isVideo: boolean,
|
||||||
isDirectory: 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
|
package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -12,27 +10,17 @@ import (
|
|||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
||||||
|
"github.com/marcopeocchi/yt-dlp-web-ui/server/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DirectoryEntry struct {
|
type DirectoryEntry struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
SHASum string `json:"shaSum"`
|
SHASum string `json:"shaSum"`
|
||||||
|
IsVideo bool `json:"isVideo"`
|
||||||
IsDirectory bool `json:"isDirectory"`
|
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) {
|
func walkDir(root string) (*[]DirectoryEntry, error) {
|
||||||
files := []DirectoryEntry{}
|
files := []DirectoryEntry{}
|
||||||
|
|
||||||
@@ -42,7 +30,7 @@ func walkDir(root string) (*[]DirectoryEntry, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range dirs {
|
for _, d := range dirs {
|
||||||
if !isValidEntry(d) {
|
if !utils.IsValidEntry(d) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +39,8 @@ func walkDir(root string) (*[]DirectoryEntry, error) {
|
|||||||
files = append(files, DirectoryEntry{
|
files = append(files, DirectoryEntry{
|
||||||
Path: path,
|
Path: path,
|
||||||
Name: d.Name(),
|
Name: d.Name(),
|
||||||
SHASum: shaSumString(path),
|
SHASum: utils.ShaSumString(path),
|
||||||
|
IsVideo: utils.IsVideo(d),
|
||||||
IsDirectory: d.IsDir(),
|
IsDirectory: d.IsDir(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -91,7 +80,7 @@ func DeleteFile(ctx *fiber.Ctx) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := shaSumString(req.Path)
|
sum := utils.ShaSumString(req.Path)
|
||||||
if sum != req.SHASum {
|
if sum != req.SHASum {
|
||||||
return errors.New("shasum mismatch")
|
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