From 336cd3d63200ba25c230210ad75760a13d7ae3e9 Mon Sep 17 00:00:00 2001 From: Daryl Koopersmith Date: Thu, 27 Sep 2012 05:52:42 +0000 Subject: [PATCH] When uploading files through the Plupload bridge, attempt to detect images on upload. see #21390. git-svn-id: http://core.svn.wordpress.org/trunk@22041 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/plupload/wp-plupload.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/wp-includes/js/plupload/wp-plupload.js b/wp-includes/js/plupload/wp-plupload.js index 31f21f6260..9149e24ada 100644 --- a/wp-includes/js/plupload/wp-plupload.js +++ b/wp-includes/js/plupload/wp-plupload.js @@ -142,11 +142,29 @@ if ( typeof wp === 'undefined' ) this.uploader.bind( 'FilesAdded', function( up, files ) { _.each( files, function( file ) { - file.attachment = wp.media.model.Attachment.create( _.extend({ + var attributes, image; + + // Generate attributes for a new `Attachment` model. + attributes = _.extend({ file: file, uploading: true, date: new Date() - }, _.pick( file, 'loaded', 'size', 'percent' ) ) ); + }, _.pick( file, 'loaded', 'size', 'percent' ) ); + + // Handle early mime type scanning for images. + image = /(?:jpe?g|png|gif)$/i.exec( file.name ); + + // Did we find an image? + if ( image ) { + attributes.type = 'image'; + + // `jpeg`, `png` and `gif` are valid subtypes. + // `jpg` is not, so map it to `jpeg`. + attributes.subtype = ( 'jpg' === image[0] ) ? 'jpeg' : image[0]; + } + + // Create the `Attachment`. + file.attachment = wp.media.model.Attachment.create( attributes ); Uploader.queue.add( file.attachment );