From 61c71e6c8dfae4ac7dfc3b5e929fed3af5eca96b Mon Sep 17 00:00:00 2001 From: MrFry Date: Sat, 26 Oct 2019 09:52:20 +0200 Subject: [PATCH] Handled no such file exception --- modules/stuff.js | 56 ++++++++++++++++++-------------- views/stuff/nofile.ejs | 74 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 views/stuff/nofile.ejs diff --git a/modules/stuff.js b/modules/stuff.js index 08c72ba..5113387 100644 --- a/modules/stuff.js +++ b/modules/stuff.js @@ -49,7 +49,7 @@ app.use(bodyParser.json({ // -------------------------------------------------------------- app.get('/*', function (req, res) { - let parsedUrl = req.url.replace(/%20/g, " ") + let parsedUrl = req.url.replace(/%20/g, ' ') let curr = listedFiles + '/' + parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0] let relPath = curr.substring('./public/files'.length, curr.length) @@ -64,37 +64,43 @@ app.get('/*', function (req, res) { logger.LogReq(req) - if (fs.lstatSync(curr).isDirectory()) { - if (curr[curr.length - 1] !== '/') { curr += '/' } + try { + if (fs.lstatSync(curr).isDirectory()) { + if (curr[curr.length - 1] !== '/') { curr += '/' } - let f = [] + let f = [] - fs.readdirSync(curr).forEach((item) => { - if (item[0] !== '.') { - let res = { name: item } - let stats = fs.statSync(curr + '/' + item) + fs.readdirSync(curr).forEach((item) => { + if (item[0] !== '.') { + let res = { name: item } + let stats = fs.statSync(curr + '/' + item) - let fileSizeInBytes = stats['size'] - res.size = Math.round(fileSizeInBytes / 1000000) + let fileSizeInBytes = stats['size'] + res.size = Math.round(fileSizeInBytes / 1000000) - res.path = relPath - if (res.path[res.path.length - 1] !== '/') { res.path += '/' } - res.path += item + res.path = relPath + if (res.path[res.path.length - 1] !== '/') { res.path += '/' } + res.path += item - res.mtime = stats['mtime'].toLocaleString() + res.mtime = stats['mtime'].toLocaleString() - f.push(res) - } + f.push(res) + } + }) + + res.render('stuff/folders', { + folders: f, + dirname: relPath, + prevDir + }) + } else { + let fileStream = fs.createReadStream(curr) + fileStream.pipe(res) + } + } catch (e) { + res.render('stuff/nofile', { + missingFile: curr }) - - res.render('stuff/folders', { - folders: f, - dirname: relPath, - prevDir - }) - } else { - let fileStream = fs.createReadStream(curr) - fileStream.pipe(res) } }) diff --git a/views/stuff/nofile.ejs b/views/stuff/nofile.ejs new file mode 100644 index 0000000..8d7c0ad --- /dev/null +++ b/views/stuff/nofile.ejs @@ -0,0 +1,74 @@ + + + + + + + No such file/folder + + + +
+

+ No such file / folder: +
+ <%= missingFile %> +
+ " > Back to root +

+
+ +