feat: redis cache and detach image into s3
This commit is contained in:
43
utils/image-processor.mjs
Normal file
43
utils/image-processor.mjs
Normal file
@@ -0,0 +1,43 @@
|
||||
// utils/image-processor.mjs
|
||||
import { logger } from './logger.mjs'; // Using the logger
|
||||
|
||||
/**
|
||||
* Extracts base64 content and format from a data URL string.
|
||||
* E.g., "data:image/jpeg;base64,xxxxxxxxxxxxxxx"
|
||||
* @param {string} dataUrl The full data URL string.
|
||||
* @returns {object|null} An object { base64Content: string, format: string } or null if not found.
|
||||
*/
|
||||
export function extractBase64Image(dataUrl) {
|
||||
if (typeof dataUrl !== 'string' || !dataUrl.startsWith('data:image/')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const markers = [
|
||||
{ prefix: "data:image/png;base64,", format: "png" },
|
||||
{ prefix: "data:image/jpeg;base64,", format: "jpeg" },
|
||||
{ prefix: "data:image/jpg;base64,", format: "jpg" },
|
||||
{ prefix: "data:image/gif;base64,", format: "gif" },
|
||||
{ prefix: "data:image/svg+xml;base64,", format: "svg" }, // svg+xml -> svg
|
||||
{ prefix: "data:image/webp;base64,", format: "webp" }
|
||||
];
|
||||
|
||||
for (const marker of markers) {
|
||||
if (dataUrl.startsWith(marker.prefix)) {
|
||||
const base64Content = dataUrl.substring(marker.prefix.length);
|
||||
logger.debug(`Found image of format: ${marker.format}`);
|
||||
return { base64Content, format: marker.format };
|
||||
}
|
||||
}
|
||||
|
||||
logger.warn("No known base64 image marker found in the provided data URL:", dataUrl.substring(0, 50) + "...");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a base64 string to a Buffer.
|
||||
* @param {string} base64String The base64 encoded string (without the data URI prefix).
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
export function decodeBase64Image(base64String) {
|
||||
return Buffer.from(base64String, 'base64');
|
||||
}
|
||||
Reference in New Issue
Block a user