@@ -7,6 +7,7 @@ var config = require('../config');
77var _ = require ( 'lodash' ) ;
88var validator = require ( 'validator' ) ;
99var qiniu = require ( "qiniu" ) ;
10+ var upyun = require ( 'upyun' ) ;
1011var common = { } ;
1112var AppError = require ( '../app-error' ) ;
1213var jschardet = require ( "jschardet" ) ;
@@ -225,6 +226,8 @@ common.uploadFileToStorage = function (key, filePath) {
225226 return common . uploadFileToOSS ( key , filePath ) ;
226227 } else if ( storageType === 'qiniu' ) {
227228 return common . uploadFileToQiniu ( key , filePath ) ;
229+ } else if ( storageType === 'upyun' ) {
230+ return common . uploadFileToUpyun ( key , filePath ) ;
228231 } else if ( storageType === 'tencentcloud' ) {
229232 return common . uploadFileToTencentCloud ( key , filePath ) ;
230233 }
@@ -350,6 +353,43 @@ common.uploadFileToQiniu = function (key, filePath) {
350353 } ) ;
351354} ;
352355
356+ common . uploadFileToUpyun = function ( key , filePath ) {
357+ var serviceName = _ . get ( config , "upyun.serviceName" ) ;
358+ var operatorName = _ . get ( config , "upyun.operatorName" ) ;
359+ var operatorPass = _ . get ( config , "upyun.operatorPass" , "" ) ;
360+ var storageDir = _ . get ( config , "upyun.storageDir" , "" ) ;
361+ var service = new upyun . Service ( serviceName , operatorName , operatorPass ) ;
362+ var client = new upyun . Client ( service ) ;
363+ return (
364+ new Promise ( ( resolve , reject ) => {
365+ client . makeDir ( storageDir ) . then ( result => {
366+ if ( ! storageDir ) {
367+ reject ( new AppError . AppError ( 'Please config the upyun remoteDir!' ) ) ;
368+ return ;
369+ }
370+ let remotePath = storageDir + '/' + key ;
371+ log . debug ( 'uploadFileToUpyun remotePath:' , remotePath ) ;
372+ log . debug ( 'uploadFileToUpyun mkDir result:' , result ) ;
373+ client . putFile ( remotePath , fs . createReadStream ( filePath ) ) . then ( data => {
374+ log . debug ( 'uploadFileToUpyun putFile response:' , data ) ;
375+ if ( data ) {
376+ resolve ( key )
377+ } else {
378+ log . debug ( 'uploadFileToUpyun putFile failed!' , data ) ;
379+ reject ( new AppError . AppError ( 'Upload file to upyun failed!' ) ) ;
380+ }
381+ } ) . catch ( e1 => {
382+ log . debug ( 'uploadFileToUpyun putFile exception e1:' , e1 ) ;
383+ reject ( new AppError . AppError ( JSON . stringify ( e1 ) ) ) ;
384+ } )
385+ } ) . catch ( e => {
386+ log . debug ( 'uploadFileToUpyun putFile exception e:' , e ) ;
387+ reject ( new AppError . AppError ( JSON . stringify ( e ) ) ) ;
388+ } ) ;
389+ } )
390+ ) ;
391+ } ;
392+
353393common . uploadFileToS3 = function ( key , filePath ) {
354394 var AWS = require ( 'aws-sdk' ) ;
355395 return (
0 commit comments