frontend performance, rpc/rest jwt authentication
This commit is contained in:
61
frontend/src/lib/argsParser.ts
Normal file
61
frontend/src/lib/argsParser.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
export class CliArguments {
|
||||
private _extractAudio: boolean
|
||||
private _noMTime: boolean
|
||||
private _proxy: string
|
||||
|
||||
constructor(extractAudio = false, noMTime = true) {
|
||||
this._extractAudio = extractAudio
|
||||
this._noMTime = noMTime
|
||||
this._proxy = ""
|
||||
}
|
||||
|
||||
public get extractAudio(): boolean {
|
||||
return this._extractAudio
|
||||
}
|
||||
|
||||
public toggleExtractAudio() {
|
||||
this._extractAudio = !this._extractAudio
|
||||
return this
|
||||
}
|
||||
|
||||
public disableExtractAudio() {
|
||||
this._extractAudio = false
|
||||
return this
|
||||
}
|
||||
|
||||
public get noMTime(): boolean {
|
||||
return this._noMTime
|
||||
}
|
||||
|
||||
public toggleNoMTime() {
|
||||
this._noMTime = !this._noMTime
|
||||
return this
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
let args = ''
|
||||
|
||||
if (this._extractAudio) {
|
||||
args += '-x '
|
||||
}
|
||||
|
||||
if (this._noMTime) {
|
||||
args += '--no-mtime '
|
||||
}
|
||||
|
||||
return args.trim()
|
||||
}
|
||||
|
||||
public fromString(str: string): CliArguments {
|
||||
if (str) {
|
||||
if (str.includes('-x')) {
|
||||
this._extractAudio = true
|
||||
}
|
||||
|
||||
if (str.includes('--no-mtime')) {
|
||||
this._noMTime = true
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
}
|
||||
10
frontend/src/lib/events.ts
Normal file
10
frontend/src/lib/events.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export function on(eventType: string, listener: any) {
|
||||
document.addEventListener(eventType, listener)
|
||||
}
|
||||
|
||||
export const serverStates = {
|
||||
PROC_DOWNLOAD: 'download',
|
||||
PROC_MERGING: 'merging',
|
||||
PROC_ABORT: 'abort',
|
||||
PROG_DONE: 'status_done',
|
||||
}
|
||||
21
frontend/src/lib/httpClient.ts
Normal file
21
frontend/src/lib/httpClient.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import * as E from 'fp-ts/Either'
|
||||
import { pipe } from 'fp-ts/function'
|
||||
|
||||
type FetchInit = {
|
||||
url: string,
|
||||
opt?: RequestInit
|
||||
}
|
||||
|
||||
export async function ffetch<T>(
|
||||
url: string,
|
||||
onSuccess: (res: T) => void,
|
||||
onError: (err: string) => void,
|
||||
opt?: RequestInit,
|
||||
) {
|
||||
const res = await fetch(url, opt)
|
||||
if (!res.ok) {
|
||||
onError(await res.text())
|
||||
return
|
||||
}
|
||||
onSuccess(await res.json() as T)
|
||||
}
|
||||
28
frontend/src/lib/intl.ts
Normal file
28
frontend/src/lib/intl.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// @ts-nocheck
|
||||
import i18n from "../assets/i18n.yaml"
|
||||
|
||||
export default class I18nBuilder {
|
||||
private language: string
|
||||
private textMap = i18n.languages
|
||||
|
||||
constructor(language: string) {
|
||||
this.language = language
|
||||
}
|
||||
|
||||
getLanguage(): string {
|
||||
return this.language
|
||||
}
|
||||
|
||||
setLanguage(language: string): void {
|
||||
this.language = language
|
||||
}
|
||||
|
||||
t(key: string): string {
|
||||
const map = this.textMap[this.language]
|
||||
if (map) {
|
||||
const translation = map[key]
|
||||
return translation ?? 'caption not defined'
|
||||
}
|
||||
return 'caption not defined'
|
||||
}
|
||||
}
|
||||
105
frontend/src/lib/rpcClient.ts
Normal file
105
frontend/src/lib/rpcClient.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import type { DLMetadata, RPCRequest, RPCResponse } from '../types'
|
||||
|
||||
import { webSocket } from 'rxjs/webSocket'
|
||||
import { getHttpRPCEndpoint, getWebSocketEndpoint } from '../utils'
|
||||
|
||||
export const socket$ = webSocket<any>(getWebSocketEndpoint())
|
||||
|
||||
export class RPCClient {
|
||||
private seq: number
|
||||
|
||||
constructor() {
|
||||
this.seq = 0
|
||||
}
|
||||
|
||||
private incrementSeq() {
|
||||
return String(this.seq++)
|
||||
}
|
||||
|
||||
private send(req: RPCRequest) {
|
||||
socket$.next({
|
||||
...req,
|
||||
id: this.incrementSeq(),
|
||||
})
|
||||
}
|
||||
|
||||
private async sendHTTP<T>(req: RPCRequest) {
|
||||
const res = await fetch(getHttpRPCEndpoint(), {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
...req,
|
||||
id: this.incrementSeq(),
|
||||
})
|
||||
})
|
||||
const data: RPCResponse<T> = await res.json()
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
public download(url: string, args: string, pathOverride = '', renameTo = '') {
|
||||
if (url) {
|
||||
this.send({
|
||||
method: 'Service.Exec',
|
||||
params: [{
|
||||
URL: url.split("?list").at(0)!,
|
||||
Params: args.split(" ").map(a => a.trim()),
|
||||
Path: pathOverride,
|
||||
Rename: renameTo,
|
||||
}]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public formats(url: string) {
|
||||
if (url) {
|
||||
return this.sendHTTP<DLMetadata>({
|
||||
method: 'Service.Formats',
|
||||
params: [{
|
||||
URL: url.split("?list").at(0)!,
|
||||
}]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public running() {
|
||||
this.send({
|
||||
method: 'Service.Running',
|
||||
params: [],
|
||||
})
|
||||
}
|
||||
|
||||
public kill(id: string) {
|
||||
this.send({
|
||||
method: 'Service.Kill',
|
||||
params: [id],
|
||||
})
|
||||
}
|
||||
|
||||
public killAll() {
|
||||
this.send({
|
||||
method: 'Service.KillAll',
|
||||
params: [],
|
||||
})
|
||||
}
|
||||
|
||||
public freeSpace() {
|
||||
return this.sendHTTP<number>({
|
||||
method: 'Service.FreeSpace',
|
||||
params: [],
|
||||
})
|
||||
}
|
||||
|
||||
public directoryTree() {
|
||||
return this.sendHTTP<string[]>({
|
||||
method: 'Service.DirectoryTree',
|
||||
params: [],
|
||||
})
|
||||
}
|
||||
|
||||
public updateExecutable() {
|
||||
return this.sendHTTP({
|
||||
method: 'Service.UpdateExecutable',
|
||||
params: []
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user