+29
-4
@@ -16,6 +16,8 @@ export interface SanitizeOptions {
|
||||
jpegQuality?: number;
|
||||
webpQuality?: number;
|
||||
signal?: AbortSignal;
|
||||
/** A caller-selected, non-identifying output name. The detected extension is enforced. */
|
||||
outputName?: string;
|
||||
}
|
||||
|
||||
export async function sanitizeStaticImage(
|
||||
@@ -72,10 +74,9 @@ export async function sanitizeStaticImage(
|
||||
);
|
||||
throwIfAborted(options.signal);
|
||||
const outputBytes = await blob.arrayBuffer();
|
||||
const outputName = cleanOutputName(
|
||||
source.safeName,
|
||||
source.identity.detectedKind,
|
||||
);
|
||||
const outputName = options.outputName
|
||||
? enforceOutputExtension(options.outputName, source.identity.detectedKind)
|
||||
: cleanOutputName(source.safeName, source.identity.detectedKind);
|
||||
const outputScan = await scanImageBytes({
|
||||
id: `${source.id}-clean`,
|
||||
name: outputName,
|
||||
@@ -393,6 +394,30 @@ function cleanOutputName(
|
||||
return sanitizeDownloadFilename(`${withoutExtension}.clean.${extension}`);
|
||||
}
|
||||
|
||||
export function genericOutputName(
|
||||
index: number,
|
||||
kind: ImageScanResult["identity"]["detectedKind"],
|
||||
): string {
|
||||
if (!Number.isSafeInteger(index) || index < 0)
|
||||
throw new RangeError(
|
||||
"Generic output index must be a non-negative integer.",
|
||||
);
|
||||
const extension = kind === "jpeg" ? "jpg" : kind;
|
||||
if (!new Set(["jpg", "png", "webp"]).has(extension))
|
||||
throw new TypeError(`No generic clean-copy name is available for ${kind}.`);
|
||||
return `image-${String(index + 1).padStart(3, "0")}.clean.${extension}`;
|
||||
}
|
||||
|
||||
function enforceOutputExtension(
|
||||
input: string,
|
||||
kind: ImageScanResult["identity"]["detectedKind"],
|
||||
): string {
|
||||
const extension = kind === "jpeg" ? "jpg" : kind;
|
||||
const safe = sanitizeDownloadFilename(input, `image.clean.${extension}`);
|
||||
const stem = safe.replace(/\.[^.]*$/u, "") || "image.clean";
|
||||
return `${stem}.${extension}`;
|
||||
}
|
||||
|
||||
function findingSignature(finding: MetadataFinding): string {
|
||||
const label = finding.label
|
||||
.toLowerCase()
|
||||
|
||||
Reference in New Issue
Block a user