first commit

This commit is contained in:
dahyeon committed 2025-11-08 18:27:51 +09:00
commit c809bb3893
40 files changed
+3836

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
import { MinioClient } from './minio.js';
import { NatsClient } from './nats.js';
import { FFmpegConverter } from './ffmpeg.js';
import { extname, parse } from 'path';
export class ImageDerivativesGenerator {
minioClient;
natsClient;
ffmpegConverter;
resolutions = [320, 640, 960, 1280, 1600, 1920];
formats = ['avif', 'webp', 'jpeg'];
constructor(minioClient, natsClient, ffmpegConverter) {
this.minioClient = minioClient;
this.natsClient = natsClient;
this.ffmpegConverter = ffmpegConverter;
}
async start() {
console.log('🚀 Starting Image Derivatives Generator Service');
console.log(`📊 Resolutions: ${this.resolutions.join(', ')}`);
console.log(`🎨 Formats: ${this.formats.join(', ')}`);
await this.natsClient.subscribe('generate-derivaties', async (message) => {
await this.processMessage(message);
});
console.log('✅ Service ready');
}
async processMessage(message) {
const { s3ObjectKey } = message;
console.log(`\n📥 Processing: ${s3ObjectKey}`);
try {
// Download original image from MinIO
const originalBuffer = await this.minioClient.downloadFile(s3ObjectKey);
console.log(` ✓ Downloaded original (${(originalBuffer.length / 1024).toFixed(2)} KB)`);
// Parse the original file path
const parsedPath = parse(s3ObjectKey);
const baseDir = parsedPath.dir;
const baseName = parsedPath.name;
let totalGenerated = 0;
let totalSkipped = 0;
// Generate all combinations of resolutions and formats
for (const width of this.resolutions) {
for (const format of this.formats) {
const derivativeKey = `${baseDir}/${baseName}_${width}.${format}`;
// Check if derivative already exists
const exists = await this.minioClient.fileExists(derivativeKey);
if (exists) {
totalSkipped++;
continue;
}
try {
// Convert image
const result = await this.ffmpegConverter.convert(originalBuffer, {
width,
format,
});
// Upload to MinIO
const contentType = this.ffmpegConverter.getContentType(format);
await this.minioClient.uploadFile(derivativeKey, result.buffer, contentType);
totalGenerated++;
console.log(` ✓ Generated ${width}w ${format} (${(result.buffer.length / 1024).toFixed(2)} KB)`);
}
catch (error) {
console.error(` ✗ Failed to generate ${width}w ${format}:`, error instanceof Error ? error.message : error);
}
}
}
console.log(`\n✅ Completed: ${totalGenerated} generated, ${totalSkipped} skipped for ${s3ObjectKey}`);
}
catch (error) {
console.error(`❌ Error processing ${s3ObjectKey}:`, error);
}
}
async stop() {
console.log('\n🛑 Stopping service...');
await this.natsClient.close();
console.log('✓ Service stopped');
}
}
//# sourceMappingURL=image-derivaties-generator.js.map