Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE
$args['generate_sub_sizes'] = array(
'type' => 'boolean',
'default' => true,
'description' => __( 'Whether to generate image sub sizes.' ),
'description' => __( 'Whether to generate image sub sizes. When false, the main image is also not converted to the image output format.' ),
);

$args['convert_format'] = array(
Expand Down Expand Up @@ -503,6 +503,7 @@ public function create_item_permissions_check( $request ) {
*
* @since 4.7.0
* @since 7.1.0 Added the `generate_sub_sizes`, `convert_format`, and `url` parameters.
* @since 7.3.0 The main image is no longer converted to the image output format when `generate_sub_sizes` is false.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
Expand Down Expand Up @@ -634,6 +635,14 @@ public function create_item( $request ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';

/*
* The client sideloads the converted main file. This is added after the upload is
* stored, so wp_unique_filename() still reserves the converted file names.
*/
if ( false === $request['generate_sub_sizes'] ) {
add_filter( 'image_editor_output_format', '__return_empty_array', 100 );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmmm, i'm not sure disabling the filtered mapping is the best solution here, how else could we fix this?

Can we make this change at a different part of the stack?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

how else could we fix this? Can we make this change at a different part of the stack?

The mapping is still used. The upload response still returns image/webp as image_output_format (the new test checks that), and the browser uses it to convert the main file and the sub sizes. This filter only stops the server from converting the main file during the upload request, so the server stores only the original, as you described on the ticket. It's the same filter convert_format: false already adds in create_item().

The other part of the stack is the client: WordPress/gutenberg#87135 makes the browser create the converted main file, which the server used to make.

I also tried a server only fix: keep the server's conversion, and when the browser's scaled or rotated main file arrives in sideload_item(), keep photo.jpg as original_image and delete the server's photo.webp. That stops the leak too, but the server still converts every upload at full size only to delete it, which is the work client side processing is meant to move to the browser.

}

/*
* Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta.
* At this point the server may run out of resources and post-processing of uploaded images may fail.
Expand Down Expand Up @@ -3074,6 +3083,16 @@ public function sideload_item( WP_REST_Request $request ) {
);
}

// Update the post mime type when the supplied image is in a different format.
if ( get_post_mime_type( $attachment_id ) !== $type ) {
wp_update_post(
array(
'ID' => $attachment_id,
'post_mime_type' => $type,
)
);
}

$sub_size_data['width'] = $size[0];
$sub_size_data['height'] = $size[1];
$sub_size_data['filesize'] = $filesize;
Expand Down
131 changes: 131 additions & 0 deletions tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4490,6 +4490,137 @@ static function () {
$this->assertSame( array(), array_values( $remaining ), 'Deleting the attachment should leave no files behind.' );
}

/**
* Tests that the main file of a client-side upload is not converted when an
* `image_editor_output_format` filter maps it to another format, since the
* client sideloads the converted main file.
*
* @ticket 66763
*
* @covers WP_REST_Attachments_Controller::create_item
* @requires function imagejpeg
*/
public function test_client_side_upload_main_file_is_not_converted() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'This test requires an image editor that can save WebP.' );
}

$this->enable_client_side_media_processing();

wp_set_current_user( self::$author_id );

add_filter( 'image_editor_output_format', array( $this, 'filter_output_format_jpeg_to_webp' ) );

$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
$request->set_header( 'Content-Type', 'image/jpeg' );
$request->set_header( 'Content-Disposition', 'attachment; filename=client-side-photo.jpg' );
$request->set_param( 'generate_sub_sizes', false );
$request->set_body( file_get_contents( self::$test_file ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 201, $response->get_status(), 'Uploading the image should succeed.' );
$this->assertSame( 'image/webp', $data['image_output_format'], 'The client should still be asked to convert to WebP.' );

$attached_file = get_attached_file( $data['id'] );
$this->assertSame( 'client-side-photo.jpg', wp_basename( $attached_file ), 'The upload should stay the attached file.' );
$this->assertFileDoesNotExist( dirname( $attached_file ) . '/client-side-photo.webp', 'The server should not write a converted copy.' );
}

/**
* Tests that a client-side upload whose main file the client converts keeps the
* upload as `original_image` and leaves no files behind after it is deleted.
*
* @ticket 66763
* @ticket 53665
*
* @dataProvider data_client_side_converted_main_file_flow
*
* @covers WP_REST_Attachments_Controller::create_item
* @covers WP_REST_Attachments_Controller::sideload_item
* @covers WP_REST_Attachments_Controller::finalize_item
* @requires function imagejpeg
*
* @param string $image_size The image size the client sideloads the main file as.
* @param string $source The test image to upload.
* @param string $main_file The expected name of the converted main file.
*/
public function test_client_side_converted_main_file_flow_leaves_no_files( string $image_size, string $source, string $main_file ) {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'This test requires an image editor that can save WebP.' );
}

$this->enable_client_side_media_processing();

wp_set_current_user( self::$author_id );

add_filter( 'image_editor_output_format', array( $this, 'filter_output_format_jpeg_to_webp' ) );

$upload_dir = wp_upload_dir();
$files_before = (array) glob( $upload_dir['path'] . '/*' );

$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
$request->set_header( 'Content-Type', 'image/jpeg' );
$request->set_header( 'Content-Disposition', 'attachment; filename=flow-photo.jpg' );
$request->set_param( 'generate_sub_sizes', false );
$request->set_body( file_get_contents( DIR_TESTDATA . '/images/' . $source ) );
$response = rest_get_server()->dispatch( $request );
$attachment_id = $response->get_data()['id'];

$this->assertSame( 201, $response->get_status(), 'Uploading the image should succeed.' );

// Convert the main file as the client does, scaled down for a 'scaled' sideload.
$editor = wp_get_image_editor( DIR_TESTDATA . '/images/' . $source );
if ( 'scaled' === $image_size ) {
$editor->resize( 1000, 1000 );
}
$converted = $editor->save( get_temp_dir() . 'flow-photo-converted.webp', 'image/webp' );

$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment_id}/sideload" );
$request->set_header( 'Content-Type', 'image/webp' );
$request->set_header( 'Content-Disposition', 'attachment; filename=' . $main_file );
$request->set_param( 'image_size', $image_size );
$request->set_param( 'convert_format', false );
$request->set_body( file_get_contents( $converted['path'] ) );
$response = rest_get_server()->dispatch( $request );
$main_data = $response->get_data();
unlink( $converted['path'] );

$this->assertSame( 200, $response->get_status(), 'Sideloading the converted main file should succeed.' );

$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment_id}/finalize" );
$request->set_param( 'sub_sizes', array( $main_data ) );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'Finalize should succeed.' );

$metadata = wp_get_attachment_metadata( $attachment_id );

$this->assertSame( 'flow-photo.jpg', $metadata['original_image'], 'The untouched upload should be recorded as original_image.' );
$this->assertSame( $main_file, wp_basename( $metadata['file'] ), 'The converted main file should become the attached file.' );
$this->assertSame( 'image/webp', get_post_mime_type( $attachment_id ), 'The attachment MIME type should match the converted main file.' );

$written = array_map( 'wp_basename', array_diff( (array) glob( $upload_dir['path'] . '/*' ), $files_before ) );
$this->assertEqualsCanonicalizing( array( 'flow-photo.jpg', $main_file ), $written, 'Only the upload and the converted main file should be written.' );

wp_delete_attachment( $attachment_id, true );

$remaining = array_diff( (array) glob( $upload_dir['path'] . '/*' ), $files_before );
$this->assertSame( array(), array_values( $remaining ), 'Deleting the attachment should leave no files behind.' );
}

/**
* Data provider.
*
* @return array<string, array{ 0: string, 1: string, 2: string }>
*/
public static function data_client_side_converted_main_file_flow(): array {
return array(
'scaled image' => array( 'scaled', '33772.jpg', 'flow-photo-scaled.webp' ),
'original image' => array( 'original', 'canola.jpg', 'flow-photo.webp' ),
);
}

/**
* Tests that sideloading scaled image requires authentication.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -3152,7 +3152,7 @@ mockedApiResponse.Schema = {
"generate_sub_sizes": {
"type": "boolean",
"default": true,
"description": "Whether to generate image sub sizes.",
"description": "Whether to generate image sub sizes. When false, the main image is also not converted to the image output format.",
"required": false
},
"convert_format": {
Expand Down
Loading