Skip to content

Commit b2b6123

Browse files
committed
Throw error if name argument is not provided to .file() when uploading a buffer (#418)
Fixes #344 props @cungminh2710, @mvhirsch
1 parent 1b0650f commit b2b6123

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ Files may be uploaded to the WordPress media library by creating a media record
524524
The file to upload can be specified as
525525

526526
- a `String` describing an image file path, _e.g._ `'/path/to/the/image.jpg'`
527-
- a `Buffer` with file content, _e.g._ `new Buffer()`
527+
- a `Buffer` with file content, _e.g._ `Buffer.from()` (or the result of a `readFile` call)
528528
- a file object from a `<input>` element, _e.g._ `document.getElementById( 'file-input' ).files[0]`
529529

530530
The file is passed into the `.file()` method:
@@ -533,7 +533,7 @@ The file is passed into the `.file()` method:
533533
wp.media().file(content [, name])...
534534
```
535535

536-
The optional second string argument specifies the file name to use for the uploaded media. If the name argument is omitted `file()` will try to infer a filename from the provided content.
536+
The optional second string argument specifies the file name to use for the uploaded media. If the name argument is omitted `file()` will try to infer a filename from the provided file path. Note that when uploading a Buffer object `name` is a required argument, because no name can be automatically inferred from the buffer.
537537

538538
#### Adding Media to a Post
539539

lib/constructors/wp-request.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ WPRequest.prototype.auth = function( credentials ) {
633633
* .file( '/path/to/file.jpg' )
634634
* .create({})...
635635
*
636+
* wp.media()
637+
* // Pass .file() an image as a Buffer object, and a filename string
638+
* .file( imgBuffer, 'desired-title.jpg' )
639+
* .create({})...
640+
*
636641
* @example <caption>within a browser context</caption>
637642
*
638643
* wp.media()
@@ -644,10 +649,14 @@ WPRequest.prototype.auth = function( credentials ) {
644649
* @chainable
645650
* @param {string|object} file A path to a file (in Node) or an file object
646651
* (Node or Browser) to attach to the request
647-
* @param {string} [name] An (optional) filename to use for the file
652+
* @param {string} [name] A filename to use for the file, required when
653+
* providing file data as a Buffer object
648654
* @returns {WPRequest} The WPRequest instance (for chaining)
649655
*/
650656
WPRequest.prototype.file = function( file, name ) {
657+
if ( global.Buffer && file instanceof global.Buffer && ! name ) {
658+
throw new Error( '.file(): File name is a required argument when uploading a Buffer' );
659+
}
651660
this._attachment = file;
652661
// Explicitly set to undefined if not provided, to override any previously-
653662
// set attachment name property that might exist from a prior `.file()` call

0 commit comments

Comments
 (0)