feat: conversion CBR → CBZ via job asynchrone
Ajoute la possibilité de convertir un livre CBR en CBZ depuis le backoffice. La conversion est sécurisée : le CBR original n'est supprimé qu'après vérification du CBZ généré et mise à jour de la base de données. - parsers: nouvelle fn `convert_cbr_to_cbz` (unar extract → zip pack → vérification → rename atomique) - api: `POST /books/:id/convert` crée un job `cbr_to_cbz` (vérifie format CBR, détecte collision) - indexer: nouveau `converter.rs` dispatché depuis `job.rs` - backoffice: bouton "Convert to CBZ" sur la page détail (visible si CBR), label dans JobRow - migrations: colonne `book_id` sur `index_jobs` + type `cbr_to_cbz` dans le check constraint Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17
apps/backoffice/app/api/books/[bookId]/convert/route.ts
Normal file
17
apps/backoffice/app/api/books/[bookId]/convert/route.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { convertBook } from "@/lib/api";
|
||||
|
||||
export async function POST(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ bookId: string }> }
|
||||
) {
|
||||
const { bookId } = await params;
|
||||
try {
|
||||
const data = await convertBook(bookId);
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to start conversion";
|
||||
const status = message.includes("409") ? 409 : 500;
|
||||
return NextResponse.json({ error: message }, { status });
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { fetchLibraries, getBookCoverUrl, BookDto, apiFetch } from "../../../lib/api";
|
||||
import { BookPreview } from "../../components/BookPreview";
|
||||
import { ConvertButton } from "../../components/ConvertButton";
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
@@ -115,7 +116,10 @@ export default async function BookDetailPage({
|
||||
{book.file_format && (
|
||||
<div className="flex items-center justify-between py-2 border-b border-border">
|
||||
<span className="text-sm text-muted-foreground">File Format:</span>
|
||||
<span className="text-sm text-foreground">{book.file_format.toUpperCase()}</span>
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-sm text-foreground">{book.file_format.toUpperCase()}</span>
|
||||
{book.file_format === "cbr" && <ConvertButton bookId={book.id} />}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
71
apps/backoffice/app/components/ConvertButton.tsx
Normal file
71
apps/backoffice/app/components/ConvertButton.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { Button } from "./ui";
|
||||
|
||||
interface ConvertButtonProps {
|
||||
bookId: string;
|
||||
}
|
||||
|
||||
type ConvertState =
|
||||
| { type: "idle" }
|
||||
| { type: "loading" }
|
||||
| { type: "success"; jobId: string }
|
||||
| { type: "error"; message: string };
|
||||
|
||||
export function ConvertButton({ bookId }: ConvertButtonProps) {
|
||||
const [state, setState] = useState<ConvertState>({ type: "idle" });
|
||||
|
||||
const handleConvert = async () => {
|
||||
setState({ type: "loading" });
|
||||
try {
|
||||
const res = await fetch(`/api/books/${bookId}/convert`, { method: "POST" });
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({ error: res.statusText }));
|
||||
setState({ type: "error", message: body.error || "Conversion failed" });
|
||||
return;
|
||||
}
|
||||
const job = await res.json();
|
||||
setState({ type: "success", jobId: job.id });
|
||||
} catch (err) {
|
||||
setState({ type: "error", message: err instanceof Error ? err.message : "Unknown error" });
|
||||
}
|
||||
};
|
||||
|
||||
if (state.type === "success") {
|
||||
return (
|
||||
<div className="flex items-center gap-2 text-sm text-success">
|
||||
<span>Conversion started.</span>
|
||||
<Link href={`/jobs/${state.jobId}`} className="text-primary hover:underline font-medium">
|
||||
View job →
|
||||
</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (state.type === "error") {
|
||||
return (
|
||||
<div className="flex flex-col gap-1">
|
||||
<span className="text-sm text-destructive">{state.message}</span>
|
||||
<button
|
||||
className="text-xs text-muted-foreground hover:underline text-left"
|
||||
onClick={() => setState({ type: "idle" })}
|
||||
>
|
||||
Dismiss
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={handleConvert}
|
||||
disabled={state.type === "loading"}
|
||||
>
|
||||
{state.type === "loading" ? "Converting…" : "Convert to CBZ"}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
@@ -93,7 +93,9 @@ export function JobRow({ job, libraryName, highlighted, onCancel, formatDate, fo
|
||||
<td className="px-4 py-3 text-sm text-foreground">
|
||||
{job.library_id ? libraryName || job.library_id.slice(0, 8) : "—"}
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm text-foreground">{job.type}</td>
|
||||
<td className="px-4 py-3 text-sm text-foreground">
|
||||
{job.type === "cbr_to_cbz" ? "CBR → CBZ" : job.type}
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<StatusBadge status={job.status} />
|
||||
|
||||
Reference in New Issue
Block a user