Skip to content

Commit

Permalink
add ability for admin to control localstorage toggle and imgur client id
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebe-leong committed Sep 26, 2024
1 parent 5ec005a commit ce94424
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

10 changes: 10 additions & 0 deletions frontend/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ <h3 class="montserrat-subheader">Admin Page</h3><br>
<label class="montserrat-regular">Enter post ids to unpin, separated by commas</label><br>
<input type="text" id="pinRemove"><br><br>

<label class="montserrat-regular">Enter Imgur Client Id. Changes will come into effect after server restart.</label><br>
<input type="text" id="imgurClient"><br><br>

<label id="localStorageLabel" class="montserrat-regular">Local Image Storage is <strong>ON</strong>. Changes will come into effect after server restart.</label><br>
<input type="radio" id="storageOn" name="localstorage" value="ON">
<label class="montserrat-regular">ON</label><br>

<input type="radio" id="storageOff" name="localstorage" value="OFF">
<label class="montserrat-regular">OFF</label><br><br>

<button onclick="nuke()">Delete all posts</button>
<button onclick="applyChanges()">Apply Changes</button>

Expand Down
34 changes: 34 additions & 0 deletions frontend/public/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down Expand Up @@ -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()
}

Expand All @@ -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 <strong>${localStorageOn}</strong>. Changes will come into effect after server restart.`
if (localStorageOn == "ON") {
document.getElementById("storageOn").checked = true
} else {
document.getElementById("storageOff").checked = true
}
}
2 changes: 1 addition & 1 deletion frontend/public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ dialog:focus-visible {
width: 12.33em; /* 165px */
}

#pinAdd, #pinRemove {
#pinAdd, #pinRemove, #imgurClient {
width: 16.88em; /* 225px */
}

Expand Down
47 changes: 43 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions serverconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"localImageStorage":true,"imgurClientId":""}
2 changes: 1 addition & 1 deletion start
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node --env-file=.env server.js
node server.js

0 comments on commit ce94424

Please sign in to comment.