mira-app-server
Version:
Mira Server - standalone server application using mira-app-core
65 lines • 3.09 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DownloadRoutes = void 0;
const express_1 = require("express");
/**
* 下载执行器路由
*
* POST /api/download/start body: { libraryId, urls: string[], folderId?, clientId? } → { batchId }
* GET /api/download/progress/:batchId → BatchProgress
*
* 全挂 authMiddleware,按 req.user.id。库权限校验沿用全局 permission 中间件。
*/
class DownloadRoutes {
constructor(backend, authRouter) {
this.backend = backend;
this.authRouter = authRouter;
this.router = (0, express_1.Router)();
this.setupRoutes();
}
setupRoutes() {
this.router.post('/start', this.authRouter.authMiddleware(), async (req, res) => {
try {
if (!this.backend.downloadExecutor) {
return res.status(503).json({ code: 503, message: '下载执行器尚未就绪' });
}
const userId = req.user?.id;
if (!userId)
return res.status(401).json({ code: 401, message: '未登录' });
const { libraryId, urls, folderId, clientId } = req.body || {};
if (!libraryId)
return res.status(400).json({ code: 400, message: 'libraryId 必填' });
const urlList = Array.isArray(urls) ? urls.filter((u) => typeof u === 'string' && u.trim()) : [];
if (urlList.length === 0)
return res.status(400).json({ code: 400, message: 'urls 为空' });
const batchId = await this.backend.downloadExecutor.enqueueBatch(urlList.map((u) => ({ url: u.trim(), libraryId, userId, folderId: folderId ?? null, clientId: clientId ?? null })));
res.json({ code: 0, data: { batchId, total: urlList.length } });
}
catch (error) {
res.status(500).json({ code: 500, message: error.message });
}
});
this.router.get('/progress/:batchId', this.authRouter.authMiddleware(), async (req, res) => {
try {
if (!this.backend.downloadExecutor) {
return res.status(503).json({ code: 503, message: '下载执行器尚未就绪' });
}
const userId = req.user?.id;
if (!userId)
return res.status(401).json({ code: 401, message: '未登录' });
const progress = this.backend.downloadExecutor.getProgress(req.params.batchId);
if (!progress)
return res.status(404).json({ code: 404, message: '批次不存在或已过期' });
res.json({ code: 0, data: progress });
}
catch (error) {
res.status(500).json({ code: 500, message: error.message });
}
});
}
getRouter() {
return this.router;
}
}
exports.DownloadRoutes = DownloadRoutes;
//# sourceMappingURL=DownloadRoutes.js.map