feat: sincronização de desenvolvimento local da VPS 4 para o Gitea
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
import sharp from 'sharp';
|
||||
|
||||
export interface OptimizationResult {
|
||||
buffer: Buffer;
|
||||
mimetype: string;
|
||||
}
|
||||
|
||||
export class ImageOptimizer {
|
||||
/**
|
||||
* Optimizes an image: converts to WebP, strips metadata,
|
||||
* and applies a 1:1 Smart Crop if needed.
|
||||
*/
|
||||
static async optimize(
|
||||
buffer: Buffer,
|
||||
category: string = 'media',
|
||||
options: { aspect?: string; size?: number } = {}
|
||||
): Promise<OptimizationResult> {
|
||||
try {
|
||||
let pipeline = sharp(buffer);
|
||||
const metadata = await pipeline.metadata();
|
||||
|
||||
if (!metadata.format) return { buffer, mimetype: 'application/octet-stream' };
|
||||
|
||||
// 1. Identify Target Size
|
||||
// Default sizes based on category
|
||||
let targetSize = options.size || 1024;
|
||||
if (category === 'partners' || category === 'users') targetSize = 400;
|
||||
if (category === 'benefits') targetSize = 800;
|
||||
|
||||
// 2. Smart Crop 1:1 if aspect=1:1 is requested or implied by category
|
||||
const shouldCrop = options.aspect === '1:1' || ['partners', 'users'].includes(category);
|
||||
|
||||
if (shouldCrop) {
|
||||
pipeline = pipeline.resize({
|
||||
width: targetSize,
|
||||
height: targetSize,
|
||||
fit: sharp.fit.cover,
|
||||
position: sharp.strategy.entropy // Smart focal point detection
|
||||
});
|
||||
} else {
|
||||
// Resize without cropping if it's too large
|
||||
pipeline = pipeline.resize({
|
||||
width: targetSize,
|
||||
withoutEnlargement: true,
|
||||
fit: sharp.fit.inside
|
||||
});
|
||||
}
|
||||
|
||||
// 3. Convert to WebP & Strip Metadata
|
||||
const optimizedBuffer = await pipeline
|
||||
.webp({ quality: 85, effort: 4 })
|
||||
.toBuffer();
|
||||
|
||||
return {
|
||||
buffer: optimizedBuffer,
|
||||
mimetype: 'image/webp'
|
||||
};
|
||||
} catch (err) {
|
||||
console.error('[SmartVision] Optimization failed:', err);
|
||||
return { buffer, mimetype: 'image/jpeg' }; // Fallback
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { PluginInstance, PluginContext } from '../../backend/src/core/types';
|
||||
import { ImageOptimizer } from './backend/Optimizer';
|
||||
import manifest from './manifest.json';
|
||||
|
||||
const plugin: PluginInstance = {
|
||||
manifest: manifest as any,
|
||||
async activate(ctx: PluginContext): Promise<void> {
|
||||
// Register for the upload:transform hook
|
||||
ctx.hooks.register('upload:transform', async (payload: any) => {
|
||||
const { buffer, mimetype, category } = payload;
|
||||
|
||||
// Only process common image formats
|
||||
const imageTypes = ['image/jpeg', 'image/png', 'image/webp', 'image/avif'];
|
||||
if (!imageTypes.includes(mimetype)) {
|
||||
return null; // Skip non-images
|
||||
}
|
||||
|
||||
ctx.logger.info(`[SmartVision] Optimizing image for category: ${category}`);
|
||||
|
||||
const result = await ImageOptimizer.optimize(buffer, category);
|
||||
|
||||
return {
|
||||
buffer: result.buffer,
|
||||
mimetype: result.mimetype
|
||||
};
|
||||
});
|
||||
|
||||
ctx.logger.info('SmartVision Optimizer activated');
|
||||
},
|
||||
async deactivate(ctx: PluginContext): Promise<void> {
|
||||
ctx.logger.info('SmartVision Optimizer deactivated');
|
||||
}
|
||||
};
|
||||
|
||||
export default plugin;
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "SmartVision",
|
||||
"displayName": "SmartVision Optimizer",
|
||||
"version": "1.0.0",
|
||||
"description": "Otimização inteligente de imagens com WebP e Smart Crop 1:1.",
|
||||
"author": "Clube67 Team",
|
||||
"category": "integration",
|
||||
"enabled": true,
|
||||
"canDisable": true,
|
||||
"dependencies": [
|
||||
"uploads"
|
||||
],
|
||||
"hooks": {
|
||||
"subscribes": [
|
||||
"upload:transform"
|
||||
],
|
||||
"emits": []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user