-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfolders.controller.ts
34 lines (28 loc) · 1.59 KB
/
folders.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Controller, ForbiddenException, Get, Inject, NotFoundException, Param, Res } from "@nestjs/common";
import { Response } from "express";
import { CurrentUserInterface } from "../../auth/current-user/current-user";
import { GetCurrentUser } from "../../auth/decorators/get-current-user.decorator";
import { ACCESS_CONTROL_SERVICE } from "../../user-permissions/user-permissions.constants";
import { AccessControlServiceInterface } from "../../user-permissions/user-permissions.types";
import { FoldersService } from "./folders.service";
@Controller("dam/folders")
export class FoldersController {
constructor(
private readonly foldersService: FoldersService,
@Inject(ACCESS_CONTROL_SERVICE) private accessControlService: AccessControlServiceInterface,
) {}
@Get("/:folderId/zip")
async createZip(@Param("folderId") folderId: string, @Res() res: Response, @GetCurrentUser() user: CurrentUserInterface): Promise<void> {
const folder = await this.foldersService.findOneById(folderId);
if (!folder) {
throw new NotFoundException("Folder not found");
}
if (folder.scope && !this.accessControlService.isAllowed(user, "dam", folder.scope)) {
throw new ForbiddenException("The current user is not allowed to access this scope and download this folder.");
}
const zipStream = await this.foldersService.createZipStreamFromFolder(folderId);
res.setHeader("Content-Disposition", `attachment; filename="${folder.name}.zip"`);
res.setHeader("Content-Type", "application/zip");
zipStream.pipe(res);
}
}