From 48591e37a58d5349214e0bfee9f76e201d45e7bf Mon Sep 17 00:00:00 2001 From: Rafael Marçalo Date: Fri, 28 Oct 2022 00:40:47 +0100 Subject: Started working on settings menu --- app.js | 12 +++++++++++- routes/settings.js | 8 ++++++++ server/js/config.js | 28 ++++++++++++++++++++++++++++ server/js/paths.js | 37 +++++++++++++++++++++++++++++++++++++ views/partials/top/navbar.ejs | 6 ++++++ views/settings.ejs | 5 +++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 routes/settings.js create mode 100644 server/js/config.js create mode 100644 server/js/paths.js create mode 100644 views/settings.ejs diff --git a/app.js b/app.js index da2a3b7..d87b6e0 100644 --- a/app.js +++ b/app.js @@ -4,12 +4,20 @@ const bodyParser = require('body-parser'); const express = require('express'); const path = require('path'); + +// handling variables const app = express(); +const { ensureFolder, envPaths } = require('./server/js/paths'); +const loadConfig = require('./server/js/config').loadConfig; +const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); +const configPath = path.join(configFolder, 'config.json'); const PORT = module.exports.PORT = process.env.PORT || 3000; +var config = require('./server/js/config').config; -// set the public folder to public acess and added a body parser +// loading configurations app.use(express.static(path.join(__dirname, '/public'))); app.use(bodyParser.urlencoded({extended: true})); +config = loadConfig(configPath); // set the view engine to ejs app.set('view engine', 'ejs'); @@ -20,10 +28,12 @@ const home = require("./routes/home"); const info = require("./routes/info"); const search = require("./routes/search"); const visit = require("./routes/visit"); +const settings = require("./routes/settings"); app.get('/', home); app.get('/info', info); app.get('/visit', visit); +app.get('/settings', settings); app.post('/search', search); // app start diff --git a/routes/settings.js b/routes/settings.js new file mode 100644 index 0000000..658c0f2 --- /dev/null +++ b/routes/settings.js @@ -0,0 +1,8 @@ +"use strict"; + +function settings(req, res) +{ + res.render('settings', {showSearchBar: true}); +}; + +module.exports = settings; diff --git a/server/js/config.js b/server/js/config.js new file mode 100644 index 0000000..077f928 --- /dev/null +++ b/server/js/config.js @@ -0,0 +1,28 @@ +"use strict"; + +const fs = require('fs'); + +const defaultConfig = { + 'settings': { + 'nsfw_content': false + } +}; +var config = {}; + +function createConfig(configPath, data) +{ + fs.writeFileSync(configPath, JSON.stringify(data), 'utf-8'); +} + +function loadConfig(configPath) +{ + if ( !fs.existsSync(configPath) ) + { + createConfig(configPath, defaultConfig); + } + + return JSON.parse(fs.readFileSync(configPath)); +} + + +module.exports = {loadConfig, config}; \ No newline at end of file diff --git a/server/js/paths.js b/server/js/paths.js new file mode 100644 index 0000000..ee10807 --- /dev/null +++ b/server/js/paths.js @@ -0,0 +1,37 @@ +"use strict"; + +const fs = require('fs'); +const path = require('path'); +const os = require('os'); + +function ensureFolder(folder) +{ + if (!fs.existsSync(folder)) + { + fs.mkdirSync(folder, { recursive: true }); + } + return folder; +} + +const envPaths = { + 'linux': function () { + return { + HOME: os.homedir(), + DATA: process.env.XDG_DATA_HOME || ensureFolder(path.join(os.homedir(), '.local', 'share')), + CONFIG: process.env.XDG_CONFIG_HOME || ensureFolder(path.join(os.homedir(), '.config')), + CACHE: process.env.XDG_CACHE_HOME || ensureFolder(path.join(os.homedir(), '.cache')) + } + }, + + 'win32': function () { + return { + HOME: os.homedir(), + DATA: process.env.APPDATA || ensureFolder(path.join(os.homedir(), 'AppData', 'Roaming')), + CONFIG: process.env.APPDATA || ensureFolder(path.join(os.homedir(), 'AppData', 'Roaming')), + CACHE: process.env.TEMP || process.env.TMP || ensureFolder(path.join(process.env.LOCALAPPDATA, 'Temp')) + } + } + +} [os.platform()](); + +module.exports = {ensureFolder, envPaths}; \ No newline at end of file diff --git a/views/partials/top/navbar.ejs b/views/partials/top/navbar.ejs index 356cb0b..4059dc1 100644 --- a/views/partials/top/navbar.ejs +++ b/views/partials/top/navbar.ejs @@ -5,6 +5,12 @@ SNEEDBAY
+ + + + + + diff --git a/views/settings.ejs b/views/settings.ejs new file mode 100644 index 0000000..6919dc4 --- /dev/null +++ b/views/settings.ejs @@ -0,0 +1,5 @@ +<%- include('partials/header'); %> + +

Settings

+ +<%- include('partials/footer'); %> -- cgit v1.2.3 From 5d6081e34777c8b99a3e9f2ed42a6de0732a4ff3 Mon Sep 17 00:00:00 2001 From: Rafael Marçalo Date: Sat, 29 Oct 2022 14:51:03 +0100 Subject: Added config file handling functionality --- app.js | 9 +++------ server/js/config.js | 32 +++++++++++++++++++------------- server/js/paths.js | 2 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index d87b6e0..4184d74 100644 --- a/app.js +++ b/app.js @@ -4,24 +4,21 @@ const bodyParser = require('body-parser'); const express = require('express'); const path = require('path'); +var config = require('./server/js/config'); // handling variables const app = express(); -const { ensureFolder, envPaths } = require('./server/js/paths'); -const loadConfig = require('./server/js/config').loadConfig; -const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); -const configPath = path.join(configFolder, 'config.json'); +const configPath = config.defaultConfigPath; const PORT = module.exports.PORT = process.env.PORT || 3000; -var config = require('./server/js/config').config; // loading configurations app.use(express.static(path.join(__dirname, '/public'))); app.use(bodyParser.urlencoded({extended: true})); -config = loadConfig(configPath); // set the view engine to ejs app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, '/views')); +config.loadConfig(configPath); // routes management const home = require("./routes/home"); diff --git a/server/js/config.js b/server/js/config.js index 077f928..b1e6836 100644 --- a/server/js/config.js +++ b/server/js/config.js @@ -1,28 +1,34 @@ "use strict"; +// Imports const fs = require('fs'); +const path = require('path'); +const { ensureFolder, envPaths } = require('./paths'); -const defaultConfig = { - 'settings': { - 'nsfw_content': false - } -}; -var config = {}; +// Handling Variables +const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); +const defaultConfigPath = path.join(configFolder, 'config.json'); +var loadedConfig = {}; -function createConfig(configPath, data) +function createConfig() { - fs.writeFileSync(configPath, JSON.stringify(data), 'utf-8'); + const defaultConfig = { + 'settings': { + 'nsfw_content': false + } + }; + + fs.writeFileSync(configPath, JSON.stringify(defaultConfig), 'utf-8'); } function loadConfig(configPath) { - if ( !fs.existsSync(configPath) ) + if (!fs.existsSync(configPath)) { createConfig(configPath, defaultConfig); } - - return JSON.parse(fs.readFileSync(configPath)); -} + this.loadedConfig = JSON.parse(fs.readFileSync(configPath)); +} -module.exports = {loadConfig, config}; \ No newline at end of file +module.exports = { loadConfig, defaultConfigPath, loadedConfig }; \ No newline at end of file diff --git a/server/js/paths.js b/server/js/paths.js index ee10807..3bf6824 100644 --- a/server/js/paths.js +++ b/server/js/paths.js @@ -28,7 +28,7 @@ const envPaths = { HOME: os.homedir(), DATA: process.env.APPDATA || ensureFolder(path.join(os.homedir(), 'AppData', 'Roaming')), CONFIG: process.env.APPDATA || ensureFolder(path.join(os.homedir(), 'AppData', 'Roaming')), - CACHE: process.env.TEMP || process.env.TMP || ensureFolder(path.join(process.env.LOCALAPPDATA, 'Temp')) + CACHE: process.env.TEMP || process.env.TMP || path.join(process.env.LOCALAPPDATA, 'Temp') || ensureFolder(path.join(os.homedir(), 'AppData', 'Local', 'Temp')) } } -- cgit v1.2.3 From 2cb8f9157ee1a919368d6bb0d0543124c67b5a0a Mon Sep 17 00:00:00 2001 From: Rafael Marçalo Date: Sat, 29 Oct 2022 23:57:43 +0100 Subject: Some error handling --- server/js/config.js | 24 ++++++++++++++++++++---- server/js/paths.js | 13 +++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/server/js/config.js b/server/js/config.js index b1e6836..c21a246 100644 --- a/server/js/config.js +++ b/server/js/config.js @@ -18,17 +18,33 @@ function createConfig() } }; - fs.writeFileSync(configPath, JSON.stringify(defaultConfig), 'utf-8'); + try + { + fs.writeFileSync(configPath, JSON.stringify(defaultConfig), 'utf-8'); + } + + catch (err) + { + console.log(err); + } } function loadConfig(configPath) { - if (!fs.existsSync(configPath)) + try { - createConfig(configPath, defaultConfig); + if (!fs.existsSync(configPath)) + { + createConfig(configPath, defaultConfig); + } + + this.loadedConfig = JSON.parse(fs.readFileSync(configPath)); } - this.loadedConfig = JSON.parse(fs.readFileSync(configPath)); + catch (err) + { + console.log(err); + } } module.exports = { loadConfig, defaultConfigPath, loadedConfig }; \ No newline at end of file diff --git a/server/js/paths.js b/server/js/paths.js index 3bf6824..003d9c0 100644 --- a/server/js/paths.js +++ b/server/js/paths.js @@ -6,10 +6,19 @@ const os = require('os'); function ensureFolder(folder) { - if (!fs.existsSync(folder)) + try { - fs.mkdirSync(folder, { recursive: true }); + if (!fs.existsSync(folder)) + { + fs.mkdirSync(folder, { recursive: true }); + } } + + catch (err) + { + console.log(err); + } + return folder; } -- cgit v1.2.3 From ba8c332375ebfad4969c6c432fd8ea8d1b7ada82 Mon Sep 17 00:00:00 2001 From: Rafael Marçalo Date: Sat, 5 Nov 2022 13:51:05 +0000 Subject: Some progress on working configs --- app.js | 7 ++++--- routes/settings.js | 25 +++++++++++++++++++++++-- server/js/config.js | 38 ++++++++++++++++++++++++-------------- views/settings.ejs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index 4184d74..00fabf1 100644 --- a/app.js +++ b/app.js @@ -9,7 +9,8 @@ var config = require('./server/js/config'); // handling variables const app = express(); const configPath = config.defaultConfigPath; -const PORT = module.exports.PORT = process.env.PORT || 3000; +config.loadConfig(configPath); +const PORT = module.exports.PORT = config.loadedConfig.settings.port || process.env.PORT || 3000; // loading configurations app.use(express.static(path.join(__dirname, '/public'))); @@ -18,19 +19,19 @@ app.use(bodyParser.urlencoded({extended: true})); // set the view engine to ejs app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, '/views')); -config.loadConfig(configPath); // routes management const home = require("./routes/home"); const info = require("./routes/info"); const search = require("./routes/search"); const visit = require("./routes/visit"); -const settings = require("./routes/settings"); +const {save, settings} = require("./routes/settings"); app.get('/', home); app.get('/info', info); app.get('/visit', visit); app.get('/settings', settings); +app.post('/settings', save); app.post('/search', search); // app start diff --git a/routes/settings.js b/routes/settings.js index 658c0f2..59b9f80 100644 --- a/routes/settings.js +++ b/routes/settings.js @@ -1,8 +1,29 @@ "use strict"; +const config = require("../server/js/config"); + function settings(req, res) { - res.render('settings', {showSearchBar: true}); + res.render('settings', {showSearchBar: true, config: config.loadedConfig}); +}; + +function save(req, res) +{ + let newConfig = { + 'settings': { + 'nsfw_content': ((req.body.nsfw === "on") ? true : false), + 'port': ((!isNaN(parseInt(req.body.port))) ? parseInt(req.body.port) : 3000), + 'tor': { + 'host': ((req.body.torHost) ? req.body.torHost : undefined), + 'port': ((!isNaN(parseInt(req.body.torPort))) ? parseInt(req.body.torPort) : undefined) + } + } + }; + + config.createConfig(newConfig); + config.loadConfig(); + + res.render('settings', {showSearchBar: true, config: config.loadedConfig}); }; -module.exports = settings; +module.exports = {settings, save}; diff --git a/server/js/config.js b/server/js/config.js index c21a246..f688bb7 100644 --- a/server/js/config.js +++ b/server/js/config.js @@ -8,19 +8,27 @@ const { ensureFolder, envPaths } = require('./paths'); // Handling Variables const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); const defaultConfigPath = path.join(configFolder, 'config.json'); +const defaultConfig = { + 'settings': { + 'nsfw_content': false, + 'port': undefined, + 'tor': { + 'host': undefined, + 'port': undefined + } + } +}; var loadedConfig = {}; -function createConfig() +// Functions +function createConfig(customConfig, configPath) { - const defaultConfig = { - 'settings': { - 'nsfw_content': false - } - }; + const path = configPath || defaultConfigPath; + const config = customConfig || defaultConfig; try { - fs.writeFileSync(configPath, JSON.stringify(defaultConfig), 'utf-8'); + fs.writeFileSync(path, JSON.stringify(config), 'utf-8'); } catch (err) @@ -31,14 +39,16 @@ function createConfig() function loadConfig(configPath) { - try + const path = configPath || defaultConfigPath; + + if (configPath && !fs.existsSync(path)) { - if (!fs.existsSync(configPath)) - { - createConfig(configPath, defaultConfig); - } + createConfig(defaultConfig, path); + } - this.loadedConfig = JSON.parse(fs.readFileSync(configPath)); + try + { + this.loadedConfig = JSON.parse(fs.readFileSync(path)); } catch (err) @@ -47,4 +57,4 @@ function loadConfig(configPath) } } -module.exports = { loadConfig, defaultConfigPath, loadedConfig }; \ No newline at end of file +module.exports = { loadConfig, defaultConfigPath, loadedConfig, createConfig }; \ No newline at end of file diff --git a/views/settings.ejs b/views/settings.ejs index 6919dc4..3538770 100644 --- a/views/settings.ejs +++ b/views/settings.ejs @@ -2,4 +2,48 @@

Settings

-<%- include('partials/footer'); %> +
+
+
+

Content

+
+
+ checked="true" <% } %> id="nsfw"> + +
+
+
+ +
+

Network

+
+
+ + value="<%= config.settings.port %>" <% } %> id="port"> +
+
+ + value="<%= config.settings.tor.host %>" <% } %> id="host"> + value="<%= config.settings.tor.port %>" <% } %> id="port"> +
+
+
+ +
+
+
+ +
+
+
+
+
+ +<%- include('partials/footer'); %> \ No newline at end of file -- cgit v1.2.3 From 79033ac8707408f4f7bc0460a732485c5abeec7d Mon Sep 17 00:00:00 2001 From: Rafael Marçalo Date: Sat, 5 Nov 2022 15:07:12 +0000 Subject: NSFW Content Filter working and fixed port behavior --- routes/search.js | 17 +++++++++++++++++ routes/settings.js | 4 ++-- server/js/config.js | 3 ++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/routes/search.js b/routes/search.js index e7a1b41..6732c52 100644 --- a/routes/search.js +++ b/routes/search.js @@ -1,12 +1,29 @@ "use strict"; const apibay = require('../server/js/apibay'); +const config = require('../server/js/config'); const template = require('../server/js/template'); async function search(req, res) { let query = req.body.search; let results = await apibay.hit(query); + + // Filter NSFW Content + if (!config.loadedConfig.settings.nsfw_content) + { + results = Array.prototype.filter.call(results, result => { + + let category = parseInt(result.category); + + if ( !(category > 500 && category < 600) ) + { + return result; + } + + }); + } + res.render('result', {query: query, results: results, functions: template, showSearchBar: true}); }; diff --git a/routes/settings.js b/routes/settings.js index 59b9f80..6f3baf6 100644 --- a/routes/settings.js +++ b/routes/settings.js @@ -12,7 +12,7 @@ function save(req, res) let newConfig = { 'settings': { 'nsfw_content': ((req.body.nsfw === "on") ? true : false), - 'port': ((!isNaN(parseInt(req.body.port))) ? parseInt(req.body.port) : 3000), + 'port': ((!isNaN(parseInt(req.body.port))) ? parseInt(req.body.port) : undefined), 'tor': { 'host': ((req.body.torHost) ? req.body.torHost : undefined), 'port': ((!isNaN(parseInt(req.body.torPort))) ? parseInt(req.body.torPort) : undefined) @@ -23,7 +23,7 @@ function save(req, res) config.createConfig(newConfig); config.loadConfig(); - res.render('settings', {showSearchBar: true, config: config.loadedConfig}); + res.redirect('/'); }; module.exports = {settings, save}; diff --git a/server/js/config.js b/server/js/config.js index f688bb7..a184515 100644 --- a/server/js/config.js +++ b/server/js/config.js @@ -6,6 +6,7 @@ const path = require('path'); const { ensureFolder, envPaths } = require('./paths'); // Handling Variables +const indentation = '\t'; const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); const defaultConfigPath = path.join(configFolder, 'config.json'); const defaultConfig = { @@ -28,7 +29,7 @@ function createConfig(customConfig, configPath) try { - fs.writeFileSync(path, JSON.stringify(config), 'utf-8'); + fs.writeFileSync(path, JSON.stringify(config, null, indentation), 'utf-8'); } catch (err) -- cgit v1.2.3 From cec82f62825a2ac1ce2ede1ed5cecc72a179ae19 Mon Sep 17 00:00:00 2001 From: Rafael Marçalo Date: Sat, 5 Nov 2022 20:40:04 +0000 Subject: Deprecated TOR Proxy, apibay blocks it --- public/css/extra.css | 2 +- routes/settings.js | 6 +----- server/js/config.js | 6 +----- views/index.ejs | 2 +- views/settings.ejs | 7 +------ 5 files changed, 5 insertions(+), 18 deletions(-) diff --git a/public/css/extra.css b/public/css/extra.css index 8e34546..0e6d508 100644 --- a/public/css/extra.css +++ b/public/css/extra.css @@ -28,7 +28,7 @@ white-space: nowrap; } -.search-bar:focus::placeholder{ +.mandatory-text-box:focus::placeholder{ color: transparent; } diff --git a/routes/settings.js b/routes/settings.js index 6f3baf6..c616a4c 100644 --- a/routes/settings.js +++ b/routes/settings.js @@ -12,11 +12,7 @@ function save(req, res) let newConfig = { 'settings': { 'nsfw_content': ((req.body.nsfw === "on") ? true : false), - 'port': ((!isNaN(parseInt(req.body.port))) ? parseInt(req.body.port) : undefined), - 'tor': { - 'host': ((req.body.torHost) ? req.body.torHost : undefined), - 'port': ((!isNaN(parseInt(req.body.torPort))) ? parseInt(req.body.torPort) : undefined) - } + 'port': ((!isNaN(parseInt(req.body.port))) ? parseInt(req.body.port) : undefined) } }; diff --git a/server/js/config.js b/server/js/config.js index a184515..5e0bc7b 100644 --- a/server/js/config.js +++ b/server/js/config.js @@ -12,11 +12,7 @@ const defaultConfigPath = path.join(configFolder, 'config.json'); const defaultConfig = { 'settings': { 'nsfw_content': false, - 'port': undefined, - 'tor': { - 'host': undefined, - 'port': undefined - } + 'port': undefined } }; var loadedConfig = {}; diff --git a/views/index.ejs b/views/index.ejs index f934a13..cf3ad42 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -3,7 +3,7 @@
diff --git a/views/settings.ejs b/views/settings.ejs index 3538770..7587da8 100644 --- a/views/settings.ejs +++ b/views/settings.ejs @@ -19,12 +19,7 @@
- value="<%= config.settings.port %>" <% } %> id="port"> -
-
- - value="<%= config.settings.tor.host %>" <% } %> id="host"> - value="<%= config.settings.tor.port %>" <% } %> id="port"> + value="<%= config.settings.port %>" <% } %> id="port">
-- cgit v1.2.3