-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfolders.controller.ts
30 lines (24 loc) · 1.44 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
import { Controller, ForbiddenException, Get, 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 { ContentScopeService } from "../../content-scope/content-scope.service";
import { FoldersService } from "./folders.service";
@Controller("dam/folders")
export class FoldersController {
constructor(private readonly foldersService: FoldersService, private readonly contentScopeService: ContentScopeService) {}
@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 !== undefined && !this.contentScopeService.canAccessScope(folder.scope, user)) {
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);
}
}