Okay, I fixed issue with special characters in file's names passed to proxy by serving files by NGiNX. So now I add a /files prefix to a full path. So previous commit is partly reverted. :) And need in file-type package is eliminated.

This commit is contained in:
Alexander Andreev 2021-02-17 03:05:50 +04:00
parent f728f4de4f
commit 1c632dcf80
Signed by: Arav
GPG Key ID: 610DF2574456329F
2 changed files with 3 additions and 21 deletions

View File

@ -5,7 +5,6 @@ const Koa = require("koa");
const koaPug = require("koa-pug");
const koaRouter = require("koa-router");
const koaRange = require("koa-range");
const FileType = require("file-type");
const config = require("./config");
const util = require("./util");
@ -44,17 +43,7 @@ function sortByNameField(a, b) {
return 0;
}
async function fileType(name) {
let f = await FileType.fromStream(fs.createReadStream(name));
if (f === undefined) {
if (name.endsWith("txt"))
return "text/plain";
} else
return f.mime;
return "octet-stream";
}
async function getDirectoryList(dir_path) {
async function getDirectoryList(dir_path, orig_url) {
let dirs = [];
let files = [];
let total_files_size = 0;
@ -73,7 +62,7 @@ async function getDirectoryList(dir_path) {
total_files_size += stat.size;
files.push({
name: dirent.name,
link: `${encodeURIComponent(dirent.name)}`,
link: `/files${orig_url}${encodeURIComponent(dirent.name)}`,
datetime: util.datetime(stat.mtime, util.formats.file_date),
size: +s,
size_unit: u });
@ -93,7 +82,7 @@ function setRoutes(router) {
let stat = fs.lstatSync(file_path);
if (stat.isDirectory()) {
const [items, total_directories, total_files, total_files_size]
= await getDirectoryList(file_path);
= await getDirectoryList(file_path, decodeURI(ctx.originalUrl));
await ctx.render('index', {
title: "/ Files",
description: "File share.",
@ -103,12 +92,6 @@ function setRoutes(router) {
total_files_size: total_files_size,
total_directories: total_directories,
items: items });
} else {
ctx.type = await fileType(file_path);
ctx.set("Content-Length", stat.size);
const stream = fs.createReadStream(file_path);
ctx.body = stream;
}
});
}

View File

@ -14,7 +14,6 @@
"author": "Arav",
"license": "GPLv3",
"dependencies": {
"file-type": "^16.2.0",
"koa": "^2.13.1",
"koa-body": "^4.2.0",
"koa-pug": "^4.0.4",