import { uploadFile, getSignature } from '@/api/base'
import md5 from 'blueimp-md5'

export function uploadBlob(blob) {
  const fileName = `${Math.random().toString(36).slice(-8)}.png`
  let fileUrl = ''
  const key = 'upload/certificate/' + md5(fileName + new Date().getTime()) + fileName.substr(fileName.lastIndexOf('.'))
  return new Promise((resolve, reject) => {
    getSignature().then(response => {
      const { accessid, policy, signature, host } = response
      const data = { key, OSSAccessKeyId: accessid, policy, signature, success_action_status: '200', file: blob }
      fileUrl = `${host}/${key}`
      uploadFile(data).then(res => {
        resolve(fileUrl)
      })
    })
  })
}
