存至私人影库增加下载方式

This commit is contained in:
mtvpls
2026-05-21 23:46:26 +08:00
parent e44f565112
commit a24de1346f
8 changed files with 159 additions and 16 deletions
+16 -2
View File
@@ -8,6 +8,13 @@ import { hasFeaturePermission } from '@/lib/permissions';
export const runtime = 'nodejs';
const downloadTools = ['aria2', 'Transmission', 'qBittorrent'] as const;
type DownloadTool = typeof downloadTools[number];
function isDownloadTool(tool: unknown): tool is DownloadTool {
return typeof tool === 'string' && downloadTools.includes(tool as DownloadTool);
}
/**
* POST /api/acg/download
* 添加 ACG 资源到 OpenList 离线下载(仅管理员和站长可用)
@@ -23,7 +30,7 @@ export async function POST(req: NextRequest) {
);
}
const { url, name } = await req.json();
const { url, name, tool = 'aria2' } = await req.json();
if (!url || typeof url !== 'string') {
return NextResponse.json(
@@ -39,6 +46,13 @@ export async function POST(req: NextRequest) {
);
}
if (!isDownloadTool(tool)) {
return NextResponse.json(
{ error: '下载方式不支持' },
{ status: 400 }
);
}
// 获取 OpenList 配置
const config = await getConfig();
const openlistConfig = config.OpenListConfig;
@@ -81,7 +95,7 @@ export async function POST(req: NextRequest) {
body: JSON.stringify({
path: downloadPath,
urls: [url],
tool: 'aria2',
tool,
}),
});
+12 -2
View File
@@ -23,10 +23,14 @@ export async function GET(req: NextRequest) {
const config = await getConfig();
const animeConfig = config.AnimeSubscriptionConfig || {
Enabled: false,
DownloadTool: 'aria2',
Subscriptions: [],
};
return NextResponse.json(animeConfig);
return NextResponse.json({
...animeConfig,
DownloadTool: animeConfig.DownloadTool || 'aria2',
});
} catch (error: any) {
console.error('获取追番订阅配置失败:', error);
return NextResponse.json(
@@ -63,7 +67,13 @@ export async function POST(req: NextRequest) {
const config = await getConfig();
if (!config.AnimeSubscriptionConfig) {
config.AnimeSubscriptionConfig = { Enabled: false, Subscriptions: [] };
config.AnimeSubscriptionConfig = {
Enabled: false,
DownloadTool: 'aria2',
Subscriptions: [],
};
} else if (!config.AnimeSubscriptionConfig.DownloadTool) {
config.AnimeSubscriptionConfig.DownloadTool = 'aria2';
}
// 验证集数
@@ -7,6 +7,13 @@ import { db } from '@/lib/db';
export const runtime = 'nodejs';
const downloadTools = ['aria2', 'qBittorrent', 'Transmission'] as const;
type DownloadTool = typeof downloadTools[number];
function isDownloadTool(tool: unknown): tool is DownloadTool {
return typeof tool === 'string' && downloadTools.includes(tool as DownloadTool);
}
/**
* PUT /api/admin/anime-subscription/toggle
* 切换追番功能启用状态
@@ -19,7 +26,7 @@ export async function PUT(req: NextRequest) {
return NextResponse.json({ error: '无权限访问' }, { status: 403 });
}
const { enabled } = await req.json();
const { enabled, downloadTool } = await req.json();
if (typeof enabled !== 'boolean') {
return NextResponse.json(
@@ -28,15 +35,31 @@ export async function PUT(req: NextRequest) {
);
}
if (downloadTool !== undefined && !isDownloadTool(downloadTool)) {
return NextResponse.json(
{ error: '下载方式不支持' },
{ status: 400 }
);
}
const config = await getConfig();
if (!config.AnimeSubscriptionConfig) {
config.AnimeSubscriptionConfig = { Enabled: false, Subscriptions: [] };
}
config.AnimeSubscriptionConfig.Enabled = enabled;
if (downloadTool !== undefined) {
config.AnimeSubscriptionConfig.DownloadTool = downloadTool;
} else if (!config.AnimeSubscriptionConfig.DownloadTool) {
config.AnimeSubscriptionConfig.DownloadTool = 'aria2';
}
await db.saveAdminConfig(config);
return NextResponse.json({ success: true, enabled });
return NextResponse.json({
success: true,
enabled,
downloadTool: config.AnimeSubscriptionConfig.DownloadTool,
});
} catch (error: any) {
console.error('切换追番功能状态失败:', error);
return NextResponse.json(
+25
View File
@@ -31,6 +31,13 @@ interface AcgSearchProps {
}
type AcgSearchSource = 'acgrip' | 'mikan' | 'dmhy';
type DownloadTool = 'aria2' | 'Transmission' | 'qBittorrent';
const downloadToolOptions: Array<{ value: DownloadTool; label: string }> = [
{ value: 'aria2', label: 'aria2' },
{ value: 'qBittorrent', label: 'qBittorrent' },
{ value: 'Transmission', label: 'Transmission' },
];
export default function AcgSearch({
keyword,
@@ -47,6 +54,7 @@ export default function AcgSearch({
const [showNameDialog, setShowNameDialog] = useState(false);
const [selectedItem, setSelectedItem] = useState<AcgSearchItem | null>(null);
const [customName, setCustomName] = useState('');
const [downloadTool, setDownloadTool] = useState<DownloadTool>('aria2');
const [toast, setToast] = useState<ToastProps | null>(null);
const loadMoreRef = useRef<HTMLDivElement>(null);
const isLoadingMoreRef = useRef(false);
@@ -204,6 +212,7 @@ export default function AcgSearch({
body: JSON.stringify({
url: selectedItem.torrentUrl,
name: customName.trim(),
tool: downloadTool,
}),
});
@@ -228,6 +237,7 @@ export default function AcgSearch({
setDownloadingId(null);
setSelectedItem(null);
setCustomName('');
setDownloadTool('aria2');
}
};
@@ -365,12 +375,27 @@ export default function AcgSearch({
className='w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-green-500'
autoFocus
/>
<label className='mt-4 block text-sm font-medium text-gray-700 dark:text-gray-300'>
</label>
<select
value={downloadTool}
onChange={(e) => setDownloadTool(e.target.value as DownloadTool)}
className='mt-1 w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-green-500'
>
{downloadToolOptions.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
<div className='mt-4 flex gap-2 justify-end'>
<button
onClick={() => {
setShowNameDialog(false);
setSelectedItem(null);
setCustomName('');
setDownloadTool('aria2');
}}
className='px-4 py-2 rounded-lg bg-gray-200 text-gray-700 hover:bg-gray-300 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 transition-colors'
>
+64 -8
View File
@@ -6,13 +6,19 @@ import { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { AdminConfig } from '@/lib/admin.types';
import { AnimeSubscription } from '@/types/anime-subscription';
import { AnimeSubscription, AnimeSubscriptionDownloadTool } from '@/types/anime-subscription';
interface AnimeSubscriptionComponentProps {
config: AdminConfig | null;
refreshConfig: () => Promise<void>;
}
const downloadToolOptions: Array<{ value: AnimeSubscriptionDownloadTool; label: string }> = [
{ value: 'aria2', label: 'aria2' },
{ value: 'qBittorrent', label: 'qBittorrent' },
{ value: 'Transmission', label: 'Transmission' },
];
// Switch 组件
const Switch = ({ checked, onChange, disabled }: { checked: boolean; onChange: (checked: boolean) => void; disabled?: boolean }) => (
<button
@@ -141,6 +147,7 @@ export default function AnimeSubscriptionComponent({
refreshConfig,
}: AnimeSubscriptionComponentProps) {
const [enabled, setEnabled] = useState(false);
const [downloadTool, setDownloadTool] = useState<AnimeSubscriptionDownloadTool>('aria2');
const [subscriptions, setSubscriptions] = useState<AnimeSubscription[]>([]);
const [loading, setLoading] = useState(false);
const [showAddForm, setShowAddForm] = useState(false);
@@ -181,6 +188,7 @@ export default function AnimeSubscriptionComponent({
useEffect(() => {
if (config?.AnimeSubscriptionConfig) {
setEnabled(config.AnimeSubscriptionConfig.Enabled || false);
setDownloadTool(config.AnimeSubscriptionConfig.DownloadTool || 'aria2');
setSubscriptions(config.AnimeSubscriptionConfig.Subscriptions || []);
}
}, [config]);
@@ -205,7 +213,7 @@ export default function AnimeSubscriptionComponent({
const response = await fetch('/api/admin/anime-subscription/toggle', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled: newEnabled }),
body: JSON.stringify({ enabled: newEnabled, downloadTool }),
});
if (!response.ok) {
@@ -225,6 +233,35 @@ export default function AnimeSubscriptionComponent({
}
};
const handleDownloadToolChange = async (newDownloadTool: AnimeSubscriptionDownloadTool) => {
const previousDownloadTool = downloadTool;
setDownloadTool(newDownloadTool);
try {
setLoading(true);
const response = await fetch('/api/admin/anime-subscription/toggle', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled, downloadTool: newDownloadTool }),
});
if (!response.ok) {
throw new Error('保存下载方式失败');
}
await refreshConfig();
} catch (error) {
setDownloadTool(previousDownloadTool);
showAlert({
type: 'error',
title: '保存失败',
message: error instanceof Error ? error.message : '保存下载方式失败',
});
} finally {
setLoading(false);
}
};
// 开始添加
const handleAdd = () => {
resetForm();
@@ -408,12 +445,31 @@ export default function AnimeSubscriptionComponent({
return (
<div className='space-y-6'>
{/* 顶部控制 */}
<div className='flex items-center justify-between'>
<div className='flex items-center gap-3'>
<span className='text-sm font-medium text-gray-700 dark:text-gray-300'>
</span>
<Switch checked={enabled} onChange={handleToggleEnabled} disabled={loading} />
<div className='flex flex-col gap-4 md:flex-row md:items-center md:justify-between'>
<div className='flex flex-col gap-4 sm:flex-row sm:items-center sm:gap-6'>
<div className='flex items-center gap-3'>
<span className='text-sm font-medium text-gray-700 dark:text-gray-300'>
</span>
<Switch checked={enabled} onChange={handleToggleEnabled} disabled={loading} />
</div>
<div className='flex items-center gap-3'>
<label className='text-sm font-medium text-gray-700 dark:text-gray-300'>
</label>
<select
value={downloadTool}
onChange={(e) => handleDownloadToolChange(e.target.value as AnimeSubscriptionDownloadTool)}
disabled={loading}
className='min-w-40 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-sm text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-green-500 disabled:opacity-50'
>
{downloadToolOptions.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</div>
</div>
<button
onClick={handleAdd}
+1
View File
@@ -343,6 +343,7 @@ export interface AdminConfig {
};
AnimeSubscriptionConfig?: {
Enabled: boolean; // 是否启用追番功能
DownloadTool?: 'aria2' | 'qBittorrent' | 'Transmission'; // 追番订阅全局下载方式
Subscriptions: Array<{
id: string;
title: string;
+13 -2
View File
@@ -6,7 +6,15 @@ import { getConfig, setCachedConfig } from '@/lib/config';
import { db, getStorage } from '@/lib/db';
import { EmailService } from '@/lib/email.service';
import { OpenListClient } from '@/lib/openlist.client';
import { AnimeSubscription } from '@/types/anime-subscription';
import { AnimeSubscription, AnimeSubscriptionDownloadTool } from '@/types/anime-subscription';
const downloadTools: AnimeSubscriptionDownloadTool[] = ['aria2', 'qBittorrent', 'Transmission'];
function getAnimeSubscriptionDownloadTool(tool: unknown): AnimeSubscriptionDownloadTool {
return typeof tool === 'string' && downloadTools.includes(tool as AnimeSubscriptionDownloadTool)
? tool as AnimeSubscriptionDownloadTool
: 'aria2';
}
/**
* 从标题中提取集数
@@ -121,6 +129,9 @@ export async function addOfflineDownload(
) {
const config = await getConfig();
const openlistConfig = config.OpenListConfig;
const downloadTool = getAnimeSubscriptionDownloadTool(
config.AnimeSubscriptionConfig?.DownloadTool
);
if (!openlistConfig?.Enabled) {
throw new Error('私人影库功能未启用');
@@ -152,7 +163,7 @@ export async function addOfflineDownload(
body: JSON.stringify({
path: downloadPath,
urls: [torrentUrl],
tool: 'aria2',
tool: downloadTool,
}),
});
+3
View File
@@ -11,7 +11,10 @@ export interface AnimeSubscription {
createdBy: string;
}
export type AnimeSubscriptionDownloadTool = 'aria2' | 'qBittorrent' | 'Transmission';
export interface AnimeSubscriptionConfig {
Enabled: boolean;
DownloadTool?: AnimeSubscriptionDownloadTool;
Subscriptions: AnimeSubscription[];
}