Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper named File when uploading external images #65693

Merged
merged 1 commit into from
Sep 28, 2024
Merged
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 @@ -26,6 +26,7 @@ import { moreVertical, external } from '@wordpress/icons';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { isBlobURL } from '@wordpress/blob';
import { getFilename } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -167,8 +168,13 @@ export function MediaPreview( { media, onClick, category } ) {
.fetch( url )
.then( ( response ) => response.blob() )
.then( ( blob ) => {
const fileName = getFilename( url ) || 'image.jpg';
Copy link
Contributor

@sgomes sgomes Sep 27, 2024

Choose a reason for hiding this comment

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

Note for other reviewers: the fallback uses .jpg regardless of the actual file type, since we rely on the server to choose an appropriate file extension for us. We do need to provide some image-related file extension, however, even if it's wrong, otherwise the server will outright reject the upload.

const file = new File( [ blob ], fileName, {
type: blob.type,
} );

settings.mediaUpload( {
filesList: [ blob ],
filesList: [ file ],
additionalData: { caption },
onFileChange( [ img ] ) {
if ( isBlobURL( img.url ) ) {
Expand Down
Loading