Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions packages/upload-media/src/canvas-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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.
*
Expand Down Expand Up @@ -68,8 +78,19 @@ export async function canvasConvertToJpeg(
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,
Expand All @@ -94,8 +115,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.
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/upload-media/src/store/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ export const HEIC_MIME_TYPES: readonly string[] = [
'image/heic',
'image/heif',
] as const;

/**
* HEIC/HEIF file extensions.
*
* Used as a fallback when the browser can't report a MIME type at all
* (`file.type === ''`), which happens for HEIC files on platforms that lack
* the OS-level codec needed to identify the format (e.g. Windows without the
* HEVC Video Extension).
*/
export const HEIC_EXTENSIONS: readonly string[] = [ 'heic', 'heif' ] as const;
20 changes: 17 additions & 3 deletions packages/upload-media/src/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
convertBlobToFile,
isAnimatedGif,
renameFile,
hasFileExtension,
} from '../utils';
import { canvasConvertToJpeg } from '../canvas-utils';
import { getHeicUnsupportedMessage } from '../heic-support';
Expand All @@ -28,7 +29,11 @@ import {
exceedsClientProcessingMemory,
} from '../feature-detection';
import { getImageDimensions } from '../get-image-dimensions';
import { CLIENT_SIDE_SUPPORTED_MIME_TYPES, HEIC_MIME_TYPES } from './constants';
import {
CLIENT_SIDE_SUPPORTED_MIME_TYPES,
HEIC_MIME_TYPES,
HEIC_EXTENSIONS,
} from './constants';
import { StubFile } from '../stub-file';
import { ErrorCode, UploadError } from '../upload-error';
import { debug, measure } from './utils/debug-logger';
Expand Down Expand Up @@ -834,11 +839,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 extension so these files still reach the HEIC handling below
// instead of silently uploading as an undecodable, unconverted file.
const isHeicByExtension =
! file.type && hasFileExtension( file.name, HEIC_EXTENSIONS );

const isImage = file.type.startsWith( 'image/' ) || isHeicByExtension;
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 ) || isHeicByExtension;

// Gate very large images out of client-side processing. wasm-vips is
// capped at 1 GiB of memory, so high-megapixel images, especially
Expand Down
18 changes: 18 additions & 0 deletions packages/upload-media/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ export function getFileExtension( file: string ): string | null {
return file.includes( '.' ) ? file.split( '.' ).pop() || null : null;
}

/**
* Checks whether a file name has one of the given extensions.
*
* Case-insensitive, since filesystems and cameras don't agree on casing
* (e.g. "IMG_1234.HEIC").
*
* @param name File name.
* @param extensions Extensions to match against, without the leading dot.
* @return Whether the file name ends in one of the given extensions.
*/
export function hasFileExtension(
name: string,
extensions: readonly string[]
): boolean {
const extension = getFileExtension( name )?.toLowerCase();
return !! extension && extensions.includes( extension );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe inferring file extensions from filenames is unreliable. It might be more accurate to determine if it's an HEIC image from the file buffer, similar to how isAnimatedGif might work.

The following code was generated by AI and has not been tested. @adamsilverstein might have more insights into this area.

/**
 * ISOBMFF brands that identify a HEIC/HEIF image.
 *
 * `mif1`/`msf1` are the generic HEIF brands; they are also listed by AVIF
 * files, which is why the AVIF brands are excluded first.
 */
const HEIC_BRANDS = [ 'heic', 'heix', 'heim', 'heis', 'hevc', 'hevx', 'hevm', 'hevs', 'mif1', 'msf1' ];
const AVIF_BRANDS = [ 'avif', 'avis' ];

/**
 * Detects a HEIC/HEIF file from its ISOBMFF `ftyp` box.
 *
 * Used when the browser can't report a MIME type at all (`file.type === ''`),
 * which happens on platforms lacking the OS-level HEVC codec.
 *
 * @param buffer First bytes of the file (64 is enough for the `ftyp` box).
 * @return Whether the buffer starts with a HEIC/HEIF `ftyp` box.
 */
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;
	}

	const readBrand = ( offset: number ) =>
		String.fromCharCode( ...view.subarray( offset, offset + 4 ) );

	if ( readBrand( 4 ) !== 'ftyp' ) {
		return false;
	}

	// Major brand at 8, minor version at 12, compatible brands from 16 on.
	const boxSize = new DataView( buffer ).getUint32( 0 );
	const end = Math.min( boxSize || view.length, view.length );
	const brands = [ readBrand( 8 ) ];
	for ( let offset = 16; offset + 4 <= end; offset += 4 ) {
		brands.push( readBrand( offset ) );
	}

	// AVIF also carries the generic HEIF brands, so rule it out first.
	if ( brands.some( ( brand ) => AVIF_BRANDS.includes( brand ) ) ) {
		return false;
	}

	return brands.some( ( brand ) => HEIC_BRANDS.includes( brand ) );
}
const isHeicByContent =
	! file.type && isHeicBuffer( await file.slice( 0, 64 ).arrayBuffer() );

const isImage = file.type.startsWith( 'image/' ) || isHeicByContent;
const isHeic = HEIC_MIME_TYPES.includes( file.type ) || isHeicByContent;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9991048

}

/**
* Returns file basename without extension.
*
Expand Down
Loading