refactor: dedupe media and discord lowercase helpers

This commit is contained in:
Peter Steinberger
2026-04-07 13:13:20 +01:00
parent cb28d8d6b8
commit ae4f8da94f
10 changed files with 54 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
import { execFile, type ExecFileOptions } from "node:child_process";
import { promisify } from "node:util";
import { resolveSystemBin } from "../infra/resolve-system-bin.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import {
MEDIA_FFMPEG_MAX_BUFFER_BYTES,
MEDIA_FFMPEG_TIMEOUT_MS,
@@ -60,9 +61,8 @@ export async function runFfmpeg(args: string[], options?: MediaExecOptions): Pro
export function parseFfprobeCsvFields(stdout: string, maxFields: number): string[] {
return stdout
.trim()
.toLowerCase()
.split(/[,\r\n]+/, maxFields)
.map((field) => field.trim());
.map((field) => normalizeLowercaseStringOrEmpty(field));
}
export function parseFfprobeCodecAndSampleRate(stdout: string): {

View File

@@ -1,5 +1,8 @@
import path from "node:path";
import { normalizeOptionalLowercaseString } from "../shared/string-coerce.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalLowercaseString,
} from "../shared/string-coerce.js";
import { type MediaKind, mediaKindFromMime } from "./constants.js";
let fileTypeModulePromise: Promise<typeof import("file-type")> | undefined;
@@ -94,12 +97,12 @@ export function getFileExtension(filePath?: string | null): string | undefined {
try {
if (/^https?:\/\//i.test(filePath)) {
const url = new URL(filePath);
return path.extname(url.pathname).toLowerCase() || undefined;
return normalizeLowercaseStringOrEmpty(path.extname(url.pathname)) || undefined;
}
} catch {
// fall back to plain path parsing
}
const ext = path.extname(filePath).toLowerCase();
const ext = normalizeLowercaseStringOrEmpty(path.extname(filePath));
return ext || undefined;
}
@@ -123,7 +126,7 @@ function isGenericMime(mime?: string): boolean {
if (!mime) {
return true;
}
const m = mime.toLowerCase();
const m = normalizeLowercaseStringOrEmpty(mime);
return m === "application/octet-stream" || m === "application/zip";
}
@@ -171,7 +174,7 @@ export function isGifMedia(opts: {
contentType?: string | null;
fileName?: string | null;
}): boolean {
if (opts.contentType?.toLowerCase() === "image/gif") {
if (normalizeOptionalLowercaseString(opts.contentType) === "image/gif") {
return true;
}
const ext = getFileExtension(opts.fileName);
@@ -182,7 +185,7 @@ export function imageMimeFromFormat(format?: string | null): string | undefined
if (!format) {
return undefined;
}
switch (format.toLowerCase()) {
switch (normalizeLowercaseStringOrEmpty(format)) {
case "jpg":
case "jpeg":
return "image/jpeg";

View File

@@ -3,7 +3,10 @@ import { logVerbose, shouldLogVerbose } from "../globals.js";
import { SafeOpenError, readLocalFileSafely } from "../infra/fs-safe.js";
import { assertNoWindowsNetworkPath, safeFileURLToPath } from "../infra/local-file-access.js";
import type { SsrFPolicy } from "../infra/net/ssrf.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
} from "../shared/string-coerce.js";
import { resolveUserPath } from "../utils.js";
import { maxBytesForKind, type MediaKind } from "./constants.js";
import { fetchRemoteMedia } from "./fetch.js";
@@ -182,7 +185,9 @@ async function optimizeImageWithFallback(params: {
meta?: { contentType?: string; fileName?: string };
}): Promise<OptimizedImage> {
const { buffer, cap, meta } = params;
const isPng = meta?.contentType === "image/png" || meta?.fileName?.toLowerCase().endsWith(".png");
const isPng =
meta?.contentType === "image/png" ||
normalizeLowercaseStringOrEmpty(meta?.fileName).endsWith(".png");
const hasAlpha = isPng && (await hasAlphaChannel(buffer));
if (hasAlpha) {