mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Add server side syntax validation, and paste view counter functionality. Improve frontend syntax highlighting, add more syntax highlighting options. Improve code block styling (majority of it is from github dark theme), better addContent function since highlightjs lets you use the code blocks for plain text.
This commit is contained in:
@@ -7,3 +7,4 @@ SharpbinV2 is a re-write to the original messy code base. Instead of using html
|
|||||||
- Raw Text Serving
|
- Raw Text Serving
|
||||||
- Paste Compression at Rest and on Response Transit (Files are only compressed when there is a considerable file size decrease)
|
- Paste Compression at Rest and on Response Transit (Files are only compressed when there is a considerable file size decrease)
|
||||||
- Cleaner Code Base
|
- Cleaner Code Base
|
||||||
|
- Paste Analytics (Views etc.)
|
||||||
+119
-10
@@ -16,6 +16,34 @@ namespace SharpbinV2.Server
|
|||||||
{
|
{
|
||||||
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 = 5000000; // 5MB
|
||||||
|
public static string[] ValidSyntaxLanguages =
|
||||||
|
{
|
||||||
|
"None",
|
||||||
|
"AutoHotkey",
|
||||||
|
"AutoIt",
|
||||||
|
"bash",
|
||||||
|
"c",
|
||||||
|
"cpp",
|
||||||
|
"csharp",
|
||||||
|
"css",
|
||||||
|
"Dart",
|
||||||
|
"html",
|
||||||
|
"java",
|
||||||
|
"javascript",
|
||||||
|
"json",
|
||||||
|
"lua",
|
||||||
|
"markdown",
|
||||||
|
"php",
|
||||||
|
"python",
|
||||||
|
"ruby",
|
||||||
|
"rust",
|
||||||
|
"sql",
|
||||||
|
"swift",
|
||||||
|
"typescript",
|
||||||
|
"toml",
|
||||||
|
"xml"
|
||||||
|
};
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
await Initialize();
|
await Initialize();
|
||||||
@@ -115,18 +143,25 @@ namespace SharpbinV2.Server
|
|||||||
context.Response.Redirect("/error?error=400&message=Paste not found.");
|
context.Response.Redirect("/error?error=400&message=Paste not found.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
paste.Views++;
|
var requestdetails = GetRequestDetails(context);
|
||||||
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
var user = await Database.UserFromToken(requestdetails.Token);
|
||||||
|
|
||||||
|
if (user != null)
|
||||||
{
|
{
|
||||||
await connection.OpenAsync();
|
if (user.UUID != paste.AuthorUUID)
|
||||||
using (var command = connection.CreateCommand())
|
|
||||||
{
|
{
|
||||||
command.CommandText = "UPDATE pastes SET Views = @Views WHERE UUID = @UUID;";
|
if (!await Database.HasAlreadyViewedFromUserDetails(user))
|
||||||
command.Parameters.AddWithValue("@Views", paste.Views);
|
{
|
||||||
command.Parameters.AddWithValue("@UUID", paste.UUID);
|
await Database.AddViewToPaste(user, paste, requestdetails);
|
||||||
await command.ExecuteNonQueryAsync();
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!await Database.HasAlreadyViewedFromRqDetails(requestdetails))
|
||||||
|
{
|
||||||
|
await Database.AddViewToPaste(null, paste, requestdetails);
|
||||||
}
|
}
|
||||||
await connection.CloseAsync();
|
|
||||||
}
|
}
|
||||||
context.Response.StatusCode = 200;
|
context.Response.StatusCode = 200;
|
||||||
context.Response.Headers.Add("Content-Type", "text/html");
|
context.Response.Headers.Add("Content-Type", "text/html");
|
||||||
@@ -166,6 +201,10 @@ namespace SharpbinV2.Server
|
|||||||
await context.Response.SendFileAsync(paste.FilePath);
|
await context.Response.SendFileAsync(paste.FilePath);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
app.MapGet("/paste.html", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
context.Response.Redirect("/");
|
||||||
|
});
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region API
|
#region API
|
||||||
@@ -392,7 +431,12 @@ namespace SharpbinV2.Server
|
|||||||
paste.Views = 0;
|
paste.Views = 0;
|
||||||
paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null;
|
paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null;
|
||||||
|
|
||||||
Console.WriteLine(paste.Syntax);
|
if (!string.IsNullOrEmpty(paste.Syntax) && !ValidSyntaxLanguages.Contains(paste.Syntax))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid syntax language." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
bool shouldCompress = await Compression.ShouldCompress(body);
|
bool shouldCompress = await Compression.ShouldCompress(body);
|
||||||
if (shouldCompress)
|
if (shouldCompress)
|
||||||
@@ -621,6 +665,14 @@ namespace SharpbinV2.Server
|
|||||||
UserAgent TEXT NOT NULL
|
UserAgent TEXT NOT NULL
|
||||||
);";
|
);";
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
|
command.CommandText = @"CREATE TABLE IF NOT EXISTS views (
|
||||||
|
UserUUID TEXT NOT NULL,
|
||||||
|
PasteUUID INTEGER NOT NULL,
|
||||||
|
Ip TEXT NOT NULL,
|
||||||
|
UserAgent TEXT NOT NULL,
|
||||||
|
Created INTEGER NOT NULL
|
||||||
|
);";
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
await connection.CloseAsync();
|
await connection.CloseAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -808,6 +860,63 @@ namespace SharpbinV2.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static async Task<bool> HasAlreadyViewedFromRqDetails(RequestDetails details)
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM views WHERE Ip = @Ip AND UserAgent = @UserAgent;";
|
||||||
|
command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(details.Ip, Bcrypt.GenerateSalt(10)));
|
||||||
|
command.Parameters.AddWithValue("@UserAgent", details.UserAgent);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
return reader.HasRows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<bool> HasAlreadyViewedFromUserDetails(User user)
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM views WHERE UserUUID = @UserUUID;";
|
||||||
|
command.Parameters.AddWithValue("@UserUUID", user.UUID);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
return reader.HasRows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task AddViewToPaste(User user, Paste paste, RequestDetails details)
|
||||||
|
{
|
||||||
|
if (paste == null || details == null)
|
||||||
|
return;
|
||||||
|
var _user = user ?? new User { UUID = "0" };
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "INSERT INTO views (UserUUID, PasteUUID, Ip, UserAgent, Created) VALUES (@UserUUID, @PasteUUID, @Ip, @UserAgent, @Created);";
|
||||||
|
command.Parameters.AddWithValue("@UserUUID", _user.UUID);
|
||||||
|
command.Parameters.AddWithValue("@PasteUUID", paste.UUID);
|
||||||
|
command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(details.Ip, Bcrypt.GenerateSalt(10)));
|
||||||
|
command.Parameters.AddWithValue("@UserAgent", details.UserAgent);
|
||||||
|
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
command.CommandText = "UPDATE pastes SET Views = Views + 1 WHERE UUID = @UUID;";
|
||||||
|
command.Parameters.AddWithValue("@UUID", paste.UUID);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
await connection.CloseAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class Logging
|
public class Logging
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -37,6 +37,10 @@
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.main-container {
|
||||||
|
max-height: 80% !important;
|
||||||
|
}
|
||||||
|
|
||||||
.paste-content {
|
.paste-content {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
@@ -47,13 +51,20 @@
|
|||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
border: 1px solid var(--neutral-700);
|
border: 1px solid var(--neutral-700);
|
||||||
}
|
}
|
||||||
|
|
||||||
#paste-content {
|
#paste-content {
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
|
max-height: 20vh;
|
||||||
|
overflow-y: scroll;
|
||||||
overflow-wrap: break-word;
|
overflow-wrap: break-word;
|
||||||
word-wrap: break-word;
|
word-wrap: break-word;
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
|
border-radius: 0px;
|
||||||
|
border-bottom-right-radius: 5px;
|
||||||
|
border-bottom-left-radius: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.paste-actions {
|
.paste-actions {
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
@@ -72,4 +83,125 @@ button {
|
|||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hljs {
|
||||||
|
padding: 1rem;
|
||||||
|
border-radius: 6px;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-ln-numbers {
|
||||||
|
border-right: 1px solid var(--neutral-700);
|
||||||
|
padding-right: 8px !important;
|
||||||
|
text-align: center;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-ln-code {
|
||||||
|
padding-left: 16px !important;
|
||||||
|
text-align: left;
|
||||||
|
user-select: text;
|
||||||
|
}
|
||||||
|
|
||||||
|
pre code.hljs {
|
||||||
|
display: block;
|
||||||
|
overflow-x: auto;
|
||||||
|
padding: 1em
|
||||||
|
}
|
||||||
|
|
||||||
|
code.hljs {
|
||||||
|
padding: 3px 5px
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
SLIGHTLY MODIFIED GITHUB DARK THEME
|
||||||
|
*/
|
||||||
|
.hljs {
|
||||||
|
color: #c9d1d9;
|
||||||
|
background: var(--neutral-900);
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-doctag,
|
||||||
|
.hljs-keyword,
|
||||||
|
.hljs-meta .hljs-keyword,
|
||||||
|
.hljs-template-tag,
|
||||||
|
.hljs-template-variable,
|
||||||
|
.hljs-type,
|
||||||
|
.hljs-variable.language_ {
|
||||||
|
color: #ff7b72
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-title,
|
||||||
|
.hljs-title.class_,
|
||||||
|
.hljs-title.class_.inherited__,
|
||||||
|
.hljs-title.function_ {
|
||||||
|
color: #d2a8ff
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-attr,
|
||||||
|
.hljs-attribute,
|
||||||
|
.hljs-literal,
|
||||||
|
.hljs-meta,
|
||||||
|
.hljs-number,
|
||||||
|
.hljs-operator,
|
||||||
|
.hljs-selector-attr,
|
||||||
|
.hljs-selector-class,
|
||||||
|
.hljs-selector-id,
|
||||||
|
.hljs-variable {
|
||||||
|
color: #79c0ff
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-meta .hljs-string,
|
||||||
|
.hljs-regexp,
|
||||||
|
.hljs-string {
|
||||||
|
color: #a5d6ff
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-built_in,
|
||||||
|
.hljs-symbol {
|
||||||
|
color: #ffa657
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-code,
|
||||||
|
.hljs-comment,
|
||||||
|
.hljs-formula {
|
||||||
|
color: #8b949e
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-name,
|
||||||
|
.hljs-quote,
|
||||||
|
.hljs-selector-pseudo,
|
||||||
|
.hljs-selector-tag {
|
||||||
|
color: #7ee787
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-subst {
|
||||||
|
color: #c9d1d9
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-section {
|
||||||
|
color: #1f6feb;
|
||||||
|
font-weight: 700
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-bullet {
|
||||||
|
color: #f2cc60
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-emphasis {
|
||||||
|
color: #c9d1d9;
|
||||||
|
font-style: italic
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-strong {
|
||||||
|
color: #c9d1d9;
|
||||||
|
font-weight: 700
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-addition {
|
||||||
|
color: #aff5b4;
|
||||||
|
background-color: #033a16
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-deletion {
|
||||||
|
color: #ffdcd7;
|
||||||
|
background-color: #67060c
|
||||||
|
}
|
||||||
@@ -23,11 +23,14 @@
|
|||||||
<label style="margin-bottom: -2.3rem; margin-top: 1.1rem; z-index: 2; align-self: flex-start; margin-left: 1rem; pointer-events: none;">Syntax Highlighting -</label>
|
<label style="margin-bottom: -2.3rem; margin-top: 1.1rem; z-index: 2; align-self: flex-start; margin-left: 1rem; pointer-events: none;">Syntax Highlighting -</label>
|
||||||
<select name="syntax" id="syntax">
|
<select name="syntax" id="syntax">
|
||||||
<option value="none">None</option>
|
<option value="none">None</option>
|
||||||
|
<option value="AutoHotkey">AutoHotkey</option>
|
||||||
|
<option value="AutoIt">AutoIt</option>
|
||||||
<option value="bash">Bash</option>
|
<option value="bash">Bash</option>
|
||||||
<option value="c">C</option>
|
<option value="c">C</option>
|
||||||
<option value="cpp">C++</option>
|
<option value="cpp">C++</option>
|
||||||
<option value="csharp">C#</option>
|
<option value="csharp">C#</option>
|
||||||
<option value="css">CSS</option>
|
<option value="css">CSS</option>
|
||||||
|
<option value="Dart">Dart</option>
|
||||||
<option value="html">HTML</option>
|
<option value="html">HTML</option>
|
||||||
<option value="java">Java</option>
|
<option value="java">Java</option>
|
||||||
<option value="javascript">JavaScript</option>
|
<option value="javascript">JavaScript</option>
|
||||||
@@ -41,6 +44,7 @@
|
|||||||
<option value="sql">SQL</option>
|
<option value="sql">SQL</option>
|
||||||
<option value="swift">Swift</option>
|
<option value="swift">Swift</option>
|
||||||
<option value="typescript">TypeScript</option>
|
<option value="typescript">TypeScript</option>
|
||||||
|
<option value="toml">TOML/Ini</option>
|
||||||
<option value="xml">XML</option>
|
<option value="xml">XML</option>
|
||||||
</select>
|
</select>
|
||||||
<select name="visibility" id="visibility" onchange="changeVisibility()">
|
<select name="visibility" id="visibility" onchange="changeVisibility()">
|
||||||
|
|||||||
@@ -71,19 +71,20 @@ function addInfo(paste) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addContent(content, syntax) {
|
function addContent(content, syntax) {
|
||||||
if (syntax === "none" || syntax === "" || syntax === null) {
|
|
||||||
document.getElementById("paste-content").innerText = content;
|
|
||||||
} else {
|
|
||||||
var code = document.createElement("code");
|
var code = document.createElement("code");
|
||||||
code.innerText = content;
|
code.textContent = content;
|
||||||
var pre = document.getElementById("paste-content");
|
var pre = document.getElementById("paste-content");
|
||||||
pre.innerText = "";
|
pre.innerText = "";
|
||||||
pre.appendChild(code);
|
pre.appendChild(code);
|
||||||
pre.removeAttribute("id");
|
pre.removeAttribute("id");
|
||||||
code.id = "paste-content";
|
code.id = "paste-content";
|
||||||
code.classList.add("language-" + syntax);
|
code.classList.add("language-" + syntax === "plaintext" ? "none" : syntax);
|
||||||
|
if (syntax !== "plaintext" && syntax !== "none" && syntax !== "" && syntax !== null) {
|
||||||
|
console.log(syntax);
|
||||||
hljs.highlightElement(code);
|
hljs.highlightElement(code);
|
||||||
}
|
}
|
||||||
|
hljs.lineNumbersBlock(code);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function decryptAES(content, password) {
|
async function decryptAES(content, password) {
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
<link
|
<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"
|
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">
|
rel="stylesheet">
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
|
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user