UNPKG

@luka-cat-mimi/n8n-nodes-feishu-project

Version:
102 lines 4.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const n8n_workflow_1 = require("n8n-workflow"); const RequestUtils_1 = __importDefault(require("../../../help/utils/RequestUtils")); const NodeUtils_1 = __importDefault(require("../../../help/utils/NodeUtils")); const sharedOptions_1 = require("../../../help/utils/sharedOptions"); const description_1 = require("../../../help/description"); const form_data_1 = __importDefault(require("form-data")); const MAX_FILE_SIZE = 100 * 1024 * 1024; const parseUploadResponse = (response) => { if (typeof response === 'string') { const trimmed = response.trim(); if (!trimmed) { return undefined; } if (trimmed.startsWith('{') || trimmed.startsWith('[')) { try { return parseUploadResponse(JSON.parse(trimmed)); } catch { return trimmed.startsWith('http') ? trimmed : undefined; } } return trimmed; } if (Buffer.isBuffer(response) || response instanceof Uint8Array) { try { return parseUploadResponse(JSON.parse(Buffer.from(response).toString('utf-8'))); } catch { return undefined; } } if (Array.isArray(response)) { for (const item of response) { const url = parseUploadResponse(item); if (url) { return url; } } return undefined; } if (response && typeof response === 'object') { return parseUploadResponse(response.data); } return undefined; }; const RichTextFileUploadOperate = { name: '上传文件或富文本图片', value: 'file:upload', order: 20, description: '通用文件上传接口,主要用于富文本中上传图片。', options: [ description_1.DESCRIPTIONS.PROJECT_KEY, { displayName: 'Input Binary Field', name: 'binaryPropertyName', type: 'string', required: true, default: 'data', description: '包含要上传文件的二进制数据字段名。form 表单字段名为 file,目前最大支持 100MB。详见:<a href="https://project.feishu.cn/b/helpcenter/2.0.0/1p8d7djs/g33r3mo4">文件上传</a>', }, sharedOptions_1.commonOptions, ], async call(index) { const project_key = this.getNodeParameter('project_key', index, '', { extractValue: true, }); const binaryPropertyName = this.getNodeParameter('binaryPropertyName', index); const options = this.getNodeParameter('options', index, {}); const file = await NodeUtils_1.default.buildUploadFileData.call(this, binaryPropertyName, index); if (!file || !file.value) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), '未找到文件数据,请检查二进制文件字段名是否正确'); } const fileSize = file.value.length; if (fileSize > MAX_FILE_SIZE) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `文件大小 (${Math.round(fileSize / 1024 / 1024)}MB) 超过限制,附件最大支持100MB`); } const formData = new form_data_1.default(); formData.append('file', file.value, { filename: file.options.filename || 'file', contentType: file.options.contentType || 'application/octet-stream', }); const response = await RequestUtils_1.default.request.call(this, { method: 'POST', url: `/open_api/${project_key}/file/upload`, body: formData, headers: formData.getHeaders(), timeout: options.timeout, }); const fileUrl = parseUploadResponse(response); if (!fileUrl) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), '上传成功但未返回资源路径'); } return { fileUrl }; }, }; exports.default = RichTextFileUploadOperate; //# sourceMappingURL=RichTextFileUploadOperate.js.map