#1 Convert to base64
import fs from 'fs/promises';
export default async function run({execution_id, input, data}) {
// It is assumed that file_content contains the local file path
const fileContentPath = data["{{26.`result`.`file`.`content`}}"];
try {
// Asynchronously read the file
const fileBuffer = await fs.readFile(fileContentPath);
// Convert the file content to a base64 string
const fileBase64 = fileBuffer.toString('base64');
return {
file_base64: fileBase64
};
} catch (error) {
console.error('Error reading file and converting to base64:', error);
return {};
}
}
#2 Extracting text from TXT
import axios from 'axios';
export default async function run({execution_id, input, data, store}) {
// Extracting the file URL from the data object
const fileurl = data["{{21.body.Files.[0].Url}}"];
// Check if there are any URLs to process
if (!fileurl || fileurl.length === 0) {
console.log('No file URLs found');
return {};
}
try {
// Making a GET request to the file URL to fetch the content
const response = await axios.get(fileurl); // Removed erroneous [0] index
// Assuming the file content is plain text and can be accessed directly from response.data
const fileContent = response.data;
// Returning the file content
return {
fileContent: fileContent
};
} catch (error) {
console.error('Error fetching file content:', error);
// Return an empty object or error message based on your error handling strategy
return {};
}
}
#3 Create JSONs from plaintext
export default async function run({execution_id, input, data, store}) {
// Retrieve the plain text content from the data parameter.
const content = data["{{12.result.messages.body.data.[0].content.[0].text.value}}"];
// Initialize variables to hold extracted information.
let name, email, experience;
// Use regular expressions to find name, email, and experience in the text.
// These are simplistic patterns and might need adjustments.
const nameMatch = content.match(/name:\\s*(.*)/i);
const emailMatch = content.match(/email:\\s*(.*)/i);
const experienceMatch = content.match(/experience:\\s*(.*)/i);
// If matches are found, extract the information.
if (nameMatch) {
name = nameMatch[1];
}
if (emailMatch) {
email = emailMatch[1];
}
if (experienceMatch) {
experience = experienceMatch[1];
}
// Check if we have extracted the information successfully.
if (name && email && experience) {
// Prepare the details to return.
const filename = { name };
const filemail = { email };
const filexp = { experience };
// Return the extracted details.
return {
filename,
filemail,
filexp
};
} else {
// If any information couldn't be extracted, return an error message.
return {
error: "Failed to extract all required information from the content. Please ensure the content is correctly formatted."
};
}
}