mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
Static module: extract logic from controller to service
This commit is contained in:
@@ -1,106 +1,21 @@
|
|||||||
import {
|
import { Controller, Get, Next, Req, Res } from '@nestjs/common';
|
||||||
Controller,
|
|
||||||
Get,
|
|
||||||
Next,
|
|
||||||
NotFoundException,
|
|
||||||
Req,
|
|
||||||
Res,
|
|
||||||
UnauthorizedException,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { NextFunction, Request, Response } from 'express';
|
import { NextFunction, Request, Response } from 'express';
|
||||||
import { readdir, stat } from 'fs/promises';
|
import { StaticService } from './static.service';
|
||||||
import { join } from 'path';
|
|
||||||
import { Stats } from 'fs';
|
|
||||||
|
|
||||||
@Controller('*')
|
@Controller('*')
|
||||||
export class StaticController {
|
export class StaticController {
|
||||||
|
constructor(private readonly staticService: StaticService) {}
|
||||||
@Get()
|
@Get()
|
||||||
async main(
|
async main(
|
||||||
@Req() request: Request,
|
@Req() request: Request,
|
||||||
@Res() response: Response,
|
@Res() response: Response,
|
||||||
@Next() next: NextFunction,
|
@Next() next: NextFunction,
|
||||||
) {
|
) {
|
||||||
//todo - refactor to use service
|
const dirIndexHtml = await this.staticService.provideDirIndex(request.url);
|
||||||
const relPath = request.url;
|
if (dirIndexHtml === null) {
|
||||||
//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()) {
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
response.status(200).send(await readDirectory(path, relPath));
|
return response.status(200).send(dirIndexHtml);
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//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 = `
|
|
||||||
<html lang="en">
|
|
||||||
<head><title>Index of ${relPath} </title></head>
|
|
||||||
<body>
|
|
||||||
<h1>Index of ${relPath}</h1>
|
|
||||||
<hr><pre>`;
|
|
||||||
|
|
||||||
result = `<a href="../">../</a>`;
|
|
||||||
|
|
||||||
for (const file of files) {
|
|
||||||
const stats: Stats | false = await stat(join(path, file)).catch(
|
|
||||||
() => false,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (stats === false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const arrDate: Array<string> = 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' + `<a href="${file}/">${file}/</a>${strDate + strSize}`;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let strSize = stats.size.toString();
|
|
||||||
strSize = ' '.repeat(20 - strSize.length) + strSize;
|
|
||||||
|
|
||||||
result += '\n' + `<a href="${file}">${file}</a> ${strDate + strSize}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const resultHtmlEnd = `</pre><hr></body></html>`;
|
|
||||||
|
|
||||||
return resultHtmlStart + result + resultHtmlEnd;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { ServeStaticModule } from '@nestjs/serve-static';
|
|||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { StaticController } from './static.controller';
|
import { StaticController } from './static.controller';
|
||||||
import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware';
|
import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware';
|
||||||
|
import { StaticService } from './static.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -16,7 +17,7 @@ import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middle
|
|||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
controllers: [StaticController],
|
controllers: [StaticController],
|
||||||
providers: [],
|
providers: [StaticService],
|
||||||
})
|
})
|
||||||
export class StaticModule implements NestModule {
|
export class StaticModule implements NestModule {
|
||||||
configure(consumer: MiddlewareConsumer) {
|
configure(consumer: MiddlewareConsumer) {
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
import {
|
||||||
|
Injectable,
|
||||||
|
NotFoundException,
|
||||||
|
UnauthorizedException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { readdir, stat } from 'fs/promises';
|
||||||
|
import { Stats } from 'fs';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class StaticService {
|
||||||
|
async provideDirIndex(relPath: string): Promise<string> {
|
||||||
|
if (relPath.indexOf('..') > -1) {
|
||||||
|
throw new UnauthorizedException(
|
||||||
|
'You are not authorized to visit ' + relPath,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.readDirectory(path, relPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async 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 = `
|
||||||
|
<html lang="en">
|
||||||
|
<head><title>Index of ${relPath} </title></head>
|
||||||
|
<body>
|
||||||
|
<h1>Index of ${relPath}</h1>
|
||||||
|
<hr><pre>`;
|
||||||
|
|
||||||
|
result = `<a href="../">../</a>`;
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
const stats: Stats | false = await stat(join(path, file)).catch(
|
||||||
|
() => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (stats === false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const arrDate: Array<string> = 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' + `<a href="${file}/">${file}/</a>${strDate + strSize}`;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let strSize = stats.size.toString();
|
||||||
|
strSize = ' '.repeat(20 - strSize.length) + strSize;
|
||||||
|
|
||||||
|
result += '\n' + `<a href="${file}">${file}</a> ${strDate + strSize}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const resultHtmlEnd = `</pre><hr></body></html>`;
|
||||||
|
|
||||||
|
return resultHtmlStart + result + resultHtmlEnd;
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-17
@@ -1,14 +1,5 @@
|
|||||||
import {
|
import { Module } from '@nestjs/common';
|
||||||
//MiddlewareConsumer,
|
|
||||||
Module,
|
|
||||||
//NestModule,
|
|
||||||
//RequestMethod,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
//import { AppService } from './app.service';
|
|
||||||
//import { KodiApiController } from './KodiApi/kodi-api.controller';
|
|
||||||
//import { StaticController } from './Static/static.controller';
|
|
||||||
//import { KodiApiUrlRewriteMiddleware } from './KodiApi/UrlRewrite/url-rewrite.middleware';
|
|
||||||
import { KodiApiModule } from './KodiApi/kodi-api.module';
|
import { KodiApiModule } from './KodiApi/kodi-api.module';
|
||||||
import { StaticModule } from './Static/static.module';
|
import { StaticModule } from './Static/static.module';
|
||||||
|
|
||||||
@@ -18,10 +9,3 @@ import { StaticModule } from './Static/static.module';
|
|||||||
providers: [],
|
providers: [],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
// export class AppModule implements NestModule {
|
|
||||||
// configure(consumer: MiddlewareConsumer) {
|
|
||||||
// consumer
|
|
||||||
// .apply(KodiApiUrlRewriteMiddleware)
|
|
||||||
// .forRoutes({ path: '/api*', method: RequestMethod.ALL });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
+2
-1
@@ -16,6 +16,7 @@
|
|||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"strictBindCallApply": false,
|
"strictBindCallApply": false,
|
||||||
"forceConsistentCasingInFileNames": false,
|
"forceConsistentCasingInFileNames": false,
|
||||||
"noFallthroughCasesInSwitch": false
|
"noFallthroughCasesInSwitch": false,
|
||||||
|
"strict": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user