"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInfoByUrl = getInfoByUrl; exports.uploadExtraFile = uploadExtraFile; exports.mergeChunkedUpload = mergeChunkedUpload; exports.presignFile = presignFile; exports.presignMultiPartUpload = presignMultiPartUpload; const tslib_1 = require("tslib"); const WechatSDK_1 = tslib_1.__importDefault(require("oak-external-sdk/lib/WechatSDK")); const uuid_1 = require("oak-domain/lib/utils/uuid"); const fs_1 = tslib_1.__importDefault(require("fs")); const assert_1 = require("oak-domain/lib/utils/assert"); const lodash_1 = require("oak-domain/lib/utils/lodash"); const Projection_1 = require("../types/Projection"); const index_backend_1 = require("../utils/cos/index.backend"); // 请求链接获取标题,发布时间,图片等信息 async function getInfoByUrl(params) { const { url } = params; return await WechatSDK_1.default.analyzePublicArticle(url); } async function uploadExtraFile(params, // FormData表单提交 context) { const { applicationId, file, extraFileId, } = params; (0, assert_1.assert)(applicationId); const filename = file.originalFilename; const filetype = file.mimetype; const fileLength = file.size; const fileStream = fs_1.default.createReadStream(file.filepath); const [application] = await context.select('application', { data: (0, lodash_1.cloneDeep)(Projection_1.applicationProjection), filter: { id: applicationId, }, }, { dontCollect: true, }); const { type, config } = application; // 保存文件 if (extraFileId) { const closeRootMode = context.openRootMode(); try { await context.operate('extraFile', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'update', data: {}, filter: { id: extraFileId, }, }, {}); closeRootMode(); } catch (err) { closeRootMode(); throw err; } } return { success: true, }; } /** * 合并分片上传的文件 */ async function mergeChunkedUpload(params, context) { const { extraFileId } = params; (0, assert_1.assert)(extraFileId, 'extraFileId不能为空'); const [extrafile] = await context.select('extraFile', { data: { ...Projection_1.extraFileProjection, application: { ...Projection_1.applicationProjection, }, enableChunkedUpload: 1, chunkInfo: 1, }, filter: { id: extraFileId, } }, { dontCollect: true }); (0, assert_1.assert)(extrafile, `找不到id为${extraFileId}的extraFile记录`); (0, assert_1.assert)(extrafile.enableChunkedUpload, `extraFile ${extraFileId} 未启用分片上传功能`); (0, assert_1.assert)(extrafile.chunkInfo, `extraFile ${extraFileId} 的chunkInfo信息缺失`); (0, assert_1.assert)(!extrafile.chunkInfo.merged, `extraFile ${extraFileId} 已经合并过分片,无需重复合并`); // 必须保证所有分片都有上传完成 const cos = (0, index_backend_1.getCosBackend)(extrafile.origin); const { parts } = await cos.listMultipartUploads(extrafile.application, extrafile, context); const allPartsDone = parts.every(part => part.etag && part.size > 0); (0, assert_1.assert)(allPartsDone, `extraFile ${extraFileId} 存在未上传完成的分片,无法合并`); await cos.mergeChunkedUpload(extrafile.application, extrafile, parts.map(part => ({ partNumber: part.partNumber, etag: part.etag, })), context); // 更新chunkInfo状态 const closeRootMode = context.openRootMode(); try { await context.operate('extraFile', { id: await (0, uuid_1.generateNewIdAsync)(), action: 'update', data: { chunkInfo: { ...extrafile.chunkInfo, merged: true, parts: parts.map(part => part.etag), }, }, filter: { id: extraFileId, }, }, {}); closeRootMode(); } catch (err) { closeRootMode(); throw err; } } async function presignFile(params, context) { const { extraFileId, method = 'GET' } = params; (0, assert_1.assert)(extraFileId, 'extraFileId不能为空'); const [extrafile] = await context.select('extraFile', { data: { ...Projection_1.extraFileProjection, application: { ...Projection_1.applicationProjection, }, }, filter: { id: extraFileId, } }, { dontCollect: true }); (0, assert_1.assert)(extrafile, `找不到id为${extraFileId}的extraFile记录`); const cos = (0, index_backend_1.getCosBackend)(extrafile.origin); return await cos.presignFile(method, extrafile.application, extrafile, context); } async function presignMultiPartUpload(params, context) { const { extraFileId, from, to } = params; (0, assert_1.assert)(extraFileId, 'extraFileId不能为空'); (0, assert_1.assert)(from >= 1, 'from必须大于等于1'); (0, assert_1.assert)(to >= from, 'to必须大于等于from'); const [extrafile] = await context.select('extraFile', { data: { ...Projection_1.extraFileProjection, application: { ...Projection_1.applicationProjection, }, chunkInfo: 1, enableChunkedUpload: 1, }, filter: { id: extraFileId, } }, { dontCollect: true }); (0, assert_1.assert)(extrafile, `找不到id为${extraFileId}的extraFile记录`); const cos = (0, index_backend_1.getCosBackend)(extrafile.origin); return cos.presignMultiPartUpload(extrafile.application, extrafile, from, to, context); }