From 47aed189199d99b2b6d0e9c1b40843cf9092daad Mon Sep 17 00:00:00 2001 From: whoswhip Date: Mon, 19 Aug 2024 03:45:06 -0400 Subject: [PATCH] Improvement to pastes archive, added navigator to the top of each page --- PasteBin/Program.cs | 38 +++++++++++++++++++++- PasteBin/assets/archives.html | 51 +++++++++++++++++++++++++++--- PasteBin/assets/error.html | 38 ++++++++++++++++++++++ PasteBin/assets/index.html | 38 ++++++++++++++++++++++ PasteBin/assets/paste.html | 40 ++++++++++++++++++++++- PasteBin/assets/recent_pastes.html | 38 ++++++++++++++++++++++ 6 files changed, 237 insertions(+), 6 deletions(-) diff --git a/PasteBin/Program.cs b/PasteBin/Program.cs index 4521022..a637fab 100644 --- a/PasteBin/Program.cs +++ b/PasteBin/Program.cs @@ -3,6 +3,7 @@ using Microsoft.Data.Sqlite; using System.Threading.RateLimiting; using Newtonsoft.Json.Linq; using System.Data; +using System.Runtime.InteropServices; class Program { @@ -33,6 +34,15 @@ class Program options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst; options.QueueLimit = 2; })); + builder.Services.AddRateLimiter(_ => _ + .AddFixedWindowLimiter(policyName: "fixed-bigger", options => + { + options.PermitLimit = 8; + options.Window = TimeSpan.FromSeconds(6); + options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst; + options.QueueLimit = 4; + })); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -156,6 +166,10 @@ class Program { continue; } + if(paste.Title == "" || string.IsNullOrEmpty(paste.Title)) + { + paste.Title = $"Untitled {reader.GetString(0)}"; + } pastes += $"
  • {paste.Title} {paste.Date} {ConvertToBytes(paste.Size)}
  • "; } if(string.IsNullOrEmpty(pastes)) @@ -202,20 +216,40 @@ class Program { continue; } + if (paste.Title == "" || string.IsNullOrEmpty(paste.Title)) + { + paste.Title = $"Untitled {reader.GetString(0)}"; + } pastes += $"
  • {paste.Title} {paste.Date} {ConvertToBytes(paste.Size)}
  • "; } + var findCountCommand = connection.CreateCommand(); + findCountCommand.CommandText = "SELECT COUNT(*) FROM pastes WHERE Visibility != 'Private'"; + int totalPastes = Convert.ToInt32(await findCountCommand.ExecuteScalarAsync() ); + int totalPages = (int)Math.Ceiling((double)totalPastes / pastesPerPage); + + if (pageNumber > totalPages && totalPages != 0) + { + context.Response.Redirect($"/archive?page={totalPages}"); + return; + } if (string.IsNullOrEmpty(pastes)) { pastes = "

    No pastes found

    "; + html = html.Replace("", ""); + html = html.Replace("", ""); + html += ".buttons-wrapper{\r\n display: none !important;\r\n}"; + html += "\n"; } html = html.Replace("{pastes}", pastes); html = html.Replace("{backpagenum}", $"{pageNumber - 1}"); html = html.Replace("{pagenum}", $"{pageNumber + 1}"); + html = html.Replace("{currentpagenum}", $"{pageNumber}"); + html = html.Replace("{totalpages}", $"{totalPages}"); context.Response.ContentType = "text/html"; await context.Response.WriteAsync(html); return; } - }); + }).RequireRateLimiting("fixed-bigger"); #endregion @@ -271,6 +305,8 @@ class Program responseJson["pasteDate"] = paste.Date; responseJson["pasteVisibility"] = paste.Visibility; + Console.WriteLine($"Created a {ConvertToBytes(paste.Size ?? string.Empty)} sized paste with the id of {paste.Id} at {paste.Date}"); + await context.Response.WriteAsJsonAsync(responseJson.ToString()); return; diff --git a/PasteBin/assets/archives.html b/PasteBin/assets/archives.html index db6242b..799e06d 100644 --- a/PasteBin/assets/archives.html +++ b/PasteBin/assets/archives.html @@ -7,6 +7,11 @@ archive +

    paste archive

    @@ -14,10 +19,9 @@ {pastes}
    - back a page - create paste - recent pastes - next page + < +

    {currentpagenum}/{totalpages}

    + >
    @@ -119,5 +123,44 @@ li:hover{ background-color: #333333; border: 2px solid #3a3a3a; } +.nav-div{ + top: 0; + margin-top: 10px; + position: absolute; + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + width: fit-content; + background-color: #181818; + padding-left: 10px; + padding-right: 10px; + color: white; + border-radius: 5px; + height: fit-content; + overflow-y: auto; + border: #222222 2px solid; +} +.nav-div a{ + text-decoration: none; + color: white; + padding: 10px; + margin: 10px; + border-radius: 5px; + background-color: #222222; + transition: 300ms; + border: #2a2a2a00 2px solid; +} +.nav-div a:hover{ + transition: 300ms; + background-color: #333333; + border: #3a3a3a 2px solid; +} +.arrow-button{ + min-width: fit-content !important; + padding-left: 1rem !important; + padding-right: 1rem !important; +} + \ No newline at end of file diff --git a/PasteBin/assets/error.html b/PasteBin/assets/error.html index 3818862..b97a7db 100644 --- a/PasteBin/assets/error.html +++ b/PasteBin/assets/error.html @@ -7,6 +7,11 @@ create paste +

    Error {code}

    @@ -67,5 +72,38 @@ body{ background-color: #333333; border: 2px solid #3a3a3a; } +.nav-div{ + top: 0; + margin-top: 10px; + position: absolute; + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + width: fit-content; + background-color: #181818; + padding-left: 10px; + padding-right: 10px; + color: white; + border-radius: 5px; + height: fit-content; + overflow-y: auto; + border: #222222 2px solid; +} +.nav-div a{ + text-decoration: none; + color: white; + padding: 10px; + margin: 10px; + border-radius: 5px; + background-color: #222222; + transition: 300ms; + border: #2a2a2a00 2px solid; +} +.nav-div a:hover{ + transition: 300ms; + background-color: #333333; + border: #3a3a3a 2px solid; +} \ No newline at end of file diff --git a/PasteBin/assets/index.html b/PasteBin/assets/index.html index 4de5b48..8a75f0f 100644 --- a/PasteBin/assets/index.html +++ b/PasteBin/assets/index.html @@ -7,6 +7,11 @@ create paste +

    create paste

    @@ -152,5 +157,38 @@ body{ background-color: #333333; border: 2px solid #3a3a3a; } +.nav-div{ + top: 0; + margin-top: 10px; + position: absolute; + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + width: fit-content; + background-color: #181818; + padding-left: 10px; + padding-right: 10px; + color: white; + border-radius: 5px; + height: fit-content; + overflow-y: auto; + border: #222222 2px solid; +} +.nav-div a{ + text-decoration: none; + color: white; + padding: 10px; + margin: 10px; + border-radius: 5px; + background-color: #222222; + transition: 300ms; + border: #2a2a2a00 2px solid; +} +.nav-div a:hover{ + transition: 300ms; + background-color: #333333; + border: #3a3a3a 2px solid; +} \ No newline at end of file diff --git a/PasteBin/assets/paste.html b/PasteBin/assets/paste.html index df74e7c..d001283 100644 --- a/PasteBin/assets/paste.html +++ b/PasteBin/assets/paste.html @@ -7,6 +7,11 @@ {pastetitle} +

    {pastetitle}

    @@ -53,7 +58,7 @@ body{ justify-content: center; align-items: center; width: 100%; - height: 100vh; + height: 98.7vh; } .paste-container{ display: flex; @@ -115,5 +120,38 @@ body{ background-color: #333333; border: 2px solid #3a3a3a; } +.nav-div{ + top: 0; + margin-top: 10px; + position: absolute; + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + width: fit-content; + background-color: #181818; + padding-left: 10px; + padding-right: 10px; + color: white; + border-radius: 5px; + height: fit-content; + overflow-y: auto; + border: #222222 2px solid; +} +.nav-div a{ + text-decoration: none; + color: white; + padding: 10px; + margin: 10px; + border-radius: 5px; + background-color: #222222; + transition: 300ms; + border: #2a2a2a00 2px solid; +} +.nav-div a:hover{ + transition: 300ms; + background-color: #333333; + border: #3a3a3a 2px solid; +} \ No newline at end of file diff --git a/PasteBin/assets/recent_pastes.html b/PasteBin/assets/recent_pastes.html index facc597..1e16ff6 100644 --- a/PasteBin/assets/recent_pastes.html +++ b/PasteBin/assets/recent_pastes.html @@ -7,6 +7,11 @@ recent pastes +

    recent pastes

    @@ -117,5 +122,38 @@ li:hover{ background-color: #333333; border: 2px solid #3a3a3a; } +.nav-div{ + top: 0; + margin-top: 10px; + position: absolute; + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + width: fit-content; + background-color: #181818; + padding-left: 10px; + padding-right: 10px; + color: white; + border-radius: 5px; + height: fit-content; + overflow-y: auto; + border: #222222 2px solid; +} +.nav-div a{ + text-decoration: none; + color: white; + padding: 10px; + margin: 10px; + border-radius: 5px; + background-color: #222222; + transition: 300ms; + border: #2a2a2a00 2px solid; +} +.nav-div a:hover{ + transition: 300ms; + background-color: #333333; + border: #3a3a3a 2px solid; +} \ No newline at end of file