@luka-cat-mimi/n8n-nodes-feishu-project
Version:
飞书项目 N8N 集成插件,支持附件管理等功能
137 lines • 6.93 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 = 20 * 1024 * 1024;
const FileStreamUploadFormOperate = {
name: '上传文件',
value: 'file:uploadForm',
order: 10,
description: '上传一个小于 20MB 的文件,上传后需再绑定到评论等业务中。未绑定业务点位前不能下载,超过 7 天未绑定会自动删除。',
options: [
{
displayName: '该接口用于上传一个小于 20MB 的文件。<br><br>上传完成的文件需要再绑定到评论等业务中,在未绑定到业务点位之前不能下载,超过 7 天未绑定业务点位会自动删除文件(如果是一个附件,会占用容量,在删除时会解除容量占用)。<br><br><b>注意事项</b><br>遇到浏览器无法展示附件预览的情况,需检查上传时的文件 MimeType 设置,填写与文件类型匹配的 MimeType。如 jpeg 格式图片:image/jpeg,mp4 格式音频:audio/mp4,mp4 格式视频:video/mp4 等。',
name: 'uploadFormNotice',
type: 'notice',
default: '',
},
description_1.DESCRIPTIONS.PROJECT_KEY,
{
displayName: '资源类型',
name: 'resource_type',
type: 'options',
default: 'CommentAttachment',
required: true,
description: '文件所属的业务资源类型',
options: [
{
name: '评论的附件 (CommentAttachment)',
value: 'CommentAttachment',
},
{
name: '评论富文本中的图片 (CommentMultiTextImg)',
value: 'CommentMultiTextImg',
},
],
},
{
displayName: 'Input Binary Field',
name: 'binaryPropertyName',
type: 'string',
required: true,
default: 'data',
description: '包含要上传文件的二进制数据字段名,对应 form 表单 file 字段,单文件不支持多文件同时上传,文件最大支持 20MB。若无法预览附件,请确保二进制数据的 MimeType 与文件类型匹配(如 image/jpeg、audio/mp4、video/mp4)。',
},
{
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: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: '自定义文件名',
name: 'file_name',
type: 'string',
default: '',
description: '带后缀的文件名,例如:test.pdf。不填则使用原始文件名',
},
{
displayName: '业务类型',
name: 'comment_biz_type',
type: 'string',
default: 'WORKITEM',
description: 'JSON field_map 中的 comment_biz_type,一般写死为 WORKITEM',
},
sharedOptions_1.batchingOption,
sharedOptions_1.timeoutOption,
],
},
],
async call(index) {
var _a, _b, _c;
const project_key = this.getNodeParameter('project_key', index, '', {
extractValue: true,
});
const resource_type = this.getNodeParameter('resource_type', index);
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', index);
const work_item_type_key = this.getNodeParameter('work_item_type_key', index);
const work_item_id = this.getNodeParameter('work_item_id', 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) 超过限制,该接口最大支持 20MB`);
}
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 fieldMap = {
comment_biz_type: ((_c = options.comment_biz_type) === null || _c === void 0 ? void 0 : _c.trim()) || 'WORKITEM',
work_item_type_key,
work_item_id,
};
const formData = new form_data_1.default();
formData.append('file', file.value, {
filename: fileName,
contentType: file.options.contentType || 'application/octet-stream',
});
formData.append('field_map', JSON.stringify(fieldMap));
return RequestUtils_1.default.request.call(this, {
method: 'POST',
url: `/open_api/${project_key}/file/stream/resource/${resource_type}/upload_form`,
body: formData,
headers: formData.getHeaders(),
timeout: options.timeout,
});
},
};
exports.default = FileStreamUploadFormOperate;
//# sourceMappingURL=FileStreamUploadFormOperate.js.map