diff options
author | Rafael Marçalo <63216029+rafa-99@users.noreply.github.com> | 2022-11-05 20:44:56 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-05 20:44:56 +0000 |
commit | fd5dc21f2da79298f43ce023a9d84f59c20de620 (patch) | |
tree | 63d3572d212b45228577a1e701fccf0eb05eef9a | |
parent | 2f9c4d5f287c40125703c98e49ab0658a847d99b (diff) | |
parent | cec82f62825a2ac1ce2ede1ed5cecc72a179ae19 (diff) |
Merge pull request #1 from rafa-99/settings
Settings menu
-rw-r--r-- | app.js | 12 | ||||
-rw-r--r-- | public/css/extra.css | 2 | ||||
-rw-r--r-- | routes/search.js | 17 | ||||
-rw-r--r-- | routes/settings.js | 25 | ||||
-rw-r--r-- | server/js/config.js | 57 | ||||
-rw-r--r-- | server/js/paths.js | 46 | ||||
-rw-r--r-- | views/index.ejs | 2 | ||||
-rw-r--r-- | views/partials/top/navbar.ejs | 6 | ||||
-rw-r--r-- | views/settings.ejs | 44 |
9 files changed, 207 insertions, 4 deletions
@@ -4,10 +4,15 @@ 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 PORT = module.exports.PORT = process.env.PORT || 3000; +const configPath = config.defaultConfigPath; +config.loadConfig(configPath); +const PORT = module.exports.PORT = config.loadedConfig.settings.port || process.env.PORT || 3000; -// 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})); @@ -20,10 +25,13 @@ const home = require("./routes/home"); const info = require("./routes/info"); const search = require("./routes/search"); const visit = require("./routes/visit"); +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/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/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 new file mode 100644 index 0000000..c616a4c --- /dev/null +++ b/routes/settings.js @@ -0,0 +1,25 @@ +"use strict"; + +const config = require("../server/js/config"); + +function settings(req, res) +{ + 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) : undefined) + } + }; + + config.createConfig(newConfig); + config.loadConfig(); + + res.redirect('/'); +}; + +module.exports = {settings, save}; diff --git a/server/js/config.js b/server/js/config.js new file mode 100644 index 0000000..5e0bc7b --- /dev/null +++ b/server/js/config.js @@ -0,0 +1,57 @@ +"use strict"; + +// Imports +const fs = require('fs'); +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 = { + 'settings': { + 'nsfw_content': false, + 'port': undefined + } +}; +var loadedConfig = {}; + +// Functions +function createConfig(customConfig, configPath) +{ + const path = configPath || defaultConfigPath; + const config = customConfig || defaultConfig; + + try + { + fs.writeFileSync(path, JSON.stringify(config, null, indentation), 'utf-8'); + } + + catch (err) + { + console.log(err); + } +} + +function loadConfig(configPath) +{ + const path = configPath || defaultConfigPath; + + if (configPath && !fs.existsSync(path)) + { + createConfig(defaultConfig, path); + } + + try + { + this.loadedConfig = JSON.parse(fs.readFileSync(path)); + } + + catch (err) + { + console.log(err); + } +} + +module.exports = { loadConfig, defaultConfigPath, loadedConfig, createConfig };
\ No newline at end of file diff --git a/server/js/paths.js b/server/js/paths.js new file mode 100644 index 0000000..003d9c0 --- /dev/null +++ b/server/js/paths.js @@ -0,0 +1,46 @@ +"use strict"; + +const fs = require('fs'); +const path = require('path'); +const os = require('os'); + +function ensureFolder(folder) +{ + try + { + if (!fs.existsSync(folder)) + { + fs.mkdirSync(folder, { recursive: true }); + } + } + + catch (err) + { + console.log(err); + } + + 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 || path.join(process.env.LOCALAPPDATA, 'Temp') || ensureFolder(path.join(os.homedir(), 'AppData', 'Local', 'Temp')) + } + } + +} [os.platform()](); + +module.exports = {ensureFolder, envPaths};
\ No newline at end of file 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 @@ <div class="text-center mt-5"> <img class="mw-100 w-50 img-fluid" src="static/images/sneedab.gif" alt="info_conver"> <form class="p-4" action="search" method="POST"> - <input type="text" name="search" class="mw-100 w-50 form-control text-bg-dark border-0 shadow text-center m-auto search-bar" placeholder="Search for torrent"> + <input type="text" name="search" class="mw-100 w-50 form-control text-bg-dark border-0 shadow text-center m-auto mandatory-text-box" placeholder="Search for torrent"> </form> <p class="text-muted fs-7">In order to download and seed the torrents, I recommend either using <a href="https://www.qbittorrent.org/">qBitorrent</a> or <a href="https://transmissionbt.com/">transmission</a> softwares</p> </div> 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 </a> <div class="align-items-center d-flex justify-content-between gap-3"> + <a href="/settings" class="link-light"> + <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" viewBox="0 0 16 16"> + <path d="M7.068.727c.243-.97 1.62-.97 1.864 0l.071.286a.96.96 0 0 0 1.622.434l.205-.211c.695-.719 1.888-.03 1.613.931l-.08.284a.96.96 0 0 0 1.187 1.187l.283-.081c.96-.275 1.65.918.931 1.613l-.211.205a.96.96 0 0 0 .434 1.622l.286.071c.97.243.97 1.62 0 1.864l-.286.071a.96.96 0 0 0-.434 1.622l.211.205c.719.695.03 1.888-.931 1.613l-.284-.08a.96.96 0 0 0-1.187 1.187l.081.283c.275.96-.918 1.65-1.613.931l-.205-.211a.96.96 0 0 0-1.622.434l-.071.286c-.243.97-1.62.97-1.864 0l-.071-.286a.96.96 0 0 0-1.622-.434l-.205.211c-.695.719-1.888.03-1.613-.931l.08-.284a.96.96 0 0 0-1.186-1.187l-.284.081c-.96.275-1.65-.918-.931-1.613l.211-.205a.96.96 0 0 0-.434-1.622l-.286-.071c-.97-.243-.97-1.62 0-1.864l.286-.071a.96.96 0 0 0 .434-1.622l-.211-.205c-.719-.695-.03-1.888.931-1.613l.284.08a.96.96 0 0 0 1.187-1.186l-.081-.284c-.275-.96.918-1.65 1.613-.931l.205.211a.96.96 0 0 0 1.622-.434l.071-.286zM12.973 8.5H8.25l-2.834 3.779A4.998 4.998 0 0 0 12.973 8.5zm0-1a4.998 4.998 0 0 0-7.557-3.779l2.834 3.78h4.723zM5.048 3.967c-.03.021-.058.043-.087.065l.087-.065zm-.431.355A4.984 4.984 0 0 0 3.002 8c0 1.455.622 2.765 1.615 3.678L7.375 8 4.617 4.322zm.344 7.646.087.065-.087-.065z"/> + </svg> + </a> + <a href="/info" class="link-light"> <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" viewBox="0 0 16 16"> <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z" /> diff --git a/views/settings.ejs b/views/settings.ejs new file mode 100644 index 0000000..7587da8 --- /dev/null +++ b/views/settings.ejs @@ -0,0 +1,44 @@ +<%- include('partials/header'); %> + +<h1>Settings</h1> + +<div class="text-center mt-5"> + <form action="settings" method="POST"> + <div name="content"> + <h2 class="display-6">Content</h2> + <div class="row justify-content-center"> + <div class="col-sm-4 mt-2"> + <input class="form-check-input" type="checkbox" name="nsfw" <% if (config.settings.nsfw_content) { %> checked="true" <% } %> id="nsfw"> + <label class="form-check-label" for="nsfw">NSFW Content</label> + </div> + </div> + </div> + + <div name="network" class="mt-5"> + <h2 class="display-6">Network</h2> + <div class="row justify-content-center"> + <div class="col-sm-4"> + <label class="form-check-label" for="port">Port Number</label> + <input type="text" class="text-bg-dark form-control w-50 m-auto mt-2 text-center mandatory-text-box" placeholder="3000" name="port" <% if (config.settings.port) { %> value="<%= config.settings.port %>" <% } %> id="port"> + </div> + </div> + </div> + + <div name="network" class="mt-5"> + <div class="row justify-content-center"> + <div class="col-sm-4"> + <button type="submit" class="btn btn-primary"> + <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" + viewBox="0 0 16 16"> + <path + d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z"> + </svg> + Save + </button> + </div> + </div> + </div> + </form> +</div> + +<%- include('partials/footer'); %>
\ No newline at end of file |