Skip to content

Commit 78733a8

Browse files
committed
Implement the Delete endpoint for the brows section
1 parent 83c2222 commit 78733a8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/browser/browser.controller.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Post, Body, Res,Req, HttpStatus, Param, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
1+
import { Controller, Get, Post, Body, Res, Req, HttpStatus, Param, UseGuards, UsePipes, ValidationPipe, Delete } from '@nestjs/common';
22
import { BrowserService } from './browser.service';
33
import { CreateBrowserDto,CreateBrowserResponseDto } from '../dto/create-browser.dto';
44
import { Response, Request } from 'express';
@@ -62,6 +62,24 @@ export class BrowserController {
6262

6363
}
6464

65+
@Delete("api/browse/:id")
66+
@UseGuards(BrowseGuard)
67+
async fileDestroy(@Param() params, @Req() req: Request, @Res() res: Response) {
68+
69+
let id = params.id.indexOf('.pdf') > -1 ? params.id.substr(0, params.id.length - 4) : params.id;
70+
let path = `./storage/${id}.pdf`;
71+
72+
try {
73+
await fs.accessSync(path, fs.constants.F_OK);
74+
fs.unlinkSync(path);
75+
} catch (err) {
76+
return res.status(HttpStatus.BAD_REQUEST).json({statusCode: HttpStatus.BAD_REQUEST, message: `Something went wrong while trying to delete "${id}"`});
77+
}
78+
79+
res.status(HttpStatus.OK).json({statusCode:HttpStatus.OK});
80+
81+
}
82+
6583
@Post("api/browse")
6684
@UseGuards(BrowseGuard)
6785
@UsePipes(new ValidationPipe({ transform: true }))

0 commit comments

Comments
 (0)