Restart repository history from target event
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import { addTorrent, recheckTorrent } from '@/lib/qbittorrent';
|
||||
import { QbittorrentInjectResponse } from '@/types';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { albumPath } = await request.json();
|
||||
|
||||
if (!albumPath || !(await fs.pathExists(albumPath))) {
|
||||
return NextResponse.json({ error: 'Invalid album path' }, { status: 400 });
|
||||
}
|
||||
|
||||
const parentDir = path.dirname(albumPath);
|
||||
const albumDirName = path.basename(albumPath);
|
||||
|
||||
// Logic to find the torrent file (similar to torrent creation route)
|
||||
let torrentPath = path.join(parentDir, `${albumDirName}.torrent`);
|
||||
|
||||
if (!await fs.pathExists(torrentPath)) {
|
||||
// Try to find a recent torrent file if exact match fails
|
||||
const files = await fs.readdir(parentDir);
|
||||
const recentTorrent = (await Promise.all(files
|
||||
.filter(f => f.endsWith('.torrent'))
|
||||
.map(async f => {
|
||||
const stat = await fs.stat(path.join(parentDir, f));
|
||||
return { name: f, mtime: stat.mtimeMs };
|
||||
})
|
||||
))
|
||||
.sort((a, b) => b.mtime - a.mtime)[0];
|
||||
|
||||
if (recentTorrent) {
|
||||
torrentPath = path.join(parentDir, recentTorrent.name);
|
||||
} else {
|
||||
return NextResponse.json({ error: 'Torrent file not found. Please create it first.' }, { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
const torrentData = await fs.readFile(torrentPath);
|
||||
const filename = path.basename(torrentPath);
|
||||
|
||||
// 1. Add torrent (paused)
|
||||
const hash = await addTorrent(torrentData, filename, 'RED');
|
||||
|
||||
// 2. Trigger recheck
|
||||
await recheckTorrent(hash);
|
||||
|
||||
const response: QbittorrentInjectResponse = { success: true, hash };
|
||||
return NextResponse.json(response);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error injecting torrent:', error);
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
return NextResponse.json({
|
||||
error: 'Failed to inject torrent',
|
||||
details: errorMessage
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { resumeTorrent } from '@/lib/qbittorrent';
|
||||
import { QbittorrentResumeResponse } from '@/types';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { hash } = await request.json();
|
||||
|
||||
if (!hash) {
|
||||
return NextResponse.json({ error: 'Hash is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
await resumeTorrent(hash);
|
||||
|
||||
const response: QbittorrentResumeResponse = { success: true };
|
||||
return NextResponse.json(response);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error resuming torrent:', error);
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
return NextResponse.json({
|
||||
error: 'Failed to resume torrent',
|
||||
details: errorMessage
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getTorrentInfo } from '@/lib/qbittorrent';
|
||||
import { QbittorrentStatusResponse } from '@/types';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const hash = searchParams.get('hash');
|
||||
|
||||
if (!hash) {
|
||||
return NextResponse.json({ error: 'Hash is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const info = await getTorrentInfo(hash);
|
||||
if (!info) {
|
||||
return NextResponse.json({ error: 'Torrent not found' }, { status: 404 });
|
||||
}
|
||||
const response: QbittorrentStatusResponse = { success: true, info };
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
console.error('Error getting torrent status:', error);
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
return NextResponse.json({
|
||||
error: 'Failed to get status',
|
||||
details: errorMessage
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user