comments and code refactoring

This commit is contained in:
2024-04-24 11:52:14 +02:00
parent 01c327d308
commit 00bacf5c41
7 changed files with 39 additions and 20 deletions

View File

@@ -40,7 +40,7 @@ import { useI18n } from '../hooks/useI18n'
import { useRPC } from '../hooks/useRPC'
import { CliArguments } from '../lib/argsParser'
import type { DLMetadata } from '../types'
import { isValidURL, toFormatArgs } from '../utils'
import { toFormatArgs } from '../utils'
import ExtraDownloadOptions from './ExtraDownloadOptions'
const Transition = forwardRef(function Transition(
@@ -166,7 +166,6 @@ const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {
file
.split('\n')
.filter(u => isValidURL(u))
.forEach(u => sendUrl(u))
}

View File

@@ -20,17 +20,10 @@ export function validateDomain(url: string): boolean {
return urlRegex.test(url) || name === 'localhost' && slugRegex.test(slug)
}
export function isValidURL(url: string): boolean {
let urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/
return urlRegex.test(url)
}
export function ellipsis(str: string, lim: number): string {
if (str) {
return str.length > lim ? `${str.substring(0, lim)}...` : str
}
return ''
}
export const ellipsis = (str: string, lim: number) =>
str.length > lim
? `${str.substring(0, lim)}...`
: str
export function toFormatArgs(codes: string[]): string {
if (codes.length > 1) {

View File

@@ -2,6 +2,7 @@ package internal
import "time"
// Used to unmarshall yt-dlp progress
type ProgressTemplate struct {
Percentage string `json:"percentage"`
Speed float32 `json:"speed"`
@@ -9,6 +10,7 @@ type ProgressTemplate struct {
Eta float32 `json:"eta"`
}
// Defines where and how the download needs to be saved
type DownloadOutput struct {
Path string
Filename string
@@ -92,6 +94,7 @@ type SetCookiesRequest struct {
Cookies string `json:"cookies"`
}
// represents a user defined collection of yt-dlp arguments
type CustomTemplate struct {
Id string `json:"id"`
Name string `json:"name"`

View File

@@ -168,7 +168,7 @@ func (p *Process) Start() {
p.Logger.Info("progress",
slog.String("id", p.getShortId()),
slog.String("url", p.Url),
slog.String("percentege", progress.Percentage),
slog.String("percentage", progress.Percentage),
)
})
}()

View File

@@ -8,6 +8,15 @@ import (
"time"
)
/*
File base logger with log-rotate capabilities.
The rotate process must be initiated from an external goroutine.
After rotation the previous logs file are compressed with gzip algorithm.
The rotated log follows this naming: [filename].UTC time.gz
*/
// implements io.Writer interface
type LogRotateWriter struct {
mu sync.Mutex

View File

@@ -6,6 +6,17 @@ import (
"github.com/reactivex/rxgo/v2"
)
/*
Logger implementation using the observable pattern.
Implements io.Writer interface.
The observable is an event source which drops everythigng unless there's
a subscriber connected.
The observer implementatios are a http ServerSentEvents handler and a
websocket one in handler.go
*/
var (
logsChan = make(chan rxgo.Item, 100)
logsObservable = rxgo.

View File

@@ -26,10 +26,12 @@ func DirectoryTree() (*[]string, error) {
children []Node
}
rootPath := config.Instance().DownloadPath
var (
rootPath = config.Instance().DownloadPath
stack := internal.NewStack[Node]()
flattened := make([]string, 0)
stack = internal.NewStack[Node]()
flattened = make([]string, 0)
)
stack.Push(Node{path: rootPath})
@@ -37,14 +39,16 @@ func DirectoryTree() (*[]string, error) {
for stack.IsNotEmpty() {
current := stack.Pop().Value
children, err := os.ReadDir(current.path)
if err != nil {
return nil, err
}
for _, entry := range children {
childPath := filepath.Join(current.path, entry.Name())
childNode := Node{path: childPath}
var (
childPath = filepath.Join(current.path, entry.Name())
childNode = Node{path: childPath}
)
if entry.IsDir() {
current.children = append(current.children, childNode)
stack.Push(childNode)