mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
Static: extract view file
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
export default class FileDto {
|
||||
constructor(
|
||||
readonly name: string,
|
||||
readonly size: number,
|
||||
readonly date: Date,
|
||||
readonly isDirectory: boolean,
|
||||
) {
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
this.date = date;
|
||||
this.isDirectory = isDirectory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import FileDto from '../Dto/file.dto';
|
||||
|
||||
export default class IndexView {
|
||||
public render(relPath: string, files: Array<FileDto>): string {
|
||||
return this.buildHtml(relPath, files);
|
||||
}
|
||||
|
||||
private buildHtml(relPath: string, files: Array<FileDto>): string {
|
||||
const longestFileLength = this.getLongestFileLength(files);
|
||||
|
||||
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) {
|
||||
result += this.getFileLink(file, longestFileLength);
|
||||
}
|
||||
|
||||
const resultHtmlEnd = `</pre><hr></body></html>`;
|
||||
|
||||
return resultHtmlStart + result + resultHtmlEnd;
|
||||
}
|
||||
|
||||
private getFileLink(file: FileDto, longestFileLength: number) {
|
||||
const arrDate: Array<string> = file.date.toUTCString().split(' ');
|
||||
const strDate: string =
|
||||
' '.repeat(longestFileLength - file.name.length) +
|
||||
`${arrDate[1]}-${arrDate[2]}-${arrDate[3]} ${arrDate[4]}`;
|
||||
|
||||
if (file.isDirectory) {
|
||||
const strSize = ' '.repeat(19) + '-';
|
||||
return (
|
||||
'\n' + `<a href="${file.name}/">${file.name}/</a>${strDate + strSize}`
|
||||
);
|
||||
}
|
||||
let strSize = file.size.toString();
|
||||
strSize = ' '.repeat(20 - strSize.length) + strSize;
|
||||
|
||||
return (
|
||||
'\n' + `<a href="${file.name}">${file.name}</a> ${strDate + strSize}`
|
||||
);
|
||||
}
|
||||
|
||||
private getLongestFileLength(files: Array<FileDto>) {
|
||||
let longestFileLength = 0;
|
||||
for (const file of files) {
|
||||
longestFileLength =
|
||||
longestFileLength < file.name.length
|
||||
? file.name.length
|
||||
: longestFileLength;
|
||||
}
|
||||
longestFileLength += 10;
|
||||
return longestFileLength;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Controller, Get, Next, Req, Res } from '@nestjs/common';
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
import { StaticService } from './static.service';
|
||||
import IndexView from './View/index.view';
|
||||
|
||||
@Controller('*')
|
||||
export class StaticController {
|
||||
@@ -11,11 +12,12 @@ export class StaticController {
|
||||
@Res() response: Response,
|
||||
@Next() next: NextFunction,
|
||||
) {
|
||||
const dirIndexHtml = await this.staticService.provideDirIndex(request.url);
|
||||
if (dirIndexHtml === null) {
|
||||
const dirIndex = await this.staticService.provideDirIndex(request.url);
|
||||
if (dirIndex === null) {
|
||||
return next();
|
||||
}
|
||||
const view = new IndexView();
|
||||
|
||||
return response.status(200).send(dirIndexHtml);
|
||||
return response.status(200).send(view.render(request.url, dirIndex));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@ import {
|
||||
import { readdir, stat } from 'fs/promises';
|
||||
import { Stats } from 'fs';
|
||||
import { join } from 'path';
|
||||
import FileDto from './Dto/file.dto';
|
||||
|
||||
@Injectable()
|
||||
export class StaticService {
|
||||
async provideDirIndex(relPath: string): Promise<string> {
|
||||
async provideDirIndex(relPath: string): Promise<Array<FileDto>> {
|
||||
if (relPath.indexOf('..') > -1) {
|
||||
throw new UnauthorizedException(
|
||||
'You are not authorized to visit ' + relPath,
|
||||
@@ -28,35 +29,17 @@ export class StaticService {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.readDirectory(path, relPath);
|
||||
return this.readDirectory(path);
|
||||
}
|
||||
|
||||
private async readDirectory(path: string, relPath: string) {
|
||||
private async readDirectory(path: string): Promise<Array<FileDto>> {
|
||||
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;
|
||||
return fileName[0] !== '.';
|
||||
});
|
||||
|
||||
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>`;
|
||||
|
||||
const filesDtos: Array<FileDto> = [];
|
||||
for (const file of files) {
|
||||
const stats: Stats | false = await stat(join(path, file)).catch(
|
||||
() => false,
|
||||
@@ -65,24 +48,16 @@ export class StaticService {
|
||||
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]}`;
|
||||
const fileDto = new FileDto(
|
||||
file,
|
||||
stats.size,
|
||||
stats.birthtime,
|
||||
stats.isDirectory(),
|
||||
);
|
||||
|
||||
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}`;
|
||||
filesDtos.push(fileDto);
|
||||
}
|
||||
|
||||
const resultHtmlEnd = `</pre><hr></body></html>`;
|
||||
|
||||
return resultHtmlStart + result + resultHtmlEnd;
|
||||
return filesDtos;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user