From 7221d99b6858c66b3259c9d30276332ca9e6d413 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Mon, 19 Aug 2024 00:35:09 -0400 Subject: [PATCH] archive page has now been created --- PasteBin/Program.cs | 53 ++++++++++++++- PasteBin/assets/archives.html | 123 ++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 PasteBin/assets/archives.html diff --git a/PasteBin/Program.cs b/PasteBin/Program.cs index 3191d0a..4521022 100644 --- a/PasteBin/Program.cs +++ b/PasteBin/Program.cs @@ -168,6 +168,55 @@ class Program return; } }).RequireRateLimiting("fixed"); + app.MapGet("/archive", async (HttpContext context) => + { + var html = File.ReadAllText("assets\\archives.html"); + var page = context.Request?.Query["page"].ToString(); + + int pageNumber; + if (!int.TryParse(page, out pageNumber) || pageNumber <= 0) + { + pageNumber = 1; + } + int pastesPerPage = 50; + int skipCount = (pageNumber - 1) * pastesPerPage; + + using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) + { + await connection.OpenAsync(); + var command = connection.CreateCommand(); + command.CommandText = $"SELECT * FROM pastes WHERE Visibility != 'Private' ORDER BY UID DESC LIMIT {pastesPerPage} OFFSET {skipCount}"; + 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 += $"
  • {paste.Title} {paste.Date} {ConvertToBytes(paste.Size)}
  • "; + } + if (string.IsNullOrEmpty(pastes)) + { + pastes = "

    No pastes found

    "; + } + html = html.Replace("{pastes}", pastes); + html = html.Replace("{backpagenum}", $"{pageNumber - 1}"); + html = html.Replace("{pagenum}", $"{pageNumber + 1}"); + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(html); + return; + } + }); + #endregion @@ -188,14 +237,14 @@ class Program var content = SanitizeInput(data["content"]?.ToString() ?? ""); var visibility = SanitizeInput(data["visibility"]?.ToString() ?? "Public"); - if(visibility != "Public" && visibility != "Unlisted" && visibility != "Private") + 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)) + if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content)) { context.Response.StatusCode = 400; context.Response.ContentType = "application/json"; diff --git a/PasteBin/assets/archives.html b/PasteBin/assets/archives.html new file mode 100644 index 0000000..db6242b --- /dev/null +++ b/PasteBin/assets/archives.html @@ -0,0 +1,123 @@ + + + + + + + archive + + +
    +
    +

    paste archive

    +
    + {pastes} +
    + +
    +
    + + + \ No newline at end of file