ai-chatbot/lib/agents/psd-font-runtime.ts
Your Name c8ac5584c3
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
fix: use uploaded fonts when exporting psd text
2026-03-31 18:46:50 +08:00

136 lines
4.0 KiB
TypeScript

import "server-only";
import { execFile } from "node:child_process";
import { promises as fs } from "node:fs";
import { homedir } from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import type { AgentPsdFont } from "@/lib/agents/psd-font-config";
const execFileAsync = promisify(execFile);
const ensuredFontCache = new Map<
string,
Promise<{ fontFamily: string; installedPath?: string }>
>();
function sanitizeFileSegment(value: string, fallback: string) {
const normalized = String(value || "")
.trim()
.replace(/\s+/g, "-")
.replace(/[^a-zA-Z0-9._-]/g, "-")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "")
.toLowerCase();
return normalized || fallback;
}
function getFontExtension(font: AgentPsdFont) {
const fileName = String(font.fileName || "").trim().toLowerCase();
if (fileName.endsWith(".ttf")) return ".ttf";
if (fileName.endsWith(".otf")) return ".otf";
if (fileName.endsWith(".woff2")) return ".woff2";
if (fileName.endsWith(".woff")) return ".woff";
const contentType = String(font.contentType || "").trim().toLowerCase();
if (contentType.includes("woff2")) return ".woff2";
if (contentType.includes("woff")) return ".woff";
if (contentType.includes("otf")) return ".otf";
return ".ttf";
}
function getRuntimeFontDirectory() {
if (process.platform === "darwin") {
return path.join(homedir(), "Library", "Fonts", "ai-chatbot-psd");
}
if (process.platform === "linux") {
return path.join(homedir(), ".local", "share", "fonts", "ai-chatbot-psd");
}
return path.join(process.cwd(), ".next", "cache", "psd-fonts");
}
function parseFontconfigFamily(stdout: string) {
const firstLine = String(stdout || "")
.split("\n")
.map((line) => line.trim())
.find(Boolean);
if (!firstLine) {
return "";
}
return firstLine
.split(",")
.map((token) => token.trim())
.find(Boolean) || "";
}
async function ensureWritableFontFile(targetPath: string, fontBuffer: Buffer) {
const existingFile = await fs.stat(targetPath).catch(() => null);
if (existingFile && existingFile.size === fontBuffer.byteLength) {
return;
}
await fs.writeFile(targetPath, fontBuffer);
}
async function detectInstalledFontFamily(targetPath: string, fallbackFamily: string) {
try {
const { stdout } = await execFileAsync(
"fc-scan",
["--format=%{family}\n", targetPath],
{ timeout: 15_000 }
);
return parseFontconfigFamily(stdout) || fallbackFamily;
} catch {
return fallbackFamily;
}
}
async function refreshFontCache(fontDirectory: string) {
try {
await execFileAsync("fc-cache", ["-f", fontDirectory], { timeout: 20_000 });
} catch {
// ignore font cache refresh errors and let caller fall back to the original family
}
}
export async function ensurePsdFontAvailableForRender(params: {
font: AgentPsdFont;
fontBuffer: Buffer;
preferredFamily?: string;
}) {
const { font, fontBuffer, preferredFamily } = params;
const cacheKey = `${font.id}:${font.updatedAt}:${font.fileUrl}`;
if (!ensuredFontCache.has(cacheKey)) {
ensuredFontCache.set(
cacheKey,
(async () => {
const fallbackFamily = String(preferredFamily || font.family || font.name || "").trim() || "sans-serif";
const fontDirectory = getRuntimeFontDirectory();
await fs.mkdir(fontDirectory, { recursive: true });
const fileExtension = getFontExtension(font);
const targetPath = path.join(
fontDirectory,
`${sanitizeFileSegment(font.id, "font")}-${sanitizeFileSegment(
font.family || font.name || "custom-font",
"custom-font"
)}${fileExtension}`
);
await ensureWritableFontFile(targetPath, fontBuffer);
await refreshFontCache(fontDirectory);
return {
fontFamily: await detectInstalledFontFamily(targetPath, fallbackFamily),
installedPath: targetPath,
};
})()
);
}
return ensuredFontCache.get(cacheKey)!;
}