const fs = require('fs');
const path = require('path');

function base64ToImageFile(base64, fileName) {
    const matches = base64.match(/^data:(.+);base64,(.+)$/);
    if (!matches) {
        throw new Error("Invalid Base64 string");
    }

    const mimeType = matches[1];  // e.g., image/png
    const base64Data = matches[2];  // Actual base64 string

    const buffer = Buffer.from(base64Data, 'base64');
    const filePath = path.join(__dirname, fileName);

    fs.writeFileSync(filePath, buffer);

    return filePath;
}

module.exports = { base64ToImageFile };
