gallery 2 api
This commit is contained in:
21
src/module/base64ToImageFile.js
Normal file
21
src/module/base64ToImageFile.js
Normal file
@@ -0,0 +1,21 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user