refactor media module
This commit is contained in:
@@ -12,8 +12,22 @@ import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorC
|
||||
import { Media } from '../../domain/entities/Media';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
// Define Multer file type locally since @types/multer is not available
|
||||
export interface MulterFile {
|
||||
fieldname: string;
|
||||
originalname: string;
|
||||
encoding: string;
|
||||
mimetype: string;
|
||||
size: number;
|
||||
buffer: Buffer;
|
||||
stream: NodeJS.ReadableStream;
|
||||
destination: string;
|
||||
filename: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
export interface UploadMediaInput {
|
||||
file: Express.Multer.File;
|
||||
file: MulterFile;
|
||||
uploadedBy: string;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
@@ -46,11 +60,14 @@ export class UploadMediaUseCase {
|
||||
|
||||
try {
|
||||
// Upload file to storage service
|
||||
const uploadResult = await this.mediaStorage.uploadMedia(input.file.buffer, {
|
||||
const uploadOptions: { filename: string; mimeType: string; metadata?: Record<string, any> } = {
|
||||
filename: input.file.originalname,
|
||||
mimeType: input.file.mimetype,
|
||||
metadata: input.metadata,
|
||||
});
|
||||
};
|
||||
if (input.metadata !== undefined) {
|
||||
uploadOptions.metadata = input.metadata;
|
||||
}
|
||||
const uploadResult = await this.mediaStorage.uploadMedia(input.file.buffer, uploadOptions);
|
||||
|
||||
if (!uploadResult.success) {
|
||||
return Result.err({
|
||||
@@ -86,10 +103,11 @@ export class UploadMediaUseCase {
|
||||
// Save to repository
|
||||
await this.mediaRepo.save(media);
|
||||
|
||||
this.output.present({
|
||||
const result: UploadMediaResult = {
|
||||
mediaId,
|
||||
url: uploadResult.url,
|
||||
});
|
||||
};
|
||||
this.output.present(result);
|
||||
|
||||
this.logger.info('[UploadMediaUseCase] Media uploaded successfully', {
|
||||
mediaId,
|
||||
@@ -99,8 +117,7 @@ export class UploadMediaUseCase {
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
this.logger.error('[UploadMediaUseCase] Error uploading media', {
|
||||
error: err.message,
|
||||
this.logger.error('[UploadMediaUseCase] Error uploading media', err, {
|
||||
filename: input.file.originalname,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user