@luka-cat-mimi/n8n-nodes-feishu-project
Version:
飞书项目 N8N 集成插件,支持附件管理等功能
163 lines • 7.62 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 FileStreamDownloadOperate = {
name: '下载文件',
value: 'file:download',
order: 20,
description: '下载一个附件。文件需已绑定到评论等业务中,未绑定业务点位前不能下载。',
options: [
{
displayName: '该接口用于下载一个附件。上传完成的文件,需要再绑定到评论等业务中,在未绑定到业务点位之前不能下载,超过 7 天未绑定业务点位会自动删除文件。',
name: 'downloadNotice',
type: 'notice',
default: '',
},
description_1.DESCRIPTIONS.PROJECT_KEY,
{
displayName: '文件 Token',
name: 'file_token',
type: 'string',
required: true,
default: '',
description: '文件 token,通过上传文件接口获取',
},
{
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, _k, _l;
const project_key = this.getNodeParameter('project_key', index, '', {
extractValue: true,
});
const file_token = this.getNodeParameter('file_token', index);
const outputPropertyName = this.getNodeParameter('outputPropertyName', index, 'data');
const options = this.getNodeParameter('options', index, {});
const url = `/open_api/${project_key}/file/stream/download/${file_token}`;
const credentialName = 'feishuProjectApi';
const credentials = await this.getCredentials(credentialName);
const baseURL = `https://${credentials.baseUrl}`;
const requestOptions = {
method: 'GET',
baseURL,
url,
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, '')) || file_token;
}
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;
}
const respCode = (_h = responseData === null || responseData === void 0 ? void 0 : responseData.err_code) !== null && _h !== void 0 ? _h : responseData === null || responseData === void 0 ? void 0 : responseData.code;
const respMsg = (_j = responseData === null || responseData === void 0 ? void 0 : responseData.err_msg) !== null && _j !== void 0 ? _j : responseData === null || responseData === void 0 ? void 0 : responseData.message;
if (respCode !== 0) {
const description = ((_k = responseData === null || responseData === void 0 ? void 0 : responseData.err) === null || _k === void 0 ? void 0 : _k.msg)
? `${responseData.err.msg}${responseData.err.log_id ? ` (log_id: ${responseData.err.log_id})` : ''}`
: `错误码: ${respCode},请参考排查文档: ${enums_1.ERROR_HELP_URL}`;
throw new n8n_workflow_1.NodeApiError(this.getNode(), responseData, {
message: `飞书项目 API 错误: ${respCode}, ${respMsg}`,
description,
});
}
return (_l = responseData.data) !== null && _l !== void 0 ? _l : responseData;
},
};
exports.default = FileStreamDownloadOperate;
//# sourceMappingURL=FileStreamDownloadOperate.js.map