@luka-cat-mimi/n8n-nodes-feishu-project
Version:
飞书项目 N8N 集成插件,支持附件管理等功能
184 lines • 8.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const n8n_workflow_1 = require("n8n-workflow");
const sharedOptions_1 = require("../../../help/utils/sharedOptions");
const enums_1 = require("../../../help/type/enums");
const description_1 = require("../../../help/description");
const BINARY_CONTENT_TYPES = [
'image/',
'audio/',
'video/',
'application/octet-stream',
'application/gzip',
'application/zip',
'application/vnd.rar',
'application/epub+zip',
'application/x-bzip',
'application/x-bzip2',
'application/x-cdf',
'application/vnd.amazon.ebook',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-fontobject',
'application/vnd.oasis.opendocument.presentation',
'application/pdf',
'application/x-tar',
'application/vnd.visio',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/x-7z-compressed',
];
const AttachmentDownloadOperate = {
name: '下载附件',
value: 'attachment:download',
order: 30,
description: '下载一个工作项下的指定附件。',
options: [
{
displayName: '该接口用于下载一个工作项下的指定附件。',
name: 'downloadNotice',
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: '附件ID',
name: 'uuid',
type: 'string',
required: true,
default: '',
description: '附件 ID,当前最大支持100MB。可从工作项详情接口下的富文本(multi_texts)或附件字段(multi_file类型)中获取',
},
{
displayName: 'Put Output File in Field',
name: 'outputPropertyName',
type: 'string',
default: 'data',
description: 'The name of the output binary field to put the file in',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
placeholder: 'e.g. myFile',
description: 'The name of the output file',
},
{
displayName: 'MIME Type',
name: 'mimeType',
type: 'string',
default: '',
placeholder: 'e.g. text/plain',
description: 'The MIME type of the output file',
},
sharedOptions_1.timeoutOption,
],
},
],
async call(index) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
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 uuid = this.getNodeParameter('uuid', index);
const outputPropertyName = this.getNodeParameter('outputPropertyName', index, 'data');
const options = this.getNodeParameter('options', index, {});
const body = { uuid };
const url = `/open_api/${project_key}/work_item/${work_item_type_key}/${work_item_id}/file/download`;
const credentialName = 'feishuProjectApi';
const credentials = await this.getCredentials(credentialName);
const baseURL = `https://${credentials.baseUrl}`;
const requestOptions = {
method: 'POST',
baseURL,
url,
body,
encoding: null,
json: false,
useStream: true,
resolveWithFullResponse: true,
};
if (options.timeout) {
requestOptions.timeout = options.timeout;
}
const response = await this.helpers.requestWithAuthentication.call(this, credentialName, requestOptions);
const responseContentType = ((_a = response.headers) === null || _a === void 0 ? void 0 : _a['content-type']) || ((_b = response.headers) === null || _b === void 0 ? void 0 : _b['Content-Type']) || '';
const isFileResponse = BINARY_CONTENT_TYPES.some((type) => responseContentType.includes(type)) ||
(!responseContentType.includes('application/json') &&
response.body &&
typeof response.body !== 'object');
if (isFileResponse && response.body) {
const mimeType = ((_c = options.mimeType) === null || _c === void 0 ? void 0 : _c.trim()) || responseContentType.split(';')[0].trim() || undefined;
let fileName = (_d = options.fileName) === null || _d === void 0 ? void 0 : _d.trim();
if (!fileName) {
const contentDisposition = ((_e = response.headers) === null || _e === void 0 ? void 0 : _e['content-disposition']) ||
((_f = response.headers) === null || _f === void 0 ? void 0 : _f['Content-Disposition']) ||
'';
const fileNameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
fileName = ((_g = fileNameMatch === null || fileNameMatch === void 0 ? void 0 : fileNameMatch[1]) === null || _g === void 0 ? void 0 : _g.replace(/['"]/g, '')) || uuid;
}
const binaryData = await this.helpers.prepareBinaryData(response.body, fileName, mimeType);
return {
binary: {
[outputPropertyName]: binaryData,
},
json: binaryData,
};
}
let responseData;
try {
if (response.body instanceof Buffer) {
const data = await this.helpers.binaryToString(response.body);
responseData = JSON.parse(data);
}
else {
responseData =
typeof response.body === 'string' ? JSON.parse(response.body) : response.body;
}
}
catch {
responseData = response.body;
}
if ((responseData === null || responseData === void 0 ? void 0 : responseData.err_code) !== 0) {
const description = ((_h = responseData === null || responseData === void 0 ? void 0 : responseData.err) === null || _h === void 0 ? void 0 : _h.msg)
? `${responseData.err.msg}${responseData.err.log_id ? ` (log_id: ${responseData.err.log_id})` : ''}`
: `错误码: ${responseData === null || responseData === void 0 ? void 0 : responseData.err_code},请参考排查文档: ${enums_1.ERROR_HELP_URL}`;
throw new n8n_workflow_1.NodeApiError(this.getNode(), responseData, {
message: `飞书项目 API 错误: ${responseData === null || responseData === void 0 ? void 0 : responseData.err_code}, ${responseData === null || responseData === void 0 ? void 0 : responseData.err_msg}`,
description,
});
}
return (_j = responseData.data) !== null && _j !== void 0 ? _j : responseData;
},
};
exports.default = AttachmentDownloadOperate;
//# sourceMappingURL=AttachmentDownloadOperate.js.map