mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Add custom search terms & retry download (admin)
Add support for per-request custom search terms and an admin retry-download flow. - DB/schema: add custom_search_terms column via Prisma migration and schema update. - Admin UI: new AdjustSearchTermsModal component and UI badges to show custom search status; RequestActionsDropdown and RecentRequestsTable updated to surface adjust/retry actions. - API: new PATCH /api/admin/requests/[id]/search-terms to set/clear custom terms (optionally trigger a new search) and new POST /api/admin/requests/[id]/retry-download to resume monitoring or re-add downloads using DownloadHistory metadata. - Behavior: interactive search now prefers customSearchTerms when present; manual import exposes cleanupSource option to organize job; admin requests listing returns downloadAttempts and customSearchTerms. - UX: add SectionToolbar, LoadMoreBar and HideAvailableToggle components and wire hide-available preference across home, search, author and series pages; authors/series endpoints/page handlers gain pagination metadata. - Misc: add connection-errors util and update related processors/services and tests to cover the new flows. These changes enable admins to override search terms per request, trigger searches from the admin UI, and retry failed downloads more robustly.
This commit is contained in:
@@ -22,7 +22,7 @@ import { removeEmptyParentDirectories } from '../utils/cleanup-helpers';
|
||||
* Handles both audiobook and ebook request types with appropriate branching
|
||||
*/
|
||||
export async function processOrganizeFiles(payload: OrganizeFilesPayload): Promise<any> {
|
||||
const { requestId, audiobookId, downloadPath, jobId } = payload;
|
||||
const { requestId, audiobookId, downloadPath, jobId, cleanupSource } = payload;
|
||||
|
||||
const logger = RMABLogger.forJob(jobId, 'OrganizeFiles');
|
||||
|
||||
@@ -264,6 +264,11 @@ export async function processOrganizeFiles(payload: OrganizeFilesPayload): Promi
|
||||
// Cleanup downloads if configured (uses IDownloadClient.postProcess for client-specific cleanup)
|
||||
await cleanupDownloadAfterOrganize(requestId, downloadPath, configService, jobId, logger);
|
||||
|
||||
// Cleanup source files if requested (manual import feature)
|
||||
if (cleanupSource) {
|
||||
await cleanupSourceAfterOrganize(downloadPath, configService, jobId, logger);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Files organized successfully',
|
||||
@@ -467,7 +472,7 @@ async function processEbookOrganization(
|
||||
request: { id: string; userId: string; type: string; user: { plexUsername: string | null } },
|
||||
logger: RMABLogger
|
||||
): Promise<any> {
|
||||
const { requestId, audiobookId, downloadPath, jobId } = payload;
|
||||
const { requestId, audiobookId, downloadPath, jobId, cleanupSource } = payload;
|
||||
|
||||
logger.info(`Processing ebook organization for request ${requestId}`);
|
||||
|
||||
@@ -726,6 +731,11 @@ async function processEbookOrganization(
|
||||
// Cleanup downloads if configured (uses IDownloadClient.postProcess for client-specific cleanup)
|
||||
await cleanupDownloadAfterOrganize(requestId, downloadPath, configService, jobId, logger);
|
||||
|
||||
// Cleanup source files if requested (manual import feature)
|
||||
if (cleanupSource) {
|
||||
await cleanupSourceAfterOrganize(downloadPath, configService, jobId, logger);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Ebook organized successfully',
|
||||
@@ -1003,6 +1013,68 @@ async function cleanupDownloadAfterOrganize(
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// SOURCE FILE CLEANUP (MANUAL IMPORT)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Delete source files after successful manual import.
|
||||
* Non-fatal: logs a warning on failure but does not fail the job.
|
||||
* Files are already safely copied to the media library at this point.
|
||||
*/
|
||||
async function cleanupSourceAfterOrganize(
|
||||
downloadPath: string,
|
||||
configService: any,
|
||||
jobId: string | undefined,
|
||||
logger: RMABLogger
|
||||
): Promise<void> {
|
||||
try {
|
||||
const fs = await import('fs/promises');
|
||||
|
||||
logger.info(`Cleaning up source files: ${downloadPath}`);
|
||||
|
||||
const stats = await fs.stat(downloadPath);
|
||||
if (stats.isDirectory()) {
|
||||
await fs.rm(downloadPath, { recursive: true, force: true });
|
||||
logger.info(`Removed source directory: ${downloadPath}`);
|
||||
} else {
|
||||
await fs.unlink(downloadPath);
|
||||
logger.info(`Removed source file: ${downloadPath}`);
|
||||
}
|
||||
|
||||
// Determine boundary path based on download path prefix
|
||||
const BOOKDROP_PATH = '/bookdrop';
|
||||
const downloadDir = await configService.get('download_dir') || '/downloads';
|
||||
const mediaDir = await configService.get('media_dir') || '/media';
|
||||
|
||||
let boundaryPath = downloadDir;
|
||||
if (downloadPath.startsWith(BOOKDROP_PATH)) {
|
||||
boundaryPath = BOOKDROP_PATH;
|
||||
} else if (downloadPath.startsWith(mediaDir)) {
|
||||
boundaryPath = mediaDir;
|
||||
}
|
||||
|
||||
const cleanupResult = await removeEmptyParentDirectories(downloadPath, {
|
||||
boundaryPath,
|
||||
logContext: jobId ? { jobId, context: 'CleanupSourceParents' } : undefined,
|
||||
});
|
||||
|
||||
if (cleanupResult.removedDirectories.length > 0) {
|
||||
logger.info(`Cleaned up ${cleanupResult.removedDirectories.length} empty parent directories`);
|
||||
}
|
||||
} catch (error) {
|
||||
// Non-fatal - files are already safely in the media library
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
||||
logger.info(`Source path already deleted: ${downloadPath}`);
|
||||
} else {
|
||||
logger.warn(
|
||||
`Failed to cleanup source files: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
{ error: error instanceof Error ? error.stack : undefined }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// HELPER FUNCTIONS
|
||||
// =========================================================================
|
||||
|
||||
Reference in New Issue
Block a user