Enhance user authentication and paste management

- Updated `AuthController.cs` to include token in `CreateSession` and improved validation for username and email.
- Refactored paste creation logic from `Program.cs` to a new `PasteController.cs`.
- Added email validation method in `HelperService.cs`.
- Introduced `CompressionService.cs` for handling string compression and decompression.
- Improved user experience with autocomplete and spellcheck in HTML forms.
- Enhanced date display in `archive.js` and refactored date formatting in `dash.js`.
- Updated API endpoints in `login.js` and `register.js` to reflect changes.
This commit is contained in:
whoswhip
2025-07-06 22:55:29 -04:00
parent cc0c6a8286
commit 3eb7374a07
16 changed files with 312 additions and 176 deletions
-91
View File
@@ -412,97 +412,6 @@ namespace SharpbinV2.Server
}).RequireRateLimiting("auth");
app.MapPost("/api/pastes/create", async (HttpContext context) =>
{
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
if (body == null)
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Body." });
return;
}
if (body.Length > MaxFileSize)
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "File size too large." });
return;
}
var paste = new Paste();
var requestdetails = GetRequestDetails(context);
var user = await Database.UserFromToken(requestdetails.Token);
var queries = context.Request.Query;
if (queries.ContainsKey("title") && queries["title"].ToString().Length > 500)
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "Title too long." });
return;
}
paste.Created = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
paste.Size = body.Length;
paste.TrueSize = body.Length;
paste.UUID = Guid.NewGuid().ToString();
paste.ID = GenerateRandomString(12);
paste.AuthorUUID = user?.UUID ?? "0"; // 0 means the user is anonymous
paste.FilePath = $"pastes/{paste.ID}.txt";
paste.Visibility = queries.ContainsKey("visibility") ? Convert.ToInt32(queries["visibility"]) : 0;
paste.Title = queries.ContainsKey("title") && !string.IsNullOrEmpty(queries["title"].ToString()) ? queries["title"].ToString() : $"Untitled-{await Database.EnumeratePastes()}";
paste.Edited = 0;
paste.Views = 0;
paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null;
if (!string.IsNullOrEmpty(paste.Syntax) && !ValidSyntaxLanguages.Contains(paste.Syntax.ToLower()))
{
context.Response.StatusCode = 400;
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid syntax language." });
return;
}
bool shouldCompress = await Compression.ShouldCompress(body);
if (shouldCompress)
{
var compressed = await Compression.CompressString(body);
paste.Size = compressed.Length;
paste.TrueSize = body.Length;
paste.FilePath += ".gz";
await File.WriteAllBytesAsync(paste.FilePath, compressed);
}
else
{
await File.WriteAllTextAsync(paste.FilePath, body);
}
using (var connection = new SqliteConnection(MainDatabaseConnection))
{
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO pastes (UUID, ID, Visibility, Title, AuthorUUID, FilePath, Created, Edited, Size, TrueSize, Views, Syntax) VALUES (@UUID, @ID, @Visibility, @Title, @AuthorUUID, @FilePath, @Created, @Edited, @Size, @TrueSize, @Views, @Syntax);";
command.Parameters.AddWithValue("@UUID", paste.UUID);
command.Parameters.AddWithValue("@ID", paste.ID);
command.Parameters.AddWithValue("@Visibility", paste.Visibility);
command.Parameters.AddWithValue("@Title", paste.Title);
command.Parameters.AddWithValue("@AuthorUUID", paste.AuthorUUID);
command.Parameters.AddWithValue("@FilePath", paste.FilePath);
command.Parameters.AddWithValue("@Created", paste.Created);
command.Parameters.AddWithValue("@Edited", paste.Edited);
command.Parameters.AddWithValue("@Size", paste.Size);
command.Parameters.AddWithValue("@TrueSize", paste.TrueSize);
command.Parameters.AddWithValue("@Views", paste.Views);
command.Parameters.AddWithValue("@Syntax", paste.Syntax);
await command.ExecuteNonQueryAsync();
}
await connection.CloseAsync();
}
paste.FilePath = null;
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { success = true, message = "Paste created.", paste = paste });
logger.LogInfo($"A Paste of {FormatBytes(paste.Size ?? 0)} was created by {paste.UUID}");
return;
}).RequireRateLimiting("uploads");
app.MapGet("/api/pastes/archive", async (HttpContext context) =>
{
var queries = context.Request.Query;