feat: enhance Stripstream configuration handling
- Introduced a new resolver function to streamline fetching Stripstream configuration from the database or environment variables. - Updated various components and API routes to utilize the new configuration resolver, improving code maintainability and reducing direct database calls. - Added optional environment variables for Stripstream URL and token in the .env.example file. - Refactored image loading logic in the reader components to improve performance and error handling.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { getCurrentUser } from "@/lib/auth-utils";
|
||||
import { getResolvedStripstreamConfig } from "./stripstream/stripstream-config-resolver";
|
||||
import type { IMediaProvider } from "./provider.interface";
|
||||
|
||||
export async function getProvider(): Promise<IMediaProvider | null> {
|
||||
@@ -13,7 +14,7 @@ export async function getProvider(): Promise<IMediaProvider | null> {
|
||||
select: {
|
||||
activeProvider: true,
|
||||
config: { select: { url: true, authHeader: true } },
|
||||
stripstreamConfig: { select: { url: true, token: true } },
|
||||
stripstreamConfig: { select: { id: true } },
|
||||
},
|
||||
});
|
||||
|
||||
@@ -21,12 +22,12 @@ export async function getProvider(): Promise<IMediaProvider | null> {
|
||||
|
||||
const activeProvider = dbUser.activeProvider ?? "komga";
|
||||
|
||||
if (activeProvider === "stripstream" && dbUser.stripstreamConfig) {
|
||||
const { StripstreamProvider } = await import("./stripstream/stripstream.provider");
|
||||
return new StripstreamProvider(
|
||||
dbUser.stripstreamConfig.url,
|
||||
dbUser.stripstreamConfig.token
|
||||
);
|
||||
if (activeProvider === "stripstream") {
|
||||
const resolved = await getResolvedStripstreamConfig(userId);
|
||||
if (resolved) {
|
||||
const { StripstreamProvider } = await import("./stripstream/stripstream.provider");
|
||||
return new StripstreamProvider(resolved.url, resolved.token);
|
||||
}
|
||||
}
|
||||
|
||||
if (activeProvider === "komga" || !dbUser.activeProvider) {
|
||||
|
||||
26
src/lib/providers/stripstream/stripstream-config-resolver.ts
Normal file
26
src/lib/providers/stripstream/stripstream-config-resolver.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export interface ResolvedStripstreamConfig {
|
||||
url: string;
|
||||
token: string;
|
||||
source: "db" | "env";
|
||||
}
|
||||
|
||||
/**
|
||||
* Résout la config Stripstream : d'abord en base (par utilisateur), sinon depuis les env STRIPSTREAM_URL et STRIPSTREAM_TOKEN.
|
||||
*/
|
||||
export async function getResolvedStripstreamConfig(
|
||||
userId: number
|
||||
): Promise<ResolvedStripstreamConfig | null> {
|
||||
const fromDb = await prisma.stripstreamConfig.findUnique({
|
||||
where: { userId },
|
||||
select: { url: true, token: true },
|
||||
});
|
||||
if (fromDb) return { ...fromDb, source: "db" };
|
||||
|
||||
const url = process.env.STRIPSTREAM_URL?.trim();
|
||||
const token = process.env.STRIPSTREAM_TOKEN?.trim();
|
||||
if (url && token) return { url, token, source: "env" };
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user