text: const axios = require('axios')
  const constants = require('core-module/constants')
  const HError = require('core-module/HError')
  const utils = require('core-module/utils')
  const {getUploadFileConfig} = require('core-module/upload')
  module.exports = function (BaaS) {
    /**
     * 上传文件。
     * @memberof BaaS
     * @param {FileParams} fileParams 文件参数
     * @param {FileMeta} metaData 文件元信息
     * @return {Promise<any>}
     */
    BaaS.uploadFile = function (fileParams, metaData) {
      let fileObj = fileParams.fileObj
      if (!fileObj || typeof fileObj !== 'object' || !fileObj.name) {
        throw new HError(605)
      }
  
      if(fileParams.fileName !== undefined && typeof fileParams.fileName !== 'string'){
        throw new HError(605)
      }
    
      if (!metaData) {
        metaData = {}
      } else if (typeof metaData !== 'object') {
        throw new HError(605)
      }
  
      let config = {}
      let fileName = fileParams.fileName || fileObj.name
      return getUploadFileConfig(fileName, utils.replaceQueryParams(metaData)).then(res => {
        config = {
          id: res.data.id,
          fileName: fileName,
          policy: res.data.policy,
          authorization: res.data.authorization,
          uploadUrl: res.data.upload_url,
          filePath: fileObj.name,
          destLink: res.data.path,
        }
  
        let fd = new FormData()
        fd.append(constants.UPLOAD.UPLOAD_FILE_KEY, fileObj, fileName)
        fd.append('policy', config.policy)
        fd.append('authorization', config.authorization)
  
        return axios.post(config.uploadUrl, fd)
      }).then(res => {
        let result = {}
        let data = res.data
        result.status = 'ok'
        result.path = config.destLink
        result.file = {
          'id': config.id,
          'path': config.destLink,
          'name': config.fileName,
          'created_at': data.time,
          'mime_type': data.mimetype,
          'cdn_path': data.url,
          'size': data.file_size,
        }
        res.data = result
        return res
      })
    }
  }
  