@@ -33,6 +33,7 @@ const spawn = child.spawn;
3333const Fs = require ( 'fs' ) ;
3434const REGEXP_SVG = / ( w i d t h = \" \d + \" ) + | ( h e i g h t = \" \d + \" ) + / g;
3535const REGEXP_PATH = / \/ / g;
36+ const REGEXP_ARG = / \' / g;
3637
3738var CACHE = { } ;
3839var middlewares = { } ;
@@ -122,7 +123,7 @@ function Image(filename, useImageMagick, width, height) {
122123 this . width = width ;
123124 this . height = height ;
124125 this . builder = [ ] ;
125- this . filename = type === 'string' ? filename . escape_bash ( ) : null ;
126+ this . filename = type === 'string' ? filename : null ;
126127 this . currentStream = type === 'object' ? filename : null ;
127128 this . isIM = useImageMagick == null ? F . config [ 'default-image-converter' ] === 'im' : useImageMagick ;
128129 this . outputType = type === 'string' ? framework_utils . getExtension ( filename ) : 'jpg' ;
@@ -194,8 +195,7 @@ Image.prototype.save = function(filename, callback, writer) {
194195 if ( typeof ( filename ) === 'function' ) {
195196 callback = filename ;
196197 filename = null ;
197- } else if ( filename )
198- filename = filename . escape_bash ( ) ;
198+ }
199199
200200 ! self . builder . length && self . minify ( ) ;
201201 filename = filename || self . filename || '' ;
@@ -228,9 +228,7 @@ Image.prototype.save = function(filename, callback, writer) {
228228 var writer = Fs . createWriteStream ( filename + '_' ) ;
229229
230230 reader . pipe ( middleware ( ) ) . pipe ( writer ) ;
231- writer . on ( 'finish' , function ( ) {
232- Fs . rename ( filename + '_' , filename , ( ) => callback ( null , true ) ) ;
233- } ) ;
231+ writer . on ( 'finish' , ( ) => Fs . rename ( filename + '_' , filename , ( ) => callback ( null , true ) ) ) ;
234232 } ) ;
235233
236234 if ( self . currentStream ) {
@@ -267,7 +265,7 @@ Image.prototype.pipe = function(stream, type, options) {
267265 if ( ! type )
268266 type = self . outputType ;
269267
270- var cmd = spawn ( self . isIM ? 'convert' : 'gm' , self . arg ( self . filename ? self . filename : '-' , ( type ? type + ':' : '' ) + '-' ) ) ;
268+ var cmd = spawn ( self . isIM ? 'convert' : 'gm' , self . arg ( self . filename ? wrap ( self . filename ) : '-' , ( type ? type + ':' : '' ) + '-' ) ) ;
271269
272270 cmd . stderr . on ( 'data' , stream . emit . bind ( stream , 'error' ) ) ;
273271 cmd . stdout . on ( 'data' , stream . emit . bind ( stream , 'data' ) ) ;
@@ -305,7 +303,7 @@ Image.prototype.stream = function(type, writer) {
305303 if ( ! type )
306304 type = self . outputType ;
307305
308- var cmd = spawn ( self . isIM ? 'convert' : 'gm' , self . arg ( self . filename ? self . filename : '-' , ( type ? type + ':' : '' ) + '-' ) ) ;
306+ var cmd = spawn ( self . isIM ? 'convert' : 'gm' , self . arg ( self . filename ? wrap ( self . filename ) : '-' , ( type ? type + ':' : '' ) + '-' ) ) ;
309307 if ( self . currentStream ) {
310308 if ( self . currentStream instanceof Buffer )
311309 cmd . stdin . end ( self . currentStream ) ;
@@ -333,7 +331,7 @@ Image.prototype.cmd = function(filenameFrom, filenameTo) {
333331 for ( var i = 0 ; i < length ; i ++ )
334332 cmd += ( cmd ? ' ' : '' ) + self . builder [ i ] . cmd ;
335333
336- return ( self . isIM ? 'convert' : 'gm -convert' ) + ' "' + filenameFrom + '"' + ' ' + cmd + ' "' + filenameTo + '"' ;
334+ return ( self . isIM ? 'convert' : 'gm -convert' ) + wrap ( filenameFrom , true ) + ' ' + cmd + wrap ( filenameTo , true ) ;
337335} ;
338336
339337Image . prototype . arg = function ( first , last ) {
@@ -371,7 +369,7 @@ Image.prototype.arg = function(first, last) {
371369Image . prototype . identify = function ( callback ) {
372370 var self = this ;
373371
374- exec ( ( self . isIM ? 'identify' : 'gm identify' ) + ' "' + self . filename + '"' , function ( err , stdout , stderr ) {
372+ exec ( ( self . isIM ? 'identify' : 'gm identify' ) + wrap ( self . filename , true ) , function ( err , stdout , stderr ) {
375373
376374 if ( err ) {
377375 callback ( err , null ) ;
@@ -400,9 +398,9 @@ Image.prototype.push = function(key, value, priority, encode) {
400398
401399 if ( value != null ) {
402400 if ( encode && typeof ( value ) === 'string' )
403- cmd += ' "' + value . escape_bash ( ) + '"' ;
401+ cmd += wrap ( value , true ) ;
404402 else
405- cmd += ' "' + value + '" ' ;
403+ cmd += ' \'' + value + '\' ' ;
406404 }
407405
408406 var obj = CACHE [ cmd ] ;
@@ -676,6 +674,13 @@ Image.prototype.command = function(key, value, priority, esc) {
676674 return this . push ( key , value , priority || 10 , esc ) ;
677675} ;
678676
677+ function wrap ( command , empty ) {
678+ var cmd = '' ;
679+ for ( var i = 0 , length = command . length ; i < length ; i ++ )
680+ cmd += command [ i ] === '\'' ? '\\' + command [ i ] : command [ i ] ;
681+ return ( empty ? ' ' : '' ) + '\'' + cmd + '\'' ;
682+ }
683+
679684exports . Image = Image ;
680685exports . Picture = Image ;
681686
0 commit comments