feat: Inicializa a aplicação Downloader com backend para gerenciamento de sessões e downloads, e frontend para interação.
This commit is contained in:
1 parent
c2cadc25f3
commit
5b1600c210
13 files changed
+1308
-738
No files matched your search
+69
-101
@@ -1,10 +1,12 @@
|
||||
import Fastify from "fastify";
|
||||
import Fastify, { FastifyRequest, FastifyReply } from "fastify";
|
||||
import cors from "@fastify/cors";
|
||||
import fastifyStatic from "@fastify/static";
|
||||
import { z } from "zod";
|
||||
import { Agent, setGlobalDispatcher, request } from "undici";
|
||||
import { Agent, setGlobalDispatcher } from "undici";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { sessionManager } from "./lib/SessionManager";
|
||||
import { DownloadManager } from "./lib/DownloadManager";
|
||||
|
||||
// Otimização: Agente HTTP global com conexões persistentes e timeout relaxado.
|
||||
// Isso ajuda a manter a performance em downloads paralelos e evita gargalos de socket.
|
||||
@@ -59,112 +61,78 @@ const streamQuerySchema = z.object({
|
||||
.optional(),
|
||||
});
|
||||
|
||||
app.get("/stream", async (requestFastify, reply) => {
|
||||
const parseResult = streamQuerySchema.safeParse(requestFastify.query);
|
||||
const downloadManager = new DownloadManager(agent);
|
||||
|
||||
if (!parseResult.success) {
|
||||
const firstError = parseResult.error.errors[0]?.message ?? "Parâmetros inválidos";
|
||||
reply.status(400);
|
||||
return { error: firstError };
|
||||
}
|
||||
// Helper to get session from header
|
||||
const getSession = (req: FastifyRequest, reply: FastifyReply) => {
|
||||
const authHeader = req.headers["x-session-id"];
|
||||
const id = Array.isArray(authHeader) ? authHeader[0] : authHeader;
|
||||
const session = sessionManager.getSession(id);
|
||||
|
||||
const { url, offset } = parseResult.data;
|
||||
// Return the ID for the client to store if it's new
|
||||
reply.header("x-session-id", session.id);
|
||||
return session;
|
||||
};
|
||||
|
||||
// 1. Session Status & Timer
|
||||
app.get("/api/session", async (request, reply) => {
|
||||
const session = getSession(request, reply);
|
||||
return {
|
||||
sessionId: session.id,
|
||||
expiresAt: sessionManager.getExpiresAt(session.id),
|
||||
remainingMs: sessionManager.getSessionTimeRemaining(session.id),
|
||||
};
|
||||
});
|
||||
|
||||
// 2. Queue Download
|
||||
const queueSchema = z.object({
|
||||
url: z.string().url(),
|
||||
});
|
||||
|
||||
app.post("/api/queue", async (request, reply) => {
|
||||
const session = getSession(request, reply);
|
||||
|
||||
const parse = queueSchema.safeParse(request.body);
|
||||
if (!parse.success) return reply.status(400).send({ error: "Invalid URL" });
|
||||
|
||||
const task = await downloadManager.queueDownload(session.id, parse.data.url);
|
||||
return task;
|
||||
});
|
||||
|
||||
// 3. List Tasks (Polling)
|
||||
app.get("/api/tasks", async (request, reply) => {
|
||||
const session = getSession(request, reply);
|
||||
const tasks = downloadManager.getTasks(session.id);
|
||||
return tasks;
|
||||
});
|
||||
|
||||
// 4. Download Completed File (Local -> User)
|
||||
app.get("/api/download/:taskId", async (request, reply) => {
|
||||
const session = getSession(request, reply);
|
||||
const { taskId } = request.params as { taskId: string };
|
||||
|
||||
const task = downloadManager.getTask(taskId);
|
||||
|
||||
if (!task) return reply.status(404).send({ error: "Task not found" });
|
||||
if (task.sessionId !== session.id) return reply.status(403).send({ error: "Unauthorized" });
|
||||
if (task.status !== "completed") return reply.status(400).send({ error: "Download not ready" });
|
||||
|
||||
const sessionPath = sessionManager.getSessionPath(session.id);
|
||||
const filePath = resolve(sessionPath, `${task.id}_${task.filename}`);
|
||||
|
||||
try {
|
||||
// HEAD para validar link e obter metadados.
|
||||
const headResponse = await request(url, {
|
||||
method: "HEAD",
|
||||
headersTimeout: 15000,
|
||||
headers: {
|
||||
"User-Agent": "Ephemeral-Downloader/1.0",
|
||||
},
|
||||
dispatcher: agent,
|
||||
});
|
||||
|
||||
if (headResponse.statusCode && headResponse.statusCode >= 400) {
|
||||
reply.status(502);
|
||||
return { error: `Erro na origem: ${headResponse.statusCode}` };
|
||||
}
|
||||
|
||||
const contentType =
|
||||
headResponse.headers["content-type"] ?? "application/octet-stream";
|
||||
const contentLength = headResponse.headers["content-length"];
|
||||
const originDisposition = headResponse.headers["content-disposition"];
|
||||
|
||||
// Nome de arquivo fallback.
|
||||
const fallbackName = (() => {
|
||||
try {
|
||||
const parsedUrl = new URL(url);
|
||||
const nameFromPath = parsedUrl.pathname.split("/").filter(Boolean).pop();
|
||||
return nameFromPath ?? "download.bin";
|
||||
} catch {
|
||||
return "download.bin";
|
||||
}
|
||||
})();
|
||||
|
||||
const filename = (() => {
|
||||
if (typeof originDisposition === "string") {
|
||||
const match = /filename\*?=(?:UTF-8''|")?([^";]+)/i.exec(originDisposition);
|
||||
if (match?.[1]) {
|
||||
return decodeURIComponent(match[1].replace(/"/g, ""));
|
||||
}
|
||||
}
|
||||
return fallbackName;
|
||||
})();
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"User-Agent": "Ephemeral-Downloader/1.0",
|
||||
};
|
||||
|
||||
if (typeof offset === "number" && Number.isFinite(offset) && offset > 0) {
|
||||
headers.Range = `bytes=${offset}-`;
|
||||
}
|
||||
|
||||
// GET streaming da origem.
|
||||
const originResponse = await request(url, {
|
||||
method: "GET",
|
||||
headers,
|
||||
headersTimeout: 30000,
|
||||
maxRedirections: 5,
|
||||
dispatcher: agent,
|
||||
});
|
||||
|
||||
if (!originResponse.body) {
|
||||
reply.status(502);
|
||||
return { error: "Sem corpo de resposta na origem." };
|
||||
}
|
||||
|
||||
if (originResponse.statusCode && originResponse.statusCode >= 400) {
|
||||
reply.status(originResponse.statusCode);
|
||||
return { error: "Falha ao baixar arquivo da origem." };
|
||||
}
|
||||
|
||||
const normalizedLength = Array.isArray(contentLength)
|
||||
? contentLength[0]
|
||||
: contentLength;
|
||||
|
||||
// Configura headers de resposta.
|
||||
reply.headers({
|
||||
"Content-Type": Array.isArray(contentType) ? contentType[0] : contentType,
|
||||
...(normalizedLength
|
||||
? {
|
||||
"Content-Length": normalizedLength,
|
||||
"X-Origin-Content-Length": normalizedLength,
|
||||
}
|
||||
: {}),
|
||||
"Content-Disposition": `attachment; filename="${filename}"`,
|
||||
});
|
||||
|
||||
// Pipe direto.
|
||||
return reply.send(originResponse.body);
|
||||
} catch (error) {
|
||||
requestFastify.log.error({ err: error, url }, "Erro no proxy");
|
||||
if (!reply.sent) {
|
||||
return reply.status(502).send({ error: "Erro interno no proxy." });
|
||||
}
|
||||
sessionManager.touch(session.id); // Reset timer on interaction
|
||||
return reply.download(filePath, task.filename); // requires @fastify/static or sendFile
|
||||
} catch (e) {
|
||||
return reply.status(404).send({ error: "File removed or missing" });
|
||||
}
|
||||
});
|
||||
|
||||
// Legacy Stream (Redirect or Deprecate)
|
||||
app.get("/stream", async (req, reply) => {
|
||||
reply.status(410).send({ error: "Direct streaming is deprecated. Use the new Dashboard." });
|
||||
});
|
||||
|
||||
// Fallback para SPA.
|
||||
app.setNotFoundHandler((requestFastify, reply) => {
|
||||
if (requestFastify.method === "GET") {
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
import { createWriteStream, existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { request, Dispatcher } from "undici";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { sessionManager } from "./SessionManager";
|
||||
|
||||
type DownloadStatus = "queued" | "downloading" | "completed" | "error";
|
||||
|
||||
export type DownloadTask = {
|
||||
id: string;
|
||||
sessionId: string;
|
||||
url: string;
|
||||
filename: string;
|
||||
status: DownloadStatus;
|
||||
progress: number;
|
||||
totalBytes: number;
|
||||
receivedBytes: number;
|
||||
speed: number;
|
||||
error?: string;
|
||||
createdAt: number;
|
||||
};
|
||||
|
||||
// Map to store active task metadata in memory (for speed)
|
||||
// Ideally this would be persisted to JSON in the session folder for crash recovery
|
||||
const tasks = new Map<string, DownloadTask>();
|
||||
const agent = new Dispatcher(); // Should use the global agent from index.ts ideally, or pass it in.
|
||||
|
||||
export class DownloadManager {
|
||||
|
||||
constructor(private globalAgent: Dispatcher) { }
|
||||
|
||||
public getTasks(sessionId: string): DownloadTask[] {
|
||||
return Array.from(tasks.values()).filter(t => t.sessionId === sessionId);
|
||||
}
|
||||
|
||||
public getTask(taskId: string): DownloadTask | undefined {
|
||||
return tasks.get(taskId);
|
||||
}
|
||||
|
||||
public async queueDownload(sessionId: string, url: string): Promise<DownloadTask> {
|
||||
const id = randomUUID();
|
||||
|
||||
// Try to determine filename
|
||||
let filename = "download.bin";
|
||||
try {
|
||||
const u = new URL(url);
|
||||
const p = u.pathname.split("/").pop();
|
||||
if (p) filename = p;
|
||||
} catch { }
|
||||
|
||||
const task: DownloadTask = {
|
||||
id,
|
||||
sessionId,
|
||||
url,
|
||||
filename, // Will be updated after HEAD request if possible
|
||||
status: "queued",
|
||||
progress: 0,
|
||||
totalBytes: 0,
|
||||
receivedBytes: 0,
|
||||
speed: 0,
|
||||
createdAt: Date.now()
|
||||
};
|
||||
|
||||
tasks.set(id, task);
|
||||
this.startDownload(task);
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
private async startDownload(task: DownloadTask) {
|
||||
const sessionPath = sessionManager.getSessionPath(task.sessionId);
|
||||
if (!existsSync(sessionPath)) {
|
||||
task.status = "error";
|
||||
task.error = "Session expired or missing";
|
||||
return;
|
||||
}
|
||||
|
||||
task.status = "downloading";
|
||||
|
||||
try {
|
||||
// Step 1: HEAD/GET to stream
|
||||
const response = await request(task.url, {
|
||||
dispatcher: this.globalAgent,
|
||||
headersTimeout: 15000,
|
||||
headers: { "User-Agent": "Ephemeral-Downloader/2.0" }
|
||||
});
|
||||
|
||||
if (response.statusCode >= 400) {
|
||||
throw new Error(`HTTP Error ${response.statusCode}`);
|
||||
}
|
||||
|
||||
// Update filename from headers if available
|
||||
const disposition = response.headers["content-disposition"];
|
||||
if (typeof disposition === "string") {
|
||||
const match = /filename\*?=(?:UTF-8''|")?([^";]+)/i.exec(disposition);
|
||||
if (match?.[1]) {
|
||||
task.filename = decodeURIComponent(match[1].replace(/"/g, ""));
|
||||
}
|
||||
}
|
||||
|
||||
// Size
|
||||
const len = response.headers["content-length"];
|
||||
if (len) {
|
||||
task.totalBytes = Number(Array.isArray(len) ? len[0] : len);
|
||||
}
|
||||
|
||||
const filePath = join(sessionPath, `${task.id}_${task.filename}`);
|
||||
const fileStream = createWriteStream(filePath);
|
||||
|
||||
let lastUpdate = Date.now();
|
||||
let bytesAtLastUpdate = 0;
|
||||
|
||||
for await (const chunk of response.body) {
|
||||
fileStream.write(chunk);
|
||||
task.receivedBytes += chunk.length;
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastUpdate > 500) { // Update speed every 500ms
|
||||
const diff = now - lastUpdate;
|
||||
const bytesDiff = task.receivedBytes - bytesAtLastUpdate;
|
||||
task.speed = (bytesDiff / diff) * 1000;
|
||||
|
||||
if (task.totalBytes > 0) {
|
||||
task.progress = Math.round((task.receivedBytes / task.totalBytes) * 100);
|
||||
}
|
||||
|
||||
lastUpdate = now;
|
||||
bytesAtLastUpdate = task.receivedBytes;
|
||||
|
||||
// Touch session to keep it alive during download
|
||||
sessionManager.touch(task.sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
fileStream.end();
|
||||
task.status = "completed";
|
||||
task.progress = 100;
|
||||
task.speed = 0;
|
||||
sessionManager.touch(task.sessionId); // keep alive after finish
|
||||
|
||||
} catch (err) {
|
||||
task.status = "error";
|
||||
task.error = err instanceof Error ? err.message : "Unknown error";
|
||||
console.error(`[DownloadManager] Download failed: ${task.url}`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import { existsSync, mkdirSync, rmSync, readdirSync, statSync } from "node:fs";
|
||||
import { join, resolve } from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
const STORAGE_ROOT = resolve(process.cwd(), "server/storage");
|
||||
const SESSION_TIMEOUT_MS = 60 * 60 * 1000; // 1 Hour
|
||||
|
||||
type SessionData = {
|
||||
id: string;
|
||||
lastActive: number;
|
||||
};
|
||||
|
||||
export class SessionManager {
|
||||
private sessions: Map<string, SessionData> = new Map();
|
||||
|
||||
constructor() {
|
||||
// Ensure storage root exists
|
||||
if (!existsSync(STORAGE_ROOT)) {
|
||||
mkdirSync(STORAGE_ROOT, { recursive: true });
|
||||
}
|
||||
|
||||
// Start Garbage Collector
|
||||
setInterval(() => this.cleanup(), 60 * 1000); // Check every minute
|
||||
console.log("[SessionManager] Garbage Collector started.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets or creates a session. Updates lastActive timestamp.
|
||||
*/
|
||||
public getSession(id?: string): SessionData {
|
||||
const now = Date.now();
|
||||
|
||||
if (id && this.sessions.has(id)) {
|
||||
const session = this.sessions.get(id)!;
|
||||
session.lastActive = now;
|
||||
return session;
|
||||
}
|
||||
|
||||
// If ID provided but not in memory, check if folder exists (recovery)
|
||||
if (id && existsSync(join(STORAGE_ROOT, id))) {
|
||||
const session = { id, lastActive: now };
|
||||
this.sessions.set(id, session);
|
||||
return session;
|
||||
}
|
||||
|
||||
// Create new session
|
||||
const newId = randomUUID();
|
||||
const session = { id: newId, lastActive: now };
|
||||
this.sessions.set(newId, session);
|
||||
|
||||
// Create folder
|
||||
const sessionPath = this.getSessionPath(newId);
|
||||
if (!existsSync(sessionPath)) {
|
||||
mkdirSync(sessionPath, { recursive: true });
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
public getSessionPath(sessionId: string): string {
|
||||
return join(STORAGE_ROOT, sessionId);
|
||||
}
|
||||
|
||||
public getSessionTimeRemaining(sessionId: string): number {
|
||||
const session = this.sessions.get(sessionId);
|
||||
if (!session) return 0;
|
||||
const expiresAt = session.lastActive + SESSION_TIMEOUT_MS;
|
||||
return Math.max(0, expiresAt - Date.now());
|
||||
}
|
||||
|
||||
public getExpiresAt(sessionId: string): string {
|
||||
const session = this.sessions.get(sessionId);
|
||||
if (!session) return new Date().toISOString();
|
||||
return new Date(session.lastActive + SESSION_TIMEOUT_MS).toISOString();
|
||||
}
|
||||
|
||||
public touch(sessionId: string) {
|
||||
if (this.sessions.has(sessionId)) {
|
||||
this.sessions.get(sessionId)!.lastActive = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes expired sessions and deletes their files.
|
||||
*/
|
||||
private cleanup() {
|
||||
const now = Date.now();
|
||||
|
||||
// Check in-memory sessions
|
||||
for (const [id, session] of this.sessions.entries()) {
|
||||
if (now - session.lastActive > SESSION_TIMEOUT_MS) {
|
||||
console.log(`[SessionManager] Session ${id} expired. Cleaning up...`);
|
||||
this.destroySession(id);
|
||||
}
|
||||
}
|
||||
|
||||
// Double check orphaned folders (in case server restarted)
|
||||
// accessible folders in storage that are not in memory OR are too old
|
||||
try {
|
||||
const dirs = readdirSync(STORAGE_ROOT);
|
||||
for (const dir of dirs) {
|
||||
if (dir === ".gitignore") continue;
|
||||
|
||||
const dirPath = join(STORAGE_ROOT, dir);
|
||||
const stats = statSync(dirPath);
|
||||
|
||||
// If folder is older than timeout and not in active memory (or in memory but expired)
|
||||
// Note: mtime might update when files are written, so this is a heuristic fallback
|
||||
const isInMemory = this.sessions.has(dir);
|
||||
if (!isInMemory && (now - stats.mtimeMs > SESSION_TIMEOUT_MS)) {
|
||||
console.log(`[SessionManager] Removing orphaned session folder: ${dir}`);
|
||||
rmSync(dirPath, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[SessionManager] Error during fallback cleanup", err);
|
||||
}
|
||||
}
|
||||
|
||||
private destroySession(id: string) {
|
||||
this.sessions.delete(id);
|
||||
const path = this.getSessionPath(id);
|
||||
if (existsSync(path)) {
|
||||
try {
|
||||
rmSync(path, { recursive: true, force: true });
|
||||
console.log(`[SessionManager] Deleted storage for ${id}`);
|
||||
} catch (e) {
|
||||
console.error(`[SessionManager] Failed to delete ${path}`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const sessionManager = new SessionManager();
|
||||
Reference in new issue
Block a user