diff --git a/.env b/.env deleted file mode 100644 index bc4cecb..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -CLIENT_ID=8b367dbb12ff791 -LOCALSTORAGEON=false \ No newline at end of file diff --git a/frontend/admin.html b/frontend/admin.html index d34acd8..100a581 100644 --- a/frontend/admin.html +++ b/frontend/admin.html @@ -37,6 +37,16 @@

Admin Page





+
+

+ +
+ +
+ + +

+ diff --git a/frontend/public/admin.js b/frontend/public/admin.js index 7842a17..a37c40c 100644 --- a/frontend/public/admin.js +++ b/frontend/public/admin.js @@ -2,6 +2,10 @@ async function applyChanges() { const subheaderInput = document.getElementById("subheaderInput").value const pinAdd = document.getElementById("pinAdd").value const pinRemove = document.getElementById("pinRemove").value + const imgurClient = document.getElementById("imgurClient").value + + const storageOn = document.getElementById("storageOn").checked + const storageOff = document.getElementById("storageOff").checked if (pinAdd != "") { await fetch("/data/pinned.json", { @@ -40,6 +44,25 @@ async function applyChanges() { }) }) } + + if (imgurClient != "" || storageOn || storageOff) { + let localStorageOn = undefined + if (storageOn && !storageOff) { localStorageOn = true } + else { localStorageOn = false } + + await fetch("/data/serverconfig.json", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + imgurClientId: imgurClient, + localImageStorage: localStorageOn + }) + }) + } + + alert("Changes saved") location.reload() } @@ -56,4 +79,15 @@ window.onload = async () => { const ip = await fetch("/data/ip.txt") .then((data) => data.text()) document.getElementById("serverIP").innerHTML += ip + + const config = await fetch("/data/serverconfig.json") + .then((data) => data.json()) + const localStorageOn = (config.localImageStorage) ? "ON" : "OFF" + + document.getElementById("localStorageLabel").innerHTML = `Local Image Storage is ${localStorageOn}. Changes will come into effect after server restart.` + if (localStorageOn == "ON") { + document.getElementById("storageOn").checked = true + } else { + document.getElementById("storageOff").checked = true + } } \ No newline at end of file diff --git a/frontend/public/style.css b/frontend/public/style.css index c26b408..6958de0 100644 --- a/frontend/public/style.css +++ b/frontend/public/style.css @@ -284,7 +284,7 @@ dialog:focus-visible { width: 12.33em; /* 165px */ } -#pinAdd, #pinRemove { +#pinAdd, #pinRemove, #imgurClient { width: 16.88em; /* 225px */ } diff --git a/server.js b/server.js index 4dc097a..da49a4e 100644 --- a/server.js +++ b/server.js @@ -5,15 +5,15 @@ const fs = require("fs") const { ImgurClient } = require("imgur") const ip = require("ip") -const app = express() -const imgur = new ImgurClient({clientId: process.env.CLIENT_ID}) - const PORT = 8000 const CHARACTERLIMIT = 650 const FEEDLIMIT = 30 const MAXPINS = 5 const WINCHECKFALSE = (process.platform != "win32") -const LOCALSTORAGEON = (process.env.LOCALSTORAGEON != undefined) ? stringToBool(process.env.LOCALSTORAGEON) : false +const LOCALSTORAGEON = (accessServerConfig("localImageStorage") != undefined) ? accessServerConfig("localImageStorage") : false + +const app = express() +const imgur = new ImgurClient({clientId: accessServerConfig("imgurClientId")}) const Ids = [] @@ -35,6 +35,16 @@ const multerstorage = multer.diskStorage({ }) const upload = multer({storage: multerstorage}) +function accessServerConfig(value) { + const file = jsonfile.readFileSync((WINCHECKFALSE) ? `${__dirname}/serverconfig.json` : `${__dirname}\\serverconfig.json`) + if (value == "imgurClientId") { + return file.imgurClientId + } else if (value == "localImageStorage") { + return file.localImageStorage + } + return null +} + function itemToIndex(item, array) { for (let i = 0; i < array.length; i++) { if ( array[i] == item) { @@ -193,6 +203,14 @@ app.get("/data/pinned.json", (req, res) => { } }) +app.get("/data/serverconfig.json", (req, res) => { + if (req.ip === "::1") { + res.sendFile((WINCHECKFALSE) ? `${__dirname}/serverconfig.json` : `${__dirname}\\serverconfig.json`) + } else { + res.status(401).send("Unathorised access: user is not admin") + } +}) + app.get("/data/:id.json", (req, res) => { if (!isIdValid(req.params.id)) { res.status(400).send("Invalid post id") @@ -307,6 +325,27 @@ app.post("/data/views.json", (req, res) => { res.send() }) +app.post("/data/serverconfig.json", (req, res) => { + if (req.ip === "::1") { + if (req.body.localImageStorage == null && req.body.imgurClientId == null) { + res.status(400).send("Missing required items") + return + } + + const file = jsonfile.readFileSync((WINCHECKFALSE) ? `${__dirname}/serverconfig.json` : `${__dirname}\\serverconfig.json`) + if (req.body.localImageStorage != null && req.body.localImageStorage != undefined) { + file.localImageStorage = req.body.localImageStorage + } + if (req.body.imgurClientId != null) { + file.imgurClientId = req.body.imgurClientId + } + jsonfile.writeFileSync((WINCHECKFALSE) ? `${__dirname}/serverconfig.json` : `${__dirname}\\serverconfig.json`, file) + res.send() + } else { + res.status(401).send("Unathorised access: user is not admin") + } +}) + app.post("/data/messages.json", upload.single("mediaFile"), async (req, res) => { const imageDir = (!LOCALSTORAGEON) ? "tempimages" : "images" diff --git a/serverconfig.json b/serverconfig.json new file mode 100644 index 0000000..2621e71 --- /dev/null +++ b/serverconfig.json @@ -0,0 +1 @@ +{"localImageStorage":true,"imgurClientId":""} diff --git a/start b/start index c152c26..9c4dee9 100755 --- a/start +++ b/start @@ -1 +1 @@ -node --env-file=.env server.js \ No newline at end of file +node server.js \ No newline at end of file