Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/upload-media/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Automatically retry failed uploads with exponential backoff for transient (network/server) errors. Retry behavior is configurable via the `retry` store setting; non-transient failures and child sideloads are not retried. The upload queue can also be paused and resumed, allowing uploads to halt while the browser is offline and continue on reconnect ([#76765](https://github.com/WordPress/gutenberg/pull/76765)).

### Bug Fix

- Route very large images, especially interlaced/progressive JPEGs, to server-side processing instead of attempting client-side processing that would exceed the 1 GiB wasm-vips memory cap and fail. [#78949](https://github.com/WordPress/gutenberg/pull/78949).

### Code Quality

- Add missing `@types/react` dependency. [#78882](https://github.com/WordPress/gutenberg/pull/78882).
Expand Down
54 changes: 54 additions & 0 deletions packages/upload-media/src/feature-detection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import type { ImageDimensions } from './get-image-dimensions';

interface NavigatorNetworkInformation {
saveData: boolean;
effectiveType: 'slow-2g' | '2g' | '3g' | '4g';
Expand Down Expand Up @@ -190,3 +195,52 @@ export function isHeicCanvasSupported(): boolean {
export function clearFeatureDetectionCache(): void {
cachedResult = null;
}

/**
* Estimated bytes of WASM memory required per decoded pixel.
*
* A decoded image needs roughly width * height * 4 bytes (RGBA) in memory,
* plus additional working buffers while vips processes and re-encodes it.
* Four bytes per pixel is a deliberately conservative lower bound.
*/
const BYTES_PER_PIXEL = 4;

/**
* Memory budget (in bytes) for processing interlaced images client-side.
*
* wasm-vips is hard-capped at 1 GiB of WASM memory. Progressive JPEGs and
* Adam7-interlaced PNGs cannot be decoded with shrink-on-load, so the entire
* image must be buffered at once. A tighter ~0.5 GiB budget leaves headroom
* for the encode step and avoids the out-of-memory failures these images hit.
*/
const INTERLACED_MEMORY_BUDGET = 0.5 * 1024 * 1024 * 1024;

/**
* Memory budget (in bytes) for processing non-interlaced images client-side.
*
* Baseline images can be shrunk during decode, so vips needs far less peak
* memory. A generous ~0.9 GiB budget acts as a backstop against extreme sizes
* while leaving the vast majority of real-world uploads on the client.
*/
const BASELINE_MEMORY_BUDGET = 0.9 * 1024 * 1024 * 1024;

/**
* Determines whether an image is too large to process client-side.
*
* Very large images, especially interlaced/progressive ones, can exceed the
* 1 GiB wasm-vips memory cap and fail to process. Such images are better
* handled by the server, which has no comparable per-image memory ceiling.
*
* @param dimensions The image's parsed dimensions and interlacing.
* @return Whether the image's estimated memory use exceeds the client budget.
*/
export function exceedsClientProcessingMemory(
dimensions: ImageDimensions
): boolean {
const { width, height, interlaced } = dimensions;
const estimatedBytes = width * height * BYTES_PER_PIXEL;
const budget = interlaced
? INTERLACED_MEMORY_BUDGET
: BASELINE_MEMORY_BUDGET;
return estimatedBytes > budget;
}
183 changes: 183 additions & 0 deletions packages/upload-media/src/get-image-dimensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**
* Image dimensions and encoding details parsed from a file header.
*/
export interface ImageDimensions {
/**
* Image width in pixels.
*/
width: number;
/**
* Image height in pixels.
*/
height: number;
/**
* Whether the image is interlaced (progressive JPEG or Adam7 PNG).
*
* Interlaced images cannot be decoded with shrink-on-load, so the full
* image must be buffered in memory at once. This matters for deciding
* whether an image is safe to process client-side within the wasm-vips
* memory cap.
*/
interlaced: boolean;
}

/**
* Maximum number of bytes to read from the start of a file when probing for
* dimensions. Generous enough to skip past large EXIF/ICC segments that can
* precede a JPEG's frame header, while avoiding reading huge files in full.
*/
const MAX_HEADER_BYTES = 512 * 1024;

/**
* Parses dimensions and interlacing from a JPEG file header.
*
* Walks the JPEG marker segments until the Start Of Frame (SOFn) marker, which
* carries the image dimensions. Progressive DCT frames (SOF2/6/10/14) are
* reported as interlaced.
*
* @param view DataView over the file header bytes.
* @return Parsed dimensions, or null if they could not be determined.
*/
function parseJpeg( view: DataView ): ImageDimensions | null {
let offset = 2; // Skip the SOI marker (0xFFD8).

while ( offset < view.byteLength ) {
// Markers begin with 0xFF; bail if we have lost alignment.
if ( view.getUint8( offset ) !== 0xff ) {
return null;
}

// Skip any 0xFF padding bytes preceding the marker code.
while ( offset < view.byteLength && view.getUint8( offset ) === 0xff ) {
offset++;
}

if ( offset >= view.byteLength ) {
return null;
}

const marker = view.getUint8( offset );
offset++;

// Standalone markers without a length payload: TEM, RSTn, SOI, EOI.
if ( marker === 0x01 || ( marker >= 0xd0 && marker <= 0xd9 ) ) {
continue;
}

// Start Of Scan: pixel data begins, so no frame header was found.
if ( marker === 0xda ) {
return null;
}

if ( offset + 2 > view.byteLength ) {
return null;
}

const segmentLength = view.getUint16( offset );
if ( segmentLength < 2 ) {
return null;
}

// Start Of Frame markers (0xC0-0xCF) carry the dimensions, except
// DHT (0xC4), JPG extension (0xC8), and DAC/arithmetic (0xCC).
const isStartOfFrame =
marker >= 0xc0 &&
marker <= 0xcf &&
marker !== 0xc4 &&
marker !== 0xc8 &&
marker !== 0xcc;

if ( isStartOfFrame ) {
if ( offset + 7 > view.byteLength ) {
return null;
}
// Segment layout: length(2) precision(1) height(2) width(2).
const height = view.getUint16( offset + 3 );
const width = view.getUint16( offset + 5 );
// Progressive DCT frames: SOF2, SOF6, SOF10, SOF14.
const interlaced =
marker === 0xc2 ||
marker === 0xc6 ||
marker === 0xca ||
marker === 0xce;
return { width, height, interlaced };
}

offset += segmentLength;
}

return null;
}

/**
* Parses dimensions and interlacing from a PNG file header.
*
* Reads the IHDR chunk, which is always the first chunk and contains the
* dimensions and the interlace method (0 = none, 1 = Adam7).
*
* @param view DataView over the file header bytes.
* @return Parsed dimensions, or null if they could not be determined.
*/
function parsePng( view: DataView ): ImageDimensions | null {
// Signature (8 bytes) + IHDR length (4) + "IHDR" (4) + 13 bytes of data.
if ( view.byteLength < 29 ) {
return null;
}

// The IHDR chunk type must immediately follow the 8-byte signature and
// 4-byte chunk length.
const isIhdr =
view.getUint8( 12 ) === 0x49 && // I
view.getUint8( 13 ) === 0x48 && // H
view.getUint8( 14 ) === 0x44 && // D
view.getUint8( 15 ) === 0x52; // R

if ( ! isIhdr ) {
return null;
}

const width = view.getUint32( 16 );
const height = view.getUint32( 20 );
const interlaceMethod = view.getUint8( 28 );

return { width, height, interlaced: interlaceMethod !== 0 };
}

/**
* Reads an image's dimensions and interlacing from its header bytes.
*
* Only the leading bytes of the file are read, so this is cheap even for very
* large images and never fully decodes the pixel data. Currently supports JPEG
* and PNG, the formats most commonly affected by client-side memory limits;
* other formats return null (treated as unknown).
*
* @param file The image file to inspect.
* @return The parsed dimensions, or null if they could not be determined.
*/
export async function getImageDimensions(
file: File
): Promise< ImageDimensions | null > {
try {
const headerBytes = Math.min( file.size, MAX_HEADER_BYTES );
const buffer = await file.slice( 0, headerBytes ).arrayBuffer();
const view = new DataView( buffer );

// JPEG: starts with the SOI marker 0xFFD8.
if ( view.byteLength >= 3 && view.getUint16( 0 ) === 0xffd8 ) {
return parseJpeg( view );
}

// PNG: 8-byte signature 89 50 4E 47 0D 0A 1A 0A.
if (
view.byteLength >= 8 &&
view.getUint32( 0 ) === 0x89504e47 &&
view.getUint32( 4 ) === 0x0d0a1a0a
) {
return parsePng( view );
}

return null;
} catch {
return null;
}
}
27 changes: 24 additions & 3 deletions packages/upload-media/src/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ type WPDataRegistry = ReturnType< typeof createRegistry >;
*/
import { cloneFile, convertBlobToFile, renameFile } from '../utils';
import { canvasConvertToJpeg } from '../canvas-utils';
import { isClientSideMediaSupported } from '../feature-detection';
import {
isClientSideMediaSupported,
exceedsClientProcessingMemory,
} from '../feature-detection';
import { getImageDimensions } from '../get-image-dimensions';
import { CLIENT_SIDE_SUPPORTED_MIME_TYPES, HEIC_MIME_TYPES } from './constants';
import { StubFile } from '../stub-file';
import { UploadError } from '../upload-error';
Expand Down Expand Up @@ -690,6 +694,20 @@ export function prepareItem( id: QueueItemId ) {
);
const isHeic = HEIC_MIME_TYPES.includes( file.type );

// Gate very large images out of client-side processing. wasm-vips is
// capped at 1 GiB of memory, so high-megapixel images, especially
// interlaced/progressive ones, which can't be decoded with
// shrink-on-load, can exhaust it and fail. These are routed to the
// server, which has no comparable per-image ceiling. If dimensions
// can't be determined, the image stays on the client-side path.
let tooLargeForClient = false;
if ( isImage && isVipsSupported ) {
const dimensions = await getImageDimensions( file );
if ( dimensions && exceedsClientProcessingMemory( dimensions ) ) {
tooLargeForClient = true;
}
}

// For images that can be processed by vips, upload the original and
// let generateThumbnails() handle threshold scaling as a sideload.
//
Expand All @@ -704,7 +722,7 @@ export function prepareItem( id: QueueItemId ) {
// image_editor_output_format filter during create_item.
// The response carries image_output_format so generateThumbnails
// can transcode sub-sizes to the same target format.
if ( isImage && isVipsSupported ) {
if ( isImage && isVipsSupported && ! tooLargeForClient ) {
operations.push(
OperationType.Upload,
OperationType.ThumbnailGeneration,
Expand Down Expand Up @@ -773,7 +791,10 @@ export function prepareItem( id: QueueItemId ) {
convert_format: true,
},
};
} else if ( ! isVipsSupported || ! isImage ) {
} else if ( ! isVipsSupported || ! isImage || tooLargeForClient ) {
// Either the format isn't vips-processable, it isn't an image, or
// it's too large for client-side processing. Let the server
// generate sub-sizes and handle format conversion.
updates = {
additionalData: {
...item.additionalData,
Expand Down
55 changes: 55 additions & 0 deletions packages/upload-media/src/store/test/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,61 @@ describe( 'actions', () => {
);
expect( updatedItem.additionalData.convert_format ).toBe( true );
} );

it( 'routes very large interlaced images to the server', async () => {
// A progressive JPEG (SOF2) header reporting 20000x11857, which
// exceeds the client-side memory budget for interlaced images.
const dimensions = new Uint8Array( 4 );
const dimView = new DataView( dimensions.buffer );
dimView.setUint16( 0, 11857 ); // height
dimView.setUint16( 2, 20000 ); // width
const bytes = new Uint8Array( [
0xff,
0xd8, // SOI
0xff,
0xc2, // SOF2 (progressive)
0x00,
0x11, // segment length
0x08, // precision
...dimensions, // height (2) + width (2)
0x03, // components
0x00,
0x00,
0x00,
] );
const largeJpeg = new File( [ bytes ], 'huge.jpg', {
type: 'image/jpeg',
} );

unlock( registry.dispatch( uploadStore ) ).addItem( {
file: largeJpeg,
} );

const item = unlock(
registry.select( uploadStore )
).getAllItems()[ 0 ];

await unlock( registry.dispatch( uploadStore ) ).prepareItem(
item.id
);

const updatedItem = unlock(
registry.select( uploadStore )
).getAllItems()[ 0 ];

// Should fall back to server-side processing: only Upload, no
// client-side thumbnail generation.
expect( updatedItem.operations ).toEqual(
expect.arrayContaining( [ OperationType.Upload ] )
);
expect( updatedItem.operations ).not.toEqual(
expect.arrayContaining( [ OperationType.ThumbnailGeneration ] )
);
expect( updatedItem.additionalData.generate_sub_sizes ).toBe(
true
);
expect( updatedItem.additionalData.convert_format ).toBe( true );
} );
} );

describe( 'concurrent sideloads', () => {
Expand Down
Loading
Loading