New page, decreased default maxfilesizem, add max title length.

This commit is contained in:
whoswhip
2025-01-20 00:20:41 -05:00
parent e0a2f20546
commit 94fb936b32
9 changed files with 330 additions and 58 deletions
+5
View File
@@ -28,3 +28,8 @@ README.md
!.git/config !.git/config
!.git/packed-refs !.git/packed-refs
!.git/refs/heads/** !.git/refs/heads/**
SharpbinV2.Server/*.db
SharpbinV2.Server/logs
SharpbinV2.Server/pastes
SharpbinV2.Server/.env
+3 -51
View File
@@ -70,29 +70,7 @@ artifacts/
# ASP.NET Scaffolding # ASP.NET Scaffolding
ScaffoldingReadMe.txt ScaffoldingReadMe.txt
# StyleCop # StyleCop6
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj *_wpftmp.csproj
*.log *.log
*.vspscc *.vspscc
@@ -204,22 +182,7 @@ PublishScripts/
**/[Pp]ackages/* **/[Pp]ackages/*
# except build/, which is used as an MSBuild target. # except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/ !**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed # Uncomment if necessar6
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/ BundleArtifacts/
Package.StoreAssociation.xml Package.StoreAssociation.xml
_pkginfo.txt _pkginfo.txt
@@ -320,18 +283,7 @@ paket-files/
__pycache__/ __pycache__/
*.pyc *.pyc
# Cake - Uncomment if you are using it # Cake - Uncomment i6
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs *.btm.cs
*.odx.cs *.odx.cs
*.xsd.cs *.xsd.cs
+12
View File
@@ -0,0 +1,12 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
]
}
+90 -3
View File
@@ -16,7 +16,7 @@ namespace SharpbinV2.Server
class Program class Program
{ {
public static string MainDatabaseConnection = "Data Source=data.db"; public static string MainDatabaseConnection = "Data Source=data.db";
public static long MaxFileSize = 5000000; // 5MB public static long MaxFileSize = 1_000_000; // 1MB
public static string[] ValidSyntaxLanguages = public static string[] ValidSyntaxLanguages =
{ {
"none", "none",
@@ -104,7 +104,9 @@ namespace SharpbinV2.Server
#region Static Endpoints #region Static Endpoints
app.MapGet("/", (HttpContext context) => app.MapGet("/", (HttpContext context) =>
{ {
context.Response.Redirect("/index.html"); context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "text/html");
return context.Response.SendFileAsync("wwwroot/index.html");
}); });
app.MapGet("/{pasteid}", async (HttpContext context, IWebHostEnvironment env) => app.MapGet("/{pasteid}", async (HttpContext context, IWebHostEnvironment env) =>
{ {
@@ -185,6 +187,10 @@ namespace SharpbinV2.Server
context.Response.Headers.Add("Content-Type", "text/html"); context.Response.Headers.Add("Content-Type", "text/html");
await context.Response.SendFileAsync("wwwroot/error.html"); await context.Response.SendFileAsync("wwwroot/error.html");
}); });
app.MapGet("/error.html", (HttpContext context) =>
{
context.Response.Redirect("/error");
});
app.MapGet("/raw/{pasteid}", async (HttpContext context) => app.MapGet("/raw/{pasteid}", async (HttpContext context) =>
{ {
var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null; var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
@@ -217,6 +223,12 @@ namespace SharpbinV2.Server
{ {
context.Response.Redirect("/"); context.Response.Redirect("/");
}); });
app.MapGet("/archive", async (HttpContext context) =>
{
context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "text/html");
await context.Response.SendFileAsync("wwwroot/archive.html");
});
#endregion #endregion
#region API #region API
@@ -458,6 +470,13 @@ namespace SharpbinV2.Server
var user = await Database.UserFromToken(requestdetails.Token); var user = await Database.UserFromToken(requestdetails.Token);
var queries = context.Request.Query; var queries = context.Request.Query;
if (queries.ContainsKey("title") && queries["title"].ToString().Length > 500)
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "Title too long." });
return;
}
paste.Created = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); paste.Created = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
paste.Size = body.Length; paste.Size = body.Length;
@@ -467,7 +486,7 @@ namespace SharpbinV2.Server
paste.AuthorUUID = user?.UUID ?? "0"; // 0 means the user is anonymous paste.AuthorUUID = user?.UUID ?? "0"; // 0 means the user is anonymous
paste.FilePath = $"pastes/{paste.ID}.txt"; paste.FilePath = $"pastes/{paste.ID}.txt";
paste.Visibility = queries.ContainsKey("visibility") ? Convert.ToInt32(queries["visibility"]) : 0; paste.Visibility = queries.ContainsKey("visibility") ? Convert.ToInt32(queries["visibility"]) : 0;
paste.Title = queries.ContainsKey("title") ? queries["title"].ToString() : $"Untitled-{await Database.EnumeratePastes()}"; paste.Title = queries.ContainsKey("title") && !string.IsNullOrEmpty(queries["title"].ToString()) ? queries["title"].ToString() : $"Untitled-{await Database.EnumeratePastes()}";
paste.Edited = 0; paste.Edited = 0;
paste.Views = 0; paste.Views = 0;
paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null; paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null;
@@ -573,6 +592,74 @@ namespace SharpbinV2.Server
} }
return; return;
}); });
app.MapGet("/api/pastes/archive", async (HttpContext context) =>
{
var queries = context.Request.Query;
if (!queries.ContainsKey("page") && !queries.ContainsKey("limit"))
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "Missing required fields." });
return;
}
var page = queries.ContainsKey("page") ? Convert.ToInt32(queries["page"]) : 0;
var limit = queries.ContainsKey("limit") ? Convert.ToInt32(queries["limit"]) : 10;
int pages = await Database.EnumeratePastes() / limit;
if (page < 0 || limit <= 0 || limit > 25)
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid page or limit." });
return;
}
if (page > pages)
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "Page out of range.", pages });
return;
}
using (var connection = new SqliteConnection(MainDatabaseConnection))
{
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM pastes WHERE Visibility NOT IN (1, 2) ORDER BY Created DESC LIMIT @Limit OFFSET @Offset;";
command.Parameters.AddWithValue("@Limit", limit);
command.Parameters.AddWithValue("@Offset", page * limit);
using (var reader = await command.ExecuteReaderAsync())
{
if (!reader.HasRows)
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "No pastes found." });
return;
}
var pastes = new List<Paste>();
while (await reader.ReadAsync())
{
pastes.Add(new Paste
{
UUID = reader.GetString(1),
ID = reader.GetString(2),
Visibility = reader.GetInt32(3),
Title = reader.IsDBNull(4) ? null : reader.GetString(4),
AuthorUUID = reader.GetString(5),
Created = reader.GetInt64(7),
Edited = reader.GetInt64(8),
Size = reader.GetInt32(9),
TrueSize = reader.GetInt32(10),
Views = reader.GetInt32(11),
Syntax = reader.IsDBNull(12) ? null : reader.GetString(12)
});
}
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { success = true, pastes, pages });
return;
}
}
}
});
#endregion #endregion
+44
View File
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paste Archives - Sharpbin</title>
<link rel="preload" href="css/style.css" as="style">
<link rel="preload" href="css/archive.css" as="style">
<link rel="preload" href="js/archive.js" as="script">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/archive.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=Noto+Sans+Mono:wght@100..900&display=swap"
rel="stylesheet">
</head>
<body>
<div class="main-container">
<h1 id="archive-header">Paste Archives</h1>
<div class="archive-information">
<p id="archive-title">Title</p>
<p id="archive-date">Date</p>
<p id="archive-syntax">Syntax</p>
</div>
<ol class="archive-list" id="archives">
<!-- <li>
<a href="paste.html?id=1">
<p id="archive-title">Title</p>
<p id="archive-date">Date</p>
<p id="archive-syntax">Syntax</p>
</a>
</li> -->
</ol>
<div class="paste-actions">
<button id="previous-button" onclick="previousPage()">&lt;</button>
<p id="paste-pages">0/0</p>
<button id="next-button" onclick="nextPage()">></button>
</div>
</div>
</body>
<script src="js/archive.js"></script>
<script src="js/navbar.js"></script>
</html>
+62
View File
@@ -0,0 +1,62 @@
.archive-list {
list-style-type: none;
margin: 0;
padding: 0;
width: 100%;
max-height: 100%;
overflow-y: scroll;
}
.archive-list li {
margin: 5px 0 5px 0;
padding: 0;
width: 100%;
}
.archive-list li a {
display: block;
padding: 1rem;
background-color: var(--neutral-800);
color: var(--neutral-50);
text-decoration: none;
transition: background-color 0.3s;
border-radius: 5px;
border: 1px solid var(--neutral-700);
width: 100%;
display: flex;
justify-content: space-between;
}
#archive-title {
min-width: 50%;
max-width: 50%;
}
.archive-information {
padding: 0 2px 0 2px;
width: 100%;
display: flex;
justify-content: space-between;
}
.main-container {
max-height: 80% !important;
}
.paste-actions {
width: 100%;
margin-top: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
button {
padding: 10px 40px 10px 40px;
margin: 5px 0;
border-radius: 5px;
border: 1px solid var(--neutral-800);
background-color: var(--neutral-800);
color: var(--neutral-50);
cursor: pointer;
transition: background-color 0.3s;
font-size: 1rem;
}
button:hover {
background-color: var(--neutral-700);
}
+111
View File
@@ -0,0 +1,111 @@
pages = 0;
document.addEventListener('DOMContentLoaded', async () => {
var queries = new URLSearchParams(window.location.search);
var page = queries.get("page");
var limit = queries.get("limit");
if (parseInt(page).isNaN) {
page = 0;
}
var archiveList = document.getElementById("archives");
var archiveListHTML = "";
var pagesElement = document.getElementById("paste-pages");
if (page == null || page < 0) {
page = 0;
}
if (limit == null || limit < 1 || limit > 25) {
limit = 25;
}
var archiveData = await getPastes(page, limit);
var pastes = archiveData.pastes;
pages = archiveData.pages;
pagesElement.innerText = `${parseInt(page) + 1}/${pages + 1}`;
pastes.forEach(paste => {
var title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
archiveListHTML += `
<li>
<a href="/${paste.id}">
<p id="archive-title" title="${paste.title}">${title}</p>
<p id="archive-date">${formatDate(paste.created)}</p>
<p id="archive-syntax">${paste.syntax}</p>
</a>
</li>
`;
});
archiveList.innerHTML = archiveListHTML;
});
async function getPastes(page, limit) {
const response = await fetch(`/api/pastes/archive?page=${page}&limit=${limit}`);
const data = await response.json();
return data;
}
function formatDate(unix) {
if (typeof unix !== 'number' || isNaN(unix)) {
return 'Invalid date';
}
var nowunix = Math.floor(Date.now() / 1000);
var diff = nowunix - unix;
if (diff < 60) {
return `${diff} second${diff !== 1 ? 's' : ''} ago`;
} else if (diff < 3600) {
const minutes = Math.floor(diff / 60);
return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`;
} else if (diff < 86400) {
const hours = Math.floor(diff / 3600);
return `${hours} hour${hours !== 1 ? 's' : ''} ago`;
} else {
const days = Math.floor(diff / 86400);
return `${days} day${days !== 1 ? 's' : ''} ago`;
}
}
function nextPage() {
var queries = new URLSearchParams(window.location.search);
var page = queries.get("page");
var limit = queries.get("limit");
if (page == null || page < 0) {
page = 0;
}
if (limit == null || limit < 1 || limit > 25) {
limit = 25;
}
if (page + 1 > pages) {
return;
}
page++;
window.location.href = `/archive?page=${page}&limit=${limit}`;
}
function previousPage() {
var queries = new URLSearchParams(window.location.search);
var page = queries.get("page");
var limit = queries.get("limit");
if (page == null || page < 0) {
page = 0;
}
if (limit == null || limit < 1 || limit > 25) {
limit = 25;
}
if (page < pages) {
return;
}
page--;
window.location.href = `/archive?page=${page}&limit=${limit}`;
}
-1
View File
@@ -15,7 +15,6 @@ document.getElementById("create-form").addEventListener("submit", async function
if (visibility === "2" && password !== "" && password.length >= 6) { if (visibility === "2" && password !== "" && password.length >= 6) {
content = await encryptAES(content, password); content = await encryptAES(content, password);
console.log(content);
} }
else if (visibility === "2") { else if (visibility === "2") {
alert("Password must be at least 6 characters long"); alert("Password must be at least 6 characters long");
+2 -2
View File
@@ -2,8 +2,8 @@ const navbarHTML = `
<div class="nav-bar"> <div class="nav-bar">
<button class="hamburger" onclick="toggleMenu()">☰</button> <button class="hamburger" onclick="toggleMenu()">☰</button>
<div class="nav-links"> <div class="nav-links">
<a href="index.html" class="nav-link">Home</a> <a href="/" class="nav-link">Home</a>
<a href="archive.html" class="nav-link">Paste Archive</a> <a href="/archive" class="nav-link">Paste Archive</a>
</div> </div>
<style> <style>
.nav-bar {display: flex;justify-content: space-between;align-items: center;padding: 1rem 2rem;background-color: var(--neutral-900);color: #fff;position: fixed;width: 100%;top: 0;left: 0;right: 0;}.nav-link {color: #fff;text-decoration: none;font-size: 1.2rem;transition: color 0.3s;}.nav-link:hover {color: var(--neutral-300);}.nav-links .nav-link {width: auto;padding: 0 1rem;border-bottom: none;}.nav-links .nav-link:last-child {border-bottom: none;}.hamburger {display: none;background: none;border: none;font-size: 2rem;cursor: pointer;margin: 0 auto;}.nav-links {display: flex;flex-direction: row;position: static;background-color: transparent;padding: 0;max-height: none;transition: none;}.nav-links.show {display: flex;flex-direction: column;position: absolute;top: 100%;left: 0;right: 0;background-color: var(--neutral-900);padding: 1rem 2rem;max-height: 500px;transition: max-height 0.3s ease-out;overflow: hidden;}@media (max-width: 800px) {.nav-links {display: none;flex-direction: column;width: 100%;position: absolute;top: 100%;left: 0;right: 0;border-top: 1px solid var(--neutral-800);background-color: var(--neutral-900);padding: 1rem 2rem;max-height: 0;transition: max-height 0.3s ease-out;overflow: hidden;}.nav-link{width: 100% !important;padding: 1rem 0 !important;text-align: center;border-bottom: 1px solid var(--neutral-800) !important;}.nav-links.show {display: flex;max-height: 500px;flex-direction: column;align-items: center;}.hamburger {display: block;margin: 0 auto;}.nav-bar {justify-content: center;}} .nav-bar {display: flex;justify-content: space-between;align-items: center;padding: 1rem 2rem;background-color: var(--neutral-900);color: #fff;position: fixed;width: 100%;top: 0;left: 0;right: 0;}.nav-link {color: #fff;text-decoration: none;font-size: 1.2rem;transition: color 0.3s;}.nav-link:hover {color: var(--neutral-300);}.nav-links .nav-link {width: auto;padding: 0 1rem;border-bottom: none;}.nav-links .nav-link:last-child {border-bottom: none;}.hamburger {display: none;background: none;border: none;font-size: 2rem;cursor: pointer;margin: 0 auto;}.nav-links {display: flex;flex-direction: row;position: static;background-color: transparent;padding: 0;max-height: none;transition: none;}.nav-links.show {display: flex;flex-direction: column;position: absolute;top: 100%;left: 0;right: 0;background-color: var(--neutral-900);padding: 1rem 2rem;max-height: 500px;transition: max-height 0.3s ease-out;overflow: hidden;}@media (max-width: 800px) {.nav-links {display: none;flex-direction: column;width: 100%;position: absolute;top: 100%;left: 0;right: 0;border-top: 1px solid var(--neutral-800);background-color: var(--neutral-900);padding: 1rem 2rem;max-height: 0;transition: max-height 0.3s ease-out;overflow: hidden;}.nav-link{width: 100% !important;padding: 1rem 0 !important;text-align: center;border-bottom: 1px solid var(--neutral-800) !important;}.nav-links.show {display: flex;max-height: 500px;flex-direction: column;align-items: center;}.hamburger {display: block;margin: 0 auto;}.nav-bar {justify-content: center;}}