From 0e2aa78b5ca3b007e972c2279b1b10a5581b72f1 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Thu, 5 Sep 2024 18:07:30 -0400 Subject: [PATCH] Change Log 9/5/2024 - Search feature added to archives --- PasteBin/Program.cs | 77 ++++++++ PasteBin/assets/archives.html | 338 ++++++++++++++++++++-------------- 2 files changed, 279 insertions(+), 136 deletions(-) diff --git a/PasteBin/Program.cs b/PasteBin/Program.cs index 85100bd..6dc077d 100644 --- a/PasteBin/Program.cs +++ b/PasteBin/Program.cs @@ -1175,6 +1175,83 @@ class Program await context.Response.WriteAsJsonAsync(new JObject { ["message"] = "Paste deleted" }.ToString()); } + }).RequireRateLimiting("fixed"); + app.MapPost("/api/pastes/search", async (HttpContext context) => + { + var log = await Logging.LogRequestAsync(context); + var parameters = context.Request.Query; + string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); + if (string.IsNullOrEmpty(requestBody)) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No data uploaded" }.ToString()); + return; + } + var data = JObject.Parse(requestBody); + var query = SanitizeInput(data["query"]?.ToString() ?? ""); + Console.WriteLine($"erm: {query}"); + if (string.IsNullOrEmpty(query) || string.IsNullOrWhiteSpace(query)) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid request body" }.ToString()); + return; + } + + using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) + { + await connection.OpenAsync(); + var command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM pastes WHERE Title LIKE @Query"; + command.Parameters.AddWithValue("@Query", $"%{query}%"); + var reader = await command.ExecuteReaderAsync(); + JArray pastes = new JArray(); + string pastesHTML = string.Empty; + while (await reader.ReadAsync()) + { + var paste = new Paste + { + Title = reader.GetString(1), + UnixDate = long.Parse(reader.GetString(2)), + Size = reader.GetString(3), + Visibility = reader.GetString(4), + Id = reader.GetString(5), + }; + var difference = GetTimeDifference(DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0), DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.Now.ToUnixTimeSeconds())); + if (paste.Visibility == "Private" || paste.Visibility == "Unlisted") + { + continue; + } + if (string.IsNullOrEmpty(parameters["html"])) + { + JObject pasteJson = new JObject(); + pasteJson["title"] = paste.Title; + pasteJson["id"] = paste.Id; + pasteJson["size"] = ConvertToBytes(paste.Size); + pasteJson["date"] = difference; + pasteJson["visibility"] = paste.Visibility; + pastes.Add(pasteJson); + } + else + { + pastesHTML += $"
  • {paste.Title} {difference}
  • "; + } + + } + context.Response.StatusCode = 200; + if (string.IsNullOrEmpty(parameters["html"])) + { + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(pastes.ToString()); + } + else + { + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(pastesHTML); + } + } + }).RequireRateLimiting("fixed"); app.MapPost("/api/accounts/create", async (HttpContext context) => { diff --git a/PasteBin/assets/archives.html b/PasteBin/assets/archives.html index a55db69..019afe8 100644 --- a/PasteBin/assets/archives.html +++ b/PasteBin/assets/archives.html @@ -1,11 +1,13 @@ + archive + -
    +

    paste archive

    +
    {pastes}
    - < + <

    {currentpagenum}/{totalpages}

    >
    + + \ No newline at end of file