diff --git a/src/domain/artifactFormatRecognition.ts b/src/domain/artifactFormatRecognition.ts index f8af8d6c05c..4a64f7044ec 100644 --- a/src/domain/artifactFormatRecognition.ts +++ b/src/domain/artifactFormatRecognition.ts @@ -194,6 +194,25 @@ const when = ({ prefix }: ArtifactRecognitionInput): Recognition | undefined => claims(prefix) ? { state: "recognized", format } : undefined; +const whenClaimed = + ( + format: RecognizedArtifactFormat, + recognize: ( + prefix: Buffer, + fileSize: number, + ) => + | { state: "recognized" } + | { state: "refused"; reason: string } + | undefined, + ): ArtifactFormatRecognizer => + ({ prefix, fileSize }) => { + const claim = recognize(prefix, fileSize); + if (claim === undefined) return undefined; + return claim.state === "recognized" + ? { state: "recognized", format } + : { state: "refused", format, reason: claim.reason }; + }; + /** * The Mega Drive layout this prefix shows. It yields to ELF, Mach-O, DOS MZ * and FAT, so a native header is not claimed because its bytes also match. @@ -225,13 +244,7 @@ export const ARTIFACT_FORMAT_RECOGNIZERS: readonly ArtifactFormatRecognizer[] = ? { state: "recognized", format: "ines" } : { state: "refused", format: "ines", reason: layout.error.detail }; }, - ({ prefix, fileSize }) => { - const claim = n64RomRecognition(prefix, fileSize); - if (claim === undefined) return undefined; - return claim.state === "recognized" - ? { state: "recognized", format: "n64-rom" } - : { state: "refused", format: "n64-rom", reason: claim.reason }; - }, + whenClaimed("n64-rom", n64RomRecognition), when("zip", hasZipSignature), when("godot-pck", hasGodotPckHeader), when("unityfs", hasUnityFsHeader), @@ -263,35 +276,9 @@ export const ARTIFACT_FORMAT_RECOGNIZERS: readonly ArtifactFormatRecognizer[] = when("android-boot-image", hasAndroidBootImageMagic), when("android-dtbo", hasAndroidDtboMagic), when("android-vbmeta", hasAndroidVbmetaMagic), - ({ prefix }) => { - const ota = recognizeAndroidOtaPayload(prefix); - if (ota === undefined) return undefined; - return ota.state === "recognized" - ? { state: "recognized", format: "android-ota-payload" } - : { - state: "refused", - format: "android-ota-payload", - reason: ota.reason, - }; - }, - ({ prefix, fileSize }) => { - const ubi = recognizeUbiImage(prefix, fileSize); - if (ubi === undefined) return undefined; - return ubi.state === "recognized" - ? { state: "recognized", format: "ubi-image" } - : { state: "refused", format: "ubi-image", reason: ubi.reason }; - }, - ({ prefix }) => { - const sparse = recognizeAndroidSparseImage(prefix); - if (sparse === undefined) return undefined; - return sparse.state === "recognized" - ? { state: "recognized", format: "android-sparse-image" } - : { - state: "refused", - format: "android-sparse-image", - reason: sparse.reason, - }; - }, + whenClaimed("android-ota-payload", recognizeAndroidOtaPayload), + whenClaimed("ubi-image", recognizeUbiImage), + whenClaimed("android-sparse-image", recognizeAndroidSparseImage), ({ prefix }) => { const format = detectHexRecordEncoding(prefix); return format === undefined ? undefined : { state: "recognized", format };