import { getUploadSign, onOnlyFileUpload } from '@/utils/oss-file-upload'
const bucketName = 'gyt'
const ACL = 'public-read'
const ossUploadUrl = `https://${bucketName}.ks3-cn-beijing.ksyuncs.com/`

const getPolicy = async (fileName: string) => {
  const obj = {
    filename: fileName,
    bucketName: bucketName,
    postData: {
      filename: fileName,
      acl: ACL,
      key: fileName,
      unknowValueField: []
    }
  }
  const { data } = await getUploadSign(obj)
  console.log(data)
  return data
}
const upload = async (policy: any, fileName: string, file: any, progress: Function) => {
  const fileParams = {
    policy: policy.policy,
    signature: policy.signature,
    key: fileName,
    KSSAccessKeyId: policy.kssAccessKeyId,
    acl: ACL,
    name: fileName,
    file: file
  }
  return await onOnlyFileUpload(ossUploadUrl, fileParams)
}

export const api_uploadFile = async (file: any, progress: Function) => {
  const fileName = Date.now() + file.name?.slice(file.name.lastIndexOf('.'))
  const policy = await getPolicy(fileName)
  const url = await upload(policy, fileName, file, progress)
  return url
}
