feat: add modular services and repositories for improved code organization
Introduce repository pattern across multiple services, including `favorites`, `tags`, `sessions`, `reports`, `auth`, and more. Decouple crypto functionalities into modular services like `HashingService`, `JwtService`, and `EncryptionService`. Improve testability and maintainability by simplifying dependencies and consolidating utility logic.
This commit is contained in:
44
backend/src/media/strategies/image-processor.strategy.ts
Normal file
44
backend/src/media/strategies/image-processor.strategy.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||
import sharp from "sharp";
|
||||
import type { MediaProcessingResult } from "../../common/interfaces/media.interface";
|
||||
import type { IMediaProcessorStrategy } from "./media-processor.strategy";
|
||||
|
||||
@Injectable()
|
||||
export class ImageProcessorStrategy implements IMediaProcessorStrategy {
|
||||
private readonly logger = new Logger(ImageProcessorStrategy.name);
|
||||
|
||||
canHandle(mimeType: string): boolean {
|
||||
return mimeType.startsWith("image/");
|
||||
}
|
||||
|
||||
async process(
|
||||
buffer: Buffer,
|
||||
options: { format: "webp" | "avif" } = { format: "webp" },
|
||||
): Promise<MediaProcessingResult> {
|
||||
try {
|
||||
const { format } = options;
|
||||
let pipeline = sharp(buffer);
|
||||
const metadata = await pipeline.metadata();
|
||||
|
||||
if (format === "webp") {
|
||||
pipeline = pipeline.webp({ quality: 80, effort: 6 });
|
||||
} else {
|
||||
pipeline = pipeline.avif({ quality: 65, effort: 6 });
|
||||
}
|
||||
|
||||
const processedBuffer = await pipeline.toBuffer();
|
||||
|
||||
return {
|
||||
buffer: processedBuffer,
|
||||
mimeType: `image/${format}`,
|
||||
extension: format,
|
||||
width: metadata.width,
|
||||
height: metadata.height,
|
||||
size: processedBuffer.length,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`Error processing image: ${error.message}`);
|
||||
throw new BadRequestException("Failed to process image");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user