-
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.
- Loading branch information
1 parent
c48c800
commit 1b70586
Showing
25 changed files
with
954 additions
and
312 deletions.
There are no files selected for viewing
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export * from './components'; | ||
|
||
export { uploadMedia } from './utils/upload-media'; | ||
export { transformAttachment } from './utils/transform-attachment'; | ||
export { validateFileSize } from './utils/validate-file-size'; | ||
export { validateMimeType } from './utils/validate-mime-type'; | ||
export { validateMimeTypeForUser } from './utils/validate-mime-type-for-user'; | ||
|
||
export type { Attachment, RestAttachment } from './utils/types'; |
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,26 @@ | ||
/** | ||
* Recursively flatten data passed to form data, to allow using multi-level objects. | ||
* | ||
* @param {FormData} formData Form data object. | ||
* @param {string} key Key to amend to form data object | ||
* @param {string|Object} data Data to be amended to form data. | ||
*/ | ||
export function flattenFormData( | ||
formData: FormData, | ||
key: string, | ||
data: string | undefined | Record< string, string > | ||
) { | ||
if ( typeof data === 'object' ) { | ||
for ( const name in data ) { | ||
if ( Object.prototype.hasOwnProperty.call( data, name ) ) { | ||
flattenFormData( | ||
formData, | ||
`${ key }[${ name }]`, | ||
data[ name ] | ||
); | ||
} | ||
} | ||
} else if ( data !== undefined ) { | ||
formData.append( key, data ); | ||
} | ||
} |
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,29 @@ | ||
/** | ||
* Browsers may use unexpected mime types, and they differ from browser to browser. | ||
* This function computes a flexible array of mime types from the mime type structured provided by the server. | ||
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ] | ||
* | ||
* @param {?Object} wpMimeTypesObject Mime type object received from the server. | ||
* Extensions are keys separated by '|' and values are mime types associated with an extension. | ||
* | ||
* @return An array of mime types or null | ||
*/ | ||
export function getMimeTypesArray( | ||
wpMimeTypesObject?: Record< string, string > | null | ||
) { | ||
if ( ! wpMimeTypesObject ) { | ||
return null; | ||
} | ||
return Object.entries( wpMimeTypesObject ).flatMap( | ||
( [ extensionsString, mime ] ) => { | ||
const [ type ] = mime.split( '/' ); | ||
const extensions = extensionsString.split( '|' ); | ||
return [ | ||
mime, | ||
...extensions.map( | ||
( extension ) => `${ type }/${ extension }` | ||
), | ||
]; | ||
} | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
packages/media-utils/src/utils/test/get-mime-types-array.ts
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,47 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getMimeTypesArray } from '../get-mime-types-array'; | ||
|
||
describe( 'getMimeTypesArray', () => { | ||
it( 'should return null if it is "falsy" e.g: undefined or null', () => { | ||
expect( getMimeTypesArray( null ) ).toEqual( null ); | ||
expect( getMimeTypesArray( undefined ) ).toEqual( null ); | ||
} ); | ||
|
||
it( 'should return an empty array if an empty object is passed', () => { | ||
expect( getMimeTypesArray( {} ) ).toEqual( [] ); | ||
} ); | ||
|
||
it( 'should return the type plus a new mime type with type and subtype with the extension if a type is passed', () => { | ||
expect( getMimeTypesArray( { ext: 'chicken' } ) ).toEqual( [ | ||
'chicken', | ||
'chicken/ext', | ||
] ); | ||
} ); | ||
|
||
it( 'should return the mime type passed and a new mime type with type and the extension as subtype', () => { | ||
expect( getMimeTypesArray( { ext: 'chicken/ribs' } ) ).toEqual( [ | ||
'chicken/ribs', | ||
'chicken/ext', | ||
] ); | ||
} ); | ||
|
||
it( 'should return the mime type passed and an additional mime type per extension supported', () => { | ||
expect( getMimeTypesArray( { 'jpg|jpeg|jpe': 'image/jpeg' } ) ).toEqual( | ||
[ 'image/jpeg', 'image/jpg', 'image/jpeg', 'image/jpe' ] | ||
); | ||
} ); | ||
|
||
it( 'should handle multiple mime types', () => { | ||
expect( | ||
getMimeTypesArray( { 'ext|aaa': 'chicken/ribs', aaa: 'bbb' } ) | ||
).toEqual( [ | ||
'chicken/ribs', | ||
'chicken/ext', | ||
'chicken/aaa', | ||
'bbb', | ||
'bbb/aaa', | ||
] ); | ||
} ); | ||
} ); |
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,24 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { UploadError } from '../upload-error'; | ||
|
||
describe( 'UploadError', () => { | ||
it( 'holds error code and file name', () => { | ||
const file = new File( [], 'example.jpg', { | ||
lastModified: 1234567891, | ||
type: 'image/jpeg', | ||
} ); | ||
|
||
const error = new UploadError( { | ||
code: 'some_error', | ||
message: 'An error occurred', | ||
file, | ||
} ); | ||
|
||
expect( error ).toStrictEqual( expect.any( Error ) ); | ||
expect( error.code ).toBe( 'some_error' ); | ||
expect( error.message ).toBe( 'An error occurred' ); | ||
expect( error.file ).toBe( file ); | ||
} ); | ||
} ); |
Oops, something went wrong.