diff --git a/config.ini b/config.ini index 1d0ce4c..7d420cd 100644 --- a/config.ini +++ b/config.ini @@ -1,4 +1,5 @@ [config] HOST = 0.0.0.0 PORT = 8080 -SECRET_KEY = placeholder \ No newline at end of file +SECRET_KEY = placeholder +MAX_STORAGE = 25000000 \ No newline at end of file diff --git a/main b/main index 83ba212..4208243 100644 --- a/main +++ b/main @@ -14,6 +14,7 @@ config.read("config.ini") HOST = config["config"]["HOST"] PORT = config["config"]["PORT"] SECRET_KEY = config["config"]["SECRET_KEY"] +MAX_STORAGE = config["config"]["MAX_STORAGE"] if SECRET_KEY == "placeholder": print("[WARNING] Secret key not set") @@ -46,6 +47,15 @@ def get_note(id): return "error" return post +def get_space(id): + conn = get_db_connection() + notes = conn.execute("SELECT content FROM notes WHERE creator = ? ORDER BY id DESC;", (id,)).fetchall() + conn.close() + spacetaken = 0 + for x in notes: + spacetaken = spacetaken + len(x["content"].encode("utf-8")) + return spacetaken + def get_session(id): conn = get_db_connection() post = conn.execute("SELECT * FROM sessions WHERE session = ?", @@ -171,7 +181,9 @@ def apiuserinfo(): datatemplate = { "username": user["username"], "id": user["id"], - "created": user["created"] + "created": user["created"], + "storageused": get_space(user["id"]), + "storagemax": int(MAX_STORAGE) } return datatemplate @@ -253,6 +265,9 @@ def apieditnote(): user = get_user(userCookie["id"]) note = get_note(noteId) + + if get_space(user["id"]) + len(content.encode("utf-8")) > int(MAX_STORAGE): + return {}, 418 if (note != "error"): if (user["id"] == note["creator"]): diff --git a/removeuser b/removeuser new file mode 100644 index 0000000..73e6d7c --- /dev/null +++ b/removeuser @@ -0,0 +1,31 @@ +#!/usr/bin/python3 +import sqlite3 +import sys + +print("type n to cancel") +answer = input("delete user, what is user id?") + +if (answer == "n"): + sys.exit() + +def get_db_connection(): + conn = sqlite3.connect("database.db") + conn.row_factory = sqlite3.Row + return conn + + +print("deleting notes") + +conn = get_db_connection() +notes = conn.execute("DELETE FROM notes WHERE creator = ?", (int(answer),)) +conn.commit() +conn.close() + +print("deleting account") + +conn = get_db_connection() +conn.execute("DELETE FROM users WHERE id = ?", (int(answer),)) +conn.commit() +conn.close() + +print("success") \ No newline at end of file diff --git a/static/css/style.css b/static/css/style.css index 35fd480..63ab466 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -104,6 +104,43 @@ body { font-family: "Inter", sans-serif; } +.optionsCoverDiv { + position: fixed; + width: 100%; + height: 100%; + z-index: 2; + background-color: rgba(0, 0, 0, 0.7); + ; +} + +.optionsDiv { + position: fixed; + left: 12.5%; + top: 12.5%; + width: 75%; + height: 75%; + background-color: white; + padding: 10px; + color: black; + border-radius: 8px; +} + +.optionsDiv button { + padding: 10px; + padding-left: 15px; + padding-right: 15px; + color: white; + border: none; + text-decoration: none; + background-color: #157efb; + border-radius: 8px; +} + +.optionsDiv .exit { + background-color: #e9e9e9; + color: black; +} + /* Sign up/log in div */ .inoutdiv { @@ -148,6 +185,7 @@ body { .mainDiv { text-align: center; } + .mainDiv a { padding: 10px; padding-left: 15px; @@ -157,6 +195,7 @@ body { background-color: #157efb; border-radius: 8px; } + .mainDiv .feature { width: 80%; margin-left: 10%; @@ -165,12 +204,15 @@ body { border-radius: 8px; font-size: 17px; } + .mainDiv .green { background-color: #ebffeb; } + .mainDiv .yellow { background-color: #ffffeb; } + .mainDiv img { margin: 10px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); diff --git a/static/js/login.js b/static/js/login.js index 8a742d7..02557e5 100644 --- a/static/js/login.js +++ b/static/js/login.js @@ -30,6 +30,15 @@ signupButton.addEventListener("click", (event) => { let username = usernameBox.value let password = passwordBox.value + if (username == "") { + statusBox.innerText = "username required" + return + } + if (password == "") { + statusBox.innerText = "password required" + return + } + showElements(false) statusBox.innerText = "welcome back!" diff --git a/static/js/main.js b/static/js/main.js index 487713a..f230659 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -7,10 +7,17 @@ if (localStorage.getItem("DONOTSHARE-password") === null) { throw new Error(); } +function formatBytes(a, b = 2) { if (!+a) return "0 Bytes"; const c = 0 > b ? 0 : b, d = Math.floor(Math.log(a) / Math.log(1024)); return `${parseFloat((a / Math.pow(1024, d)).toFixed(c))} ${["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"][d]}` } + let secretkey = localStorage.getItem("DONOTSHARE-secretkey") let password = localStorage.getItem("DONOTSHARE-password") let usernameBox = document.getElementById("usernameBox") +let optionsCoverDiv = document.getElementById("optionsCoverDiv") +let exitThing = document.getElementById("exitThing") +let storageThing = document.getElementById("storageThing") +let usernameThing = document.getElementById("usernameThing") +let logOutButton = document.getElementById("logOutButton") let notesBar = document.getElementById("notesBar") let notesDiv = document.getElementById("notesDiv") let newNote = document.getElementById("newNote") @@ -85,8 +92,16 @@ fetch("/api/userinfo", { let responseData = await response.json() usernameBox.innerText = responseData["username"] usernameBox.addEventListener("click", (event) => { + optionsCoverDiv.classList.remove("hidden") + usernameThing.innerText = "logged in as " + responseData["username"] + storageThing.innerText = "you've used " + formatBytes(responseData["storageused"]) + " out of " + formatBytes(responseData["storagemax"]) + }); + logOutButton.addEventListener("click", (event) => { window.location.href = "/api/logout" }); + exitThing.addEventListener("click", (event) => { + optionsCoverDiv.classList.add("hidden") + }); } doStuff() }); @@ -139,6 +154,12 @@ function selectNote(nameithink) { "Content-type": "application/json; charset=UTF-8" } }) + .then((response) => response) + .then((response) => { + if (response.status == 418) { + alert("you've ran out of storage :3 changes will not be saved until you free up storage!!! owo") + } + }) } }, waitTime); }); diff --git a/templates/app.html b/templates/app.html index b1bab9f..b36ec36 100644 --- a/templates/app.html +++ b/templates/app.html @@ -23,6 +23,17 @@
+ + +