mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
add html folder index response to static module
This commit is contained in:
@@ -1,30 +1,14 @@
|
||||
<p align="center">
|
||||
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
|
||||
</p>
|
||||
|
||||
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
|
||||
[circleci-url]: https://circleci.com/gh/nestjs/nest
|
||||
|
||||
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
|
||||
<p align="center">
|
||||
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
|
||||
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
|
||||
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
|
||||
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
|
||||
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
|
||||
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
|
||||
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
|
||||
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
|
||||
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
|
||||
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
|
||||
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
|
||||
</p>
|
||||
<!--[](https://opencollective.com/nest#backer)
|
||||
[](https://opencollective.com/nest#sponsor)-->
|
||||
An API server for personal KODI plugin.
|
||||
|
||||
## Description
|
||||
|
||||
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
|
||||
A rewrite attempt from "vanilla" node.js into framework based (Nest.js).
|
||||
|
||||
## License
|
||||
|
||||
No licence (Nest is MIT)
|
||||
|
||||
## Below are left from nest readme
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -57,17 +41,3 @@ $ npm run test:e2e
|
||||
# test coverage
|
||||
$ npm run test:cov
|
||||
```
|
||||
|
||||
## Support
|
||||
|
||||
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
||||
|
||||
## Stay in touch
|
||||
|
||||
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
|
||||
- Website - [https://nestjs.com](https://nestjs.com/)
|
||||
- Twitter - [@nestframework](https://twitter.com/nestframework)
|
||||
|
||||
## License
|
||||
|
||||
Nest is [MIT licensed](LICENSE).
|
||||
|
||||
Generated
+2
-2
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "nest-test",
|
||||
"name": "kodi-api-server",
|
||||
"version": "0.0.1",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "nest-test",
|
||||
"name": "kodi-api-server",
|
||||
"version": "0.0.1",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
|
||||
+48
-126
@@ -10,6 +10,7 @@ import {
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
import { readdir, stat } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { Stats } from 'fs';
|
||||
|
||||
@Controller('*')
|
||||
export class StaticController {
|
||||
@@ -19,6 +20,7 @@ export class StaticController {
|
||||
@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('.');
|
||||
@@ -31,154 +33,74 @@ export class StaticController {
|
||||
|
||||
const path = join(__dirname, process.env.STATIC_SERVE_FOLDER, relPath);
|
||||
|
||||
const stats: any = await stat(path).catch(() => false);
|
||||
const stats: Stats | false = await stat(path).catch(() => false);
|
||||
|
||||
if (stats === false) {
|
||||
throw new NotFoundException('Path not found: ' + relPath);
|
||||
}
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
if (!stats.isDirectory()) {
|
||||
return next();
|
||||
}
|
||||
|
||||
response.status(200).send(await readDirectory(path, relPath));
|
||||
//.send(JSON.stringify(await readDirectory(path, relPath), null, ' '));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
}
|
||||
|
||||
//todo - move this to service
|
||||
async function readDirectory(path: string, relPath: string) {
|
||||
let files = await readdir(path);
|
||||
let longestFileLenght = 0;
|
||||
let longestFileLength = 0;
|
||||
|
||||
files = files.filter((fileName: string) => {
|
||||
const fileIncluded = fileName[0] !== '.';
|
||||
longestFileLenght = fileIncluded ? fileName.length : longestFileLenght;
|
||||
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>`;
|
||||
|
||||
if (files.length == 0) {
|
||||
//strPath = strPath.replace(strRootDir, "")
|
||||
result = "<html>\n"
|
||||
result = result + "<head><title>Index of " + relPath + "</title></head>\n"
|
||||
result = result + '<body bgcolor="white">\n'
|
||||
result = result + '<h1>Index of ' + relPath + '</h1><hr><pre><a href="../">../</a>'
|
||||
result = result + "\n</pre><hr></body>"
|
||||
result = result + "</html>"
|
||||
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}`;
|
||||
}
|
||||
|
||||
return result;
|
||||
const resultHtmlEnd = `</pre><hr></body></html>`;
|
||||
|
||||
return resultHtmlStart + result + resultHtmlEnd;
|
||||
}
|
||||
|
||||
// function getFilesInDirHtml(strPath, callback) {
|
||||
// readdir
|
||||
|
||||
// fs.readdir(strPath, function (err, arrItemsAll) {
|
||||
|
||||
// if (err) { callback(err, null) }
|
||||
|
||||
// var arrItemsNotHidden = []
|
||||
// var numLength = 0
|
||||
|
||||
// if (!arrItemsAll) arrItemsAll = []
|
||||
|
||||
// //remove hidden (with dot in front)
|
||||
// //find longest item length
|
||||
// arrItemsAll.forEach(function (strItem) {
|
||||
|
||||
// if (strItem[0] != ".") {
|
||||
|
||||
// arrItemsNotHidden[arrItemsNotHidden.length] = strItem
|
||||
// if (strItem.length > numLength) { numLength = strItem.length }
|
||||
|
||||
// }
|
||||
|
||||
// });
|
||||
|
||||
// numLength = numLength + 10
|
||||
|
||||
// var arrRows = []
|
||||
// arrItemsNotHidden.sort()
|
||||
|
||||
// if (arrItemsNotHidden.length == 0) {
|
||||
// strPath = strPath.replace(strRootDir, "")
|
||||
// var result = "<html>\n"
|
||||
// result = result + "<head><title>Index of " + strPath + "</title></head>\n"
|
||||
// result = result + '<body bgcolor="white">\n'
|
||||
// result = result + '<h1>Index of ' + strPath + '</h1><hr><pre><a href="../">../</a>'
|
||||
// result = result + "\n</pre><hr></body>"
|
||||
// result = result + "</html>"
|
||||
// callback(null, result)
|
||||
// }
|
||||
|
||||
// arrItemsNotHidden.forEach(function (strItem) {
|
||||
|
||||
// fs.stat(strPath + strItem, function (err, stats) {
|
||||
|
||||
// if (err) {
|
||||
// arrRows[arrRows.length] = "error"
|
||||
// return
|
||||
// }
|
||||
|
||||
// var strRow = ""
|
||||
// var strSize = ""
|
||||
// var strDate = ""
|
||||
// var strName = ""
|
||||
|
||||
// //get size and name and date
|
||||
// if (!stats.isDirectory()) {
|
||||
|
||||
// strSize = stats.size.toString()
|
||||
// strSize = space(20 - strSize.length) + strSize
|
||||
|
||||
// //add name
|
||||
// strName = strItem
|
||||
// //get date
|
||||
// var arrDate = stats.birthtime.toUTCString().split(" ")
|
||||
// strDate = space(numLength - strItem.length) + arrDate[1] + "-" + arrDate[2] + "-" + arrDate[3] + " " + arrDate[4]
|
||||
// //strDate = space(numLength - strItem.length) + stats.birthtime.toISOString()
|
||||
|
||||
// } else {
|
||||
// strSize = space(19) + "-"
|
||||
// //add name
|
||||
// strName = strItem + "/"
|
||||
// //get date
|
||||
// var arrDate = stats.birthtime.toUTCString().split(" ")
|
||||
// strDate = space(numLength - strItem.length - 1) + arrDate[1] + "-" + arrDate[2] + "-" + arrDate[3] + " " + arrDate[4]
|
||||
// //strDate = space(numLength - strItem.length - 1) + stats.birthtime.toISOString()
|
||||
// }
|
||||
|
||||
// //combine row
|
||||
// //<a href="Animation/">Animation/</a> 28-Apr-2018 20:56 -
|
||||
// strRow = '<a href="' + strName + '">' + strName + '</a>' + strDate + strSize
|
||||
|
||||
// //strRow = strName + strDate + strSize
|
||||
// arrRows[arrRows.length] = strRow
|
||||
|
||||
// //check if all done, call callback
|
||||
// if (arrRows.length == arrItemsNotHidden.length) {
|
||||
// //callback(null, result)
|
||||
// arrRows.sort()
|
||||
// strPath = strPath.replace(strRootDir, "");
|
||||
// var result = '<html>\n';
|
||||
// result =
|
||||
// result + '<head><title>Index of ' + strPath + '</title></head>\n';
|
||||
// result = result + '<body bgcolor="white">\n'
|
||||
// result =
|
||||
// result +
|
||||
// '<h1>Index of ' +
|
||||
// strPath +
|
||||
// '</h1><hr><pre><a href="../">../</a>\n';
|
||||
// result = result + arrRows.join('\n');
|
||||
// result = result + '\n</pre><hr></body>';
|
||||
// result = result + '</html>';
|
||||
// callback(null, result);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user