feat: handle binary data

This commit is contained in:
karishmas6
2024-10-16 03:07:33 +05:30
parent c2356f066e
commit b3d76ae664

View File

@@ -34,66 +34,58 @@ class BinaryOutputService {
* @param binaryOutput - The binary output object containing data to upload. * @param binaryOutput - The binary output object containing data to upload.
* @returns A map of Minio URLs pointing to the uploaded binary data. * @returns A map of Minio URLs pointing to the uploaded binary data.
*/ */
async uploadAndStoreBinaryOutput(run: Run, binaryOutput: Record<string, any>): Promise<Record<string, string>> { async uploadAndStoreBinaryOutput(run: Run, binaryOutput: Record<string, any>, runId: string): Promise<Record<string, string>> {
const uploadedBinaryOutput: Record<string, string> = {}; const uploadedBinaryOutput: Record<string, string> = {};
for (const key of Object.keys(binaryOutput)) { for (const key of Object.keys(binaryOutput)) {
let binaryData = binaryOutput[key]; let binaryData = binaryOutput[key];
// Log the key and data if (!runId) {
console.error('Run ID is undefined. Cannot upload binary data.');
continue;
}
console.log(`Processing binary output key: ${key}`); console.log(`Processing binary output key: ${key}`);
console.log(`Binary data:`, binaryData); console.log(`Binary data:`, binaryData);
// Check if binaryData is an object with a "data" field (containing the Buffer string) // Check if binaryData has a valid Buffer structure and parse it
if (binaryData && typeof binaryData.data === 'string') { if (binaryData && typeof binaryData.data === 'string') {
try { try {
const parsedData = JSON.parse(binaryData.data); const parsedData = JSON.parse(binaryData.data);
// Check if the parsed data has the "type" and "data" fields
if (parsedData && parsedData.type === 'Buffer' && Array.isArray(parsedData.data)) { if (parsedData && parsedData.type === 'Buffer' && Array.isArray(parsedData.data)) {
// Convert the parsed array into a Buffer
binaryData = Buffer.from(parsedData.data); binaryData = Buffer.from(parsedData.data);
console.log(`Successfully parsed and converted binary data to Buffer for key: ${key}`);
} else { } else {
console.error(`Invalid Buffer format for key: ${key}`); console.error(`Invalid Buffer format for key: ${key}`);
continue; // Skip invalid data continue;
} }
} catch (jsonError) { } catch (error) {
console.error(`Failed to parse JSON for key: ${key}`, jsonError); console.error(`Failed to parse JSON for key: ${key}`, error);
continue; // Skip if parsing fails continue;
} }
} }
// Handle cases where data might still be invalid // Handle cases where binaryData might not be a Buffer
if (!Buffer.isBuffer(binaryData)) { if (!Buffer.isBuffer(binaryData)) {
console.error(`Binary data for key ${key} is not a valid Buffer.`); console.error(`Binary data for key ${key} is not a valid Buffer.`);
continue; continue;
} }
try { try {
const minioKey = run.runId ? `${run.runId}/${key}`: key; const minioKey = `${run.id}/${key}`;
console.log(`Uploading data to MinIO with key: ${minioKey}`);
await run.uploadBinaryOutputToMinioBucket(minioKey, binaryData); await run.uploadBinaryOutputToMinioBucket(minioKey, binaryData);
// Save the Minio URL in the result object // Save the Minio URL in the result object
uploadedBinaryOutput[key] = `minio://${this.bucketName}/${minioKey}`; uploadedBinaryOutput[key] = `minio://${this.bucketName}/${minioKey}`;
console.log(`Successfully uploaded ${key} to MinIO`);
} catch (error) { } catch (error) {
console.error(`Error uploading key ${key} to MinIO:`, error); console.error(`Error uploading key ${key} to MinIO:`, error);
} }
} }
// Log the binary output after processing
console.log('Uploaded Binary Output:', uploadedBinaryOutput); console.log('Uploaded Binary Output:', uploadedBinaryOutput);
// Update the run with the Minio URLs for binary output
try { try {
await run.update({ await run.update({ binaryOutput: uploadedBinaryOutput });
binaryOutput: uploadedBinaryOutput
});
console.log('Run successfully updated with binary output'); console.log('Run successfully updated with binary output');
} catch (updateError) { } catch (updateError) {
console.error('Error updating run with binary output:', updateError); console.error('Error updating run with binary output:', updateError);