From 349f71969f8bf59933942119f726a6737dafeb4c Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Fri, 14 Feb 2025 14:42:45 +0100 Subject: [PATCH] refacto(db): removing komgaconfig service --- src/app/libraries/[libraryId]/page.tsx | 2 - src/app/page.tsx | 2 - src/app/series/[seriesId]/page.tsx | 3 - src/lib/services/komga-config.service.ts | 134 ----------------------- 4 files changed, 141 deletions(-) delete mode 100644 src/lib/services/komga-config.service.ts diff --git a/src/app/libraries/[libraryId]/page.tsx b/src/app/libraries/[libraryId]/page.tsx index e60abf4..6fdb6b5 100644 --- a/src/app/libraries/[libraryId]/page.tsx +++ b/src/app/libraries/[libraryId]/page.tsx @@ -1,6 +1,4 @@ -import { cookies } from "next/headers"; import { PaginatedSeriesGrid } from "@/components/library/PaginatedSeriesGrid"; -import { komgaConfigService } from "@/lib/services/komga-config.service"; import { LibraryService } from "@/lib/services/library.service"; interface PageProps { diff --git a/src/app/page.tsx b/src/app/page.tsx index 7617450..8464672 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,5 @@ import { HomeContent } from "@/components/home/HomeContent"; import { HomeService } from "@/lib/services/home.service"; -import { cookies } from "next/headers"; -import { komgaConfigService } from "@/lib/services/komga-config.service"; import { redirect } from "next/navigation"; export default async function HomePage() { diff --git a/src/app/series/[seriesId]/page.tsx b/src/app/series/[seriesId]/page.tsx index 0278288..e82f597 100644 --- a/src/app/series/[seriesId]/page.tsx +++ b/src/app/series/[seriesId]/page.tsx @@ -1,9 +1,6 @@ -import { cookies } from "next/headers"; import { PaginatedBookGrid } from "@/components/series/PaginatedBookGrid"; import { SeriesHeader } from "@/components/series/SeriesHeader"; -import { komgaConfigService } from "@/lib/services/komga-config.service"; import { SeriesService } from "@/lib/services/series.service"; -import { KomgaSeries } from "@/types/komga"; interface PageProps { params: { seriesId: string }; diff --git a/src/lib/services/komga-config.service.ts b/src/lib/services/komga-config.service.ts deleted file mode 100644 index e5a7cdc..0000000 --- a/src/lib/services/komga-config.service.ts +++ /dev/null @@ -1,134 +0,0 @@ -import { AuthConfig } from "@/types/auth"; -import { storageService } from "./storage.service"; -import { STORAGE_KEYS } from "@/lib/constants"; - -const { CREDENTIALS } = STORAGE_KEYS; - -class KomgaConfigService { - private static instance: KomgaConfigService; - - private constructor() {} - - public static getInstance(): KomgaConfigService { - if (!KomgaConfigService.instance) { - KomgaConfigService.instance = new KomgaConfigService(); - } - return KomgaConfigService.instance; - } - - /** - * Récupère la configuration Komga (fonctionne côté client et serveur) - */ - getConfig(serverCookies?: any): AuthConfig | null { - // Côté serveur - if (typeof window === "undefined" && serverCookies) { - try { - const configCookie = serverCookies.get(CREDENTIALS)?.value; - if (!configCookie) return null; - return JSON.parse(atob(configCookie)); - } catch (error) { - console.error( - "KomgaConfigService - Erreur lors de la récupération de la config côté serveur:", - error - ); - return null; - } - } - - // Côté client - return storageService.getCredentials(); - } - - /** - * Définit la configuration Komga (côté client uniquement) - */ - setConfig(config: AuthConfig, remember: boolean = false): void { - if (typeof window === "undefined") { - console.warn("KomgaConfigService - setConfig ne peut être utilisé que côté client"); - return; - } - - const storage = remember ? localStorage : sessionStorage; - const encoded = btoa(JSON.stringify(config)); - - // Stocker dans le storage - storage.setItem(CREDENTIALS, encoded); - - // Définir le cookie - const cookieValue = `${CREDENTIALS}=${encoded}; path=/; samesite=strict`; - const maxAge = remember ? `; max-age=${30 * 24 * 60 * 60}` : ""; - document.cookie = cookieValue + maxAge; - } - - /** - * Vérifie si la configuration est valide - */ - isConfigValid(config: AuthConfig | null): boolean { - if (!config) return false; - return !!(config.serverUrl && config.credentials?.username && config.credentials?.password); - } - - /** - * Efface la configuration - */ - clearConfig(): void { - if (typeof window === "undefined") return; - - localStorage.removeItem(CREDENTIALS); - sessionStorage.removeItem(CREDENTIALS); - document.cookie = `${CREDENTIALS}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; - } - - /** - * Récupère l'URL du serveur à partir de la configuration - */ - getServerUrl(serverCookies?: any): string | null { - const config = this.getConfig(serverCookies); - return config?.serverUrl || null; - } - - /** - * Récupère les credentials à partir de la configuration - */ - getCredentials(serverCookies?: any): { username: string; password: string } | null { - const config = this.getConfig(serverCookies); - return config?.credentials || null; - } - - /** - * Construit une URL complète pour l'API Komga - */ - buildApiUrl(path: string, serverCookies?: any): string { - const serverUrl = this.getServerUrl(serverCookies); - if (!serverUrl) throw new Error("URL du serveur non disponible"); - return `${serverUrl}/api/v1/${path}`; - } - - /** - * Génère les en-têtes d'authentification pour les requêtes - */ - getAuthHeaders(serverCookies?: any): Headers { - const credentials = this.getCredentials(serverCookies); - if (!credentials) throw new Error("Credentials non disponibles"); - - const auth = Buffer.from(`${credentials.username}:${credentials.password}`).toString("base64"); - const headers = new Headers(); - headers.set("Authorization", `Basic ${auth}`); - headers.set("Accept", "application/json"); - - return headers; - } - - /** - * Vérifie et récupère la configuration complète, lance une erreur si invalide - */ - validateAndGetConfig(serverCookies?: any): AuthConfig { - const config = this.getConfig(serverCookies); - if (!this.isConfigValid(config)) { - throw new Error("Configuration Komga manquante ou invalide"); - } - return config as AuthConfig; - } -} - -export const komgaConfigService = KomgaConfigService.getInstance();