feat: Initial commit - Base application with Next.js - Configuration, Auth, Library navigation, CBZ/CBR reader, Cache, Responsive design
This commit is contained in:
60
src/app/api/komga/books/[bookId]/pages/[pageNumber]/route.ts
Normal file
60
src/app/api/komga/books/[bookId]/pages/[pageNumber]/route.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { bookId: string; pageNumber: string } }
|
||||
) {
|
||||
try {
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const configCookie = cookies().get("komgaCredentials");
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = JSON.parse(atob(configCookie.value));
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!config.credentials?.username || !config.credentials?.password) {
|
||||
return NextResponse.json({ error: "Credentials Komga manquants" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Appel à l'API Komga
|
||||
const response = await fetch(
|
||||
`${config.serverUrl}/api/v1/books/${params.bookId}/pages/${params.pageNumber}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${config.credentials.username}:${config.credentials.password}`
|
||||
).toString("base64")}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération de la page" },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
// Récupérer le type MIME de l'image
|
||||
const contentType = response.headers.get("content-type");
|
||||
const imageBuffer = await response.arrayBuffer();
|
||||
|
||||
// Retourner l'image avec le bon type MIME
|
||||
return new NextResponse(imageBuffer, {
|
||||
headers: {
|
||||
"Content-Type": contentType || "image/jpeg",
|
||||
"Cache-Control": "public, max-age=31536000, immutable",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de la page:", error);
|
||||
return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
104
src/app/api/komga/books/[bookId]/route.ts
Normal file
104
src/app/api/komga/books/[bookId]/route.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { config } from "@/lib/config";
|
||||
import { serverCacheService } from "@/lib/services/server-cache.service";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { bookId: string } }) {
|
||||
try {
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const cookieStore = cookies();
|
||||
const configCookie = cookieStore.get("komgaCredentials");
|
||||
console.log("API Books - Cookie komgaCredentials:", configCookie?.value);
|
||||
|
||||
if (!configCookie) {
|
||||
console.log("API Books - Cookie komgaCredentials manquant");
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let komgaConfig;
|
||||
try {
|
||||
komgaConfig = JSON.parse(atob(configCookie.value));
|
||||
console.log("API Books - Config décodée:", {
|
||||
serverUrl: komgaConfig.serverUrl,
|
||||
hasCredentials: !!komgaConfig.credentials,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("API Books - Erreur de décodage du cookie:", error);
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!komgaConfig.credentials?.username || !komgaConfig.credentials?.password) {
|
||||
console.log("API Books - Credentials manquants dans la config");
|
||||
return NextResponse.json({ error: "Credentials Komga manquants" }, { status: 401 });
|
||||
}
|
||||
|
||||
const auth = Buffer.from(
|
||||
`${komgaConfig.credentials.username}:${komgaConfig.credentials.password}`
|
||||
).toString("base64");
|
||||
|
||||
console.log("API Books - Appel à l'API Komga pour le livre:", params.bookId);
|
||||
|
||||
// Clé de cache unique pour ce livre
|
||||
const cacheKey = `book-${params.bookId}`;
|
||||
|
||||
// Fonction pour récupérer les données du livre
|
||||
const fetchBookData = async () => {
|
||||
// Récupération des détails du tome
|
||||
const bookResponse = await fetch(`${komgaConfig.serverUrl}/api/v1/books/${params.bookId}`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${auth}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!bookResponse.ok) {
|
||||
console.error("API Books - Erreur de l'API Komga (book):", {
|
||||
status: bookResponse.status,
|
||||
statusText: bookResponse.statusText,
|
||||
});
|
||||
throw new Error("Erreur lors de la récupération des détails du tome");
|
||||
}
|
||||
|
||||
const book = await bookResponse.json();
|
||||
|
||||
// Récupération des pages du tome
|
||||
const pagesResponse = await fetch(
|
||||
`${komgaConfig.serverUrl}/api/v1/books/${params.bookId}/pages`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Basic ${auth}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!pagesResponse.ok) {
|
||||
console.error("API Books - Erreur de l'API Komga (pages):", {
|
||||
status: pagesResponse.status,
|
||||
statusText: pagesResponse.statusText,
|
||||
});
|
||||
throw new Error("Erreur lors de la récupération des pages du tome");
|
||||
}
|
||||
|
||||
const pages = await pagesResponse.json();
|
||||
|
||||
// Retourner les données combinées
|
||||
return {
|
||||
book,
|
||||
pages: pages.map((page: any) => page.number),
|
||||
};
|
||||
};
|
||||
|
||||
// Récupérer les données du cache ou faire l'appel API
|
||||
const data = await serverCacheService.getOrSet(cacheKey, fetchBookData, 5 * 60); // Cache de 5 minutes
|
||||
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("API Books - Erreur:", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
error instanceof Error ? error.message : "Erreur lors de la récupération des données",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
54
src/app/api/komga/images/books/[bookId]/thumbnail/route.ts
Normal file
54
src/app/api/komga/images/books/[bookId]/thumbnail/route.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { bookId: string } }) {
|
||||
try {
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const configCookie = cookies().get("komgaCredentials");
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = JSON.parse(atob(configCookie.value));
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!config.credentials?.username || !config.credentials?.password) {
|
||||
return NextResponse.json({ error: "Credentials Komga manquants" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Appel à l'API Komga
|
||||
const response = await fetch(`${config.serverUrl}/api/v1/books/${params.bookId}/thumbnail`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${config.credentials.username}:${config.credentials.password}`
|
||||
).toString("base64")}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération de l'image" },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
// Récupérer le type MIME de l'image
|
||||
const contentType = response.headers.get("content-type");
|
||||
const imageBuffer = await response.arrayBuffer();
|
||||
|
||||
// Retourner l'image avec le bon type MIME
|
||||
return new NextResponse(imageBuffer, {
|
||||
headers: {
|
||||
"Content-Type": contentType || "image/jpeg",
|
||||
"Cache-Control": "public, max-age=31536000, immutable",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de l'image:", error);
|
||||
return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { seriesId: string } }) {
|
||||
try {
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const configCookie = cookies().get("komga_credentials");
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = JSON.parse(atob(configCookie.value));
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Appel à l'API Komga
|
||||
const response = await fetch(`${config.serverUrl}/api/v1/series/${params.seriesId}/thumbnail`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${config.credentials?.username}:${config.credentials?.password}`
|
||||
).toString("base64")}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération de l'image" },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
// Récupérer le type MIME de l'image
|
||||
const contentType = response.headers.get("content-type");
|
||||
const imageBuffer = await response.arrayBuffer();
|
||||
|
||||
// Retourner l'image avec le bon type MIME
|
||||
return new NextResponse(imageBuffer, {
|
||||
headers: {
|
||||
"Content-Type": contentType || "image/jpeg",
|
||||
"Cache-Control": "public, max-age=31536000, immutable",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de l'image:", error);
|
||||
return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
72
src/app/api/komga/libraries/[libraryId]/series/route.ts
Normal file
72
src/app/api/komga/libraries/[libraryId]/series/route.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { serverCacheService } from "@/lib/services/server-cache.service";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { libraryId: string } }) {
|
||||
try {
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const configCookie = cookies().get("komgaCredentials");
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = JSON.parse(atob(configCookie.value));
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!config.credentials?.username || !config.credentials?.password) {
|
||||
return NextResponse.json({ error: "Credentials Komga manquants" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Récupérer les paramètres de pagination depuis l'URL
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = searchParams.get("page") || "0";
|
||||
const size = searchParams.get("size") || "20";
|
||||
|
||||
// Clé de cache unique pour cette page de séries
|
||||
const cacheKey = `library-${params.libraryId}-series-${page}-${size}`;
|
||||
|
||||
// Fonction pour récupérer les séries
|
||||
const fetchSeries = async () => {
|
||||
const response = await fetch(
|
||||
`${config.serverUrl}/api/v1/series?library_id=${params.libraryId}&page=${page}&size=${size}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${config.credentials.username}:${config.credentials.password}`
|
||||
).toString("base64")}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(
|
||||
JSON.stringify({
|
||||
error: "Erreur lors de la récupération des séries",
|
||||
details: errorData,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
};
|
||||
|
||||
// Récupérer les données du cache ou faire l'appel API
|
||||
const data = await serverCacheService.getOrSet(cacheKey, fetchSeries, 5 * 60); // Cache de 5 minutes
|
||||
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération des séries:", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Erreur serveur",
|
||||
details: error instanceof Error ? error.message : "Erreur inconnue",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
64
src/app/api/komga/libraries/route.ts
Normal file
64
src/app/api/komga/libraries/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { AuthConfig } from "@/types/auth";
|
||||
import { serverCacheService } from "@/lib/services/server-cache.service";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Vérifier l'authentification de l'utilisateur
|
||||
const userCookie = cookies().get("komgaUser");
|
||||
if (!userCookie) {
|
||||
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const userData = JSON.parse(atob(userCookie.value));
|
||||
if (!userData.authenticated) {
|
||||
throw new Error("User not authenticated");
|
||||
}
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Session invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const configCookie = cookies().get("komgaCredentials");
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let config: AuthConfig;
|
||||
try {
|
||||
config = JSON.parse(atob(configCookie.value));
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Clé de cache unique pour les bibliothèques
|
||||
const cacheKey = "libraries";
|
||||
|
||||
// Fonction pour récupérer les bibliothèques
|
||||
const fetchLibraries = async () => {
|
||||
const response = await fetch(`${config.serverUrl}/api/v1/libraries`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${config.credentials?.username}:${config.credentials?.password}`
|
||||
).toString("base64")}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors de la récupération des bibliothèques");
|
||||
}
|
||||
|
||||
return response.json();
|
||||
};
|
||||
|
||||
// Récupérer les données du cache ou faire l'appel API
|
||||
const data = await serverCacheService.getOrSet(cacheKey, fetchLibraries, 5 * 60); // Cache de 5 minutes
|
||||
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération des bibliothèques:", error);
|
||||
return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
46
src/app/api/komga/series/[seriesId]/read-progress/route.ts
Normal file
46
src/app/api/komga/series/[seriesId]/read-progress/route.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { serverCacheService } from "@/services/serverCacheService";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { seriesId: string } }) {
|
||||
const configCookie = cookies().get("komgaCredentials");
|
||||
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const config = JSON.parse(atob(configCookie.value));
|
||||
const cacheKey = `series-${params.seriesId}-read-progress`;
|
||||
const cachedData = await serverCacheService.get(cacheKey);
|
||||
|
||||
if (cachedData) {
|
||||
return NextResponse.json(cachedData);
|
||||
}
|
||||
|
||||
const readProgress = await fetchReadProgress(config, params.seriesId);
|
||||
await serverCacheService.set(cacheKey, readProgress, 300); // Cache for 5 minutes
|
||||
|
||||
return NextResponse.json(readProgress);
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération de la progression" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchReadProgress(config: any, seriesId: string) {
|
||||
const { serverUrl, credentials } = config;
|
||||
const response = await fetch(`${serverUrl}/api/v1/series/${seriesId}/read-progress/tachiyomi`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${credentials}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
74
src/app/api/komga/series/[seriesId]/route.ts
Normal file
74
src/app/api/komga/series/[seriesId]/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { seriesId: string } }) {
|
||||
try {
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const configCookie = cookies().get("komga_credentials");
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = JSON.parse(atob(configCookie.value));
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!config.credentials?.username || !config.credentials?.password) {
|
||||
return NextResponse.json({ error: "Credentials Komga manquants" }, { status: 401 });
|
||||
}
|
||||
|
||||
const auth = Buffer.from(
|
||||
`${config.credentials.username}:${config.credentials.password}`
|
||||
).toString("base64");
|
||||
|
||||
// Appel à l'API Komga pour récupérer les détails de la série
|
||||
const [seriesResponse, booksResponse] = await Promise.all([
|
||||
// Détails de la série
|
||||
fetch(`${config.serverUrl}/api/v1/series/${params.seriesId}`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${auth}`,
|
||||
},
|
||||
}),
|
||||
// Liste des tomes (on récupère tous les tomes avec size=1000)
|
||||
fetch(
|
||||
`${config.serverUrl}/api/v1/series/${params.seriesId}/books?page=0&size=1000&unpaged=true&sort=metadata.numberSort,asc`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Basic ${auth}`,
|
||||
},
|
||||
}
|
||||
),
|
||||
]);
|
||||
|
||||
if (!seriesResponse.ok || !booksResponse.ok) {
|
||||
const errorResponse = !seriesResponse.ok ? seriesResponse : booksResponse;
|
||||
const errorData = await errorResponse.json().catch(() => null);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Erreur lors de la récupération des données de la série",
|
||||
details: errorData,
|
||||
},
|
||||
{ status: errorResponse.status }
|
||||
);
|
||||
}
|
||||
|
||||
const [series, booksData] = await Promise.all([seriesResponse.json(), booksResponse.json()]);
|
||||
|
||||
// On extrait la liste des tomes de la réponse paginée
|
||||
const books = booksData.content;
|
||||
|
||||
return NextResponse.json({ series, books });
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de la série:", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Erreur serveur",
|
||||
details: error instanceof Error ? error.message : "Erreur inconnue",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
70
src/app/api/komga/test/route.ts
Normal file
70
src/app/api/komga/test/route.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { serverUrl, username, password } = await request.json();
|
||||
|
||||
// Vérification des paramètres requis
|
||||
if (!serverUrl || !username || !password) {
|
||||
return NextResponse.json({ error: "Tous les champs sont requis" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Test de connexion à Komga en utilisant la route /api/v1/libraries
|
||||
const response = await fetch(`${serverUrl}/api/v1/libraries`, {
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`,
|
||||
},
|
||||
});
|
||||
|
||||
// Log de la réponse pour le debug
|
||||
console.log("Komga response status:", response.status);
|
||||
console.log("Komga response headers:", Object.fromEntries(response.headers.entries()));
|
||||
|
||||
if (!response.ok) {
|
||||
let errorMessage = "Impossible de se connecter au serveur Komga";
|
||||
let errorDetails = null;
|
||||
|
||||
try {
|
||||
errorDetails = await response.json();
|
||||
} catch (e) {
|
||||
// Si on ne peut pas parser la réponse, on utilise le texte brut
|
||||
try {
|
||||
errorDetails = await response.text();
|
||||
} catch (e) {
|
||||
// Si on ne peut pas récupérer le texte non plus, on garde le message par défaut
|
||||
}
|
||||
}
|
||||
|
||||
// Personnalisation du message d'erreur en fonction du status
|
||||
if (response.status === 401) {
|
||||
errorMessage = "Identifiants Komga invalides";
|
||||
} else if (response.status === 404) {
|
||||
errorMessage = "Le serveur Komga n'est pas accessible à cette adresse";
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: errorMessage,
|
||||
details: {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
errorDetails,
|
||||
},
|
||||
},
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
const libraries = await response.json();
|
||||
return NextResponse.json({ success: true, libraries });
|
||||
} catch (error) {
|
||||
console.error("Erreur lors du test de connexion:", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Le serveur Komga est inaccessible",
|
||||
details: error instanceof Error ? error.message : "Erreur inconnue",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
54
src/app/api/komga/thumbnail/[...path]/route.ts
Normal file
54
src/app/api/komga/thumbnail/[...path]/route.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { path: string[] } }) {
|
||||
try {
|
||||
// Récupérer les credentials Komga depuis le cookie
|
||||
const configCookie = cookies().get("komga_credentials");
|
||||
if (!configCookie) {
|
||||
return NextResponse.json({ error: "Configuration Komga manquante" }, { status: 401 });
|
||||
}
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = JSON.parse(atob(configCookie.value));
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: "Configuration Komga invalide" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Reconstruire le chemin de l'image
|
||||
const imagePath = params.path.join("/");
|
||||
const imageUrl = `${config.serverUrl}/api/v1/${imagePath}`;
|
||||
|
||||
// Appel à l'API Komga
|
||||
const response = await fetch(imageUrl, {
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(
|
||||
`${config.credentials?.username}:${config.credentials?.password}`
|
||||
).toString("base64")}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération de l'image" },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
// Récupérer les headers de l'image
|
||||
const contentType = response.headers.get("content-type");
|
||||
const buffer = await response.arrayBuffer();
|
||||
|
||||
// Retourner l'image avec les bons headers
|
||||
return new NextResponse(buffer, {
|
||||
headers: {
|
||||
"Content-Type": contentType || "image/jpeg",
|
||||
"Cache-Control": "public, max-age=31536000",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de l'image:", error);
|
||||
return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user