"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_backend_1 = require("../utils/cos/index.backend"); const Exception_1 = require("oak-domain/lib/types/Exception"); const Projection_1 = require("../types/Projection"); const triggers = [ { name: '生成extraFile需要的上传meta', when: 'before', entity: 'extraFile', action: 'create', fn: async ({ operation }, context) => { const { data } = operation; const formMeta = async (data) => { const { origin } = data; if (!data.applicationId) { data.applicationId = context.getApplicationId(); } if (origin === 'unknown') { Object.assign(data, { uploadState: 'success', }); return; } const cos = (0, index_backend_1.getCosBackend)(origin); if (!cos) { throw new Exception_1.OakException(`origin为${origin}的extraFile没有定义Cos类,请调用registerCos注入`); } await cos.formUploadMeta(context.getApplication(), data); Object.assign(data, { uploadState: 'uploading', }); }; if (data instanceof Array) { await Promise.all(data.map(ele => formMeta(ele))); return data.length; } await formMeta(data); return 1; } }, { name: '删除extraFile时远端也进行删除', when: 'commit', strict: 'makeSure', entity: 'extraFile', action: 'remove', fn: async ({ ids }, context) => { const fn = context.openRootMode(); const rows = await context.select('extraFile', { data: { id: 1, origin: 1, objectId: 1, bucket: 1, extension: 1, applicationId: 1, application: Projection_1.applicationProjection }, filter: { id: { $in: ids }, }, }, { includedDeleted: true }); for (const extraFile of rows) { const { origin, objectId, application } = extraFile; // 用objectId来去重,只有当没有还有效的objectId方可删除 const count = await context.count('extraFile', { filter: { objectId: objectId, }, }, {}); if (count === 0) { const uploader = (0, index_backend_1.getCosBackend)(origin); const application2 = context.getApplication() || application; await uploader.removeFile(application2, extraFile); } } fn(); } }, ]; exports.default = triggers;