n8n-nodes-openai-analytics
Version:
n8n node for OpenAI Analytics
91 lines • 3.73 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadFile = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
async function uploadFile(context) {
var _a, _b;
const { openai, functionThis, items, i } = context;
const binaryPropertyName = functionThis.getNodeParameter('binaryPropertyName', i);
const purpose = functionThis.getNodeParameter('filePurpose', i);
// 모든 바이너리 속성을 찾아 업로드
const item = items[i];
const binaryProperties = [];
// 기본 바이너리 속성 확인
if ((_a = item.binary) === null || _a === void 0 ? void 0 : _a[binaryPropertyName]) {
binaryProperties.push(binaryPropertyName);
}
// 여러 바이너리 속성 확인 (data_1, data_2 등)
if (binaryPropertyName === 'data') {
for (const key of Object.keys(item.binary || {})) {
if (key.startsWith('data_') && !isNaN(Number(key.split('_')[1]))) {
binaryProperties.push(key);
}
}
}
// 업로드된 파일들의 ID 목록
const uploadedFiles = [];
// 각 바이너리 속성에 대해 파일 업로드
for (const prop of binaryProperties) {
const binaryData = (_b = item.binary) === null || _b === void 0 ? void 0 : _b[prop];
if (!binaryData)
continue;
// 바이너리 데이터를 스트림으로 변환
const buffer = Buffer.from(binaryData.data, 'base64');
const originalFileName = binaryData.fileName || `file_${Date.now()}.${binaryData.fileExtension || 'bin'}`;
const tempFilePath = path.join(os.tmpdir(), originalFileName);
fs.writeFileSync(tempFilePath, buffer);
const fileStream = fs.createReadStream(tempFilePath);
// 타입 문제 해결을 위한 업로드 파라미터 생성
const uploadParams = {
file: fileStream,
purpose: purpose === 'assistants_input' ? 'assistants_input' : purpose,
};
const fileUploadResult = await openai.files.create(uploadParams);
// 임시 파일 삭제
try {
fs.unlinkSync(tempFilePath);
}
catch (e) {
// 파일 삭제 실패 시 무시
}
uploadedFiles.push(fileUploadResult);
}
// 업로드된 모든 파일 정보 반환
if (uploadedFiles.length === 1) {
return {
json: { ...uploadedFiles[0] }
};
}
else {
return {
json: { files: uploadedFiles }
};
}
}
exports.uploadFile = uploadFile;
//# sourceMappingURL=uploadFile.js.map