Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
714fa5a
try: add error when uploading image type not supported by server
adamsilverstein Jan 10, 2025
a1d3fc1
Merge branch 'trunk' of github.com:WordPress/gutenberg into trunk
adamsilverstein Jan 13, 2025
605bf32
Fix settings to match file.type
adamsilverstein Jan 13, 2025
571d4ef
Code cleanup
adamsilverstein Jan 13, 2025
b5c1bd9
Merge branch 'trunk' into fix/image-upload-types
adamsilverstein Jan 20, 2025
f97809b
move new settings to lib/compat
adamsilverstein Jan 20, 2025
0bcf76c
add newline at end of file
adamsilverstein Jan 21, 2025
610fa15
Merge branch 'trunk' into fix/image-upload-types
adamsilverstein Feb 3, 2025
1045b9a
Build typesNotSupportedByServer in loop
adamsilverstein Feb 3, 2025
a2fb518
Swith typesNotSupportedByServer to simple array of types
adamsilverstein Feb 3, 2025
7c2e8b6
Revert change in use-media-upload-settings.js
adamsilverstein Feb 3, 2025
0964ef6
fix/image-upload-types
adamsilverstein Feb 3, 2025
ee096ab
Check all image types for support
adamsilverstein Feb 3, 2025
8ff3fef
Doc blocks: clarify data is unsupported images
adamsilverstein Feb 3, 2025
fc21009
Revert line move
adamsilverstein Feb 3, 2025
9d6a22e
Move check into validateMimeTypeForServer
adamsilverstein Feb 3, 2025
4a761b7
Revert validateMimeTypeForUser
adamsilverstein Feb 3, 2025
87c7eb8
Add a test
adamsilverstein Feb 3, 2025
edaa08f
correct test message
adamsilverstein Feb 3, 2025
ef1eaa2
Move files to proper location
adamsilverstein Feb 3, 2025
0739a9d
Correct upload test logic
adamsilverstein Feb 3, 2025
6c972c5
ensure settings are loaded correctly
adamsilverstein Feb 3, 2025
052e3ad
Correct UploadError import
adamsilverstein Feb 3, 2025
1fd268b
Rename serverUnsupportedTypes -> wpUnsupportedMimeTypes
adamsilverstein Feb 4, 2025
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
Prev Previous commit
Next Next commit
fix/image-upload-types
  • Loading branch information
adamsilverstein committed Feb 3, 2025
commit 0964ef6797061a380f9bb50f076d273eceadd817
4 changes: 2 additions & 2 deletions lib/compat/wordpress-6.8/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* @return array Modified settings.
*/
function gutenberg_extend_block_editor_settings_with_image_support( $settings ) {
$settings['typesNotSupportedByServer'] = array();
$settings['serverUnsupportedTypes'] = array();

$image_types_to_check_for_support = array( 'image/webp', 'image/avif', 'image/heic' );
foreach ( $image_types_to_check_for_support as $type ) {
// Check if image type can be edited
if ( ! wp_image_editor_supports( array( 'mime_type' => $type ) ) ) {
$settings['typesNotSupportedByServer'][] = $type;
$settings['serverUnsupportedTypes'][] = $type;
}
}
return $settings;
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const SETTINGS_DEFAULTS = {
allowedMimeTypes: null,

// List of types not supported by the server.
typesNotSupportedByServer: [],
serverUnsupportedTypes: [],

// Allows to disable block locking interface.
canLockBlocks: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const BLOCK_EDITOR_SETTINGS = [
'blockInspectorTabs',
'maxUploadFileSize',
'allowedMimeTypes',
'typesNotSupportedByServer',
'serverUnsupportedTypes',
'bodyPlaceholder',
'canLockBlocks',
'canUpdateBlockBindings',
Expand Down
5 changes: 2 additions & 3 deletions packages/editor/src/utils/media-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export default function mediaUpload( {
unlockPostSaving,
} = dispatch( editorStore );

const { wpAllowedMimeTypes, typesNotSupportedByServer } =
getEditorSettings();
const { wpAllowedMimeTypes, serverUnsupportedTypes } = getEditorSettings();
const lockKey = `image-upload-${ uuid() }`;
let imageIsUploading = false;
maxUploadFileSize =
Expand Down Expand Up @@ -93,6 +92,6 @@ export default function mediaUpload( {
onError( message );
},
wpAllowedMimeTypes,
typesNotSupportedByServer,
serverUnsupportedTypes,
} );
}
4 changes: 2 additions & 2 deletions packages/media-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _Parameters_
- _$0.onError_ `UploadMediaArgs[ 'onError' ]`: Function called when an error happens.
- _$0.onFileChange_ `UploadMediaArgs[ 'onFileChange' ]`: Function called each time a file or a temporary representation of the file is available.
- _$0.signal_ `UploadMediaArgs[ 'signal' ]`: Abort signal.
- _$0.typesNotSupportedByServer_ `UploadMediaArgs[ 'typesNotSupportedByServer' ]`: List of types not supported by the server.
- _$0.serverUnsupportedTypes_ `UploadMediaArgs[ 'serverUnsupportedTypes' ]`: List of types not supported by the server.

### validateFileSize

Expand All @@ -75,7 +75,7 @@ _Parameters_

- _file_ `File`: File object.
- _allowedTypes_ `string[]`: List of allowed mime types.
- _typesNotSupportedByServer_ `[Record< string, boolean >]`: List of types not supported by the server.
- _serverUnsupportedTypes_ `[Record< string, boolean >]`: List of types not supported by the server.

### validateMimeTypeForUser

Expand Down
30 changes: 13 additions & 17 deletions packages/media-utils/src/utils/upload-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ interface UploadMediaArgs {
// Abort signal.
signal?: AbortSignal;
// List of types not supported by the server.
typesNotSupportedByServer?: Record< string, boolean >;
serverUnsupportedTypes?: Record< string, boolean >;
}
/**
* Upload a media file when the file upload button is activated
* or when adding a file to the editor via drag & drop.
*
* @param $0 Parameters object passed to the function.
* @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
* @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.
* @param $0.additionalData Additional data to include in the request.
* @param $0.filesList List of files.
* @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.
* @param $0.onError Function called when an error happens.
* @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.
* @param $0.signal Abort signal.
* @param $0.typesNotSupportedByServer List of types not supported by the server.
* @param $0 Parameters object passed to the function.
* @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
* @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.
* @param $0.additionalData Additional data to include in the request.
* @param $0.filesList List of files.
* @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.
* @param $0.onError Function called when an error happens.
* @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.
* @param $0.signal Abort signal.
* @param $0.serverUnsupportedTypes List of types not supported by the server.
*/
export function uploadMedia( {
wpAllowedMimeTypes,
Expand All @@ -69,7 +69,7 @@ export function uploadMedia( {
onError,
onFileChange,
signal,
typesNotSupportedByServer,
serverUnsupportedTypes,
}: UploadMediaArgs ) {
const validFiles = [];

Expand Down Expand Up @@ -100,11 +100,7 @@ export function uploadMedia( {
// Check if the caller (e.g. a block) supports this mime type.
// Defer to the server when type not detected.
try {
validateMimeType(
mediaFile,
allowedTypes,
typesNotSupportedByServer
);
validateMimeType( mediaFile, allowedTypes, serverUnsupportedTypes );
} catch ( error: unknown ) {
onError?.( error as Error );
continue;
Expand Down
10 changes: 5 additions & 5 deletions packages/media-utils/src/utils/validate-mime-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { UploadError } from './upload-error';
/**
* Verifies if the caller (e.g. a block) supports this mime type.
*
* @param file File object.
* @param allowedTypes List of allowed mime types.
* @param [typesNotSupportedByServer] List of types not supported by the server.
* @param file File object.
* @param allowedTypes List of allowed mime types.
* @param [serverUnsupportedTypes] List of types not supported by the server.
*/
export function validateMimeType(
file: File,
allowedTypes?: string[],
typesNotSupportedByServer?: Record< string, boolean >
serverUnsupportedTypes?: Record< string, boolean >
) {
if ( ! allowedTypes ) {
return;
Expand Down Expand Up @@ -46,7 +46,7 @@ export function validateMimeType(
} );
}

if ( typesNotSupportedByServer && typesNotSupportedByServer[ file.type ] ) {
if ( serverUnsupportedTypes && serverUnsupportedTypes[ file.type ] ) {
throw new UploadError( {
code: 'MIME_TYPE_NOT_SUPPORTED',
message: sprintf(
Expand Down
Loading