mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
File rename templates & admin torrent approval
Add support for admin-driven interactive torrent selection and a file rename/template feature. Integrates an InteractiveTorrentSearchModal into the pending-approval admin UI, adds an admin approve flow that accepts an admin-selected torrent, and surfaces user/admin-selected torrent details in the UI. Introduces fileRenameEnabled and fileRenameTemplate settings (API + UI), persists them to configuration, and clears related caches. Pass renameConfig through the organize/organizeEbook flows and implement renaming in the FileOrganizer (single/multi-file handling). Enhance path-template utilities with conditional block resolution, filename-template validation, mock filename previews, and a buildRenamedFilename helper. Update tests to cover conditional templates and filename preview behavior.
This commit is contained in:
@@ -14,6 +14,7 @@ const logger = RMABLogger.create('API.Admin.Requests.Approve');
|
||||
|
||||
const ApprovalActionSchema = z.object({
|
||||
action: z.enum(['approve', 'deny']),
|
||||
selectedTorrent: z.any().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -37,8 +38,8 @@ export async function POST(
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
|
||||
// Validate action
|
||||
const { action } = ApprovalActionSchema.parse(body);
|
||||
// Validate action and optional admin-selected torrent
|
||||
const { action, selectedTorrent: adminSelectedTorrent } = ApprovalActionSchema.parse(body);
|
||||
|
||||
// Fetch the request
|
||||
const existingRequest = await prisma.request.findUnique({
|
||||
@@ -78,12 +79,15 @@ export async function POST(
|
||||
const jobQueue = getJobQueueService();
|
||||
const isEbookRequest = existingRequest.type === 'ebook';
|
||||
|
||||
// Check if request has a pre-selected torrent (from interactive search)
|
||||
if (existingRequest.selectedTorrent) {
|
||||
const selectedTorrent = existingRequest.selectedTorrent as any;
|
||||
// Use admin-provided torrent (from admin interactive search) or fall back to user's pre-selected torrent
|
||||
const effectiveTorrent = adminSelectedTorrent || existingRequest.selectedTorrent;
|
||||
|
||||
// User pre-selected a specific torrent - download that torrent directly
|
||||
logger.info(`Request ${id} has pre-selected torrent, starting download`, {
|
||||
if (effectiveTorrent) {
|
||||
const selectedTorrent = effectiveTorrent as any;
|
||||
const torrentSource = adminSelectedTorrent ? 'admin' : 'user';
|
||||
|
||||
// Download the selected torrent directly
|
||||
logger.info(`Request ${id} has ${torrentSource}-selected torrent, starting download`, {
|
||||
requestId: id,
|
||||
userId: existingRequest.userId,
|
||||
adminId: req.user.sub,
|
||||
@@ -167,17 +171,20 @@ export async function POST(
|
||||
logger.error('Failed to queue notification', { error: error instanceof Error ? error.message : String(error) });
|
||||
});
|
||||
|
||||
logger.info(`Request ${id} approved by admin ${req.user.sub}, downloading pre-selected torrent`, {
|
||||
logger.info(`Request ${id} approved by admin ${req.user.sub}, downloading ${torrentSource}-selected torrent`, {
|
||||
requestId: id,
|
||||
userId: updatedRequest.userId,
|
||||
audiobookTitle: existingRequest.audiobook.title,
|
||||
adminId: req.user.sub,
|
||||
type: existingRequest.type,
|
||||
torrentSource,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Request approved and download started with pre-selected torrent',
|
||||
message: adminSelectedTorrent
|
||||
? 'Request approved and download started with admin-selected torrent'
|
||||
: 'Request approved and download started with pre-selected torrent',
|
||||
request: updatedRequest,
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -15,7 +15,7 @@ export async function PUT(request: NextRequest) {
|
||||
return requireAuth(request, async (req: AuthenticatedRequest) => {
|
||||
return requireAdmin(req, async () => {
|
||||
try {
|
||||
const { downloadDir, mediaDir, audiobookPathTemplate, ebookPathTemplate, metadataTaggingEnabled, chapterMergingEnabled } = await request.json();
|
||||
const { downloadDir, mediaDir, audiobookPathTemplate, ebookPathTemplate, metadataTaggingEnabled, chapterMergingEnabled, fileRenameEnabled, fileRenameTemplate } = await request.json();
|
||||
|
||||
if (!downloadDir || !mediaDir) {
|
||||
return NextResponse.json(
|
||||
@@ -97,6 +97,32 @@ export async function PUT(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
|
||||
// Update file rename setting
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'file_rename_enabled' },
|
||||
update: { value: String(fileRenameEnabled ?? false) },
|
||||
create: {
|
||||
key: 'file_rename_enabled',
|
||||
value: String(fileRenameEnabled ?? false),
|
||||
category: 'automation',
|
||||
description: 'Rename audio and ebook files using a custom naming template during organization',
|
||||
},
|
||||
});
|
||||
|
||||
// Update file rename template
|
||||
if (fileRenameTemplate !== undefined) {
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'file_rename_template' },
|
||||
update: { value: fileRenameTemplate },
|
||||
create: {
|
||||
key: 'file_rename_template',
|
||||
value: fileRenameTemplate,
|
||||
category: 'automation',
|
||||
description: 'Template for renaming audio and ebook files during organization',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
logger.info('Paths settings updated');
|
||||
|
||||
// Clear config cache for all updated keys so services get fresh values
|
||||
@@ -107,6 +133,8 @@ export async function PUT(request: NextRequest) {
|
||||
configService.clearCache('ebook_path_template');
|
||||
configService.clearCache('metadata_tagging_enabled');
|
||||
configService.clearCache('chapter_merging_enabled');
|
||||
configService.clearCache('file_rename_enabled');
|
||||
configService.clearCache('file_rename_template');
|
||||
|
||||
// Invalidate all download client singletons to force reload of download_dir
|
||||
const { invalidateDownloadClientManager } = await import('@/lib/services/download-client-manager.service');
|
||||
|
||||
@@ -128,6 +128,8 @@ export async function GET(request: NextRequest) {
|
||||
ebookPathTemplate: configMap.get('ebook_path_template') || configMap.get('audiobook_path_template') || '{author}/{title} {asin}',
|
||||
metadataTaggingEnabled: configMap.get('metadata_tagging_enabled') === 'true',
|
||||
chapterMergingEnabled: configMap.get('chapter_merging_enabled') === 'true',
|
||||
fileRenameEnabled: configMap.get('file_rename_enabled') === 'true',
|
||||
fileRenameTemplate: configMap.get('file_rename_template') || '{title}',
|
||||
},
|
||||
ebook: {
|
||||
// New granular source toggles (with migration from legacy ebook_sidecar_enabled)
|
||||
|
||||
@@ -67,8 +67,8 @@ export async function POST(
|
||||
);
|
||||
}
|
||||
|
||||
// Check if request is awaiting approval
|
||||
if (requestRecord.status === 'awaiting_approval') {
|
||||
// Check if request is awaiting approval (admins can still search to override the user's selection)
|
||||
if (requestRecord.status === 'awaiting_approval' && req.user.role !== 'admin') {
|
||||
return NextResponse.json(
|
||||
{ error: 'AwaitingApproval', message: 'This request is awaiting admin approval. You cannot search for torrents until it is approved.' },
|
||||
{ status: 403 }
|
||||
|
||||
Reference in New Issue
Block a user