feat(s3): enhance logging and public URL generation
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.
This commit is contained in:
@@ -28,12 +28,13 @@ describe("MediaController", () => {
|
||||
});
|
||||
|
||||
describe("getFile", () => {
|
||||
it("should stream the file and set headers", async () => {
|
||||
it("should stream the file and set headers with path containing slashes", async () => {
|
||||
const res = {
|
||||
setHeader: jest.fn(),
|
||||
} as any;
|
||||
const stream = new Readable();
|
||||
stream.pipe = jest.fn();
|
||||
const key = "contents/user-id/test.webp";
|
||||
|
||||
mockS3Service.getFileInfo.mockResolvedValue({
|
||||
size: 100,
|
||||
@@ -41,8 +42,9 @@ describe("MediaController", () => {
|
||||
});
|
||||
mockS3Service.getFile.mockResolvedValue(stream);
|
||||
|
||||
await controller.getFile("test.webp", res);
|
||||
await controller.getFile(key, res);
|
||||
|
||||
expect(mockS3Service.getFileInfo).toHaveBeenCalledWith(key);
|
||||
expect(res.setHeader).toHaveBeenCalledWith("Content-Type", "image/webp");
|
||||
expect(res.setHeader).toHaveBeenCalledWith("Content-Length", 100);
|
||||
expect(stream.pipe).toHaveBeenCalledWith(res);
|
||||
|
||||
@@ -9,13 +9,15 @@ export class MediaController {
|
||||
@Get("*key")
|
||||
async getFile(@Param("key") key: string, @Res() res: Response) {
|
||||
try {
|
||||
const stats = await this.s3Service.getFileInfo(key);
|
||||
const stats = (await this.s3Service.getFileInfo(key)) as any;
|
||||
const stream = await this.s3Service.getFile(key);
|
||||
|
||||
res.setHeader(
|
||||
"Content-Type",
|
||||
stats.metaData["content-type"] || "application/octet-stream",
|
||||
);
|
||||
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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user