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
14 changes: 14 additions & 0 deletions packages/upload-media/src/store/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ export const DEFAULT_MAX_CONCURRENT_UPLOADS = 5;
* uploading many images at once.
*/
export const DEFAULT_MAX_CONCURRENT_IMAGE_PROCESSING = 2;

/**
* MIME types supported by client-side media processing.
*
* These are the image formats that can be processed using
* WebAssembly-based vips in the browser.
*/
export const CLIENT_SIDE_SUPPORTED_MIME_TYPES: readonly string[] = [
'image/jpeg',
'image/png',
'image/gif',
'image/webp',
'image/avif',
] as const;
22 changes: 19 additions & 3 deletions packages/upload-media/src/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type WPDataRegistry = ReturnType< typeof createRegistry >;
* Internal dependencies
*/
import { cloneFile, convertBlobToFile, renameFile } from '../utils';
import { CLIENT_SIDE_SUPPORTED_MIME_TYPES } from './constants';
import { StubFile } from '../stub-file';
import { UploadError } from '../upload-error';
import {
Expand Down Expand Up @@ -710,9 +711,12 @@ export function prepareItem( id: QueueItemId ) {
const settings = select.getSettings();

const isImage = file.type.startsWith( 'image/' );
const isVipsSupported = CLIENT_SIDE_SUPPORTED_MIME_TYPES.includes(
file.type
);

// For images, check if we need to scale down based on threshold.
if ( isImage ) {
// For images that can be processed by vips, check if we need to scale down based on threshold.
if ( isImage && isVipsSupported ) {
const { bigImageSizeThreshold, imageOutputFormats } = settings;

// If a threshold is set, add a resize operation to scale down large images.
Expand Down Expand Up @@ -758,7 +762,19 @@ export function prepareItem( id: QueueItemId ) {
operations,
} );

dispatch.finishOperation( id, {} );
// If the file is not processed by vips, tell the server to
// generate sub-sizes since they won't be created client-side.
const updates =
! isVipsSupported || ! isImage
? {
additionalData: {
...item.additionalData,
generate_sub_sizes: true,
},
}
: {};

dispatch.finishOperation( id, updates );
};
}

Expand Down
163 changes: 162 additions & 1 deletion packages/upload-media/src/store/test/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type WPDataRegistry = ReturnType< typeof createRegistry >;
* Internal dependencies
*/
import { store as uploadStore } from '..';
import { ItemStatus } from '../types';
import { ItemStatus, OperationType } from '../types';
import { unlock } from '../../lock-unlock';

jest.mock( '@wordpress/blob', () => ( {
Expand Down Expand Up @@ -162,6 +162,167 @@ describe( 'actions', () => {
} );
} );

describe( 'prepareItem', () => {
it( 'should add Upload and ThumbnailGeneration for vips-supported image types', async () => {
unlock( registry.dispatch( uploadStore ) ).addItem( {
file: jpegFile,
} );

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

// Manually call prepareItem to determine operations.
await unlock( registry.dispatch( uploadStore ) ).prepareItem(
item.id
);

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

// Should include Upload and ThumbnailGeneration (no ResizeCrop without bigImageSizeThreshold).
expect( updatedItem.operations ).toEqual(
expect.arrayContaining( [
OperationType.Upload,
OperationType.ThumbnailGeneration,
] )
);
// Server should not generate sub-sizes for vips-supported images.
expect( updatedItem.additionalData.generate_sub_sizes ).toBe(
false
);
} );

it( 'should add only Upload for non-image types', async () => {
unlock( registry.dispatch( uploadStore ) ).addItem( {
file: mp4File,
} );

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

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

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

expect( updatedItem.operations ).toEqual(
expect.arrayContaining( [ OperationType.Upload ] )
);
expect( updatedItem.operations ).not.toEqual(
expect.arrayContaining( [ OperationType.ThumbnailGeneration ] )
);
// Server should generate sub-sizes for non-vips files.
expect( updatedItem.additionalData.generate_sub_sizes ).toBe(
true
);
} );

it( 'should add only Upload for unsupported image types like SVG', async () => {
const svgFile = new File( [ '<svg></svg>' ], 'test.svg', {
lastModified: 1234567891,
type: 'image/svg+xml',
} );

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

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

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

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

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
);
} );

it( 'should add only Upload for unsupported image types like BMP', async () => {
const bmpFile = new File( [ 'bmp' ], 'test.bmp', {
lastModified: 1234567891,
type: 'image/bmp',
} );

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

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

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

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

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
);
} );

it( 'should add only Upload for PDF files', async () => {
const pdfFile = new File( [ 'pdf' ], 'document.pdf', {
lastModified: 1234567891,
type: 'application/pdf',
} );

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

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

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

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

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
);
} );
} );

describe( 'cancelItem', () => {
beforeEach( () => {
( vipsCancelOperations as jest.Mock ).mockClear();
Expand Down
Loading