Initial open-source self-hosted Songs2YT edition

This commit is contained in:
Songs2YT
2026-07-17 02:23:59 +02:00
commit 2aa8a84781
134 changed files with 17758 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
import { createWriteStream } from "fs";
import fs from "fs/promises";
import { Readable } from "stream";
import { pipeline } from "stream/promises";
export async function writeUploadedFile(file: File, destPath: string) {
const webStream = file.stream();
const nodeStream = Readable.fromWeb(webStream as Parameters<typeof Readable.fromWeb>[0]);
await pipeline(nodeStream, createWriteStream(destPath));
}
export async function moveFile(src: string, dest: string) {
try {
await fs.rename(src, dest);
} catch (err) {
const code = err && typeof err === "object" && "code" in err ? err.code : null;
if (code === "EXDEV") {
await fs.copyFile(src, dest);
await fs.unlink(src).catch(() => {});
return;
}
throw err;
}
}
export async function mapWithConcurrency<T, R>(
items: T[],
concurrency: number,
fn: (item: T, index: number) => Promise<R>,
) {
const results: R[] = new Array(items.length);
let nextIndex = 0;
async function worker() {
while (nextIndex < items.length) {
const index = nextIndex;
nextIndex += 1;
results[index] = await fn(items[index], index);
}
}
const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => worker());
await Promise.all(workers);
return results;
}