Skip to content

Commit d577970

Browse files
Media: skip server image support check when using client-side media.
When uploading images via the REST API with `generate_sub_sizes` set to `false`, skip server support checks since processing is handled on the client side. Fix an issue where uploads of formats like AVIF fail with `rest_upload_image_type_not_supported`, even though the client will handle all image processing and the server doesn't need to support the format. See also WordPress/gutenberg#76369 and WordPress/gutenberg#76371. Props adamsilverstein, westonruter, andrewserong, mukesh27. Fixes #64836. See #62717. git-svn-id: https://develop.svn.wordpress.org/trunk@61980 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 019eeb8 commit d577970

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ public function create_item_permissions_check( $request ) {
232232
*/
233233
$prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, $files['file']['type'] ?? null );
234234

235+
// When the client handles image processing (generate_sub_sizes is false),
236+
// skip the server-side image editor support check.
237+
if ( false === $request['generate_sub_sizes'] ) {
238+
$prevent_unsupported_uploads = false;
239+
}
240+
235241
// If the upload is an image, check if the server can handle the mime type.
236242
if (
237243
$prevent_unsupported_uploads &&
@@ -278,7 +284,7 @@ public function create_item( $request ) {
278284
}
279285

280286
// Handle generate_sub_sizes parameter.
281-
if ( isset( $request['generate_sub_sizes'] ) && ! $request['generate_sub_sizes'] ) {
287+
if ( false === $request['generate_sub_sizes'] ) {
282288
add_filter( 'intermediate_image_sizes_advanced', '__return_empty_array', 100 );
283289
add_filter( 'fallback_intermediate_image_sizes', '__return_empty_array', 100 );
284290
// Disable server-side EXIF rotation so the client can handle it.

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,80 @@ public function test_upload_unsupported_image_type_with_filter() {
29292929
$this->assertSame( 201, $response->get_status() );
29302930
}
29312931

2932+
/**
2933+
* Test that unsupported image type check is skipped when not generating sub-sizes.
2934+
*
2935+
* When the client handles image processing (generate_sub_sizes is false),
2936+
* the server should not check image editor support.
2937+
*
2938+
* Tests the permissions check directly with file params set, since the core
2939+
* check uses get_file_params() which is only populated for multipart uploads.
2940+
*
2941+
* @ticket 64836
2942+
*/
2943+
public function test_upload_unsupported_image_type_skipped_when_not_generating_sub_sizes() {
2944+
wp_set_current_user( self::$author_id );
2945+
2946+
add_filter( 'wp_image_editors', '__return_empty_array' );
2947+
2948+
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
2949+
$request->set_file_params(
2950+
array(
2951+
'file' => array(
2952+
'name' => 'avif-lossy.avif',
2953+
'type' => 'image/avif',
2954+
'tmp_name' => self::$test_avif_file,
2955+
'error' => 0,
2956+
'size' => filesize( self::$test_avif_file ),
2957+
),
2958+
)
2959+
);
2960+
$request->set_param( 'generate_sub_sizes', false );
2961+
2962+
$controller = new WP_REST_Attachments_Controller( 'attachment' );
2963+
$result = $controller->create_item_permissions_check( $request );
2964+
2965+
// Should pass because generate_sub_sizes is false (client handles processing).
2966+
$this->assertTrue( $result );
2967+
}
2968+
2969+
/**
2970+
* Test that unsupported image type check is enforced when generating sub-sizes.
2971+
*
2972+
* When the server handles image processing (generate_sub_sizes is true),
2973+
* the server should still check image editor support.
2974+
*
2975+
* Tests the permissions check directly with file params set, since the core
2976+
* check uses get_file_params() which is only populated for multipart uploads.
2977+
*
2978+
* @ticket 64836
2979+
*/
2980+
public function test_upload_unsupported_image_type_enforced_when_generating_sub_sizes() {
2981+
wp_set_current_user( self::$author_id );
2982+
2983+
add_filter( 'wp_image_editors', '__return_empty_array' );
2984+
2985+
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
2986+
$request->set_file_params(
2987+
array(
2988+
'file' => array(
2989+
'name' => 'avif-lossy.avif',
2990+
'type' => 'image/avif',
2991+
'tmp_name' => self::$test_avif_file,
2992+
'error' => 0,
2993+
'size' => filesize( self::$test_avif_file ),
2994+
),
2995+
)
2996+
);
2997+
2998+
$controller = new WP_REST_Attachments_Controller( 'attachment' );
2999+
$result = $controller->create_item_permissions_check( $request );
3000+
3001+
// Should fail because the server needs to generate sub-sizes but can't.
3002+
$this->assertWPError( $result );
3003+
$this->assertSame( 'rest_upload_image_type_not_supported', $result->get_error_code() );
3004+
}
3005+
29323006
/**
29333007
* Test that uploading an SVG image doesn't throw a `rest_upload_image_type_not_supported` error.
29343008
*

0 commit comments

Comments
 (0)