??
This commit is contained in:
86
server-node/src/core/HTTPServer.ts
Normal file
86
server-node/src/core/HTTPServer.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { createServer, Server } from 'http';
|
||||
import { parse as urlParse } from 'url';
|
||||
import { open, close, readFile, fstat } from 'fs';
|
||||
import { parse, join } from 'path';
|
||||
|
||||
namespace server {
|
||||
export const mimes = {
|
||||
'.html': 'text/html',
|
||||
'.ico': 'image/x-icon',
|
||||
'.js': 'text/javascript',
|
||||
'.json': 'application/json',
|
||||
'.css': 'text/css',
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.webp': 'image/webp',
|
||||
};
|
||||
}
|
||||
|
||||
class Jean {
|
||||
private workingDir: string;
|
||||
|
||||
/**
|
||||
* Jean static file server its only purpose is serving SPA and images
|
||||
* with the lowest impact possible.
|
||||
* @param workingDir sets the root directory automatically trying index.html
|
||||
* If specified the file in addition to the directory it will serve the
|
||||
* file directly.
|
||||
* *e.g* new Jean(path.join(__dirname, 'dist')) will try
|
||||
* index.html from the dist directory;
|
||||
* @author me :D
|
||||
*/
|
||||
|
||||
constructor(workingDir: string) {
|
||||
this.workingDir = workingDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a static file server
|
||||
* @returns an instance of a standard NodeJS http.Server
|
||||
*/
|
||||
public createServer(): Server {
|
||||
return createServer((req, res) => {
|
||||
// parse the current given url
|
||||
const parsedUrl = urlParse(req.url, false)
|
||||
// extract the pathname and guard it with the working dir
|
||||
let pathname = join(this.workingDir, `.${parsedUrl.pathname}`);
|
||||
// extract the file extension
|
||||
const ext = parse(pathname).ext;
|
||||
|
||||
// open the file or directory and fetch its descriptor
|
||||
open(pathname, 'r', (err, fd) => {
|
||||
// whoops, not found, send a 404
|
||||
if (err) {
|
||||
res.statusCode = 404;
|
||||
res.end(`File ${pathname} not found!`);
|
||||
return;
|
||||
}
|
||||
// something's gone wrong it's not a file or a directory
|
||||
fstat(fd, (err, stat) => {
|
||||
if (err) {
|
||||
res.statusCode = 500;
|
||||
res.end(err);
|
||||
}
|
||||
// try file index.html
|
||||
if (stat.isDirectory()) {
|
||||
pathname = join(pathname, 'index.html')
|
||||
}
|
||||
// read the file
|
||||
readFile(pathname, (err, data) => {
|
||||
if (err) {
|
||||
res.statusCode = 500;
|
||||
res.end(`Error reading the file: ${err}`);
|
||||
} else {
|
||||
// infer it's extension otherwise it's the index.html
|
||||
res.setHeader('Content-type', server.mimes[ext] || 'text/html');
|
||||
res.end(data);
|
||||
close(fd);
|
||||
}
|
||||
});
|
||||
})
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default Jean;
|
||||
166
server-node/src/core/Process.ts
Normal file
166
server-node/src/core/Process.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import { spawn } from 'child_process';
|
||||
import { join } from 'path';
|
||||
import { Readable } from 'stream';
|
||||
import { ISettings } from '../interfaces/ISettings';
|
||||
import { availableParams } from '../utils/params';
|
||||
import Logger from '../utils/BetterLogger';
|
||||
import { IDownloadFormat, IDownloadMetadata } from '../interfaces/IDownloadMetadata';
|
||||
|
||||
const log = Logger.instance;
|
||||
|
||||
/**
|
||||
* Represents a download process that spawns yt-dlp.
|
||||
* @param url - The downlaod url.
|
||||
* @param params - The cli arguments passed by the frontend.
|
||||
* @param settings - The download settings passed by the frontend.
|
||||
*/
|
||||
|
||||
class Process {
|
||||
public readonly url: string;
|
||||
public readonly params: Array<string>;
|
||||
private settings: ISettings;
|
||||
private stdout: Readable;
|
||||
private pid: number;
|
||||
private metadata?: IDownloadMetadata;
|
||||
private exePath = join(__dirname, 'yt-dlp');
|
||||
private customFileName?: string;
|
||||
|
||||
private readonly template = `download:
|
||||
{
|
||||
"eta":%(progress.eta)s,
|
||||
"percentage":"%(progress._percent_str)s",
|
||||
"speed":"%(progress._speed_str)s",
|
||||
"size":%(info.filesize_approx)s
|
||||
}`
|
||||
.replace(/\s\s+/g, ' ')
|
||||
.replace('\n', '');
|
||||
|
||||
constructor(url: string, params: Array<string>, settings: any, customFileName?: string) {
|
||||
this.url = url;
|
||||
this.params = params || [];
|
||||
this.settings = settings
|
||||
this.stdout = undefined;
|
||||
this.pid = undefined;
|
||||
this.metadata = undefined;
|
||||
this.customFileName = customFileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* function that launch the download process, sets the stdout property and the pid
|
||||
* @param callback not yet implemented
|
||||
* @returns the process instance
|
||||
*/
|
||||
public async start(callback?: Function): Promise<this> {
|
||||
const sanitizedParams = this.params.filter((param: string) => availableParams.includes(param));
|
||||
|
||||
if (this.settings?.download_path) {
|
||||
if (this.settings.download_path.charAt(this.settings.download_path.length - 1) !== '/') {
|
||||
this.settings.download_path = `${this.settings.download_path}/`
|
||||
}
|
||||
}
|
||||
|
||||
const ytldp = spawn(this.exePath,
|
||||
[
|
||||
'-o', `${this.settings?.download_path || 'downloads/'}${this.customFileName || '%(title)s'}.%(ext)s`,
|
||||
'--progress-template', this.template,
|
||||
'--no-colors',
|
||||
]
|
||||
.concat(sanitizedParams)
|
||||
.concat((this.settings?.cliArgs ?? []).map(arg => arg.split(' ')).flat())
|
||||
.concat([this.url])
|
||||
);
|
||||
|
||||
this.pid = ytldp.pid;
|
||||
this.stdout = ytldp.stdout;
|
||||
|
||||
log.info('proc', `Spawned a new process, pid: ${this.pid}`)
|
||||
|
||||
if (callback) {
|
||||
callback()
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* function used internally by the download process to fetch information, usually thumbnail and title
|
||||
* @returns Promise to the lock
|
||||
*/
|
||||
public getMetadata(): Promise<IDownloadMetadata> {
|
||||
if (!this.metadata) {
|
||||
let stdoutChunks = [];
|
||||
const ytdlpInfo = spawn(this.exePath, ['-J', this.url]);
|
||||
|
||||
ytdlpInfo.stdout.on('data', (data) => {
|
||||
stdoutChunks.push(data);
|
||||
});
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
ytdlpInfo.on('exit', () => {
|
||||
try {
|
||||
const buffer = Buffer.concat(stdoutChunks);
|
||||
const json = JSON.parse(buffer.toString());
|
||||
const info = {
|
||||
formats: json.formats.map((format: IDownloadFormat) => {
|
||||
return {
|
||||
format_id: format.format_id ?? '',
|
||||
format_note: format.format_note ?? '',
|
||||
fps: format.fps ?? '',
|
||||
resolution: format.resolution ?? '',
|
||||
vcodec: format.vcodec ?? '',
|
||||
acodec: format.acodec ?? '',
|
||||
}
|
||||
}).filter((format: IDownloadFormat) => format.format_note !== 'storyboard'),
|
||||
best: {
|
||||
format_id: json.format_id ?? '',
|
||||
format_note: json.format_note ?? '',
|
||||
fps: json.fps ?? '',
|
||||
resolution: json.resolution ?? '',
|
||||
vcodec: json.vcodec ?? '',
|
||||
acodec: json.acodec ?? '',
|
||||
},
|
||||
thumbnail: json.thumbnail,
|
||||
title: json.title,
|
||||
}
|
||||
resolve(info);
|
||||
this.metadata = info;
|
||||
|
||||
} catch (e) {
|
||||
reject('failed fetching formats, downloading best available');
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
return new Promise((resolve) => { resolve(this.metadata!) });
|
||||
}
|
||||
|
||||
/**
|
||||
* function that kills the current process
|
||||
*/
|
||||
async kill() {
|
||||
spawn('kill', [String(this.pid)]).on('exit', () => {
|
||||
log.info('proc', `Stopped ${this.pid} because SIGKILL`)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* pid getter function
|
||||
* @returns {number} pid
|
||||
*/
|
||||
getPid(): number {
|
||||
if (!this.pid) {
|
||||
throw "Process isn't started"
|
||||
}
|
||||
return this.pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* stdout getter function
|
||||
* @returns {Readable} stdout as stream
|
||||
*/
|
||||
getStdout(): Readable {
|
||||
return this.stdout
|
||||
}
|
||||
}
|
||||
|
||||
export default Process;
|
||||
30
server-node/src/core/downloadArchive.ts
Normal file
30
server-node/src/core/downloadArchive.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { resolve as pathResolve } from "path";
|
||||
import { readdir } from "fs";
|
||||
import { ISettings } from "../interfaces/ISettings";
|
||||
import Logger from "../utils/BetterLogger";
|
||||
|
||||
let settings: ISettings;
|
||||
const log = Logger.instance;
|
||||
|
||||
try {
|
||||
settings = require('../../settings.json');
|
||||
} catch (e) {
|
||||
log.warn('dl', 'settings.json not found');
|
||||
}
|
||||
|
||||
export function listDownloaded(ctx: any) {
|
||||
return new Promise((resolve, reject) => {
|
||||
readdir(pathResolve(settings.download_path || 'download'), (err, files) => {
|
||||
if (err) {
|
||||
reject({ err: true })
|
||||
return
|
||||
}
|
||||
ctx.body = files.map(file => {
|
||||
resolve({
|
||||
filename: file,
|
||||
path: pathResolve(file),
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
251
server-node/src/core/downloader.ts
Normal file
251
server-node/src/core/downloader.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
import { spawn } from 'child_process';
|
||||
import { from, interval } from 'rxjs';
|
||||
import { map, throttle } from 'rxjs/operators';
|
||||
import { Socket } from 'socket.io';
|
||||
import MemoryDB from '../db/memoryDB';
|
||||
import { IPayload } from '../interfaces/IPayload';
|
||||
import { ISettings } from '../interfaces/ISettings';
|
||||
import { CLIProgress } from '../types';
|
||||
import Logger from '../utils/BetterLogger';
|
||||
import Process from './Process';
|
||||
import { states } from './states';
|
||||
|
||||
// settings read from settings.json
|
||||
let settings: ISettings;
|
||||
const log = Logger.instance;
|
||||
|
||||
const mem_db = new MemoryDB();
|
||||
|
||||
try {
|
||||
settings = require('../../settings.json');
|
||||
}
|
||||
catch (e) {
|
||||
new Promise(resolve => setTimeout(resolve, 500))
|
||||
.then(() => log.warn('dl', 'settings.json not found, ignore if using Docker'));
|
||||
}
|
||||
/**
|
||||
* Get download info such as thumbnail, title, resolution and list all formats
|
||||
* @param socket
|
||||
* @param url
|
||||
*/
|
||||
export async function getFormatsAndMetadata(socket: Socket, url: string) {
|
||||
let p = new Process(url, [], settings);
|
||||
try {
|
||||
const formats = await p.getMetadata();
|
||||
socket.emit('available-formats', formats)
|
||||
} catch (e) {
|
||||
log.warn('dl', e)
|
||||
socket.emit('progress', {
|
||||
status: states.PROG_DONE,
|
||||
pid: -1,
|
||||
});
|
||||
} finally {
|
||||
p = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke a new download.
|
||||
* Called by the websocket messages listener.
|
||||
* @param {Socket} socket current connection socket
|
||||
* @param {object} payload frontend download payload
|
||||
* @returns
|
||||
*/
|
||||
export async function download(socket: Socket, payload: IPayload) {
|
||||
if (!payload || payload.url === '' || payload.url === null) {
|
||||
socket.emit('progress', { status: states.PROG_DONE });
|
||||
return;
|
||||
}
|
||||
|
||||
const url = payload.url;
|
||||
const params = typeof payload.params !== 'object' ?
|
||||
payload.params.split(' ') :
|
||||
payload.params;
|
||||
|
||||
const renameTo = payload.renameTo
|
||||
|
||||
const scopedSettings: ISettings = {
|
||||
...settings,
|
||||
download_path: payload.path
|
||||
}
|
||||
|
||||
let p = new Process(url, params, scopedSettings, renameTo);
|
||||
|
||||
p.start().then(downloader => {
|
||||
mem_db.add(downloader)
|
||||
displayDownloadMetadata(downloader, socket);
|
||||
streamProcess(downloader, socket);
|
||||
});
|
||||
|
||||
// GC
|
||||
p = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send via websocket download info "chunk"
|
||||
* @param process
|
||||
* @param socket
|
||||
*/
|
||||
function displayDownloadMetadata(process: Process, socket: Socket) {
|
||||
process.getMetadata()
|
||||
.then(metadata => {
|
||||
socket.emit('metadata', {
|
||||
pid: process.getPid(),
|
||||
metadata: metadata,
|
||||
});
|
||||
})
|
||||
.catch((e) => {
|
||||
socket.emit('progress', {
|
||||
status: states.PROG_DONE,
|
||||
pid: process.getPid(),
|
||||
});
|
||||
log.warn('dl', e)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream via websocket download stdoud "chunks"
|
||||
* @param process
|
||||
* @param socket
|
||||
*/
|
||||
function streamProcess(process: Process, socket: Socket) {
|
||||
const emitAbort = () => {
|
||||
socket.emit('progress', {
|
||||
status: states.PROG_DONE,
|
||||
pid: process.getPid(),
|
||||
});
|
||||
}
|
||||
|
||||
from(process.getStdout().removeAllListeners()) // stdout as observable
|
||||
.pipe(
|
||||
throttle(() => interval(500)), // discard events closer than 500ms
|
||||
map(stdout => formatter(String(stdout), process.getPid()))
|
||||
)
|
||||
.subscribe({
|
||||
next: (stdout) => {
|
||||
socket.emit('progress', stdout)
|
||||
},
|
||||
complete: () => {
|
||||
process.kill().then(() => {
|
||||
emitAbort();
|
||||
mem_db.remove(process);
|
||||
});
|
||||
},
|
||||
error: () => {
|
||||
emitAbort();
|
||||
mem_db.remove(process);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all downloads.
|
||||
* If the server has just been launched retrieve the ones saved to the database.
|
||||
* If the server is running fetches them from the process pool.
|
||||
* @param {Socket} socket current connection socket
|
||||
* @returns
|
||||
*/
|
||||
export async function retrieveDownload(socket: Socket) {
|
||||
// it's a cold restart: the server has just been started with pending
|
||||
// downloads, so fetch them from the database and resume.
|
||||
|
||||
// if (coldRestart) {
|
||||
// coldRestart = false;
|
||||
// let downloads = [];
|
||||
// // sanitize
|
||||
// downloads = [...new Set(downloads.filter(el => el !== undefined))];
|
||||
// log.info('dl', `Cold restart, retrieving ${downloads.length} jobs`)
|
||||
// for (const entry of downloads) {
|
||||
// if (entry) {
|
||||
// await download(socket, entry);
|
||||
// }
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
|
||||
// it's an hot-reload the server it's running and the frontend ask for
|
||||
// the pending job: retrieve them from the "in-memory database" (ProcessPool)
|
||||
|
||||
const _poolSize = mem_db.size()
|
||||
log.info('dl', `Retrieving ${_poolSize} jobs from pool`)
|
||||
socket.emit('pending-jobs', _poolSize)
|
||||
|
||||
const it = mem_db.iterator();
|
||||
|
||||
// resume the jobs
|
||||
for (const entry of it) {
|
||||
const [, process] = entry
|
||||
displayDownloadMetadata(process, socket);
|
||||
streamProcess(process, socket);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abort a specific download if pid is provided, in the other case
|
||||
* calls the abortAllDownloads function
|
||||
* @see abortAllDownloads
|
||||
* @param {Socket} socket currenct connection socket
|
||||
* @param {*} args args sent by the frontend. MUST contain the PID.
|
||||
* @returns
|
||||
*/
|
||||
export function abortDownload(socket: Socket, args: any) {
|
||||
if (!args) {
|
||||
abortAllDownloads(socket);
|
||||
return;
|
||||
}
|
||||
const { pid } = args;
|
||||
|
||||
spawn('kill', [pid])
|
||||
.on('exit', () => {
|
||||
socket.emit('progress', {
|
||||
status: states.PROC_ABORT,
|
||||
process: pid,
|
||||
});
|
||||
log.warn('dl', `Aborting download ${pid}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Unconditionally kills all yt-dlp process.
|
||||
* @param {Socket} socket currenct connection socket
|
||||
*/
|
||||
export function abortAllDownloads(socket: Socket) {
|
||||
spawn('killall', ['yt-dlp'])
|
||||
.on('exit', () => {
|
||||
socket.emit('progress', { status: states.PROC_ABORT });
|
||||
log.info('dl', 'Aborting downloads');
|
||||
});
|
||||
mem_db.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pool current size
|
||||
*/
|
||||
export function getQueueSize(): number {
|
||||
return mem_db.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @private Formats the yt-dlp stdout to a frontend-readable format
|
||||
* @param {string} stdout stdout as string
|
||||
* @param {number} pid current process id relative to stdout
|
||||
* @returns
|
||||
*/
|
||||
const formatter = (stdout: string, pid: number) => {
|
||||
try {
|
||||
const p: CLIProgress = JSON.parse(stdout);
|
||||
if (p) {
|
||||
return {
|
||||
status: states.PROC_DOWNLOAD,
|
||||
progress: p.percentage,
|
||||
size: p.size,
|
||||
dlSpeed: p.speed,
|
||||
pid: pid,
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
progress: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
9
server-node/src/core/states.ts
Normal file
9
server-node/src/core/states.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Possible server states map
|
||||
*/
|
||||
export const states = {
|
||||
PROC_DOWNLOAD: 'download',
|
||||
PROC_MERGING: 'merging',
|
||||
PROC_ABORT: 'abort',
|
||||
PROG_DONE: 'status_done',
|
||||
}
|
||||
39
server-node/src/core/streamer.ts
Normal file
39
server-node/src/core/streamer.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { stat, createReadStream } from 'fs';
|
||||
import { lookup } from 'mime-types';
|
||||
|
||||
export function streamer(ctx: any, next: any) {
|
||||
const filepath = ''
|
||||
stat(filepath, (err, stat) => {
|
||||
if (err) {
|
||||
ctx.response.status = 404;
|
||||
ctx.body = { err: 'resource not found' };
|
||||
next();
|
||||
}
|
||||
const fileSize = stat.size;
|
||||
const range = ctx.headers.range;
|
||||
if (range) {
|
||||
const parts = range.replace(/bytes=/, '').split('-');
|
||||
const start = parseInt(parts[0], 10);
|
||||
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
|
||||
const chunksize = end - start + 1;
|
||||
const file = createReadStream(filepath, { start, end });
|
||||
const head = {
|
||||
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
||||
'Accept-Ranges': 'bytes',
|
||||
'Content-Length': chunksize,
|
||||
'Content-Type': lookup(filepath)
|
||||
};
|
||||
ctx.res.writeHead(206, head);
|
||||
file.pipe(ctx.res);
|
||||
next();
|
||||
} else {
|
||||
const head = {
|
||||
'Content-Length': fileSize,
|
||||
'Content-Type': 'video/mp4'
|
||||
};
|
||||
ctx.res.writeHead(200, head);
|
||||
createReadStream(ctx.params.filepath).pipe(ctx.res);
|
||||
next();
|
||||
}
|
||||
});
|
||||
}
|
||||
80
server-node/src/db/memoryDB.ts
Normal file
80
server-node/src/db/memoryDB.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Represents a download process that spawns yt-dlp.
|
||||
*/
|
||||
|
||||
import Process from "../core/Process";
|
||||
|
||||
class MemoryDB {
|
||||
private _pool: Map<number, Process>
|
||||
private _size: number
|
||||
|
||||
constructor() {
|
||||
this.init()
|
||||
}
|
||||
|
||||
private init() {
|
||||
this._pool = new Map<number, Process>()
|
||||
this._size = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Pool size getter
|
||||
* @returns {number} pool's size
|
||||
*/
|
||||
size(): number {
|
||||
return this._size
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a process to the pool
|
||||
* @param {Process} process
|
||||
*/
|
||||
add(process: Process) {
|
||||
this._pool.set(process.getPid(), process)
|
||||
this._size++
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a process from the pool
|
||||
* @param {Process} process
|
||||
*/
|
||||
remove(process: Process) {
|
||||
if (this._size === 0) return
|
||||
this._pool.delete(process.getPid())
|
||||
this._size--
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a process from the pool by its pid
|
||||
* @param {number} pid
|
||||
*/
|
||||
removeByPid(pid: number) {
|
||||
this._pool.delete(pid)
|
||||
}
|
||||
|
||||
/**
|
||||
* get an iterator for the pool
|
||||
* @returns {IterableIterator} iterator
|
||||
*/
|
||||
iterator(): IterableIterator<[number, Process]> {
|
||||
return this._pool.entries()
|
||||
}
|
||||
|
||||
/**
|
||||
* get a process by its pid
|
||||
* @param {number} pid
|
||||
* @returns {Process}
|
||||
*/
|
||||
getByPid(pid: number): Process {
|
||||
return this._pool.get(pid)
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear memory db
|
||||
*/
|
||||
flush() {
|
||||
this.init()
|
||||
}
|
||||
}
|
||||
|
||||
export default MemoryDB;
|
||||
15
server-node/src/interfaces/IDownloadMetadata.d.ts
vendored
Normal file
15
server-node/src/interfaces/IDownloadMetadata.d.ts
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
export interface IDownloadMetadata {
|
||||
formats: Array<IDownloadFormat>,
|
||||
best: IDownloadFormat,
|
||||
thumbnail: string,
|
||||
title: string,
|
||||
}
|
||||
|
||||
export interface IDownloadFormat {
|
||||
format_id: string,
|
||||
format_note: string,
|
||||
fps: number,
|
||||
resolution: string,
|
||||
vcodec: string,
|
||||
acodec: string,
|
||||
}
|
||||
13
server-node/src/interfaces/IPayload.d.ts
vendored
Normal file
13
server-node/src/interfaces/IPayload.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Represent a download payload sent by the frontend
|
||||
*/
|
||||
|
||||
export interface IPayload {
|
||||
url: string
|
||||
params: Array<string> | string
|
||||
path: string
|
||||
title?: string
|
||||
thumbnail?: string
|
||||
size?: string
|
||||
renameTo?: string
|
||||
}
|
||||
14
server-node/src/interfaces/IRecord.d.ts
vendored
Normal file
14
server-node/src/interfaces/IRecord.d.ts
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Represent a download db record
|
||||
*/
|
||||
|
||||
export interface IRecord {
|
||||
uid: string,
|
||||
url: string,
|
||||
title: string,
|
||||
thumbnail: string,
|
||||
created: Date,
|
||||
size: string,
|
||||
pid: number,
|
||||
params: string,
|
||||
}
|
||||
5
server-node/src/interfaces/ISettings.d.ts
vendored
Normal file
5
server-node/src/interfaces/ISettings.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export interface ISettings {
|
||||
download_path: string,
|
||||
cliArgs?: string[],
|
||||
port?: number,
|
||||
}
|
||||
128
server-node/src/main.ts
Normal file
128
server-node/src/main.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { splash } from './utils/logger';
|
||||
import { join } from 'path';
|
||||
import { Server } from 'socket.io';
|
||||
import { ytdlpUpdater } from './utils/updater';
|
||||
import {
|
||||
download,
|
||||
abortDownload,
|
||||
retrieveDownload,
|
||||
abortAllDownloads,
|
||||
getFormatsAndMetadata
|
||||
} from './core/downloader';
|
||||
import { getFreeDiskSpace } from './utils/procUtils';
|
||||
import { listDownloaded } from './core/downloadArchive';
|
||||
import { createServer } from 'http';
|
||||
import { streamer } from './core/streamer';
|
||||
import * as Koa from 'koa';
|
||||
import * as Router from 'koa-router';
|
||||
import * as serve from 'koa-static';
|
||||
import * as cors from '@koa/cors';
|
||||
import Logger from './utils/BetterLogger';
|
||||
import { ISettings } from './interfaces/ISettings';
|
||||
import { directoryTree } from './utils/directoryUtils';
|
||||
|
||||
const app = new Koa();
|
||||
const server = createServer(app.callback());
|
||||
const router = new Router();
|
||||
const log = Logger.instance;
|
||||
const io = new Server(server, {
|
||||
cors: {
|
||||
origin: "*",
|
||||
methods: ["GET", "POST"]
|
||||
}
|
||||
});
|
||||
|
||||
let settings: ISettings;
|
||||
|
||||
try {
|
||||
settings = require('../settings.json');
|
||||
} catch (e) {
|
||||
log.warn('settings', 'file not found, ignore if using Docker');
|
||||
}
|
||||
|
||||
// Koa routing
|
||||
router.get('/settings', (ctx, next) => {
|
||||
ctx.redirect('/')
|
||||
next()
|
||||
})
|
||||
router.get('/downloaded', (ctx, next) => {
|
||||
ctx.redirect('/')
|
||||
next()
|
||||
})
|
||||
router.get('/archive', (ctx, next) => {
|
||||
listDownloaded(ctx)
|
||||
.then((res: any) => {
|
||||
ctx.body = res
|
||||
next()
|
||||
})
|
||||
.catch((err: any) => {
|
||||
ctx.body = err;
|
||||
next()
|
||||
})
|
||||
})
|
||||
router.get('/stream/:filepath', (ctx, next) => {
|
||||
streamer(ctx, next)
|
||||
})
|
||||
router.get('/tree', (ctx, next) => {
|
||||
ctx.body = directoryTree()
|
||||
next()
|
||||
})
|
||||
|
||||
// WebSocket listeners
|
||||
io.on('connection', socket => {
|
||||
log.info('ws', `${socket.handshake.address} connected!`)
|
||||
|
||||
socket.on('send-url', (args) => {
|
||||
log.info('ws', args?.url)
|
||||
download(socket, args)
|
||||
})
|
||||
socket.on('send-url-format-selection', (args) => {
|
||||
log.info('ws', `Formats ${args?.url}`)
|
||||
if (args.url) getFormatsAndMetadata(socket, args?.url)
|
||||
})
|
||||
socket.on('abort', (args) => {
|
||||
abortDownload(socket, args)
|
||||
})
|
||||
socket.on('abort-all', () => {
|
||||
abortAllDownloads(socket)
|
||||
})
|
||||
socket.on('update-bin', () => {
|
||||
ytdlpUpdater(socket)
|
||||
})
|
||||
socket.on('retrieve-jobs', () => {
|
||||
retrieveDownload(socket)
|
||||
})
|
||||
socket.on('disk-space', () => {
|
||||
getFreeDiskSpace(socket, settings.download_path || 'downloads/')
|
||||
})
|
||||
})
|
||||
|
||||
io.on('disconnect', (socket) => {
|
||||
log.info('ws', `${socket.handshake.address} disconnected`)
|
||||
})
|
||||
|
||||
app.use(serve(join(__dirname, 'frontend')))
|
||||
app.use(cors())
|
||||
app.use(router.routes())
|
||||
|
||||
server.listen(process.env.PORT || settings.port || 3022)
|
||||
|
||||
splash()
|
||||
log.info('http', `Server started on port ${process.env.PORT || settings.port || 3022}`)
|
||||
|
||||
/**
|
||||
* Cleanup handler
|
||||
*/
|
||||
const gracefullyStop = () => {
|
||||
log.warn('proc', 'Shutting down...')
|
||||
io.disconnectSockets(true)
|
||||
server.close()
|
||||
log.info('proc', 'Done!')
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
// Intercepts singnals and perform cleanups before shutting down.
|
||||
process
|
||||
.on('SIGTERM', () => gracefullyStop())
|
||||
.on('SIGUSR1', () => gracefullyStop())
|
||||
.on('SIGUSR2', () => gracefullyStop())
|
||||
6
server-node/src/types/index.d.ts
vendored
Normal file
6
server-node/src/types/index.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
export type CLIProgress = {
|
||||
percentage: string
|
||||
speed: string
|
||||
size: number
|
||||
eta: number
|
||||
}
|
||||
59
server-node/src/utils/BetterLogger.ts
Normal file
59
server-node/src/utils/BetterLogger.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
const ansi = {
|
||||
reset: '\u001b[0m',
|
||||
red: '\u001b[31m',
|
||||
cyan: '\u001b[36m',
|
||||
green: '\u001b[32m',
|
||||
yellow: '\u001b[93m',
|
||||
bold: '\u001b[1m',
|
||||
normal: '\u001b[22m',
|
||||
}
|
||||
|
||||
class Logger {
|
||||
private static _instance: Logger;
|
||||
|
||||
constructor() { };
|
||||
|
||||
static get instance() {
|
||||
if (this._instance) {
|
||||
return this._instance
|
||||
}
|
||||
this._instance = new Logger()
|
||||
return this._instance;
|
||||
}
|
||||
/**
|
||||
* Print a standard info message
|
||||
* @param {string} proto the context/protocol/section outputting the message
|
||||
* @param {string} args the acutal message
|
||||
*/
|
||||
public info(proto: string, args: string) {
|
||||
process.stdout.write(
|
||||
this.formatter(proto, args)
|
||||
)
|
||||
}
|
||||
/**
|
||||
* Print a warn message
|
||||
* @param {string} proto the context/protocol/section outputting the message
|
||||
* @param {string} args the acutal message
|
||||
*/
|
||||
public warn(proto: string, args: string) {
|
||||
process.stdout.write(
|
||||
`${ansi.yellow}${this.formatter(proto, args)}${ansi.reset}`
|
||||
)
|
||||
}
|
||||
/**
|
||||
* Print an error message
|
||||
* @param {string} proto the context/protocol/section outputting the message
|
||||
* @param {string} args the acutal message
|
||||
*/
|
||||
public err(proto: string, args: string) {
|
||||
process.stdout.write(
|
||||
`${ansi.red}${this.formatter(proto, args)}${ansi.reset}`
|
||||
)
|
||||
}
|
||||
|
||||
private formatter(proto: any, args: any) {
|
||||
return `${ansi.bold}[${proto}]${ansi.normal}\t${args}\n`
|
||||
}
|
||||
}
|
||||
|
||||
export default Logger;
|
||||
59
server-node/src/utils/directoryUtils.ts
Normal file
59
server-node/src/utils/directoryUtils.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { readdirSync, statSync } from "fs";
|
||||
import { ISettings } from "../interfaces/ISettings";
|
||||
|
||||
let settings: ISettings;
|
||||
|
||||
class Node {
|
||||
public path: string
|
||||
public children: Node[]
|
||||
|
||||
constructor(path: string) {
|
||||
this.path = path
|
||||
this.children = []
|
||||
}
|
||||
}
|
||||
|
||||
function buildTreeDFS(rootPath: string, directoryOnly: boolean) {
|
||||
const root = new Node(rootPath)
|
||||
const stack: Node[] = []
|
||||
const flattened: string[] = []
|
||||
|
||||
stack.push(root)
|
||||
flattened.push(rootPath)
|
||||
|
||||
while (stack.length) {
|
||||
const current = stack.pop()
|
||||
if (current) {
|
||||
const children = readdirSync(current.path)
|
||||
for (const it of children) {
|
||||
const childPath = `${current.path}/${it}`
|
||||
const childNode = new Node(childPath)
|
||||
|
||||
if (directoryOnly) {
|
||||
if (statSync(childPath).isDirectory()) {
|
||||
current.children.push(childNode)
|
||||
stack.push(childNode)
|
||||
flattened.push(childNode.path)
|
||||
}
|
||||
} else {
|
||||
current.children.push(childNode)
|
||||
if (statSync(childPath).isDirectory()) {
|
||||
stack.push(childNode)
|
||||
flattened.push(childNode.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
tree: root,
|
||||
flat: flattened
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
settings = require('../../settings.json');
|
||||
} catch (e) { }
|
||||
|
||||
export const directoryTree = () => buildTreeDFS(settings.download_path || 'downloads', true)
|
||||
25
server-node/src/utils/logger.ts
Normal file
25
server-node/src/utils/logger.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Simplest logger function, takes two argument: first one put between
|
||||
* square brackets (the protocol), the second one it's the effective message
|
||||
* @param {string} proto protocol
|
||||
* @param {string} args message
|
||||
*/
|
||||
export const logger = (proto: string, args: string) => {
|
||||
console.log(`[${proto}]\t${args}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* CLI splash
|
||||
*/
|
||||
|
||||
export const splash = () => {
|
||||
const fg = "\u001b[38;2;50;113;168m"
|
||||
const reset = "\u001b[0m"
|
||||
console.log(`${fg} __ ____ __ __ ______`)
|
||||
console.log(" __ __/ /________/ / /__ _ _____ / / / / / / _/")
|
||||
console.log(" / // / __/___/ _ / / _ \\ | |/|/ / -_) _ \\/ /_/ // / ")
|
||||
console.log(" \\_, /\\__/ \\_,_/_/ .__/ |__,__/\\__/_.__/\\____/___/ ")
|
||||
console.log(`/___/ /_/ \n${reset}`)
|
||||
console.log(" yt-dlp-webUI - A web-ui for yt-dlp, simply enough")
|
||||
console.log("---------------------------------------------------\n")
|
||||
}
|
||||
4
server-node/src/utils/params.ts
Normal file
4
server-node/src/utils/params.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export const availableParams = [
|
||||
'--no-mtime',
|
||||
'-x',
|
||||
]
|
||||
36
server-node/src/utils/procUtils.ts
Normal file
36
server-node/src/utils/procUtils.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { exec, spawn } from 'child_process';
|
||||
import { statSync } from 'fs';
|
||||
import Logger from './BetterLogger';
|
||||
|
||||
const log = Logger.instance;
|
||||
|
||||
/**
|
||||
* Browse /proc in order to find the specific pid
|
||||
* @param {number} pid
|
||||
* @returns {*} process stats if any
|
||||
*/
|
||||
export function existsInProc(pid: number): any {
|
||||
try {
|
||||
return statSync(`/proc/${pid}`)
|
||||
} catch (e) {
|
||||
log.warn('proc', `pid ${pid} not found in procfs`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kills a process with a sys-call
|
||||
* @param {number} pid the killed process pid
|
||||
*/
|
||||
export async function killProcess(pid: number) {
|
||||
const res = spawn('kill', [String(pid)])
|
||||
res.on('exit', () => {
|
||||
log.info('proc', `Successfully killed yt-dlp process, pid: ${pid}`)
|
||||
})
|
||||
}
|
||||
|
||||
export function getFreeDiskSpace(socket: any, path: string) {
|
||||
const message: string = 'free-space';
|
||||
exec(`df -h ${path} | tail -1 | awk '{print $4}'`, (_, stdout) => {
|
||||
socket.emit(message, stdout)
|
||||
})
|
||||
}
|
||||
90
server-node/src/utils/updater.ts
Normal file
90
server-node/src/utils/updater.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { get } from 'https';
|
||||
import { rmSync, createWriteStream, chmod } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
// endpoint to github API
|
||||
const options = {
|
||||
hostname: 'api.github.com',
|
||||
path: '/repos/yt-dlp/yt-dlp/releases/latest',
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0'
|
||||
},
|
||||
method: 'GET',
|
||||
port: 443,
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the binary url based on the release tag
|
||||
* @param {string} release yt-dlp GitHub release tag
|
||||
* @returns {*} the fetch options with the correct tag and headers
|
||||
*/
|
||||
function buildDonwloadOptions(release) {
|
||||
return {
|
||||
hostname: 'github.com',
|
||||
path: `/yt-dlp/yt-dlp/releases/download/${release}/yt-dlp`,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0'
|
||||
},
|
||||
method: 'GET',
|
||||
port: 443,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the yt-dlp latest binary URL from GitHub API
|
||||
*/
|
||||
async function update() {
|
||||
// ensure that the binary has been removed
|
||||
try {
|
||||
rmSync(join(__dirname, '..', 'core', 'yt-dlp'))
|
||||
}
|
||||
catch (e) {
|
||||
console.log('file not found!')
|
||||
}
|
||||
// body buffer
|
||||
let chunks = []
|
||||
get(options, res => {
|
||||
// push the http packets chunks into the buffer
|
||||
res.on('data', chunk => {
|
||||
chunks.push(chunk)
|
||||
});
|
||||
// the connection has ended so build the body from the buffer
|
||||
// parse it as a JSON and get the tag_name
|
||||
res.on('end', () => {
|
||||
const buffer = Buffer.concat(chunks)
|
||||
const release = JSON.parse(buffer.toString())['tag_name']
|
||||
console.log('The latest release is:', release)
|
||||
// invoke the binary downloader
|
||||
downloadBinary(buildDonwloadOptions(release))
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Utility that Pipes the latest binary to a file
|
||||
* @param {string} url yt-dlp GitHub release url
|
||||
*/
|
||||
function downloadBinary(url) {
|
||||
get(url, res => {
|
||||
// if it is a redirect follow the url
|
||||
if (res.statusCode === 301 || res.statusCode === 302) {
|
||||
return downloadBinary(res.headers.location)
|
||||
}
|
||||
let bin = createWriteStream(join(__dirname, '..', 'core', 'yt-dlp'))
|
||||
res.pipe(bin)
|
||||
// once the connection has ended make the file executable
|
||||
res.on('end', () => {
|
||||
chmod(join(__dirname, '..', 'core', 'yt-dlp'), 0o775, err => {
|
||||
err ? console.error('failed updating!') : console.log('done!')
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Invoke the yt-dlp update procedure
|
||||
* @param {Socket} socket the current connection socket
|
||||
*/
|
||||
export function ytdlpUpdater(socket) {
|
||||
update().then(() => {
|
||||
socket.emit('updated')
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user