mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Last commit didnt add everything :(
This commit is contained in:
+105
-9
@@ -4,6 +4,10 @@ using System.Threading.RateLimiting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using System.Data;
|
||||
using System.Net.WebSockets;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
using System.Drawing;
|
||||
|
||||
class Program
|
||||
{
|
||||
@@ -15,7 +19,7 @@ class Program
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "CREATE TABLE pastes (UID INTEGER,Title TEXT, Date TEXT, Size TEXT, ID TEXT, PRIMARY KEY(UID AUTOINCREMENT))";
|
||||
command.CommandText = "CREATE TABLE pastes (UID INTEGER,Title TEXT, Date TEXT, Size TEXT, Visibility TEXT, ID TEXT, PRIMARY KEY(UID AUTOINCREMENT))";
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
@@ -54,6 +58,8 @@ class Program
|
||||
|
||||
app.MapRazorPages();
|
||||
|
||||
#region Frontend
|
||||
|
||||
app.MapGet("/", async (HttpContext context) =>
|
||||
{
|
||||
var html = File.ReadAllText("assets\\index.html");
|
||||
@@ -61,6 +67,16 @@ class Program
|
||||
await context.Response.WriteAsync(html);
|
||||
return;
|
||||
});
|
||||
app.MapGet("/paste-not-found", async (HttpContext context) =>
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
var error_html = File.ReadAllText("assets\\error.html");
|
||||
error_html = error_html.Replace("{code}", "404");
|
||||
error_html = error_html.Replace("{message}", "Paste not found");
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.WriteAsync(error_html);
|
||||
return;
|
||||
});
|
||||
app.MapGet("/{id}", async (HttpContext context) =>
|
||||
{
|
||||
var id = context.Request?.RouteValues?["id"]?.ToString();
|
||||
@@ -70,6 +86,7 @@ class Program
|
||||
return;
|
||||
}
|
||||
var content = File.ReadAllText($"pastes/{id}.txt");
|
||||
content = content.Replace("\n", "<br>").Replace("\r", "");
|
||||
using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
@@ -78,13 +95,26 @@ class Program
|
||||
command.Parameters.AddWithValue("@ID", id);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
await reader.ReadAsync();
|
||||
if(!reader.HasRows)
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
context.Response.Redirect("/paste-not-found");
|
||||
return;
|
||||
}
|
||||
var paste = new Paste
|
||||
{
|
||||
Title = reader.GetString(1),
|
||||
Date = reader.GetString(2),
|
||||
Size = reader.GetString(3),
|
||||
Visibility = reader.GetString(4),
|
||||
Id = id,
|
||||
};
|
||||
if(paste.Visibility == "Private")
|
||||
{
|
||||
// WIP Private pastes will be implemented soon enough, most likely with a password or longer ID
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
var html = File.ReadAllText("assets\\paste.html");
|
||||
html = html.Replace("{pastetitle}", paste.Title);
|
||||
html = html.Replace("{content}", content);
|
||||
@@ -107,10 +137,51 @@ class Program
|
||||
await context.Response.WriteAsync(content);
|
||||
return;
|
||||
});
|
||||
app.MapGet("/pastes", async (HttpContext context) =>
|
||||
{
|
||||
var html = File.ReadAllText("assets\\recent_pastes.html");
|
||||
using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT * FROM pastes ORDER BY UID DESC LIMIT 10";
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
string pastes = string.Empty;
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
var paste = new Paste
|
||||
{
|
||||
Title = reader.GetString(1),
|
||||
Date = reader.GetString(2),
|
||||
Size = reader.GetString(3),
|
||||
Visibility = reader.GetString(4),
|
||||
Id = reader.GetString(5),
|
||||
};
|
||||
if(paste.Visibility == "Private" || paste.Visibility == "Unlisted")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
pastes += $"<a href=\"/{paste.Id}\"><li>{paste.Title} {paste.Date} {ConvertToBytes(paste.Size)}</li></a>";
|
||||
}
|
||||
if(string.IsNullOrEmpty(pastes))
|
||||
{
|
||||
pastes = "<h2>No pastes found</h1>";
|
||||
}
|
||||
html = html.Replace("{pastes}", pastes);
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.WriteAsync(html);
|
||||
return;
|
||||
}
|
||||
}).RequireRateLimiting("fixed");
|
||||
|
||||
#endregion
|
||||
|
||||
#region API
|
||||
|
||||
app.MapPost("/api/create", async (HttpContext context) =>
|
||||
{
|
||||
string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
||||
if (requestBody.Length > 10000 || string.IsNullOrEmpty(requestBody))
|
||||
if (requestBody.Length > 500_000 || string.IsNullOrEmpty(requestBody))
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
context.Response.ContentType = "application/json";
|
||||
@@ -120,7 +191,16 @@ class Program
|
||||
var data = JObject.Parse(requestBody);
|
||||
var title = SanitizeInput(data["title"]?.ToString() ?? "Untitled");
|
||||
var content = SanitizeInput(data["content"]?.ToString() ?? "");
|
||||
if(string.IsNullOrEmpty(content))
|
||||
var visibility = SanitizeInput(data["visibility"]?.ToString() ?? "Public");
|
||||
|
||||
if(visibility != "Public" && visibility != "Unlisted" && visibility != "Private")
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
context.Response.ContentType = "application/json";
|
||||
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid visibility" }.ToString());
|
||||
return;
|
||||
}
|
||||
if(string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content))
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
context.Response.ContentType = "application/json";
|
||||
@@ -128,11 +208,13 @@ class Program
|
||||
return;
|
||||
}
|
||||
|
||||
var paste = await CreatePaste(content, title);
|
||||
var paste = await CreatePaste(content, title, visibility);
|
||||
|
||||
if (!File.Exists($"pastes/{paste?.Id}.txt") || paste == null)
|
||||
{
|
||||
context.Response.StatusCode = 500;
|
||||
context.Response.ContentType = "application/json";
|
||||
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Failed to create paste" }.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -141,36 +223,41 @@ class Program
|
||||
|
||||
JObject responseJson = new JObject();
|
||||
responseJson["pasteId"] = paste.Id;
|
||||
responseJson["pasteSize"] = paste.Size;
|
||||
responseJson["pasteSize"] = ConvertToBytes(paste.Size ?? string.Empty);
|
||||
responseJson["pasteDate"] = paste.Date;
|
||||
responseJson["pasteVisibility"] = paste.Visibility;
|
||||
|
||||
await context.Response.WriteAsJsonAsync(responseJson.ToString());
|
||||
return;
|
||||
|
||||
}).RequireRateLimiting("fixed");
|
||||
|
||||
#endregion
|
||||
|
||||
await app.RunAsync();
|
||||
}
|
||||
|
||||
public static async Task<Paste> CreatePaste(string content, string title)
|
||||
public static async Task<Paste> CreatePaste(string content, string title, string visibility)
|
||||
{
|
||||
var paste = new Paste
|
||||
{
|
||||
Content = content,
|
||||
Title = title,
|
||||
Date = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"),
|
||||
Size = ConvertToBytes(content.Length.ToString()),
|
||||
Size = content.Length.ToString(),
|
||||
Visibility = visibility,
|
||||
Id = GenerateRandomString(12)
|
||||
};
|
||||
File.WriteAllText($"pastes/{paste.Id}.txt", paste.Content);
|
||||
await File.WriteAllTextAsync($"pastes/{paste.Id}.txt", paste.Content);
|
||||
using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "INSERT INTO pastes (Title, Date, Size, ID) VALUES (@Title, @Date, @Size, @ID)";
|
||||
command.CommandText = "INSERT INTO pastes (Title, Date, Size, Visibility, ID) VALUES (@Title, @Date, @Size, @Visibility, @ID)";
|
||||
command.Parameters.AddWithValue("@Title", paste.Title);
|
||||
command.Parameters.AddWithValue("@Date", paste.Date);
|
||||
command.Parameters.AddWithValue("@Size", paste.Size);
|
||||
command.Parameters.AddWithValue("@Visibility", paste.Visibility);
|
||||
command.Parameters.AddWithValue("@ID", paste.Id);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
@@ -183,6 +270,14 @@ class Program
|
||||
}
|
||||
public static string ConvertToBytes(string length)
|
||||
{
|
||||
try
|
||||
{
|
||||
double.Parse(length);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "0 B";
|
||||
}
|
||||
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
|
||||
double len = double.Parse(length);
|
||||
int order = 0;
|
||||
@@ -207,5 +302,6 @@ public class Paste
|
||||
public string? Content { get; set; }
|
||||
public string? Size { get; set; }
|
||||
public string? Date { get; set; }
|
||||
public string? Visibility { get; set; }
|
||||
public string? Id { get; set; }
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="assets\" />
|
||||
<Folder Include="wwwroot\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&display=swap" rel="stylesheet">
|
||||
<title>create paste</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container-wrapper">
|
||||
<div class="error-container">
|
||||
<h1>Error {code}</h1>
|
||||
<h2>{message}</h2>
|
||||
<a href="/" class="button">home page</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<style>
|
||||
body{
|
||||
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
|
||||
font-weight: bold;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: #0d0d0d;
|
||||
}
|
||||
.container-wrapper{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
}
|
||||
.error-container{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 80%;
|
||||
padding: 20px;
|
||||
background-color: #181818;
|
||||
color: white;
|
||||
border-radius: 5px;
|
||||
height: fit-content;
|
||||
max-height: 80vh;
|
||||
overflow-y: auto;
|
||||
border: #222222 2px solid;
|
||||
}
|
||||
.button{
|
||||
text-decoration: none;
|
||||
transition: 300ms;
|
||||
width: fit-content;
|
||||
padding: 10px;
|
||||
margin: 10px 0;
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
background-color: #222222;
|
||||
color: white;
|
||||
border: #2a2a2a00 2px solid;
|
||||
cursor: pointer;
|
||||
font-size: 1.2em;
|
||||
}
|
||||
.button:hover{
|
||||
transition: 300ms;
|
||||
background-color: #333333;
|
||||
border: 2px solid #3a3a3a;
|
||||
}
|
||||
</style>
|
||||
</html>
|
||||
@@ -102,14 +102,16 @@ body{
|
||||
box-sizing: border-box;
|
||||
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
|
||||
font-size: 1.1em;
|
||||
outline: none;
|
||||
}
|
||||
.textinput:focus{
|
||||
transition: 300ms;
|
||||
border: #383838 2px solid;
|
||||
background-color: #333333;
|
||||
border: 2px solid #3a3a3a;
|
||||
outline: none;
|
||||
}
|
||||
.textarea{
|
||||
transition: border 300ms;
|
||||
transition: 300ms;
|
||||
width: 100%;
|
||||
min-width: 100%;
|
||||
max-width: 100%;
|
||||
@@ -124,10 +126,12 @@ body{
|
||||
box-sizing: border-box;
|
||||
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
|
||||
font-size: 1.08em;
|
||||
outline: none;
|
||||
}
|
||||
.textarea:focus{
|
||||
transition: border 300ms;
|
||||
border: #383838 2px solid;
|
||||
transition: 300ms;
|
||||
background-color: #333333;
|
||||
border: 2px solid #3a3a3a;
|
||||
outline: none;
|
||||
}
|
||||
.button{
|
||||
@@ -137,15 +141,16 @@ body{
|
||||
margin: 10px 0;
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
background-color: #282828;
|
||||
background-color: #222222;
|
||||
color: white;
|
||||
border: #2a2a2a 2px solid;
|
||||
border: #2a2a2a00 2px solid;
|
||||
cursor: pointer;
|
||||
font-size: 1.2em;
|
||||
}
|
||||
.button:hover{
|
||||
transition: 300ms;
|
||||
background-color: #383838;
|
||||
background-color: #333333;
|
||||
border: 2px solid #3a3a3a;
|
||||
}
|
||||
</style>
|
||||
</html>
|
||||
@@ -69,6 +69,7 @@ body{
|
||||
max-height: 80vh;
|
||||
min-height: 20%;
|
||||
overflow-y: auto;
|
||||
border: #222222 2px solid;
|
||||
}
|
||||
.paste-wrapper{
|
||||
width: 95%;
|
||||
@@ -83,6 +84,7 @@ body{
|
||||
font-weight: 500;
|
||||
max-height: 80%;
|
||||
overflow-y: auto;
|
||||
border: #222222 2px solid;
|
||||
}
|
||||
.buttons-wrapper{
|
||||
display: flex;
|
||||
@@ -97,9 +99,9 @@ body{
|
||||
}
|
||||
.button{
|
||||
transition: 300ms;
|
||||
background-color: #1d1d1d;
|
||||
background-color: #222222;
|
||||
color: white;
|
||||
border: 1px solid #1d1d1d;
|
||||
border: 2px solid #33333300;
|
||||
border-radius: 5px;
|
||||
padding: 5px;
|
||||
margin: 5px;
|
||||
@@ -110,8 +112,8 @@ body{
|
||||
}
|
||||
.button:hover{
|
||||
transition: 300ms;
|
||||
background-color: #2d2d2d;
|
||||
border: 1px solid #2d2d2d;
|
||||
background-color: #333333;
|
||||
border: 2px solid #3a3a3a;
|
||||
}
|
||||
</style>
|
||||
</html>
|
||||
@@ -0,0 +1,121 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&display=swap" rel="stylesheet">
|
||||
<title>recent pastes</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container-wrapper">
|
||||
<div class="pastes-container">
|
||||
<h1>recent pastes</h1>
|
||||
<div class="pastes-wrapper">
|
||||
{pastes}
|
||||
</div>
|
||||
<div class="buttons-wrapper">
|
||||
<a href="/">create paste</a>
|
||||
<a href="/archives">paste archives</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<style>
|
||||
body{
|
||||
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
|
||||
font-weight: bold;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: #0d0d0d;
|
||||
overflow-y: hidden;
|
||||
}
|
||||
.container-wrapper{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
}
|
||||
.pastes-container{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
width: 80%;
|
||||
padding: 20px;
|
||||
background-color: #181818;
|
||||
color: white;
|
||||
border-radius: 5px;
|
||||
height: fit-content;
|
||||
max-height: 80vh;
|
||||
min-height: 20%;
|
||||
border: #222222 2px solid;
|
||||
}
|
||||
.pastes-wrapper{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
width: 95%;
|
||||
padding: 20px;
|
||||
overflow-x: hidden;
|
||||
overflow-y: auto;
|
||||
max-height: 70vh;
|
||||
}
|
||||
.pastes-wrapper a{
|
||||
text-decoration: none;
|
||||
color: white;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
li{
|
||||
transition: 300ms;
|
||||
list-style: none;
|
||||
padding: 10px;
|
||||
margin: 10px;
|
||||
background-color: #222222;
|
||||
border-radius: 5px;
|
||||
width: 90%;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
border: #22222200 2px solid;
|
||||
}
|
||||
li:hover{
|
||||
transition: 300ms;
|
||||
background-color: #333333;
|
||||
border: #3a3a3a 2px solid;
|
||||
}
|
||||
.buttons-wrapper{
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
padding: 20px;
|
||||
}
|
||||
.buttons-wrapper a{
|
||||
transition: 300ms;
|
||||
text-decoration: none;
|
||||
color: white;
|
||||
width: fit-content;
|
||||
margin-left: 10px;
|
||||
margin-right: 10px;
|
||||
min-width: 20%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: #222222;
|
||||
border-radius: 5px;
|
||||
padding: 10px;
|
||||
border: #22222200 2px solid;
|
||||
}
|
||||
.buttons-wrapper a:hover{
|
||||
transition: 300ms;
|
||||
background-color: #333333;
|
||||
border: 2px solid #3a3a3a;
|
||||
}
|
||||
</style>
|
||||
</html>
|
||||
Reference in New Issue
Block a user