Add e-book sidecar integration and improve request handling

Introduces optional e-book sidecar downloads from Anna's Archive, including admin UI, settings API, FlareSolverr integration, and documentation. Enhances request creation logic to prevent duplicate downloads by checking for 'downloaded' and 'available' statuses, updates UI to reflect processing state, and adds SABnzbd support to download and cleanup flows. Also updates ranking algorithm documentation and improves cache invalidation for recent requests.
This commit is contained in:
kikootwo
2026-01-07 17:19:42 -05:00
parent 24ea53bd2f
commit 95c25ff73a
26 changed files with 1968 additions and 116 deletions
+84
View File
@@ -0,0 +1,84 @@
/**
* Component: E-book Sidecar Settings API
* Documentation: documentation/integrations/ebook-sidecar.md
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
export async function PUT(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
// Parse request body
const { enabled, format, baseUrl, flaresolverrUrl } = await request.json();
// Validate format
const validFormats = ['epub', 'pdf', 'mobi', 'azw3', 'any'];
if (format && !validFormats.includes(format)) {
return NextResponse.json(
{ error: `Invalid format. Must be one of: ${validFormats.join(', ')}` },
{ status: 400 }
);
}
// Validate baseUrl (basic check)
if (baseUrl && !baseUrl.startsWith('http')) {
return NextResponse.json(
{ error: 'Base URL must start with http:// or https://' },
{ status: 400 }
);
}
// Validate flaresolverrUrl if provided
if (flaresolverrUrl && !flaresolverrUrl.startsWith('http')) {
return NextResponse.json(
{ error: 'FlareSolverr URL must start with http:// or https://' },
{ status: 400 }
);
}
// Save configuration
const { getConfigService } = await import('@/lib/services/config.service');
const configService = getConfigService();
const configs = [
{
key: 'ebook_sidecar_enabled',
value: enabled ? 'true' : 'false',
category: 'ebook',
description: 'Enable e-book sidecar downloads from Annas Archive',
},
{
key: 'ebook_sidecar_preferred_format',
value: format || 'epub',
category: 'ebook',
description: 'Preferred e-book format',
},
{
key: 'ebook_sidecar_base_url',
value: baseUrl || 'https://annas-archive.li',
category: 'ebook',
description: 'Base URL for Annas Archive',
},
{
key: 'ebook_sidecar_flaresolverr_url',
value: flaresolverrUrl || '',
category: 'ebook',
description: 'FlareSolverr URL for bypassing Cloudflare protection',
},
];
await configService.setMany(configs);
return NextResponse.json({ success: true });
} catch (error) {
console.error('Failed to save e-book settings:', error);
return NextResponse.json(
{ error: 'Failed to save settings' },
{ status: 500 }
);
}
});
});
}
@@ -0,0 +1,45 @@
/**
* Component: FlareSolverr Connection Test API
* Documentation: documentation/integrations/ebook-sidecar.md
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
import { testFlareSolverrConnection } from '@/lib/services/ebook-scraper';
export async function POST(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
const { url } = await request.json();
if (!url) {
return NextResponse.json(
{ error: 'FlareSolverr URL is required' },
{ status: 400 }
);
}
if (!url.startsWith('http')) {
return NextResponse.json(
{ error: 'URL must start with http:// or https://' },
{ status: 400 }
);
}
const result = await testFlareSolverrConnection(url);
return NextResponse.json(result);
} catch (error) {
console.error('FlareSolverr test failed:', error);
return NextResponse.json(
{
success: false,
message: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
);
}
});
});
}
+6
View File
@@ -82,6 +82,12 @@ export async function GET(request: NextRequest) {
mediaDir: configMap.get('media_dir') || '/media/audiobooks',
metadataTaggingEnabled: configMap.get('metadata_tagging_enabled') === 'true',
},
ebook: {
enabled: configMap.get('ebook_sidecar_enabled') === 'true',
preferredFormat: configMap.get('ebook_sidecar_preferred_format') || 'epub',
baseUrl: configMap.get('ebook_sidecar_base_url') || 'https://annas-archive.li',
flaresolverrUrl: configMap.get('ebook_sidecar_flaresolverr_url') || '',
},
general: {
appName: configMap.get('app_name') || 'ReadMeABook',
allowRegistrations: configMap.get('allow_registrations') === 'true',
@@ -24,6 +24,8 @@ export async function POST(request: NextRequest) {
localPath,
} = await request.json();
console.log('[TestDownloadClient] Received request:', { type, url, hasUsername: !!username, hasPassword: !!password });
if (!type || !url) {
return NextResponse.json(
{ success: false, error: 'Type and URL are required' },
@@ -59,6 +61,7 @@ export async function POST(request: NextRequest) {
let version: string | undefined;
if (type === 'qbittorrent') {
console.log('[TestDownloadClient] Testing qBittorrent connection');
if (!username || !actualPassword) {
return NextResponse.json(
{ success: false, error: 'Username and password are required for qBittorrent' },
@@ -74,6 +77,7 @@ export async function POST(request: NextRequest) {
disableSSLVerify || false
);
} else if (type === 'sabnzbd') {
console.log('[TestDownloadClient] Testing SABnzbd connection');
if (!actualPassword) {
return NextResponse.json(
{ success: false, error: 'API key (password) is required for SABnzbd' },