feat: redis cache and detach image into s3

This commit is contained in:
JamesFlare1212
2025-05-09 19:43:01 -04:00
parent 99d1ee0a1e
commit f7252345f3
14 changed files with 2302 additions and 202 deletions

43
utils/image-processor.mjs Normal file
View 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');
}

30
utils/logger.mjs Normal file
View File

@@ -0,0 +1,30 @@
// utils/logger.mjs
import dotenv from 'dotenv';
dotenv.config(); // Ensure .env variables are loaded
const LOG_LEVEL = process.env.LOG_LEVEL || 'info'; // Example: 'debug', 'info', 'warn', 'error'
const levels = {
error: 0,
warn: 1,
info: 2,
debug: 3,
};
const currentLevel = levels[LOG_LEVEL.toLowerCase()] ?? levels.info;
const log = (level, ...args) => {
if (levels[level] <= currentLevel) {
const timestamp = new Date().toISOString();
console[level](`[${timestamp}] [${level.toUpperCase()}]`, ...args);
}
};
export const logger = {
error: (...args) => log('error', ...args),
warn: (...args) => log('warn', ...args),
info: (...args) => log('info', ...args),
debug: (...args) => log('debug', ...args),
};
export default logger;