refactor: 移除 downloadMedia,复用 useContextDownload

- 删除 PoolManager.downloadMedia 方法
- 删除 queue.js 的 downloadMedia 导出
- routes.js 直接使用 getPoolContext().getFirstPage() + useContextDownload
This commit is contained in:
daidai
2026-03-28 09:44:48 +08:00
Unverified
parent 50b09ed740
commit 463881ceda
3 changed files with 8 additions and 42 deletions
-24
View File
@@ -313,30 +313,6 @@ export class PoolManager {
}
}
/**
* 使用 Worker 的浏览器上下文下载媒体
* @param {string} url - 媒体 URL
* @returns {Promise<{image?: string, error?: string}>}
*/
async downloadMedia(url) {
// 找一个空闲的 worker
let worker = this.workers.find(w => w.page && w.busyCount === 0);
if (!worker) {
// 如果没有空闲的,用第一个有 page 的
worker = this.workers.find(w => w.page);
}
if (!worker || !worker.page) {
return { error: '没有可用的浏览器实例' };
}
try {
const { useContextDownload } = await import('../utils/download.js');
return await useContextDownload(url, worker.page);
} catch (e) {
return { error: `下载失败: ${e.message}` };
}
}
/**
* 获取第一个 Worker 的 page
*/
+7 -4
View File
@@ -48,6 +48,7 @@ import {
} from '../../../utils/history.js';
import path from 'path';
import fs from 'fs/promises';
import { useContextDownload } from '../../../backend/utils/download.js';
/**
* 读取请求体
@@ -565,13 +566,15 @@ export function createAdminRouter(context) {
const body = await readBody(req);
const mediaIndex = body.mediaIndex ?? 0;
// 使用 queueManager 的浏览器下载(如果可用)
// 使用 Pool 的浏览器下载(如果可用)
let downloadFn = null;
try {
if (queueManager && queueManager.downloadMedia) {
downloadFn = (url) => queueManager.downloadMedia(url);
const poolContext = queueManager?.getPoolContext?.();
const page = poolContext?.getFirstPage?.();
if (page) {
downloadFn = (url) => useContextDownload(url, page);
}
} catch { /* queueManager 不可用,使用后备方案 */ }
} catch { /* Pool 未初始化,使用后备方案 */ }
const result = await retryMediaDownload(id, mediaIndex, downloadFn);
if (result.success) {
+1 -14
View File
@@ -351,18 +351,6 @@ export function createQueueManager(queueConfig, callbacks) {
return await getCookies(workerName, domain);
}
/**
* 使用 Worker 的浏览器下载媒体
* @param {string} url - 媒体 URL
* @returns {Promise<{image?: string, imageUrl?: string, error?: string}>}
*/
async function downloadMedia(url) {
if (!poolContext || !poolContext.downloadMedia) {
throw new Error('Pool 未初始化或不支持 downloadMedia');
}
return await poolContext.downloadMedia(url);
}
return {
addTask,
getStatus,
@@ -370,7 +358,6 @@ export function createQueueManager(queueConfig, callbacks) {
canAcceptNonStreaming,
initializePool,
getPoolContext,
getWorkerCookies,
downloadMedia
getWorkerCookies
};
}