mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 21:00:09 +00:00
Add e-book fetch API and UI integration for requests
Introduces an API endpoint to trigger e-book downloads for completed requests, with admin UI integration in RecentRequestsTable and RequestActionsDropdown. Updates the admin dashboard to detect e-book sidecar feature availability from settings. Enhances torrent search result handling with info URLs, improves ranking algorithm normalization, and refines interactive search to show all results without threshold filtering. Also allows nullable ratings in request schemas.
This commit is contained in:
@@ -22,7 +22,7 @@ const RequestWithTorrentSchema = z.object({
|
||||
coverArtUrl: z.string().optional(),
|
||||
durationMinutes: z.number().optional(),
|
||||
releaseDate: z.string().optional(),
|
||||
rating: z.number().optional(),
|
||||
rating: z.number().nullable().optional(),
|
||||
}),
|
||||
torrent: z.object({
|
||||
guid: z.string(),
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* Component: Fetch E-book API
|
||||
* Documentation: documentation/integrations/ebook-sidecar.md
|
||||
*
|
||||
* Triggers e-book download for a completed request
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { downloadEbook } from '@/lib/services/ebook-scraper';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const DEBUG_ENABLED = process.env.LOG_LEVEL === 'debug';
|
||||
|
||||
/**
|
||||
* Sanitize path component (same logic as file-organizer)
|
||||
*/
|
||||
function sanitizePath(name: string): string {
|
||||
return (
|
||||
name
|
||||
.replace(/[<>:"/\\|?*]/g, '')
|
||||
.trim()
|
||||
.replace(/^\.+/, '')
|
||||
.replace(/\.+$/, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.slice(0, 200)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build target path (same logic as file-organizer)
|
||||
*/
|
||||
function buildTargetPath(
|
||||
baseDir: string,
|
||||
author: string,
|
||||
title: string,
|
||||
year?: number | null,
|
||||
asin?: string | null
|
||||
): string {
|
||||
const authorClean = sanitizePath(author);
|
||||
const titleClean = sanitizePath(title);
|
||||
|
||||
let folderName = titleClean;
|
||||
|
||||
if (year) {
|
||||
folderName = `${folderName} (${year})`;
|
||||
}
|
||||
|
||||
if (asin) {
|
||||
folderName = `${folderName} ${asin}`;
|
||||
}
|
||||
|
||||
return path.join(baseDir, authorClean, folderName);
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
return requireAuth(request, async (req: AuthenticatedRequest) => {
|
||||
return requireAdmin(req, async () => {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
// Check if e-book sidecar is enabled
|
||||
const ebookEnabledConfig = await prisma.configuration.findUnique({
|
||||
where: { key: 'ebook_sidecar_enabled' },
|
||||
});
|
||||
|
||||
if (ebookEnabledConfig?.value !== 'true') {
|
||||
return NextResponse.json(
|
||||
{ error: 'E-book sidecar feature is not enabled' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get the request with audiobook data
|
||||
const requestRecord = await prisma.request.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
audiobook: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!requestRecord) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Request not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if request is in completed state
|
||||
if (!['downloaded', 'available'].includes(requestRecord.status)) {
|
||||
return NextResponse.json(
|
||||
{ error: `Cannot fetch e-book for request in ${requestRecord.status} status` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const audiobook = requestRecord.audiobook;
|
||||
|
||||
// Get configuration
|
||||
const [mediaDirConfig, formatConfig, baseUrlConfig, flaresolverrConfig] = await Promise.all([
|
||||
prisma.configuration.findUnique({ where: { key: 'media_dir' } }),
|
||||
prisma.configuration.findUnique({ where: { key: 'ebook_sidecar_preferred_format' } }),
|
||||
prisma.configuration.findUnique({ where: { key: 'ebook_sidecar_base_url' } }),
|
||||
prisma.configuration.findUnique({ where: { key: 'ebook_sidecar_flaresolverr_url' } }),
|
||||
]);
|
||||
|
||||
const mediaDir = mediaDirConfig?.value || '/media/audiobooks';
|
||||
const preferredFormat = formatConfig?.value || 'epub';
|
||||
const baseUrl = baseUrlConfig?.value || 'https://annas-archive.li';
|
||||
const flaresolverrUrl = flaresolverrConfig?.value || undefined;
|
||||
|
||||
// Get year from AudibleCache if available
|
||||
let year: number | undefined;
|
||||
if (audiobook.audibleAsin) {
|
||||
const audibleCacheData = await prisma.audibleCache.findUnique({
|
||||
where: { asin: audiobook.audibleAsin },
|
||||
select: { releaseDate: true },
|
||||
});
|
||||
if (audibleCacheData?.releaseDate) {
|
||||
year = new Date(audibleCacheData.releaseDate).getFullYear();
|
||||
}
|
||||
}
|
||||
|
||||
// Build target path
|
||||
const targetPath = buildTargetPath(
|
||||
mediaDir,
|
||||
audiobook.author,
|
||||
audiobook.title,
|
||||
year,
|
||||
audiobook.audibleAsin
|
||||
);
|
||||
|
||||
if (DEBUG_ENABLED) {
|
||||
console.log(`[FetchEbook] Request: ${id}, Title: "${audiobook.title}", Author: "${audiobook.author}"`);
|
||||
console.log(`[FetchEbook] Target path: ${targetPath}`);
|
||||
console.log(`[FetchEbook] Config: format=${preferredFormat}, baseUrl=${baseUrl}, flaresolverr=${flaresolverrUrl || 'none'}`);
|
||||
}
|
||||
|
||||
// Check if target directory exists
|
||||
try {
|
||||
await fs.access(targetPath);
|
||||
} catch {
|
||||
if (DEBUG_ENABLED) {
|
||||
console.log(`[FetchEbook] Target directory not found: ${targetPath}`);
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: 'Audiobook directory not found. Was the audiobook properly organized?' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Download e-book
|
||||
const result = await downloadEbook(
|
||||
audiobook.audibleAsin || '',
|
||||
audiobook.title,
|
||||
audiobook.author,
|
||||
targetPath,
|
||||
preferredFormat,
|
||||
baseUrl,
|
||||
undefined, // No logger in API context
|
||||
flaresolverrUrl
|
||||
);
|
||||
|
||||
if (result.success) {
|
||||
console.log(`[FetchEbook] Success: ${result.filePath ? path.basename(result.filePath) : 'unknown'} for "${audiobook.title}"`);
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `E-book downloaded: ${result.filePath ? path.basename(result.filePath) : 'unknown'}`,
|
||||
format: result.format,
|
||||
});
|
||||
} else {
|
||||
console.log(`[FetchEbook] Failed for "${audiobook.title}": ${result.error}`);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: result.error || 'E-book download failed',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[FetchEbook] Unexpected error:', error instanceof Error ? error.message : error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -123,30 +123,18 @@ export async function POST(
|
||||
author: requestRecord.audiobook.author,
|
||||
}, indexerPriorities, flagConfigs);
|
||||
|
||||
// Dual threshold filtering:
|
||||
// 1. Base score must be >= 50 (quality minimum)
|
||||
// 2. Final score must be >= 50 (not disqualified by negative bonuses)
|
||||
const filteredResults = rankedResults.filter(result =>
|
||||
result.score >= 50 && result.finalScore >= 50
|
||||
);
|
||||
|
||||
const disqualifiedByNegativeBonus = rankedResults.filter(result =>
|
||||
result.score >= 50 && result.finalScore < 50
|
||||
).length;
|
||||
|
||||
console.log(`[InteractiveSearch] Ranked ${rankedResults.length} results, ${filteredResults.length} above threshold (50/100 base + final)`);
|
||||
if (disqualifiedByNegativeBonus > 0) {
|
||||
console.log(`[InteractiveSearch] ${disqualifiedByNegativeBonus} torrents disqualified by negative flag bonuses`);
|
||||
}
|
||||
// No threshold filtering for interactive search - show all results
|
||||
// User can see scores and make their own decision
|
||||
console.log(`[InteractiveSearch] Ranked ${rankedResults.length} results (no threshold filter - user decides)`);
|
||||
|
||||
// Log top 3 results with detailed score breakdown for debugging
|
||||
const top3 = filteredResults.slice(0, 3);
|
||||
const top3 = rankedResults.slice(0, 3);
|
||||
if (top3.length > 0) {
|
||||
console.log(`[InteractiveSearch] ==================== RANKING DEBUG ====================`);
|
||||
console.log(`[InteractiveSearch] Search Query: "${searchQuery}"`);
|
||||
console.log(`[InteractiveSearch] Requested Title (for ranking): "${requestRecord.audiobook.title}"`);
|
||||
console.log(`[InteractiveSearch] Requested Author (for ranking): "${requestRecord.audiobook.author}"`);
|
||||
console.log(`[InteractiveSearch] Top ${top3.length} results (out of ${filteredResults.length} above threshold):`);
|
||||
console.log(`[InteractiveSearch] Top ${top3.length} results (out of ${rankedResults.length} total):`);
|
||||
console.log(`[InteractiveSearch] --------------------------------------------------------`);
|
||||
top3.forEach((result, index) => {
|
||||
console.log(`[InteractiveSearch] ${index + 1}. "${result.title}"`);
|
||||
@@ -177,7 +165,7 @@ export async function POST(
|
||||
}
|
||||
|
||||
// Add rank position to each result
|
||||
const resultsWithRank = filteredResults.map((result, index) => ({
|
||||
const resultsWithRank = rankedResults.map((result, index) => ({
|
||||
...result,
|
||||
rank: index + 1,
|
||||
}));
|
||||
@@ -185,9 +173,9 @@ export async function POST(
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
results: resultsWithRank,
|
||||
message: filteredResults.length > 0
|
||||
? `Found ${filteredResults.length} quality matches`
|
||||
: 'No quality matches found',
|
||||
message: rankedResults.length > 0
|
||||
? `Found ${rankedResults.length} results`
|
||||
: 'No results found',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to perform interactive search:', error);
|
||||
|
||||
@@ -20,7 +20,7 @@ const CreateRequestSchema = z.object({
|
||||
coverArtUrl: z.string().optional(),
|
||||
durationMinutes: z.number().optional(),
|
||||
releaseDate: z.string().optional(),
|
||||
rating: z.number().optional(),
|
||||
rating: z.number().nullable().optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user