import { getRequestByEnvId } from '../lib/request' ; import { createPromiseCallback } from '../lib/util' ; import { MetaDataRes } from '../types' ; export const uploadFile = function ( params , callback ? ) { callback = callback || createPromiseCallback ( ) ; const request = getRequestByEnvId ( this . config . env ) ; const metaData = 'storage.getUploadMetadata' ; const { cloudPath , filePath , onUploadProgress } = params ; request . send ( metaData , { path : cloudPath } ) . then ( metaData => { const { data : { url , authorization , token , fileId , cosFileId } , requestId } = metaData ; const data = { key : cloudPath , signature : authorization , 'x-cos-meta-fileid' : cosFileId , 'success_action_status' : '201' , 'x-cos-security-token' : token } ; request . upload ( { url , data , file : filePath , name : cloudPath , onUploadProgress } ) . then ( ( res ) => { if ( res . statusCode === 201 ) { callback ( null , { fileID : fileId , requestId } ) ; } else { callback ( new Error ( `STORAGE_REQUEST_FAIL: ${ res . data }` ) ) ; } } ) . catch ( err => { callback ( err ) ; } ) ; } ) . catch ( err => { callback ( err ) ; } ) ; return callback . promise ; } ; export const getUploadMetadata = function ( params , callback ? ) { callback = callback || createPromiseCallback ( ) ; const request = getRequestByEnvId ( this . config . env ) ; const metaData = 'storage.getUploadMetadata' ; const { cloudPath } = params ; request . send ( metaData , { path : cloudPath } ) . then ( metaData => { callback ( null , metaData ) ; } ) . catch ( err => { callback ( err ) ; } ) ; return callback . promise ; } ; param { Array . < string > } fileList export const deleteFile = function ( { fileList } , callback ? ) { callback = callback || createPromiseCallback ( ) ; if ( ! fileList || ! Array . isArray ( fileList ) ) { return { code : 'INVALID_PARAM' , message : 'fileList必须是非空的数组' } ; } for ( let file of fileList ) { if ( ! file || typeof file !== 'string' ) { return { code : 'INVALID_PARAM' , message : 'fileList的元素必须是非空的字符串' } ; } } const action = 'storage.batchDeleteFile' ; const params = { fileid_list : fileList } ; const request = getRequestByEnvId ( this . config . env ) ; request . send ( action , params ) . then ( res => { if ( res . code ) { callback ( null , res ) ; } else { callback ( null , { fileList : res . data . delete_list , requestId : res . requestId } ) ; } } ) . catch ( err => { callback ( err ) ; } ) ; return callback . promise ; } ; param { Array . < Object > } fileList export const getTempFileURL = function ( { fileList } , callback ? ) { callback = callback || createPromiseCallback ( ) ; if ( ! fileList || ! Array . isArray ( fileList ) ) { callback ( null , { code : 'INVALID_PARAM' , message : 'fileList必须是非空的数组' } ) ; } let file_list = [ ] ; for ( let file of fileList ) { if ( typeof file === 'object' ) { if ( ! file . hasOwnProperty ( 'fileID' ) || ! file . hasOwnProperty ( 'maxAge' ) ) { callback ( null , { code : 'INVALID_PARAM' , message : 'fileList的元素必须是包含fileID和maxAge的对象' } ) ; } file_list . push ( { fileid : file . fileID , max_age : file . maxAge } ) ; } else if ( typeof file === 'string' ) { file_list . push ( { fileid : file } ) ; } else { callback ( null , { code : 'INVALID_PARAM' , message : 'fileList的元素必须是字符串' } ) ; } } const action = 'storage.batchGetDownloadUrl' ; const params = { file_list } ; const request = getRequestByEnvId ( this . config . env ) ; request . send ( action , params ) . then ( res => { if ( res . code ) { callback ( null , res ) ; } else { callback ( null , { fileList : res . data . download_list , requestId : res . requestId } ) ; } } ) . catch ( err => { callback ( err ) ; } ) ; return callback . promise ; } ; export const downloadFile = async function ( { fileID } , callback ? ) { const tmpUrlRes = await getTempFileURL . call ( this , { fileList : [ { fileID , maxAge : 600 } ] } ) ; const res = tmpUrlRes . fileList [ 0 ] ; if ( res . code !== 'SUCCESS' ) { return callback ? callback ( res ) : new Promise ( resolve => { resolve ( res ) ; } ) ; } const request = getRequestByEnvId ( this . config . env ) ; let tmpUrl = res . download_url ; tmpUrl = encodeURI ( tmpUrl ) ; if ( callback ) { const result = await request . download ( { url : tmpUrl } ) ; callback ( result ) ; } else { return request . download ( { url : tmpUrl } ) ; } } ;