import { assert } from 'oak-domain/lib/utils/assert'; import { OakUploadException } from '../../types/Exception'; import { OakNetworkException } from 'oak-domain/lib/types/Exception'; export default class Local { name = 'local'; autoInform() { return false; } getConfig(application) { const { system } = application; const { config } = system; const localConfig = config.Cos?.local; assert(localConfig); const { accessKey } = localConfig; const account = config.Account?.local?.find((ele) => ele.accessKey === accessKey); assert(account); return { config: localConfig, account }; } formKey(extraFile) { const { id, extension, objectId } = extraFile; assert(objectId); return `extraFile/${objectId}${extension ? '.' + extension : ''}`; } async upload(options) { const { extraFile, uploadFn, file, uploadToAspect, getPercent } = options; let result; const { applicationId, type, extra2, id } = extraFile; try { result = (await uploadToAspect(file, 'file', 'uploadExtraFile', { applicationId, extraFileId: id, }, true)); } catch (err) { // 网络错误 throw new OakNetworkException('网络异常,请求失败'); } const isSuccess = result.success === true; if (isSuccess) { return; } else { throw new OakUploadException('文件上传服务器失败'); } } composeFileUrl(options) { const { application, extraFile, style } = options; const { config: localConfig } = this.getConfig(application); let bucket = localConfig.buckets.find((ele) => ele.name === extraFile.bucket); if (bucket) { const { domain, protocol, nginxPath } = bucket; let protocol2 = protocol; if (protocol instanceof Array) { // protocol存在https: 说明域名有证书 const index = protocol.includes('https:') ? protocol.findIndex((ele) => ele === 'https:') : 0; protocol2 = protocol[index]; } return `${protocol2}//${domain}${nginxPath ? '/' + nginxPath : ''}/${this.formKey(extraFile)}${style ? style : ''}`; } return ''; } }