From 61c71e6c8dfae4ac7dfc3b5e929fed3af5eca96b Mon Sep 17 00:00:00 2001
From: MrFry
Date: Sat, 26 Oct 2019 09:52:20 +0200
Subject: [PATCH 1/2] 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
+
+
+
+
From d841e464f5c1ef9b932114bfabe1d7ecf0f17e7f Mon Sep 17 00:00:00 2001
From: MrFry
Date: Sat, 26 Oct 2019 10:08:56 +0200
Subject: [PATCH 2/2] Global site url source
---
modules/main.js | 7 +++++--
modules/old.js | 10 +++++++---
modules/qmining.js | 11 +++++++----
modules/stuff.js | 10 ++++++++--
server.js | 8 +++++++-
views/stuff/folders.ejs | 4 ++--
views/stuff/nofile.ejs | 4 +---
7 files changed, 37 insertions(+), 17 deletions(-)
diff --git a/modules/main.js b/modules/main.js
index 2c508de..b7e35cf 100644
--- a/modules/main.js
+++ b/modules/main.js
@@ -18,7 +18,7 @@
------------------------------------------------------------------------- */
-const siteUrl = 'https://qmining.frylabs.net' // http(s)//asd.basd
+let url = '' // http(s)//asd.basd
const express = require('express')
const bodyParser = require('body-parser')
@@ -49,7 +49,7 @@ app.use(bodyParser.json({
app.get('/', function (req, res) {
res.render('main/main', {
- siteurl: siteUrl
+ siteurl: url
})
})
@@ -65,5 +65,8 @@ app.post('*', function (req, res) {
})
exports.app = app
+exports.setup = (x) => {
+ url = x.url
+}
logger.Log('Main module started', logger.GetColor('yellow'))
diff --git a/modules/old.js b/modules/old.js
index f54b7c1..680d3bc 100644
--- a/modules/old.js
+++ b/modules/old.js
@@ -18,6 +18,7 @@
------------------------------------------------------------------------- */
+let url = ''
const express = require('express')
const app = express()
@@ -26,17 +27,20 @@ const logger = require('../utils/logger.js')
// --------------------------------------------------------------
app.get('/', function (req, res) {
- res.redirect('https://qmining.frylabs.net' + req.url)
+ res.redirect(url + req.url)
})
app.get('*', function (req, res) {
- res.redirect('https://qmining.frylabs.net' + req.url)
+ res.redirect(url + req.url)
})
app.post('*', function (req, res) {
- res.redirect('https://qmining.frylabs.net' + req.url)
+ res.redirect(url + req.url)
})
exports.app = app
+exports.setup = (x) => {
+ url = x.url
+}
logger.Log('Old module started', logger.GetColor('yellow'))
diff --git a/modules/qmining.js b/modules/qmining.js
index fa8b83d..6800e21 100644
--- a/modules/qmining.js
+++ b/modules/qmining.js
@@ -18,7 +18,7 @@
------------------------------------------------------------------------- */
-const siteUrl = 'https://qmining.frylabs.net' // http(s)//asd.basd
+let url = ''
const express = require('express')
const bodyParser = require('body-parser')
@@ -60,7 +60,7 @@ app.get('/', function (req, res) {
// req.hostname
res.render('qmining/main', {
- siteurl: siteUrl,
+ siteurl: url,
qa: actions.ProcessQA()
})
res.end()
@@ -97,7 +97,7 @@ app.get('/legacy', function (req, res) {
data: d,
scount: scount,
qcount: qcount,
- siteurl: siteUrl
+ siteurl: url
})
logger.LogReq(req)
@@ -154,7 +154,7 @@ app.get('/greasy', function (req, res) {
})
app.get('/install', function (req, res) {
- res.redirect('http://qmining.frylabs.net/moodle-test-userscript/stable.user.js')
+ res.redirect(url + '/moodle-test-userscript/stable.user.js')
res.end()
logger.LogReq(req)
})
@@ -225,5 +225,8 @@ app.post('*', function (req, res) {
})
exports.app = app
+exports.setup = (x) => {
+ url = x.url
+}
logger.Log('Qmining module started', logger.GetColor('yellow'))
diff --git a/modules/stuff.js b/modules/stuff.js
index 5113387..4e174eb 100644
--- a/modules/stuff.js
+++ b/modules/stuff.js
@@ -23,6 +23,7 @@ const bodyParser = require('body-parser')
const busboy = require('connect-busboy')
const fs = require('fs')
const app = express()
+let url = ''
const logger = require('../utils/logger.js')
// const utils = require('../utils/utils.js')
@@ -91,7 +92,8 @@ app.get('/*', function (req, res) {
res.render('stuff/folders', {
folders: f,
dirname: relPath,
- prevDir
+ prevDir,
+ url
})
} else {
let fileStream = fs.createReadStream(curr)
@@ -99,7 +101,8 @@ app.get('/*', function (req, res) {
}
} catch (e) {
res.render('stuff/nofile', {
- missingFile: curr
+ missingFile: curr,
+ url
})
}
})
@@ -118,5 +121,8 @@ app.post('*', function (req, res) {
})
exports.app = app
+exports.setup = (x) => {
+ url = x.url
+}
logger.Log('Stuff module started', logger.GetColor('yellow'))
diff --git a/server.js b/server.js
index c59d84b..362c910 100644
--- a/server.js
+++ b/server.js
@@ -68,7 +68,13 @@ const app = express()
Object.keys(modules).forEach(function (k, i) {
let x = modules[k]
try {
- x.app = require(x.path).app
+ let mod = require(x.path)
+ if (mod.setup) {
+ mod.setup({
+ url: 'http://' + x.urls[0] // TODO http https or neither
+ })
+ }
+ x.app = mod.app
x.urls.forEach((url) => {
app.use(vhost(url, x.app))
})
diff --git a/views/stuff/folders.ejs b/views/stuff/folders.ejs
index 77013af..a153dfe 100644
--- a/views/stuff/folders.ejs
+++ b/views/stuff/folders.ejs
@@ -65,7 +65,7 @@
<%=dirname%>
- " > Up one level
+ Up one level
@@ -75,7 +75,7 @@