diff --git a/src/Static/static.controller.ts b/src/Static/static.controller.ts index 64e66f9..d5f4a31 100644 --- a/src/Static/static.controller.ts +++ b/src/Static/static.controller.ts @@ -1,106 +1,21 @@ -import { - Controller, - Get, - Next, - NotFoundException, - Req, - Res, - UnauthorizedException, -} from '@nestjs/common'; +import { Controller, Get, Next, Req, Res } from '@nestjs/common'; import { NextFunction, Request, Response } from 'express'; -import { readdir, stat } from 'fs/promises'; -import { join } from 'path'; -import { Stats } from 'fs'; +import { StaticService } from './static.service'; @Controller('*') export class StaticController { + constructor(private readonly staticService: StaticService) {} @Get() async main( @Req() request: Request, @Res() response: Response, @Next() next: NextFunction, ) { - //todo - refactor to use service - const relPath = request.url; - //relPath = relPath.split('%2E').join('.'); - //relPath = relPath.split('%2e').join('.'); - - if (relPath.indexOf('..') > -1) { - throw new UnauthorizedException( - 'You are not authorized to visit ' + request.url, - ); - } - - const path = join(__dirname, process.env.STATIC_SERVE_FOLDER, relPath); - - const stats: Stats | false = await stat(path).catch(() => false); - - if (stats === false) { - throw new NotFoundException('Path not found: ' + relPath); - } - - if (!stats.isDirectory()) { + const dirIndexHtml = await this.staticService.provideDirIndex(request.url); + if (dirIndexHtml === null) { return next(); } - response.status(200).send(await readDirectory(path, relPath)); - - return; + return response.status(200).send(dirIndexHtml); } } - -//todo - move this to service -async function readDirectory(path: string, relPath: string) { - let files = await readdir(path); - let longestFileLength = 0; - - files = files.filter((fileName: string) => { - const fileIncluded = fileName[0] !== '.'; - longestFileLength = - fileIncluded && longestFileLength < fileName.length - ? fileName.length - : longestFileLength; - - return fileIncluded; - }); - - longestFileLength += 10; - - let result: string; - const resultHtmlStart = ` - -
`;
-
- result = `../`;
-
- for (const file of files) {
- const stats: Stats | false = await stat(join(path, file)).catch(
- () => false,
- );
-
- if (stats === false) {
- continue;
- }
- const arrDate: Array = stats.birthtime.toUTCString().split(' ');
- const strDate: string =
- ' '.repeat(longestFileLength - file.length) +
- `${arrDate[1]}-${arrDate[2]}-${arrDate[3]} ${arrDate[4]}`;
-
- if (stats.isDirectory()) {
- const strSize = ' '.repeat(19) + '-';
- result += '\n' + `${file}/${strDate + strSize}`;
- continue;
- }
- let strSize = stats.size.toString();
- strSize = ' '.repeat(20 - strSize.length) + strSize;
-
- result += '\n' + `${file} ${strDate + strSize}`;
- }
-
- const resultHtmlEnd = ` `;
+
+ result = `../`;
+
+ for (const file of files) {
+ const stats: Stats | false = await stat(join(path, file)).catch(
+ () => false,
+ );
+
+ if (stats === false) {
+ continue;
+ }
+ const arrDate: Array = stats.birthtime.toUTCString().split(' ');
+ const strDate: string =
+ ' '.repeat(longestFileLength - file.length) +
+ `${arrDate[1]}-${arrDate[2]}-${arrDate[3]} ${arrDate[4]}`;
+
+ if (stats.isDirectory()) {
+ const strSize = ' '.repeat(19) + '-';
+ result += '\n' + `${file}/${strDate + strSize}`;
+ continue;
+ }
+ let strSize = stats.size.toString();
+ strSize = ' '.repeat(20 - strSize.length) + strSize;
+
+ result += '\n' + `${file} ${strDate + strSize}`;
+ }
+
+ const resultHtmlEnd = `