-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Media Utils: add experimental
sideloadMedia
- Loading branch information
1 parent
a775b7c
commit d969b0b
Showing
12 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis'; | ||
|
||
export const { lock, unlock } = | ||
__dangerousOptInToUnstableAPIsOnlyForCoreModules( | ||
'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.', | ||
'@wordpress/media-utils' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { sideloadMedia } from './utils/sideload-media'; | ||
import { lock } from './lock-unlock'; | ||
|
||
/** | ||
* Private @wordpress/media-utils APIs. | ||
*/ | ||
export const privateApis = {}; | ||
|
||
lock( privateApis, { | ||
sideloadMedia, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { | ||
OnChangeHandler, | ||
OnErrorHandler, | ||
CreateSideloadFile, | ||
RestAttachment, | ||
} from './types'; | ||
import { sideloadToServer } from './sideload-to-server'; | ||
import { UploadError } from './upload-error'; | ||
|
||
const noop = () => {}; | ||
|
||
interface SideloadMediaArgs { | ||
// Additional data to include in the request. | ||
additionalData?: CreateSideloadFile; | ||
// File to sideload. | ||
file: File; | ||
// Attachment ID. | ||
attachmentId: RestAttachment[ 'id' ]; | ||
// Function called when an error happens. | ||
onError?: OnErrorHandler; | ||
// Function called each time a file or a temporary representation of the file is available. | ||
onFileChange?: OnChangeHandler; | ||
// Abort signal. | ||
signal?: AbortSignal; | ||
} | ||
|
||
/** | ||
* Uploads a file to the server without creating an attachment. | ||
* | ||
* @param $0 Parameters object passed to the function. | ||
* @param $0.file Media File to Save. | ||
* @param $0.attachmentId Parent attachment ID. | ||
* @param $0.additionalData Additional data to include in the request. | ||
* @param $0.signal Abort signal. | ||
* @param $0.onFileChange Function called each time a file or a temporary representation of the file is available. | ||
* @param $0.onError Function called when an error happens. | ||
*/ | ||
export async function sideloadMedia( { | ||
file, | ||
attachmentId, | ||
additionalData = {}, | ||
signal, | ||
onFileChange, | ||
onError = noop, | ||
}: SideloadMediaArgs ) { | ||
try { | ||
const attachment = await sideloadToServer( | ||
file, | ||
attachmentId, | ||
additionalData, | ||
signal | ||
); | ||
onFileChange?.( [ attachment ] ); | ||
} catch ( error ) { | ||
let message; | ||
if ( error instanceof Error ) { | ||
message = error.message; | ||
} else { | ||
message = sprintf( | ||
// translators: %s: file name | ||
__( 'Error while sideloading file %s to the server.' ), | ||
file.name | ||
); | ||
} | ||
onError( | ||
new UploadError( { | ||
code: 'GENERAL', | ||
message, | ||
file, | ||
cause: error instanceof Error ? error : undefined, | ||
} ) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { CreateSideloadFile, RestAttachment } from './types'; | ||
import { flattenFormData } from './flatten-form-data'; | ||
import { transformAttachment } from './transform-attachment'; | ||
|
||
/** | ||
* Uploads a file to the server without creating an attachment. | ||
* | ||
* @param file Media File to Save. | ||
* @param attachmentId Parent attachment ID. | ||
* @param additionalData Additional data to include in the request. | ||
* @param signal Abort signal. | ||
* | ||
* @return The saved attachment. | ||
*/ | ||
export async function sideloadToServer( | ||
file: File, | ||
attachmentId: RestAttachment[ 'id' ], | ||
additionalData: CreateSideloadFile = {}, | ||
signal?: AbortSignal | ||
) { | ||
// Create upload payload. | ||
const data = new FormData(); | ||
data.append( 'file', file, file.name || file.type.replace( '/', '.' ) ); | ||
for ( const [ key, value ] of Object.entries( additionalData ) ) { | ||
flattenFormData( | ||
data, | ||
key, | ||
value as string | Record< string, string > | undefined | ||
); | ||
} | ||
|
||
return transformAttachment( | ||
await apiFetch< RestAttachment >( { | ||
path: `/wp/v2/media/${ attachmentId }/sideload`, | ||
body: data, | ||
method: 'POST', | ||
signal, | ||
} ) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { sideloadMedia } from '../sideload-media'; | ||
import { sideloadToServer } from '../sideload-to-server'; | ||
|
||
jest.mock( '../sideload-to-server', () => ( { | ||
sideloadToServer: jest.fn(), | ||
} ) ); | ||
|
||
const imageFile = new window.File( [ 'fake_file' ], 'test.jpeg', { | ||
type: 'image/jpeg', | ||
} ); | ||
|
||
describe( 'sideloadMedia', () => { | ||
afterEach( () => { | ||
jest.clearAllMocks(); | ||
} ); | ||
|
||
it( 'should sideload to server', async () => { | ||
const onError = jest.fn(); | ||
const onFileChange = jest.fn(); | ||
await sideloadMedia( { | ||
file: imageFile, | ||
attachmentId: 1, | ||
onError, | ||
onFileChange, | ||
} ); | ||
|
||
expect( sideloadToServer ).toHaveBeenCalled(); | ||
expect( onFileChange ).toHaveBeenCalled(); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters