Extract shared constants into lib/constants.ts

- Add `lib/constants.ts` exporting `DATA_DIR`, `DATABASE_URL`, `CACHE_DIR`,
  `BACKUP_DIR`, `AVATAR_DIR`, `TMDB_API_BASE_URL`, and `TMDB_IMAGE_BASE_URL`
- Replace inline `process.env` derivations in `db/client`, `backup`,
  `image-cache`, `system-health`, `tmdb/client`, `actions/settings`,
  and `api/avatars` with imports from the new module
This commit is contained in:
2026-03-08 11:06:29 -04:00
parent 84c4356a9f
commit 17fe19e85b
8 changed files with 35 additions and 38 deletions
+1 -5
View File
@@ -4,17 +4,13 @@ import { mkdir, readdir } from "node:fs/promises";
import path from "node:path";
import { format } from "date-fns";
import { sql } from "drizzle-orm";
import { BACKUP_DIR, DATABASE_URL } from "@/lib/constants";
import { closeDatabase, db } from "@/lib/db/client";
import { runMigrations } from "@/lib/db/migrate";
import { createLogger } from "@/lib/logger";
const log = createLogger("backup");
const DATA_DIR = process.env.DATA_DIR || "./data";
const DATABASE_URL =
process.env.DATABASE_URL || path.join(DATA_DIR, "sqlite.db");
const BACKUP_DIR = path.join(DATA_DIR, "backups");
const MANUAL_PATTERN = /^sofa-manual-\d{4}-\d{2}-\d{2}-\d{6}(?:\d{3})?\.db$/;
const SCHEDULED_PATTERN =
/^sofa-scheduled-\d{4}-\d{2}-\d{2}-\d{6}(?:\d{3})?\.db$/;
+3 -10
View File
@@ -1,6 +1,7 @@
import { mkdir, rename } from "node:fs/promises";
import path from "node:path";
import { eq, inArray } from "drizzle-orm";
import { CACHE_DIR, TMDB_IMAGE_BASE_URL } from "@/lib/constants";
import { db } from "@/lib/db/client";
import {
availabilityOffers,
@@ -29,14 +30,6 @@ const CATEGORY_SIZES: Record<ImageCategory, string> = {
profiles: "w185",
};
const IMAGE_BASE_URL =
process.env.TMDB_IMAGE_BASE_URL || "https://image.tmdb.org/t/p";
const DATA_DIR = process.env.DATA_DIR || "./data";
const CACHE_DIR = process.env.CACHE_DIR
? path.join(process.env.CACHE_DIR, "images")
: path.join(DATA_DIR, "images");
export function imageCacheEnabled(): boolean {
return process.env.IMAGE_CACHE_ENABLED !== "false";
}
@@ -76,7 +69,7 @@ export async function downloadAndCacheImage(
category: ImageCategory,
): Promise<Buffer | null> {
const size = CATEGORY_SIZES[category];
const url = `${IMAGE_BASE_URL}/${size}${tmdbPath}`;
const url = `${TMDB_IMAGE_BASE_URL}/${size}${tmdbPath}`;
const res = await fetch(url);
if (!res.ok) {
@@ -121,7 +114,7 @@ export async function fetchAndMaybeCache(
// Fetch from TMDB
const size = CATEGORY_SIZES[category];
const url = `${IMAGE_BASE_URL}/${size}${tmdbPath}`;
const url = `${TMDB_IMAGE_BASE_URL}/${size}${tmdbPath}`;
const res = await fetch(url);
if (!res.ok) {
log.warn(`Fetch failed: ${url} -> ${res.status}`);
+6 -9
View File
@@ -1,6 +1,12 @@
import { access, constants, readdir } from "node:fs/promises";
import path from "node:path";
import { count, desc, eq } from "drizzle-orm";
import {
CACHE_DIR,
DATA_DIR,
DATABASE_URL,
TMDB_API_BASE_URL,
} from "@/lib/constants";
import { db } from "@/lib/db/client";
import { cronRuns, episodes, titles, user } from "@/lib/db/schema";
import { listBackups } from "@/lib/services/backup";
@@ -8,15 +14,6 @@ import { imageCacheEnabled } from "@/lib/services/image-cache";
import { getSetting } from "@/lib/services/settings";
import { isUpdateCheckEnabled } from "@/lib/services/update-check";
const DATA_DIR = process.env.DATA_DIR || "./data";
const DATABASE_URL =
process.env.DATABASE_URL || path.join(DATA_DIR, "sqlite.db");
const CACHE_DIR = process.env.CACHE_DIR
? path.join(process.env.CACHE_DIR, "images")
: path.join(DATA_DIR, "images");
const TMDB_API_BASE_URL =
process.env.TMDB_API_BASE_URL || "https://api.themoviedb.org/3";
export interface SystemHealthData {
database: {
dbSizeBytes: number;