diff --git a/packages/upload-media/CHANGELOG.md b/packages/upload-media/CHANGELOG.md index 8d8c35734ef7ac..2fb9ecd3565dd9 100644 --- a/packages/upload-media/CHANGELOG.md +++ b/packages/upload-media/CHANGELOG.md @@ -8,6 +8,7 @@ ### Bug Fixes +- HEIC uploads no longer hang indefinitely on platforms lacking the OS-level HEVC codec, where `ImageDecoder.decode()` never settles. The decode is now bounded by a timeout, and HEIC files the OS can't identify (empty `file.type`) are detected from the file header ([#81043](https://github.com/WordPress/gutenberg/issues/81043)). - A failed `/finalize` request is no longer reported as a successful upload. Finalize is the server's commit point for the attachment metadata (responsive sub-sizes and the final `-scaled` file reference); when it fails, the item is now cancelled and the error surfaced instead of showing "upload complete" and keeping an attachment that is missing its registered sizes ([#80673](https://github.com/WordPress/gutenberg/issues/80673)). ## 0.37.0 (2026-07-29) diff --git a/packages/upload-media/src/canvas-utils.ts b/packages/upload-media/src/canvas-utils.ts index 38a7d64939710a..a016f397d407e8 100644 --- a/packages/upload-media/src/canvas-utils.ts +++ b/packages/upload-media/src/canvas-utils.ts @@ -2,6 +2,16 @@ import { getFileBasename } from './utils'; import { parseHeic } from './heic-parser'; import { getHeicUnsupportedMessage } from './heic-support'; +/** + * Some platforms (e.g. Windows without the HEVC extension) have + * ImageDecoder.isTypeSupported() optimistically report HEIC support based on + * container-format detection alone. When the underlying platform codec isn't + * actually available, decoder.decode() doesn't reject, it just never settles. + * This timeout guards against that by force-closing the decoder, which per + * the WebCodecs spec rejects any of its pending decode() calls. + */ +const IMAGE_DECODER_TIMEOUT = 3000; + /** * Converts an image file to JPEG using the browser's native decoder and canvas. * @@ -58,15 +68,26 @@ export async function canvasConvertToJpeg( // Strategy 2: WebCodecs ImageDecoder API. // Uses platform codecs (e.g., macOS HEIC support) that may not be // exposed through createImageBitmap or elements. - if ( typeof ImageDecoder !== 'undefined' ) { + if ( file.type && typeof ImageDecoder !== 'undefined' ) { const supported = await ImageDecoder.isTypeSupported( file.type ); if ( supported ) { const decoder = new ImageDecoder( { type: file.type, data: file.stream(), } ); + let timeoutId: ReturnType< typeof setTimeout > | undefined; try { - const { image: videoFrame } = await decoder.decode(); + const { image: videoFrame } = await Promise.race( [ + decoder.decode(), + new Promise< never >( ( _resolve, reject ) => { + timeoutId = setTimeout( () => { + decoder.close(); + reject( + new Error( 'ImageDecoder decode timed out' ) + ); + }, IMAGE_DECODER_TIMEOUT ); + } ), + ] ); try { const canvas = new OffscreenCanvas( videoFrame.displayWidth, @@ -91,8 +112,16 @@ export async function canvasConvertToJpeg( } finally { videoFrame.close(); } + } catch { + // decode() rejected, timed out, or the platform doesn't + // actually support the codec. Fall through to strategy 3. } finally { - decoder.close(); + clearTimeout( timeoutId ); + try { + decoder.close(); + } catch { + // Already closed by the timeout handler above. + } } } } diff --git a/packages/upload-media/src/store/private-actions.ts b/packages/upload-media/src/store/private-actions.ts index 91fdcea35fc072..2cc632b3f81f19 100644 --- a/packages/upload-media/src/store/private-actions.ts +++ b/packages/upload-media/src/store/private-actions.ts @@ -7,6 +7,7 @@ import { cloneFile, convertBlobToFile, isAnimatedGif, + isHeicBuffer, renameFile, } from '../utils'; import { canvasConvertToJpeg } from '../canvas-utils'; @@ -823,11 +824,20 @@ export function prepareItem( id: QueueItemId ) { let heicJpeg: File | null = null; - const isImage = file.type.startsWith( 'image/' ); + // Browsers report an empty file.type for HEIC files on platforms that + // lack the OS-level HEVC codec needed to identify the format (e.g. + // Windows without the HEVC Video Extension). Fall back to sniffing the + // file header so these files still reach the HEIC handling below + // instead of silently uploading as an undecodable, unconverted file. + const isHeicByContent = + ! file.type && + isHeicBuffer( await file.slice( 0, 64 ).arrayBuffer() ); + + const isImage = file.type.startsWith( 'image/' ) || isHeicByContent; const isVipsSupported = CLIENT_SIDE_SUPPORTED_MIME_TYPES.includes( file.type ); - const isHeic = HEIC_MIME_TYPES.includes( file.type ); + const isHeic = HEIC_MIME_TYPES.includes( file.type ) || isHeicByContent; // Gate very large images out of client-side processing. wasm-vips is // capped at 1 GiB of memory, so high-megapixel images, especially diff --git a/packages/upload-media/src/test/is-heic-buffer.ts b/packages/upload-media/src/test/is-heic-buffer.ts new file mode 100644 index 00000000000000..1865bc786f818f --- /dev/null +++ b/packages/upload-media/src/test/is-heic-buffer.ts @@ -0,0 +1,45 @@ +import { isHeicBuffer } from '../utils'; + +/** + * Builds a minimal ISOBMFF File Type Box. + * + * @param brands Major brand, followed by any compatible brands. + * @return ArrayBuffer of a synthetic file header. + */ +function buildFtyp( ...brands: string[] ): ArrayBuffer { + const [ majorBrand, ...compatibleBrands ] = brands; + // "ftyp", the major brand, the zeroed minor version, the compatible brands. + const box = `ftyp${ majorBrand }\0\0\0\0${ compatibleBrands.join( '' ) }`; + return new Uint8Array( [ + // Box size, including these four bytes. + 0x00, + 0x00, + 0x00, + 4 + box.length, + ...[ ...box ].map( ( c ) => c.charCodeAt( 0 ) ), + ] ).buffer; +} + +describe( 'isHeicBuffer', () => { + it( 'returns false for a buffer with no File Type Box', () => { + expect( + isHeicBuffer( + new Uint8Array( [ 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10 ] ).buffer + ) + ).toBe( false ); + } ); + + it( 'returns true for a HEIC major brand', () => { + expect( isHeicBuffer( buildFtyp( 'heic' ) ) ).toBe( true ); + } ); + + it( 'returns true for a HEIC compatible brand', () => { + expect( isHeicBuffer( buildFtyp( 'mif1', 'miaf', 'heic' ) ) ).toBe( + true + ); + } ); + + it( 'returns false for AVIF, which declares the generic HEIF brand', () => { + expect( isHeicBuffer( buildFtyp( 'avif', 'mif1' ) ) ).toBe( false ); + } ); +} ); diff --git a/packages/upload-media/src/utils.ts b/packages/upload-media/src/utils.ts index 5f3ba7e86d3b63..d046bd2d9c3cd6 100644 --- a/packages/upload-media/src/utils.ts +++ b/packages/upload-media/src/utils.ts @@ -168,3 +168,88 @@ export function isAnimatedGif( buffer: ArrayBuffer ): boolean { return false; } + +/** + * Brands that identify a HEIC/HEIF image, as registered with the MP4RA. + * + * `mif1` and `msf1` are the generic HEIF brands, which AVIF files carry too, + * so the AVIF brands are ruled out before these are consulted. + */ +const HEIC_BRANDS = [ + 'heic', + 'heix', + 'heim', + 'heis', + 'hevc', + 'hevx', + 'hevm', + 'hevs', + 'mif1', + 'msf1', +]; +const AVIF_BRANDS = [ 'avif', 'avis' ]; + +/** + * Detects whether a file buffer contains a HEIC/HEIF image. + * + * Performs binary analysis of the ISOBMFF file structure: + * 1. Checks for the File Type Box marker ("ftyp" at offset 4) + * 2. Collects the brands it declares (major brand at offset 8, compatible + * brands from offset 16 on, each four ASCII characters) + * 3. Returns true if a HEIC/HEIF brand is present and no AVIF brand is + * + * Used when the browser can't report a MIME type at all (`file.type === ''`), + * which happens on platforms lacking the OS-level HEVC codec. More reliable + * than the file name, which is only a naming convention. + * + * Only the File Type Box is read, so a file can declare a HEIF brand and still + * be undecodable. That is non-destructive: it reaches the HEIC conversion path, + * where `parseHeic()` does the full parse and a failure surfaces as a regular + * decode error. + * + * Based on ISO/IEC 14496-12 (ISOBMFF) and ISO/IEC 23008-12 (HEIF): + * + * @see https://mp4ra.org/registered-types/brands + * @see https://nokiatech.github.io/heif/technical.html + * + * @param buffer File ArrayBuffer. The first 64 bytes cover the File Type Box. + * @return Whether the buffer contains a HEIC/HEIF image. + */ +export function isHeicBuffer( buffer: ArrayBuffer ): boolean { + const view = new Uint8Array( buffer ); + + // A File Type Box is at least: size (4) + "ftyp" (4) + major brand (4). + if ( view.length < 12 ) { + return false; + } + + // Check the File Type Box marker: "ftyp" (0x66 0x74 0x79 0x70) at offset 4. + if ( + view[ 4 ] !== 0x66 || + view[ 5 ] !== 0x74 || + view[ 6 ] !== 0x79 || + view[ 7 ] !== 0x70 + ) { + return false; + } + + // Collect the brands: the major one at offset 8, then the compatible ones + // every 4 bytes from 16 on, skipping the minor version at 12. The box size + // bounds the scan, as anything past it belongs to the next box. + const boxSize = new DataView( + buffer, + view.byteOffset, + view.byteLength + ).getUint32( 0 ); + const end = Math.min( boxSize || view.length, view.length ); + const brands = [ String.fromCharCode( ...view.subarray( 8, 12 ) ) ]; + for ( let i = 16; i + 4 <= end; i += 4 ) { + brands.push( String.fromCharCode( ...view.subarray( i, i + 4 ) ) ); + } + + if ( brands.some( ( brand ) => AVIF_BRANDS.includes( brand ) ) ) { + return false; + } + + return brands.some( ( brand ) => HEIC_BRANDS.includes( brand ) ); +}