This commit is contained in:
2024-02-07 15:39:44 +01:00
parent 834664184b
commit e7e4d03baf
2 changed files with 45 additions and 0 deletions

View File

@@ -23,6 +23,9 @@ export class RPCClient {
) )
} }
/**
* Websocket connection
*/
public get socket() { public get socket() {
return this._socket$ return this._socket$
} }
@@ -61,6 +64,11 @@ export class RPCClient {
return data return data
} }
/**
* Request a new download. Handles arguments sanitization.
* @param req payload
* @returns
*/
public download(req: DownloadRequestArgs) { public download(req: DownloadRequestArgs) {
if (!req.url) { if (!req.url) {
return return
@@ -105,6 +113,11 @@ export class RPCClient {
}) })
} }
/**
* Requests the available formats for a given url (-f arg)
* @param url requested url
* @returns
*/
public formats(url: string) { public formats(url: string) {
if (url) { if (url) {
return this.sendHTTP<DLMetadata>({ return this.sendHTTP<DLMetadata>({
@@ -116,6 +129,9 @@ export class RPCClient {
} }
} }
/**
* Requests all downloads
*/
public running() { public running() {
this.send({ this.send({
method: 'Service.Running', method: 'Service.Running',
@@ -123,6 +139,10 @@ export class RPCClient {
}) })
} }
/**
* Stops and removes a download asynchronously
* @param id download id
*/
public kill(id: string) { public kill(id: string) {
this.sendHTTP({ this.sendHTTP({
method: 'Service.Kill', method: 'Service.Kill',
@@ -130,6 +150,9 @@ export class RPCClient {
}) })
} }
/**
* Stops and removes all downloads
*/
public killAll() { public killAll() {
this.sendHTTP({ this.sendHTTP({
method: 'Service.KillAll', method: 'Service.KillAll',
@@ -137,6 +160,10 @@ export class RPCClient {
}) })
} }
/**
* Get asynchronously the avaliable space on downloads directory
* @returns free space in bytes
*/
public freeSpace() { public freeSpace() {
return this.sendHTTP<number>({ return this.sendHTTP<number>({
method: 'Service.FreeSpace', method: 'Service.FreeSpace',
@@ -144,6 +171,10 @@ export class RPCClient {
}) })
} }
/**
* Get asynchronously the tree view of the download directory
* @returns free space in bytes
*/
public directoryTree() { public directoryTree() {
return this.sendHTTP<string[]>({ return this.sendHTTP<string[]>({
method: 'Service.DirectoryTree', method: 'Service.DirectoryTree',
@@ -151,6 +182,10 @@ export class RPCClient {
}) })
} }
/**
* Updates synchronously yt-dlp executable
* @returns free space in bytes
*/
public updateExecutable() { public updateExecutable() {
return this.sendHTTP({ return this.sendHTTP({
method: 'Service.UpdateExecutable', method: 'Service.UpdateExecutable',

View File

@@ -6,8 +6,10 @@ import type { RPCResult } from './types'
export const rpcHost = writable<string>(localStorage.getItem('rpcHost') ?? 'localhost') export const rpcHost = writable<string>(localStorage.getItem('rpcHost') ?? 'localhost')
export const rpcPort = writable<number>(Number(localStorage.getItem('rpcPort')) || 3033) export const rpcPort = writable<number>(Number(localStorage.getItem('rpcPort')) || 3033)
// if authentication is enabled...
export const rpcWebToken = writable<string>(localStorage.getItem('rpcWebToken') ?? '') export const rpcWebToken = writable<string>(localStorage.getItem('rpcWebToken') ?? '')
// will be used to access the api and archive endpoints
export const serverApiEndpoint = derived( export const serverApiEndpoint = derived(
[rpcHost, rpcPort], [rpcHost, rpcPort],
([$host, $port]) => window.location.port == '' ([$host, $port]) => window.location.port == ''
@@ -15,6 +17,7 @@ export const serverApiEndpoint = derived(
: `${window.location.protocol}//${$host}:${$port}` : `${window.location.protocol}//${$host}:${$port}`
) )
// access the websocket JSON-RPC 1.0 to gather downloads state
export const websocketRpcEndpoint = derived( export const websocketRpcEndpoint = derived(
[rpcHost, rpcPort], [rpcHost, rpcPort],
([$host, $port]) => window.location.port == '' ([$host, $port]) => window.location.port == ''
@@ -22,14 +25,21 @@ export const websocketRpcEndpoint = derived(
: `${window.location.protocol.startsWith('https') ? 'wss:' : 'ws:'}//${$host}:${$port}/rpc/ws` : `${window.location.protocol.startsWith('https') ? 'wss:' : 'ws:'}//${$host}:${$port}/rpc/ws`
) )
// same as websocket one but using HTTP-POST mainly used to send commands (download, stop, ...)
export const httpPostRpcEndpoint = derived( export const httpPostRpcEndpoint = derived(
serverApiEndpoint, serverApiEndpoint,
$ep => window.location.port == '' ? `${$ep}/rpc/http` : `${$ep}/rpc/http` $ep => window.location.port == '' ? `${$ep}/rpc/http` : `${$ep}/rpc/http`
) )
/**
* Will handle Websocket and HTTP-POST communications based on the requested method
*/
export const rpcClient = derived( export const rpcClient = derived(
[httpPostRpcEndpoint, websocketRpcEndpoint, rpcWebToken], [httpPostRpcEndpoint, websocketRpcEndpoint, rpcWebToken],
([$http, $ws, $token]) => new RPCClient($http, $ws, $token) ([$http, $ws, $token]) => new RPCClient($http, $ws, $token)
) )
/**
* Stores all the downloads returned by the rpc
*/
export const downloads = writable<O.Option<RPCResult[]>>(O.none) export const downloads = writable<O.Option<RPCResult[]>>(O.none)