@luka-cat-mimi/n8n-nodes-feishu-project
Version:
飞书项目 N8N 集成插件,支持附件管理等功能
133 lines • 6.9 kB
JavaScript
;
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 AttachmentUploadOperate = {
name: '添加附件',
value: 'attachment:upload',
order: 10,
description: '在指定工作项的一个附件类型字段中添加附件。',
options: [
{
displayName: '该接口用于在指定工作项的一个附件类型字段中添加附件。<br><br><b>注意事项</b><br>1. 接口响应成功,页面查看无文件的情况,排查时需检查 form 表单-file 字段内容。当前情况下 file 为空,不拦截。<br>2. 遇到浏览器无法展示附件预览的情况,需检查上传时的文件 MimeType 设置,填写与文件类型匹配的 MimeType。如 jpeg 格式图片:image/jpeg,mp4 格式音频:audio/mp4,mp4 格式视频:video/mp4 等。<br>3. 工作项附件类型字段的附件数量限制为 50 个,超过 50 个时,新增附件会失败。',
name: 'uploadNotice',
type: 'notice',
default: '',
},
description_1.DESCRIPTIONS.PROJECT_KEY,
{
displayName: '工作项类型 Name or ID',
name: 'work_item_type_key',
type: 'options',
default: '',
required: true,
description: '空间下工作项类型,需要先选择空间,详见:<a href="https://project.feishu.cn/b/helpcenter/2.0.0/1p8d7djs/3pjp854w">获取空间下工作项类型</a>. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
typeOptions: {
loadOptionsMethod: 'loadWorkItemTypes',
},
},
{
displayName: '工作项实例ID',
name: 'work_item_id',
type: 'string',
required: true,
default: '',
description: '工作项实例 ID,在工作项实例详情中,展开右上角 ··· > ID 获取。',
},
{
displayName: 'Input Binary Field',
name: 'binaryPropertyName',
type: 'string',
required: true,
default: 'data',
description: '包含要上传文件的二进制数据字段名,对应 form 表单 file 字段,附件最大支持 100MB。若接口成功但页面无文件,请检查该字段是否有内容;若无法预览附件,请确保二进制数据的 MimeType 与文件类型匹配(如 image/jpeg、audio/mp4、video/mp4)。',
},
{
displayName: '附件字段 Name or ID',
name: 'field_key',
type: 'options',
default: '',
description: '选择要上传附件的目标附件类型字段。需要先选择空间和工作项类型,单个字段最多 50 个附件。Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
typeOptions: {
loadOptionsMethod: 'loadAttachmentFields',
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: '自定义文件名',
name: 'file_name',
type: 'string',
default: '',
description: '带后缀的文件名,例如:test.pdf。不填则使用原始文件名',
},
{
displayName: '数组下标',
name: 'index',
type: 'string',
default: '',
description: '复合字段适用,用于指定数组下标',
},
sharedOptions_1.batchingOption,
sharedOptions_1.timeoutOption,
],
},
],
async call(index) {
var _a, _b, _c;
const project_key = this.getNodeParameter('project_key', index, '', {
extractValue: true,
});
const work_item_type_key = this.getNodeParameter('work_item_type_key', index);
const work_item_id = this.getNodeParameter('work_item_id', index);
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', index);
const field_key = this.getNodeParameter('field_key', 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 fileName = ((_a = options.file_name) === null || _a === void 0 ? void 0 : _a.trim()) || ((_b = file.options) === null || _b === void 0 ? void 0 : _b.filename) || 'file';
const formData = new form_data_1.default();
formData.append('file', file.value, {
filename: fileName,
contentType: file.options.contentType || 'application/octet-stream',
});
if (field_key === null || field_key === void 0 ? void 0 : field_key.trim()) {
formData.append('field_key', field_key.trim());
}
if ((_c = options.index) === null || _c === void 0 ? void 0 : _c.trim()) {
formData.append('index', options.index.trim());
}
await RequestUtils_1.default.request.call(this, {
method: 'POST',
url: `/open_api/${project_key}/work_item/${work_item_type_key}/${work_item_id}/file/upload`,
body: formData,
timeout: options.timeout,
});
return {
project_key: project_key,
work_item_type_key: work_item_type_key,
work_item_id: work_item_id
};
},
};
exports.default = AttachmentUploadOperate;
//# sourceMappingURL=AttachmentUploadOperate.js.map