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
111 changes: 81 additions & 30 deletions lib/media/class-gutenberg-rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Gutenberg_REST_Attachments_Controller extends WP_REST_Attachments_Controll
*
* @var string
*/
const IMAGE_SIZE_SOURCE_ORIGINAL = 'original-heic';
const IMAGE_SIZE_SOURCE_ORIGINAL = 'source_original';

/**
* Metadata key holding the basename of the source-format original.
Expand All @@ -38,19 +38,19 @@ class Gutenberg_REST_Attachments_Controller extends WP_REST_Attachments_Controll
* as a companion of the GIF attachment.
*
* Paired with META_KEY_ANIMATED_VIDEO: used both in the `/sideload` route
* and when writing the sideloaded file to its metadata key, so the
* hyphenated size token and the underscored meta key never drift apart.
* and when writing the sideloaded file to its metadata key. Both use the
* underscore convention so the size token and meta key stay consistent.
*
* @var string
*/
const IMAGE_SIZE_ANIMATED_VIDEO = 'animated-video';
const IMAGE_SIZE_ANIMATED_VIDEO = 'animated_video';

/**
* Image size token for the static first-frame poster of a converted GIF.
*
* @var string
*/
const IMAGE_SIZE_ANIMATED_VIDEO_POSTER = 'animated-video-poster';
const IMAGE_SIZE_ANIMATED_VIDEO_POSTER = 'animated_video_poster';

/**
* Metadata key holding the basename of the converted animated-GIF video.
Expand Down Expand Up @@ -83,11 +83,11 @@ public function register_routes(): void {
'callback' => array( $this, 'sideload_item' ),
'permission_callback' => array( $this, 'sideload_item_permissions_check' ),
'args' => array(
'id' => array(
'id' => array(
'description' => __( 'Unique identifier for the attachment.', 'gutenberg' ),
'type' => 'integer',
),
'image_size' => array(
'image_size' => array(
'description' => __( 'Image size. Can be a single size name or an array of size names to register the same file under multiple sizes.', 'gutenberg' ),
'type' => array( 'string', 'array' ),
'items' => array(
Expand Down Expand Up @@ -132,12 +132,7 @@ public function register_routes(): void {
return true;
},
),
'generate_sub_sizes' => array(
'description' => __( 'Whether to generate image sub sizes from the sideloaded file.', 'gutenberg' ),
'type' => 'boolean',
'default' => false,
),
'convert_format' => array(
'convert_format' => array(
'description' => __( 'Whether to convert image formats.', 'gutenberg' ),
'type' => 'boolean',
'default' => true,
Expand Down Expand Up @@ -192,7 +187,8 @@ public function register_routes(): void {
'minimum' => 1,
),
'file' => array(
'type' => 'string',
'type' => 'string',
'minLength' => 1,
),
'mime_type' => array(
'type' => 'string',
Expand All @@ -203,7 +199,8 @@ public function register_routes(): void {
'minimum' => 1,
),
'original_image' => array(
'type' => 'string',
'type' => 'string',
'minLength' => 1,
),
),
),
Expand All @@ -220,14 +217,38 @@ public function register_routes(): void {
* Checks if a given request has access to create an attachment.
*
* Skips the server-side image type support check when the client
* will handle image processing (generate_sub_sizes is false).
* will handle image processing (generate_sub_sizes is false). Still
* HEIC/HEIF uploads always skip the check, since the browser's canvas
* fallback can decode them even when the server cannot.
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
*/
public function create_item_permissions_check( $request ) {
$bypass_mime_check = false === $request['generate_sub_sizes'];

/*
* Always allow still HEIC/HEIF uploads through even if the server's
* image editor doesn't support them. The client-side canvas fallback
* handles processing using the browser's native HEVC decoder.
*
* The '-sequence' variants (multi-frame Live Photos) are deliberately
* excluded: neither the server nor the browser fallback can process
* them yet, so they should fall through to the standard unsupported
* mime-type error rather than be stored unprocessable.
*/
if ( ! $bypass_mime_check ) {
$still_heic_mime_types = array( 'image/heic', 'image/heif' );
$files = $request->get_file_params();

if (
! empty( $files['file']['type'] ) &&
in_array( $files['file']['type'], $still_heic_mime_types, true )
) {
$bypass_mime_check = true;
}
}

if ( $bypass_mime_check ) {
add_filter( 'wp_prevent_unsupported_mime_type_uploads', '__return_false' );
}
Expand Down Expand Up @@ -538,7 +559,7 @@ public function finalize_item( WP_REST_Request $request ) {
continue;
}

if ( 'original' === $image_size ) {
if ( 'original' === $image_size && isset( $sub_size['file'] ) ) {
$metadata['original_image'] = $sub_size['file'];
} elseif ( self::IMAGE_SIZE_SOURCE_ORIGINAL === $image_size ) {
// Source-format original: stored under its own meta key so the
Expand Down Expand Up @@ -678,13 +699,20 @@ private static function filter_wp_unique_filename( $filename, $dir, $number, $at
* @return true|WP_Error True if valid, WP_Error if invalid.
*/
private function validate_image_dimensions( int $width, int $height, $image_size, int $attachment_id ) {
// 'animated-video' companion file: video, not an image. Skip *all*
// 'animated_video' companion file: video, not an image. Skip *all*
// dimension checks (the caller passes (0, 0) for this case so the
// positive-dimension assertion below would otherwise fire).
if ( self::IMAGE_SIZE_ANIMATED_VIDEO === $image_size ) {
return true;
}

// Source-format original companion file: no dimension constraint, and
// the caller passes (0, 0) because the source format (e.g. HEIC) may
// not be readable by wp_getimagesize() at all.
if ( self::IMAGE_SIZE_SOURCE_ORIGINAL === $image_size ) {
return true;
}

// Dimensions must be positive for all sizes.
if ( $width <= 0 || $height <= 0 ) {
return new WP_Error(
Expand All @@ -706,12 +734,7 @@ private function validate_image_dimensions( int $width, int $height, $image_size
return true;
}

// Source-format original companion file: no dimension constraint.
if ( self::IMAGE_SIZE_SOURCE_ORIGINAL === $image_size ) {
return true;
}

// 'animated-video-poster' companion: a static poster image for the
// 'animated_video_poster' companion: a static poster image for the
// converted video. It is a real image (so it has positive dimensions)
// but is not a registered sub-size, so it has no dimension constraint.
if ( self::IMAGE_SIZE_ANIMATED_VIDEO_POSTER === $image_size ) {
Expand Down Expand Up @@ -891,11 +914,20 @@ public function sideload_item( WP_REST_Request $request ) {
// below. Scalar 'original' is a byte-only passthrough and does not need
// dimensions, but reading them here is harmless.
//
// 'animated-video' companions are video files (MP4/WebM); the image
// 'animated_video' companions are video files (MP4/WebM); the image
// helpers can't read their dimensions and would falsely report the
// upload as "corrupted or unsupported". Skip the read for this case;
// validate_image_dimensions() also short-circuits it below.
$size = self::IMAGE_SIZE_ANIMATED_VIDEO === $image_size ? array( 0, 0 ) : wp_getimagesize( $path );
// upload as "corrupted or unsupported". Source-format originals
// ('source_original', e.g. the HEIC kept next to its JPEG derivative)
// are exempt for the same reason: their dimensions are neither
// validated nor recorded, and wp_getimagesize() may not be able to
// read the source format at all on servers without HEIC/HEIF support.
// Skip the read for both cases; validate_image_dimensions() also
// short-circuits them below.
$skip_dimension_read =
self::IMAGE_SIZE_ANIMATED_VIDEO === $image_size ||
self::IMAGE_SIZE_SOURCE_ORIGINAL === $image_size;

$size = $skip_dimension_read ? array( 0, 0 ) : wp_getimagesize( $path );

if ( ! $size ) {
// Could not determine dimensions (corrupted file, unsupported format).
Expand Down Expand Up @@ -952,12 +984,31 @@ public function sideload_item( WP_REST_Request $request ) {
$sub_size_data['file'] = wp_basename( $path );
} elseif ( 'scaled' === $image_size ) {
// Record the current attached file as the original.
$current_file = get_attached_file( $attachment_id, true );
$current_file = get_attached_file( $attachment_id, true );

if ( ! $current_file ) {
return new WP_Error(
'rest_sideload_no_attached_file',
__( 'Unable to retrieve the attached file for this attachment.', 'gutenberg' ),
array( 'status' => 404 )
);
}

$sub_size_data['original_image'] = wp_basename( $current_file );

// Update the attached file to point to the scaled version.
// This writes to _wp_attached_file meta, not _wp_attachment_metadata.
update_attached_file( $attachment_id, $path );
// Guard against a failed update so a stale original is not recorded.
if (
get_attached_file( $attachment_id, true ) !== $path &&
! update_attached_file( $attachment_id, $path )
) {
return new WP_Error(
'rest_sideload_update_attached_file_failed',
__( 'Unable to update the attached file for this attachment.', 'gutenberg' ),
array( 'status' => 500 )
);
}

$sub_size_data['width'] = $size[0];
$sub_size_data['height'] = $size[1];
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-media/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ export function cancelItem( id: QueueItemId, error: Error, silent = false ) {
* even when the companion is the only child sideload.
*/
const isOptionalCompanion =
item.additionalData?.image_size === 'animated-video' ||
item.additionalData?.image_size === 'animated-video-poster';
item.additionalData?.image_size === 'animated_video' ||
item.additionalData?.image_size === 'animated_video_poster';

if ( select.hasPendingItemsByParentId( parentId ) ) {
// Other children remain — just notify the parent so
Expand Down
12 changes: 7 additions & 5 deletions packages/upload-media/src/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ type TranscodeGifItemArgs = OperationArgs[ OperationType.TranscodeGif ];
* Runs inside a sideload item whose parent is the GIF's image attachment
* (see generateThumbnails). The next Upload op then sideloads the
* transcoded video as a companion of that attachment under the
* `animated-video` image size; the GIF stays the primary attachment and
* `animated_video` image size; the GIF stays the primary attachment and
* the editor block stays `core/image`.
*
* @param id Item ID.
Expand Down Expand Up @@ -1407,7 +1407,7 @@ export function transcodeGifItem(
parentId: item.parentId,
additionalData: {
post: item.additionalData?.post,
image_size: 'animated-video-poster',
image_size: 'animated_video_poster',
convert_format: false,
},
operations: [
Expand Down Expand Up @@ -1488,15 +1488,17 @@ export function generateThumbnails( id: QueueItemId ) {
// sideload flow owns. The HEIC was kept on item.originalHeicFile;
// the uploaded file is a JPEG conversion. parentId guarantees
// processItem routes this to the sideload endpoint, never the main
// create endpoint.
// create endpoint. The `source_original` image_size token is
// format-agnostic (it also covers HEIF) and matches the server-side
// WP_REST_Attachments_Controller::IMAGE_SIZE_SOURCE_ORIGINAL constant.
if ( item.originalHeicFile && attachment.id ) {
dispatch.addSideloadItem( {
file: item.originalHeicFile,
batchId: uuidv4(),
parentId: item.id,
additionalData: {
post: attachment.id,
image_size: 'original-heic',
image_size: 'source_original',
convert_format: false,
},
operations: [ OperationType.Upload ],
Expand All @@ -1519,7 +1521,7 @@ export function generateThumbnails( id: QueueItemId ) {
parentId: item.id,
additionalData: {
post: attachment.id,
image_size: 'animated-video',
image_size: 'animated_video',
convert_format: false,
},
operations: [
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-media/src/store/test/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ describe( 'actions', () => {
);
} );

it( 'keeps the parent GIF when its only child is a failed animated-video companion', async () => {
it( 'keeps the parent GIF when its only child is a failed animated_video companion', async () => {
const mediaDelete = jest.fn().mockResolvedValue( undefined );
const parentOnError = jest.fn();
unlock( registry.dispatch( uploadStore ) ).updateSettings( {
Expand All @@ -887,7 +887,7 @@ describe( 'actions', () => {
// accumulated sub-sizes — the pre-fix "total failure" case.
const { parent, child } = setUpParentAndChild( {
parentOnError,
imageSize: 'animated-video',
imageSize: 'animated_video',
} );

await registry
Expand Down
6 changes: 3 additions & 3 deletions packages/upload-media/src/store/test/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ describe( 'private actions', () => {
expect( poster.additionalData ).toEqual(
expect.objectContaining( {
post: 42,
image_size: 'animated-video-poster',
image_size: 'animated_video_poster',
convert_format: false,
} )
);
Expand Down Expand Up @@ -866,7 +866,7 @@ describe( 'private actions', () => {
);
}

it( 'sideloads the converted video as an animated-video companion', async () => {
it( 'sideloads the converted video as an animated_video companion', async () => {
const gif = makeGif();
const item = {
id: 'g',
Expand All @@ -892,7 +892,7 @@ describe( 'private actions', () => {
expect( sideload.additionalData ).toEqual(
expect.objectContaining( {
post: 42,
image_size: 'animated-video',
image_size: 'animated_video',
convert_format: false,
} )
);
Expand Down
Loading
Loading