Refactor for async operations and add new features

Updated Compression.cs to support asynchronous operations, including new methods for decompressing byte arrays and checking compression status.
Enhanced Logging.cs with asynchronous logging methods.
Refactored Program.cs to replace synchronous calls with asynchronous ones, added a new endpoint for editing pastes, and implemented rate limiting.
Removed outdated logging configuration.
This commit is contained in:
whoswhip
2024-10-19 00:05:59 -04:00
parent c28b9b92b1
commit bdcc3a1dc6
5 changed files with 403 additions and 117 deletions
+125 -42
View File
@@ -11,8 +11,8 @@
<a href="/">home</a>
<a href="/pastes">recent pastes</a>
<a href="/archive">archive</a>
<a href="login" id="login-signup-btn">login</a>
<a href="sign-up" id="login-signup-btn">sign up</a>
<a href="login" id="login-signup-btn" style="display: none;" >login</a>
<a href="sign-up" id="login-signup-btn" style="display: none;">sign up</a>
<a href="dash" id="dash-btn" style="display: none;">dashboard</a>
</div>
<div class="nav-hamburger-menu">
@@ -25,15 +25,15 @@
<a href="/">home</a>
<a href="/pastes">recent pastes</a>
<a href="/archive">archive</a>
<a href="login" id="login-signup-btn">login</a>
<a href="sign-up" id="login-signup-btn">sign up</a>
<a href="login" id="login-signup-btn" style="display: none;" >login</a>
<a href="sign-up" id="login-signup-btn" style="display: none;">sign up</a>
<a href="dash" id="dash-btn" style="display: none;">dashboard</a>
</div>
</div>
<div class="container-wrapper">
<div class="paste-container">
<h1 id="paste-title"></h1>
<h3 class="uploader-text"><a id="uploader-username"></a> <a id="creationdate"></a> <a id="paste-size"></a>
<h3 class="uploader-text"><a id="uploader-username" class="user-a"></a> <a id="creationdate"></a> <a id="paste-size"></a>
</h3>
<div class="paste-wrapper">
<p id="text"></p>
@@ -45,15 +45,14 @@
<button id="copy-button" class="button">copy</button>
<button id="download-button" class="button">download</button>
<button id="delete-button" class="button" style="display: none;">delete</button>
<button id="edit-button" class="button" style="display: none;">edit</button>
</div>
</div>
</div>
<div class="notifcations-wrapper" id="notifications"></div>
<div class="input-wrapper" id="input-wrapper">
<div class="input-wrapper-container">
<h2>
Are you sure you want to delete this paste?
</h2>
<h2 id="input-message"></h2>
<div>
<button id="yes-button" class="button">Yes</button>
<button id="no-button" class="button">No</button>
@@ -63,6 +62,7 @@
</body>
<script>
info = "";
editing = false;
function localizeDateTime(unixTimestamp) {
const date = new Date(unixTimestamp * 1000);
return date.toLocaleString();
@@ -72,21 +72,27 @@
.then(response => response.json())
.then(data => {
if (data.message == "Authenticated") {
if (info.uploader == data.username) {
if (info.uploader == data.username || data.type == "Admin") {
document.getElementById('delete-button').style.display = 'block';
document.getElementById('edit-button').style.display = 'block';
}
document.getElementById('login-signup-btn').style.display = 'none';
document.querySelectorAll("#login-signup-btn").forEach(element => {
element.style.display = 'none';
});
document.getElementById('dash-btn').style.display = 'block';
}else{
document.querySelectorAll("#login-signup-btn").forEach(element => {
element.style.display = 'block';
});
}
});
}
function fetchInfo() {
fetch('/api/pastes/info?id={pasteid}')
.then(response => response.json())
.then(data => {
const json = JSON.parse(data);
.then(json => {
info = json;
document.getElementById('uploader-username').innerText = json.uploader;
document.getElementById('uploader-username').innerText = json.uploader || 'Anonymous';
document.getElementById('creationdate').innerText = localizeDateTime(json.date);
document.getElementById('paste-size').innerText = json.size;
document.getElementById('paste-size').title = `Compressed Size: ${json.compressedSize}`;
@@ -96,21 +102,34 @@
document.getElementById('paste-title').innerText = `Untitled ${json.uid}`;
document.title = `Untitled ${json.uid}`;
}
if (json.uploader.startsWith("Anonymous") && json.uploader.length == 46) {
if (json.uploader && json.uploader.startsWith("Anonymous") && json.uploader.length == 46) {
document.getElementById('uploader-username').innerText = 'Anonymous';
} else {
} else if (json.uploader) {
document.getElementById('uploader-username').href = `/u/${json.uploader}`;
}
});
})
.catch(error => console.error('Error fetching paste info:', error));
}
document.getElementById("edit-button").addEventListener("click", function () {
if (editing) {
createInput('Are you sure you want to save this edit?');
document.getElementById('yes-button').addEventListener("click", finalizeEdit);
} else {
editing = true;
var editButton = document.getElementById('edit-button');
editButton.innerText = 'save';
var text = document.getElementById('text');
var innertext = text.innerText;
text.innerHTML = `<textarea class="textarea" id="edit-textarea" style="width: 100%; height: ${text.clientHeight}px;">${innertext}</textarea>`;
}
});
document.addEventListener("DOMContentLoaded", function () {
fetchInfo();
authenticated();
const creationdateElement = document.getElementById('creationdate');
const creationdateText = creationdateElement.innerText.trim();
const textElement = document.getElementById('text');
fetchInfo();
authenticated();
creationdateElement.innerText = localizeDateTime(parseInt(creationdateText));
textElement.innerText = "Loading...";
fetch(`/raw/{pasteid}`)
@@ -165,28 +184,34 @@
document.body.removeChild(element);
});
document.getElementById("delete-button").addEventListener("click", function () {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'flex';
});
document.getElementById("yes-button").addEventListener("click", function () {
var requestUrl = "/api/pastes/delete";
fetch(requestUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: '{pasteid}'
createInput('Are you sure you want to delete this paste?');
document.getElementById("yes-button").addEventListener("click", function () {
fetch(`/api/pastes/delete?id={pasteid}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: '{pasteid}'
})
})
})
.then(response => response.json())
.then(obj => {
const result = JSON.parse(obj);
if (result.message != null) {
window.location.href = `/`;
}else{
createNotification(`Error: ${result.error}`);
}
.then(response => response.json())
.then(obj => {
const result = JSON.parse(obj);
if (result.message != null) {
createNotification(`Success: ${result.message}`);
setTimeout(() => {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'none';
window.location.href = '/';
}, 2000);
} else {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'none';
createNotification(`Error: ${result.error}`);
}
});
});
});
document.getElementById("no-button").addEventListener("click", function () {
@@ -219,7 +244,45 @@
notificationDiv.remove();
}, 4900);
}
function createInput(message) {
const inputWrapper = document.getElementById('input-wrapper');
const inputMessage = document.getElementById('input-message');
inputMessage.innerText = message;
inputWrapper.style.display = 'flex';
}
function finalizeEdit() {
var textarea = document.getElementById('edit-textarea');
var text = textarea.value;
fetch(`/api/pastes/edit`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: '{pasteid}',
text: text
})
})
.then(response => response.json())
.then(obj => {
const result = JSON.parse(obj);
if (result.message != null) {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'none';
document.getElementById('edit-button').innerText = "edit";
createNotification(`Success: ${result.message}`);
document.getElementById('text').innerText = text;
document.getElementById('edit-textarea').remove();
editing = false;
} else {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'none';
document.getElementById('edit-button').innerText = "edit";
createNotification(`Error: ${result.error}`);
}
});
}
</script>
@@ -227,6 +290,19 @@
<style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&display=swap');
.textarea{
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
font-weight: 500;
max-width: 100%;
min-width: 100%;
background-color: #1d1d1d;
color: white;
border: none;
}
.textarea:focus{
outline: none;
}
.input-wrapper{
z-index: 100;
display: none;
@@ -343,11 +419,12 @@
font-weight: 600;
}
.user-a {
.user-a:any-link {
color: rgb(190, 190, 190);
text-decoration: none;
text-decoration: underline;
}
.nav-div {
top: 0;
@@ -366,6 +443,7 @@
height: fit-content;
overflow-y: auto;
border: #222222 2px solid;
z-index: 999999;
}
.nav-hamburger-menu {
@@ -500,5 +578,10 @@
margin-bottom: 10px;
justify-content: center;
align-items: center;
z-index: 102;
position: absolute;
}
.notifcations-wrapper{
z-index: 101;
}
</style>
+85 -23
View File
@@ -12,8 +12,8 @@
<a href="/">home</a>
<a href="/pastes">recent pastes</a>
<a href="/archive">archive</a>
<a href="login" id="login-signup-btn">login</a>
<a href="sign-up" id="login-signup-btn">sign up</a>
<a href="login" id="login-signup-btn" style="display: none;" >login</a>
<a href="sign-up" id="login-signup-btn" style="display: none;">sign up</a>
<a href="dash" id="dash-btn" style="display: none;">dashboard</a>
</div>
<div class="nav-hamburger-menu">
@@ -26,15 +26,15 @@
<a href="/">home</a>
<a href="/pastes">recent pastes</a>
<a href="/archive">archive</a>
<a href="login" id="login-signup-btn">login</a>
<a href="sign-up" id="login-signup-btn">sign up</a>
<a href="login" id="login-signup-btn" style="display: none;" >login</a>
<a href="sign-up" id="login-signup-btn" style="display: none;">sign up</a>
<a href="dash" id="dash-btn" style="display: none;">dashboard</a>
</div>
</div>
<div class="container-wrapper">
<div class="paste-container">
<h1 id="paste-title" title="{pastetitleattribute}">{pastetitle}</h1>
<h3 class="uploader-text"><a id="uploader-username"></a> <a id="creationdate"></a> <a id="paste-size"></a>
<h3 class="uploader-text"><a id="uploader-username" class="user-a"></a> <a id="creationdate"></a> <a id="paste-size"></a>
</h3>
<div class="paste-wrapper">
<p id="text"></p>
@@ -46,6 +46,7 @@
<button id="copy-button" class="button">copy</button>
<button id="download-button" class="button">download</button>
<button id="delete-button" class="button" style="display: none;">delete</button>
<button id="edit-button" class="button" style="display: none;">edit</button>
</div>
</div>
<div class="passwordDiv">
@@ -62,9 +63,7 @@
</div>
<div class="input-wrapper" id="input-wrapper">
<div class="input-wrapper-container">
<h2>
Are you sure you want to delete this paste?
</h2>
<h2 id="input-message"></h2>
<div>
<button id="yes-button" class="button">Yes</button>
<button id="no-button" class="button">No</button>
@@ -73,19 +72,25 @@
</div>
</body>
<script>
info = "";
editing = false;
function authenticated() {
fetch('/api/accounts/authenticated')
.then(response => response.json())
.then(data => {
if (data.message == "Authenticated") {
if (info.uploader == data.username) {
if (info.uploader == data.username || data.type == "Admin") {
document.getElementById('delete-button').style.display = 'block';
//document.getElementById('edit-button').style.display = 'block';
}
document.getElementById('login-signup-btn').style.display = 'none';
document.querySelectorAll("#login-signup-btn").forEach(element => {
element.style.display = 'none';
});
document.getElementById('dash-btn').style.display = 'block';
}else{
document.querySelectorAll("#login-signup-btn").forEach(element => {
element.style.display = 'block';
});
}
});
}
@@ -112,7 +117,21 @@
}
});
}
document.getElementById("edit-button").addEventListener("click", function () {
if (editing) {
createInput('Are you sure you want to save this edit?');
document.getElementById('yes-button').removeEventListener("click", deletePaste);
document.getElementById('yes-button').addEventListener("click", finalizeEdit);
document.getElementById('yes-button').removeEventListener("click", finalizeEdit);
} else {
editing = true;
var editButton = document.getElementById('edit-button');
editButton.innerText = 'save';
var text = document.getElementById('text');
var innertext = text.innerText;
text.innerHTML = `<textarea class="textarea" id="edit-textarea" style="width: 100%; height: ${text.clientHeight}px;">${innertext}</textarea>`;
}
});
document.getElementById("submitPassword").addEventListener("click", function () {
var password = document.getElementById("password").value;
fetch(`/api/pastes/password/{pasteid}`, {
@@ -145,11 +164,11 @@
}
document.addEventListener("DOMContentLoaded", function () {
const creationdateElement = document.getElementById('creationdate');
const creationdateText = creationdateElement.innerText.trim();
fetchInfo();
authenticated();
const creationdateElement = document.getElementById('creationdate');
const creationdateText = creationdateElement.innerText.trim();
creationdateElement.innerText = localizeDateTime(parseInt(creationdateText));
var mobile = window.mobileCheck();
if (mobile) {
@@ -183,12 +202,13 @@
document.body.removeChild(element);
});
document.getElementById("delete-button").addEventListener("click", function () {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'flex';
createInput('Are you sure you want to delete this paste?');
document.getElementById('yes-button').removeEventListener("click", finalizeEdit);
document.getElementById('yes-button').addEventListener("click", deletePaste);
document.getElementById('yes-button').removeEventListener("click", deletePaste);
});
document.getElementById("yes-button").addEventListener("click", function () {
var requestUrl = "/api/pastes/delete";
fetch(requestUrl, {
function deletePaste() {
fetch(`/api/pastes/delete?id={pasteid}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -201,15 +221,47 @@
.then(obj => {
const result = JSON.parse(obj);
if (result.message != null) {
window.location.href = `/`;
createNotification(`Success: ${result.message}`);
setTimeout(() => {
window.location.href = '/';
}, 2000);
} else {
createNotification(`Error: ${result.error}`);
}
});
});
}
function finalizeEdit() {
var textarea = document.getElementById('edit-textarea');
var text = textarea.value;
fetch(`/api/pastes/edit`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: '{pasteid}',
text: text
})
})
.then(response => response.json())
.then(obj => {
const result = JSON.parse(obj);
if (result.message != null) {
createNotification(`Success: ${result.message}`);
document.getElementById('text').innerText = text;
document.getElementById('edit-textarea').remove();
editing = false;
} else {
createNotification(`Error: ${result.error}`);
}
});
}
document.getElementById("no-button").addEventListener("click", function () {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'none';
document.getElementById('yes-button').removeEventListener("click", finalizeEdit);
document.getElementById('yes-button').removeEventListener("click", deletePaste);
});
function notification(message) {
const notification = document.getElementById('notification');
@@ -228,7 +280,12 @@
}, 4900);
}
function createInput(message) {
const inputWrapper = document.getElementById('input-wrapper');
const inputMessage = document.getElementById('input-message');
inputMessage.innerText = message;
inputWrapper.style.display = 'flex';
}
</script>
@@ -441,6 +498,7 @@
height: fit-content;
overflow-y: auto;
border: #222222 2px solid;
z-index: 999999;
}
.nav-hamburger-menu {
@@ -576,6 +634,10 @@
margin-bottom: 10px;
justify-content: center;
align-items: center;
z-index: 102;
position: absolute;
}
.notifcations-wrapper{
z-index: 101;
}
</style>