From 01a637f5dded64112f9f865e661d452d9e891b30 Mon Sep 17 00:00:00 2001
From: whoswhip <124531971+whoswhip@users.noreply.github.com>
Date: Fri, 17 Jan 2025 21:17:37 -0500
Subject: [PATCH] Add frontend files, improve backend.
---
README.md | 9 ++
SharpbinV2.Server/Program.cs | 125 +++++++++++++++++++-
SharpbinV2.Server/SharpbinV2.Server.csproj | 11 +-
SharpbinV2.Server/assets/index.html | 0
SharpbinV2.Server/wwwroot/css/paste.css | 75 ++++++++++++
SharpbinV2.Server/wwwroot/css/style.css | 130 +++++++++++++++++++++
SharpbinV2.Server/wwwroot/error.html | 23 ++++
SharpbinV2.Server/wwwroot/index.html | 58 +++++++++
SharpbinV2.Server/wwwroot/js/error.js | 22 ++++
SharpbinV2.Server/wwwroot/js/index.js | 79 +++++++++++++
SharpbinV2.Server/wwwroot/js/navbar.js | 9 ++
SharpbinV2.Server/wwwroot/js/paste.js | 127 ++++++++++++++++++++
SharpbinV2.Server/wwwroot/paste.html | 42 +++++++
13 files changed, 699 insertions(+), 11 deletions(-)
create mode 100644 README.md
delete mode 100644 SharpbinV2.Server/assets/index.html
create mode 100644 SharpbinV2.Server/wwwroot/css/paste.css
create mode 100644 SharpbinV2.Server/wwwroot/css/style.css
create mode 100644 SharpbinV2.Server/wwwroot/error.html
create mode 100644 SharpbinV2.Server/wwwroot/index.html
create mode 100644 SharpbinV2.Server/wwwroot/js/error.js
create mode 100644 SharpbinV2.Server/wwwroot/js/index.js
create mode 100644 SharpbinV2.Server/wwwroot/js/navbar.js
create mode 100644 SharpbinV2.Server/wwwroot/js/paste.js
create mode 100644 SharpbinV2.Server/wwwroot/paste.html
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b4fff61
--- /dev/null
+++ b/README.md
@@ -0,0 +1,9 @@
+# SharpbinV2
+SharpbinV2 is a re-write to the original messy code base. Instead of using html files and replacing variables it now is done with just apis and static html pages served from `wwwroot`.
+## Current Features
+- Account Systrem
+- Syntax Highlighting
+- Private Pastes with Client Side Encryption
+- Raw Text Serving
+- Paste Compression at Rest and on Response Transit (Files are only compressed when there is a considerable file size decrease)
+- Cleaner Code Base
\ No newline at end of file
diff --git a/SharpbinV2.Server/Program.cs b/SharpbinV2.Server/Program.cs
index 8018371..2256ec5 100644
--- a/SharpbinV2.Server/Program.cs
+++ b/SharpbinV2.Server/Program.cs
@@ -48,6 +48,7 @@ namespace SharpbinV2.Server
//app.UseHttpsRedirection();
app.UseRouting();
+ app.UseStaticFiles();
//app.UseRateLimiter();
@@ -63,9 +64,107 @@ namespace SharpbinV2.Server
#region Static Endpoints
app.MapGet("/", async (HttpContext context) =>
{
+ context.Response.Redirect("/index.html");
+ });
+ app.MapGet("/{pasteid}", async (HttpContext context, IWebHostEnvironment env) =>
+ {
+ var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
+ if (string.IsNullOrEmpty(pasteid))
+ {
+ context.Response.StatusCode = 400;
+ context.Response.Redirect("/error?error=400&message=Invalid paste id.");
+ return;
+ }
+
+ var filePath = Path.Combine(env.WebRootPath, pasteid);
+ if (File.Exists(filePath))
+ {
+ context.Response.StatusCode = 200;
+ switch (Path.GetExtension(filePath))
+ {
+ case "html":
+ context.Response.Headers.Add("Content-Type", "text/html");
+ break;
+ case "css":
+ context.Response.Headers.Add("Content-Type", "text/css");
+ break;
+ case "js":
+ context.Response.Headers.Add("Content-Type", "text/javascript");
+ break;
+ case "json":
+ context.Response.Headers.Add("Content-Type", "application/json");
+ break;
+ case "png":
+ context.Response.Headers.Add("Content-Type", "image/png");
+ break;
+ case "jpg":
+ case "jpeg":
+ context.Response.Headers.Add("Content-Type", "image/jpeg");
+ break;
+ case "ico":
+ context.Response.Headers.Add("Content-Type", "image/x-icon");
+ break;
+ }
+ await context.Response.SendFileAsync(filePath);
+ return;
+ }
+ var paste = await Database.GetPasteFromID(pasteid);
+ if (paste == null)
+ {
+ context.Response.StatusCode = 400;
+ context.Response.Redirect("/error?error=400&message=Paste not found.");
+ return;
+ }
+ paste.Views++;
+ using (var connection = new SqliteConnection(MainDatabaseConnection))
+ {
+ await connection.OpenAsync();
+ using (var command = connection.CreateCommand())
+ {
+ command.CommandText = "UPDATE pastes SET Views = @Views WHERE UUID = @UUID;";
+ command.Parameters.AddWithValue("@Views", paste.Views);
+ command.Parameters.AddWithValue("@UUID", paste.UUID);
+ await command.ExecuteNonQueryAsync();
+ }
+ await connection.CloseAsync();
+ }
context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "text/html");
- await context.Response.SendFileAsync("assets/index.html");
+ await context.Response.SendFileAsync("wwwroot/paste.html");
+ });
+ app.MapGet("/error", async (HttpContext context) =>
+ {
+ context.Response.StatusCode = 400;
+ context.Response.Headers.Add("Content-Type", "text/html");
+ await context.Response.SendFileAsync("wwwroot/error.html");
+ });
+ app.MapGet("/raw/{pasteid}", async (HttpContext context) =>
+ {
+ var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
+ if (string.IsNullOrEmpty(pasteid))
+ {
+ context.Response.StatusCode = 400;
+ context.Response.Redirect("/error?error=400&message=Invalid paste id.");
+ return;
+ }
+ var paste = await Database.GetPasteFromID(pasteid);
+ if (paste == null)
+ {
+ context.Response.StatusCode = 400;
+ context.Response.Redirect("/error?error=400&message=Paste not found.");
+ return;
+ }
+ context.Response.StatusCode = 200;
+ context.Response.Headers.Add("Content-Type", "text/plain");
+ if (paste.FilePath.EndsWith(".gz"))
+ {
+ context.Response.Headers.Add("Content-Encoding", "gzip");
+ await context.Response.SendFileAsync(paste.FilePath);
+ }
+ else
+ {
+ await context.Response.SendFileAsync(paste.FilePath);
+ }
});
#endregion
@@ -290,6 +389,10 @@ namespace SharpbinV2.Server
paste.Visibility = queries.ContainsKey("visibility") ? Convert.ToInt32(queries["visibility"]) : 0;
paste.Title = queries.ContainsKey("title") ? queries["title"].ToString() : $"Untitled-{await Database.EnumeratePastes()}";
paste.Edited = 0;
+ paste.Views = 0;
+ paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null;
+
+ Console.WriteLine(paste.Syntax);
bool shouldCompress = await Compression.ShouldCompress(body);
if (shouldCompress)
@@ -310,7 +413,7 @@ namespace SharpbinV2.Server
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
- command.CommandText = "INSERT INTO pastes (UUID, ID, Visibility, Title, AuthorUUID, FilePath, Created, Edited, Size, TrueSize, Views) VALUES (@UUID, @ID, @Visibility, @Title, @AuthorUUID, @FilePath, @Created, @Edited, @Size, @TrueSize, @Views);";
+ command.CommandText = "INSERT INTO pastes (UUID, ID, Visibility, Title, AuthorUUID, FilePath, Created, Edited, Size, TrueSize, Views, Syntax) VALUES (@UUID, @ID, @Visibility, @Title, @AuthorUUID, @FilePath, @Created, @Edited, @Size, @TrueSize, @Views, @Syntax);";
command.Parameters.AddWithValue("@UUID", paste.UUID);
command.Parameters.AddWithValue("@ID", paste.ID);
command.Parameters.AddWithValue("@Visibility", paste.Visibility);
@@ -322,11 +425,12 @@ namespace SharpbinV2.Server
command.Parameters.AddWithValue("@Size", paste.Size);
command.Parameters.AddWithValue("@TrueSize", paste.TrueSize);
command.Parameters.AddWithValue("@Views", paste.Views);
+ command.Parameters.AddWithValue("@Syntax", paste.Syntax);
await command.ExecuteNonQueryAsync();
}
await connection.CloseAsync();
}
-
+ paste.FilePath = null;
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { success = true, message = "Paste created.", paste = paste });
logger.LogInfo($"A Paste of {FormatBytes(paste.Size)} was created by {paste.UUID}");
@@ -348,6 +452,16 @@ namespace SharpbinV2.Server
await context.Response.WriteAsJsonAsync(new { success = false, message = "Paste not found." });
return;
}
+ if (paste.AuthorUUID != "0")
+ {
+ var user = await Database.UserFromUUID(paste.AuthorUUID);
+ paste.AuthorUUID = user.Username;
+ }
+ else
+ {
+ paste.AuthorUUID = "Anonymous";
+ }
+ paste.FilePath = null;
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { success = true, paste = paste });
});
@@ -493,6 +607,7 @@ namespace SharpbinV2.Server
Size INTEGER NOT NULL,
TrueSize INTEGER NOT NULL,
Views INTEGER NOT NULL DEFAULT 0,
+ Syntax TEXT,
PRIMARY KEY(UID AUTOINCREMENT)
);";
await command.ExecuteNonQueryAsync();
@@ -686,7 +801,8 @@ namespace SharpbinV2.Server
Edited = reader.GetInt64(8),
Size = reader.GetInt32(9),
TrueSize = reader.GetInt32(10),
- Views = reader.GetInt32(11)
+ Views = reader.GetInt32(11),
+ Syntax = reader.IsDBNull(12) ? null : reader.GetString(12)
};
}
}
@@ -839,6 +955,7 @@ namespace SharpbinV2.Server
public int Size { get; set; }
public int TrueSize { get; set; }
public int Views { get; set; }
+ public string Syntax { get; set; }
}
class Session
{
diff --git a/SharpbinV2.Server/SharpbinV2.Server.csproj b/SharpbinV2.Server/SharpbinV2.Server.csproj
index 4c7990e..c430c3e 100644
--- a/SharpbinV2.Server/SharpbinV2.Server.csproj
+++ b/SharpbinV2.Server/SharpbinV2.Server.csproj
@@ -9,13 +9,6 @@
Syntax
+Date
+Content+