Add detailed logging for S3 uploads in user and content services. Improve public URL generation logic in `S3Service` by providing better handling for `API_URL`, `DOMAIN_NAME`, and `PORT`. Update relevant tests to cover all scenarios.
30 lines
896 B
TypeScript
30 lines
896 B
TypeScript
import { Controller, Get, NotFoundException, Param, Res } from "@nestjs/common";
|
|
import type { Response } from "express";
|
|
import { S3Service } from "../s3/s3.service";
|
|
|
|
@Controller("media")
|
|
export class MediaController {
|
|
constructor(private readonly s3Service: S3Service) {}
|
|
|
|
@Get("*key")
|
|
async getFile(@Param("key") key: string, @Res() res: Response) {
|
|
try {
|
|
const stats = (await this.s3Service.getFileInfo(key)) as any;
|
|
const stream = await this.s3Service.getFile(key);
|
|
|
|
const contentType =
|
|
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) {
|
|
throw new NotFoundException("Fichier non trouvé");
|
|
}
|
|
}
|
|
}
|