@ -16,16 +16,13 @@ uploadsController.uploadCategoryPicture = function(req, res, next) {
try {
params = JSON . parse ( req . body . params ) ;
} catch ( e ) {
var err = {
error : 'Error uploading file! Error :' + e . message
} ;
fs . unlink ( uploadedFile . path ) ;
return res. se nd( r eq. xhr ? err : JSON . s tringify ( e rr) ) ;
return next ( e ) ;
}
if ( validateUpload ( req , res , uploadedFile, allowedTypes ) ) {
if ( validateUpload ( req , res , next, uploadedFile, allowedTypes ) ) {
var filename = 'category-' + params . cid + path . extname ( uploadedFile . name ) ;
uploadImage ( filename , 'category' , uploadedFile , req , res );
uploadImage ( filename , 'category' , uploadedFile , req , res , next );
}
} ;
@ -33,13 +30,14 @@ uploadsController.uploadFavicon = function(req, res, next) {
var uploadedFile = req . files . files [ 0 ] ;
var allowedTypes = [ 'image/x-icon' , 'image/vnd.microsoft.icon' ] ;
if ( validateUpload ( res , req , uploadedFile, allowedTypes ) ) {
if ( validateUpload ( res , req , next, uploadedFile, allowedTypes ) ) {
file . saveFileToLocal ( 'favicon.ico' , 'files' , uploadedFile . path , function ( err , image ) {
fs . unlink ( uploadedFile . path ) ;
if ( err ) {
return next ( err ) ;
}
var response = err ? { error : err . message } : [ { name : uploadedFile . name , url : image . url } ] ;
res . send ( req . xhr ? response : JSON . stringify ( response ) ) ;
res . json ( [ { name : uploadedFile . name , url : image . url } ] ) ;
} ) ;
}
} ;
@ -55,33 +53,31 @@ uploadsController.uploadGravatarDefault = function(req, res, next) {
function upload ( name , req , res , next ) {
var uploadedFile = req . files . files [ 0 ] ;
var allowedTypes = [ 'image/png' , 'image/jpeg' , 'image/pjpeg' , 'image/jpg' , 'image/gif' ] ;
if ( validateUpload ( req , res , uploadedFile, allowedTypes ) ) {
if ( validateUpload ( req , res , next, uploadedFile, allowedTypes ) ) {
var filename = name + path . extname ( uploadedFile . name ) ;
uploadImage ( filename , 'files' , uploadedFile , req , res );
uploadImage ( filename , 'files' , uploadedFile , req , res , next );
}
}
function validateUpload ( req , res , uploadedFile, allowedTypes ) {
function validateUpload ( req , res , next, uploadedFile, allowedTypes ) {
if ( allowedTypes . indexOf ( uploadedFile . type ) === - 1 ) {
var err = {
error : 'Invalid image type. Allowed types are: ' + allowedTypes . join ( ', ' )
} ;
fs . unlink ( uploadedFile . path ) ;
res . send ( req . xhr ? err : JSON . stringify ( err ) ) ;
next ( new Error ( '[[error:invalid-image-type, ' + allowedTypes . join ( ', ' ) + ']]' ) ) ;
return false ;
}
return true ;
}
function uploadImage ( filename , folder , uploadedFile , req , res ) {
function uploadImage ( filename , folder , uploadedFile , req , res , next ) {
function done ( err , image ) {
fs . unlink ( uploadedFile . path ) ;
if ( err ) {
return next ( err ) ;
}
var response = err ? { error : err . message } : [ { name : uploadedFile . name , url : image . url } ] ;
res . send ( req . xhr ? response : JSON . stringify ( response ) ) ;
res . json ( [ { name : uploadedFile . name , url : image . url } ] ) ;
}
if ( plugins . hasListeners ( 'filter:uploadImage' ) ) {