- Updated `getFile` method to validate `path` query parameter. - Added improved logging for file retrieval errors. - Updated test cases to cover scenarios with missing `path`.
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Logger,
|
|
NotFoundException,
|
|
Query,
|
|
Res,
|
|
} from "@nestjs/common";
|
|
import type { Response } from "express";
|
|
import type { BucketItemStat } from "minio";
|
|
import { S3Service } from "../s3/s3.service";
|
|
|
|
@Controller("media")
|
|
export class MediaController {
|
|
private readonly logger = new Logger(MediaController.name);
|
|
|
|
constructor(private readonly s3Service: S3Service) {}
|
|
|
|
@Get()
|
|
async getFile(@Query("path") path: string, @Res() res: Response) {
|
|
if (!path) {
|
|
this.logger.warn("Tentative d'accès à un média sans paramètre 'path'");
|
|
throw new NotFoundException("Paramètre 'path' manquant");
|
|
}
|
|
|
|
try {
|
|
this.logger.log(`Récupération du fichier : ${path}`);
|
|
const stats = (await this.s3Service.getFileInfo(path)) as BucketItemStat;
|
|
const stream = await this.s3Service.getFile(path);
|
|
|
|
const contentType: string =
|
|
stats.metaData?.["content-type"] ||
|
|
stats.metaData?.["Content-Type"] ||
|
|
"application/octet-stream";
|
|
|
|
res.setHeader("Content-Type", contentType);
|
|
res.setHeader("Content-Length", stats.size);
|
|
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
|
|
|
|
stream.pipe(res);
|
|
} catch (error) {
|
|
this.logger.error(
|
|
`Erreur lors de la récupération du fichier ${path} : ${error.message}`,
|
|
);
|
|
throw new NotFoundException("Fichier non trouvé");
|
|
}
|
|
}
|
|
}
|