UNPKG

n8n-nodes-openai-analytics

Version:
89 lines 3.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadFile = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); async function downloadFile(context) { var _a; const { openai, functionThis, items, i } = context; // 파일 선택 방식 확인 const fileSelectionMethod = functionThis.getNodeParameter('fileSelectionMethod', i); let fileId = ''; // 선택 방식에 따라 파일 ID 가져오기 if (fileSelectionMethod === 'byId') { fileId = functionThis.getNodeParameter('downloadFileId', i); } else { fileId = functionThis.getNodeParameter('downloadFileId', i); } const binaryPropertyName = functionThis.getNodeParameter('downloadBinaryPropertyName', i); // 파일 정보 가져오기 const fileInfo = await openai.files.retrieve(fileId); try { // 파일 콘텐츠 다운로드 const apiKey = openai.apiKey; const organization = openai.organization || ''; const baseURL = openai.baseURL || 'https://api.openai.com/v1'; const response = await (0, node_fetch_1.default)(`${baseURL}/files/${fileId}/content`, { method: 'GET', headers: { 'Authorization': `Bearer ${apiKey}`, 'OpenAI-Organization': organization, }, }); if (!response.ok) { throw new Error(`Failed to download file: ${response.statusText}`); } // 파일 내용을 Buffer로 변환 const buffer = Buffer.from(await response.arrayBuffer()); // 파일명 추출 또는 기본값 사용 const fileName = fileInfo.filename || `file_${fileId}.bin`; // 파일 확장자에 따른 MIME 타입 추정 const fileExtension = ((_a = fileName.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || 'bin'; let mimeType = 'application/octet-stream'; const mimeTypes = { 'pdf': 'application/pdf', 'txt': 'text/plain', 'csv': 'text/csv', 'json': 'application/json', 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'png': 'image/png', 'gif': 'image/gif', 'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', }; if (fileExtension in mimeTypes) { mimeType = mimeTypes[fileExtension]; } // 바이너리 데이터로 반환 const newItem = { json: { ...fileInfo, downloadSuccess: true, }, binary: { [binaryPropertyName]: await functionThis.helpers.prepareBinaryData(buffer, fileName, mimeType), }, }; // 원본 아이템을 수정 items[i] = newItem; // 가공된 아이템 반환 return newItem; } catch (error) { // 다운로드 실패 시 에러 정보 반환 return { json: { error: true, message: error.message, fileInfo, } }; } } exports.downloadFile = downloadFile; //# sourceMappingURL=downloadFile.js.map