mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Add project files.
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System.Threading.RateLimiting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
if(!File.Exists("pastes.sqlite"))
|
||||
{
|
||||
using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
|
||||
{
|
||||
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))";
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddRazorPages();
|
||||
builder.Services.AddCors();
|
||||
|
||||
|
||||
builder.Services.AddRateLimiter(_ => _
|
||||
.AddFixedWindowLimiter(policyName: "fixed", options =>
|
||||
{
|
||||
options.PermitLimit = 4;
|
||||
options.Window = TimeSpan.FromSeconds(12);
|
||||
options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
|
||||
options.QueueLimit = 2;
|
||||
}));
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/Error");
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseRateLimiter();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapRazorPages();
|
||||
|
||||
app.MapGet("/", async (HttpContext context) =>
|
||||
{
|
||||
var html = File.ReadAllText("assets\\index.html");
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.WriteAsync(html);
|
||||
return;
|
||||
});
|
||||
app.MapGet("/{id}", async (HttpContext context) =>
|
||||
{
|
||||
var id = context.Request?.RouteValues?["id"]?.ToString();
|
||||
if (!File.Exists($"pastes/{id}.txt"))
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
var content = File.ReadAllText($"pastes/{id}.txt");
|
||||
using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT * FROM pastes WHERE ID = @ID";
|
||||
command.Parameters.AddWithValue("@ID", id);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
await reader.ReadAsync();
|
||||
var paste = new Paste
|
||||
{
|
||||
Title = reader.GetString(1),
|
||||
Date = reader.GetString(2),
|
||||
Size = reader.GetString(3),
|
||||
Id = id,
|
||||
};
|
||||
var html = File.ReadAllText("assets\\paste.html");
|
||||
html = html.Replace("{pastetitle}", paste.Title);
|
||||
html = html.Replace("{content}", content);
|
||||
html = html.Replace("{pasteid}", paste.Id);
|
||||
context.Response.ContentType = "text/html";
|
||||
await context.Response.WriteAsync(html);
|
||||
return;
|
||||
}
|
||||
});
|
||||
app.MapGet("/raw/{id}", async (HttpContext context) =>
|
||||
{
|
||||
var id = context.Request?.RouteValues?["id"]?.ToString();
|
||||
if (!File.Exists($"pastes/{id}.txt"))
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
var content = File.ReadAllText($"pastes/{id}.txt");
|
||||
context.Response.ContentType = "text/plain";
|
||||
await context.Response.WriteAsync(content);
|
||||
return;
|
||||
});
|
||||
app.MapPost("/api/create", async (HttpContext context) =>
|
||||
{
|
||||
string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
||||
if (requestBody.Length > 10000 || string.IsNullOrEmpty(requestBody))
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
context.Response.ContentType = "application/json";
|
||||
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid request body" }.ToString());
|
||||
return;
|
||||
}
|
||||
var data = JObject.Parse(requestBody);
|
||||
var title = SanitizeInput(data["title"]?.ToString() ?? "Untitled");
|
||||
var content = SanitizeInput(data["content"]?.ToString() ?? "");
|
||||
if(string.IsNullOrEmpty(content))
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
context.Response.ContentType = "application/json";
|
||||
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid request body" }.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
var paste = await CreatePaste(content, title);
|
||||
|
||||
if (!File.Exists($"pastes/{paste?.Id}.txt") || paste == null)
|
||||
{
|
||||
context.Response.StatusCode = 500;
|
||||
return;
|
||||
}
|
||||
|
||||
context.Response.StatusCode = 200;
|
||||
context.Response.ContentType = "application/json";
|
||||
|
||||
JObject responseJson = new JObject();
|
||||
responseJson["pasteId"] = paste.Id;
|
||||
responseJson["pasteSize"] = paste.Size;
|
||||
responseJson["pasteDate"] = paste.Date;
|
||||
|
||||
await context.Response.WriteAsJsonAsync(responseJson.ToString());
|
||||
return;
|
||||
|
||||
}).RequireRateLimiting("fixed");
|
||||
|
||||
await app.RunAsync();
|
||||
}
|
||||
|
||||
public static async Task<Paste> CreatePaste(string content, string title)
|
||||
{
|
||||
var paste = new Paste
|
||||
{
|
||||
Content = content,
|
||||
Title = title,
|
||||
Date = DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss"),
|
||||
Size = ConvertToBytes(content.Length.ToString()),
|
||||
Id = GenerateRandomString(12)
|
||||
};
|
||||
File.WriteAllText($"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.Parameters.AddWithValue("@Title", paste.Title);
|
||||
command.Parameters.AddWithValue("@Date", paste.Date);
|
||||
command.Parameters.AddWithValue("@Size", paste.Size);
|
||||
command.Parameters.AddWithValue("@ID", paste.Id);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
return paste;
|
||||
}
|
||||
|
||||
public static string SanitizeInput(string input)
|
||||
{
|
||||
return input?.Replace("<", "<").Replace(">", ">") ?? string.Empty + input;
|
||||
}
|
||||
public static string ConvertToBytes(string length)
|
||||
{
|
||||
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
|
||||
double len = double.Parse(length);
|
||||
int order = 0;
|
||||
while (len >= 1024 && order < sizes.Length - 1)
|
||||
{
|
||||
order++;
|
||||
len = len / 1024;
|
||||
}
|
||||
return $"{len:0.##} {sizes[order]}";
|
||||
}
|
||||
public static string GenerateRandomString(int length)
|
||||
{
|
||||
var random = new Random();
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
}
|
||||
public class Paste
|
||||
{
|
||||
public string? Title { get; set; }
|
||||
public string? Content { get; set; }
|
||||
public string? Size { get; set; }
|
||||
public string? Date { get; set; }
|
||||
public string? Id { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://*:29219",
|
||||
"sslPort": 44358
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://*:5240",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://*:7220;http://*:5240",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="pastes\**" />
|
||||
<Content Remove="pastes\**" />
|
||||
<EmbeddedResource Remove="pastes\**" />
|
||||
<None Remove="pastes\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="assets\" />
|
||||
<Folder Include="wwwroot\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.8" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"DetailedErrors": true,
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<!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="creation-container">
|
||||
<h1>create paste</h1>
|
||||
<form id="createForm">
|
||||
<label for="pasteTitle" class="label">Title</label>
|
||||
<input type="text" id="pasteTitle" class="textinput" name="pasteTitle">
|
||||
<label for="pasteContent" class="label">Content</label>
|
||||
<textarea id="pasteContent" class="textarea" name="pasteContent" required ></textarea>
|
||||
<button type="submit" class="button">Create</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
document.getElementById("createForm").addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
var textAreaContent = document.getElementById("pasteContent").value;
|
||||
var textTitleContent = document.getElementById("pasteTitle").value;
|
||||
|
||||
var requestUrl = "/api/create";
|
||||
fetch(requestUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: textTitleContent,
|
||||
content: textAreaContent
|
||||
})
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(obj => {
|
||||
const result = JSON.parse(obj);
|
||||
if (result.pasteId != null) {
|
||||
window.location.href = `/${result.pasteId}`;
|
||||
}
|
||||
})
|
||||
});
|
||||
</script>
|
||||
<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;
|
||||
}
|
||||
.creation-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%;
|
||||
overflow-y: auto;
|
||||
border: #222222 2px solid;
|
||||
}
|
||||
#createForm{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
}
|
||||
.label{
|
||||
font-size: 1.5em;
|
||||
}
|
||||
.textinput{
|
||||
transition: 300ms;
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
margin: 10px 0;
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
background-color: #282828;
|
||||
color: white;
|
||||
border: #2a2a2a 2px solid;
|
||||
box-sizing: border-box;
|
||||
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
|
||||
font-size: 1.1em;
|
||||
}
|
||||
.textinput:focus{
|
||||
transition: 300ms;
|
||||
border: #383838 2px solid;
|
||||
outline: none;
|
||||
}
|
||||
.textarea{
|
||||
transition: border 300ms;
|
||||
width: 100%;
|
||||
min-width: 100%;
|
||||
max-width: 100%;
|
||||
min-height: auto;
|
||||
padding: 10px;
|
||||
margin: 10px 0;
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
background-color: #282828;
|
||||
color: white;
|
||||
border: #2a2a2a 2px solid;
|
||||
box-sizing: border-box;
|
||||
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
|
||||
font-size: 1.08em;
|
||||
}
|
||||
.textarea:focus{
|
||||
transition: border 300ms;
|
||||
border: #383838 2px solid;
|
||||
outline: none;
|
||||
}
|
||||
.button{
|
||||
transition: 300ms;
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
margin: 10px 0;
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
background-color: #282828;
|
||||
color: white;
|
||||
border: #2a2a2a 2px solid;
|
||||
cursor: pointer;
|
||||
font-size: 1.2em;
|
||||
}
|
||||
.button:hover{
|
||||
transition: 300ms;
|
||||
background-color: #383838;
|
||||
}
|
||||
</style>
|
||||
</html>
|
||||
@@ -0,0 +1,117 @@
|
||||
<!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>{pastetitle}</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container-wrapper">
|
||||
<div class="paste-container">
|
||||
<h1>{pastetitle}</h1>
|
||||
<div class="paste-wrapper">
|
||||
<p>{content}</p>
|
||||
</div>
|
||||
<div class="buttons-wrapper">
|
||||
<a href="/raw/{pasteid}">
|
||||
<button id="view-raw-button" class="button">View Raw</button>
|
||||
</a>
|
||||
<button id="copy-button" class="button">Copy</button>
|
||||
<button id="download-button" class="button">Download</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
document.getElementById("copy-button").addEventListener("click", function(){
|
||||
navigator.clipboard.writeText(document.querySelector(".paste-wrapper").innerText);
|
||||
});
|
||||
document.getElementById("download-button").addEventListener("click", function(){
|
||||
var element = document.createElement('a');
|
||||
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(document.querySelector(".paste-wrapper").innerText));
|
||||
element.setAttribute('download', '{pasteid}.txt');
|
||||
element.style.display = 'none';
|
||||
document.body.appendChild(element);
|
||||
element.click();
|
||||
document.body.removeChild(element);
|
||||
});
|
||||
</script>
|
||||
<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;
|
||||
}
|
||||
.paste-container{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: 80%;
|
||||
padding: 20px;
|
||||
background-color: #181818;
|
||||
color: white;
|
||||
border-radius: 5px;
|
||||
height: fit-content;
|
||||
max-height: 80vh;
|
||||
min-height: 20%;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.paste-wrapper{
|
||||
width: 95%;
|
||||
height: fit-content;
|
||||
padding-left: 10px;
|
||||
padding-right: 10px;
|
||||
background-color: #1d1d1d;
|
||||
color: white;
|
||||
border-radius: 5px;
|
||||
margin-top: 10px;
|
||||
text-wrap: wrap;
|
||||
font-weight: 500;
|
||||
max-height: 80%;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.buttons-wrapper{
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: fit-content;
|
||||
margin-top: 10px;
|
||||
}
|
||||
.buttons-wrapper a{
|
||||
text-decoration: none;
|
||||
}
|
||||
.button{
|
||||
transition: 300ms;
|
||||
background-color: #1d1d1d;
|
||||
color: white;
|
||||
border: 1px solid #1d1d1d;
|
||||
border-radius: 5px;
|
||||
padding: 5px;
|
||||
margin: 5px;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
font-size: 1.05rem;
|
||||
padding: 10px;
|
||||
}
|
||||
.button:hover{
|
||||
transition: 300ms;
|
||||
background-color: #2d2d2d;
|
||||
border: 1px solid #2d2d2d;
|
||||
}
|
||||
</style>
|
||||
</html>
|
||||
Reference in New Issue
Block a user