diff --git a/SharpbinV2.Server/Classes.cs b/SharpbinV2.Server/Classes.cs deleted file mode 100644 index 5bb236d..0000000 --- a/SharpbinV2.Server/Classes.cs +++ /dev/null @@ -1,66 +0,0 @@ -namespace SharpbinV2.Server -{ - public class User - { - public int? UID { get; set; } - public string? UUID { get; set; } - public int? Type { get; set; } - public string? Email { get; set; } - public string? Username { get; set; } - public string? DisplayName { get; set; } - public string? Password { get; set; } - public long? Created { get; set; } - public long? LastLogin { get; set; } - } - public class Paste - { - public string? UUID { get; set; } - public string? ID { get; set; } - public int? Visibility { get; set; } - public string? Title { get; set; } - public string? AuthorUUID { get; set; } - public string? FilePath { get; set; } - public long? Created { get; set; } - public long? Edited { get; set; } - public int? Size { get; set; } - public int? TrueSize { get; set; } - public int? Views { get; set; } - public string? Syntax { get; set; } - } - public class Session - { - public string? UUID { get; set; } - public string? UserUUID { get; set; } - public string? Token { get; set; } - public long? Created { get; set; } - public long? Expirary { get; set; } - public string? Ip { get; set; } - public string? UserAgent { get; set; } - } - public class RequestDetails - { - public string? Ip { get; set; } - public string? UserAgent { get; set; } - public string? Token { get; set; } - } - public class View - { - public string? UUID { get; set; } - public string? PasteUUID { get; set; } - public string? UserUUID { get; set; } - public string? IP { get; set; } - public string? UserAgent { get; set; } - public long? Created { get; set; } - } - public class PasswordReset - { - public string? UID { get; set; } - public string? UUID { get; set; } - public string? USERUUID { get; set; } - public string? URL { get; set; } - public string? Token { get; set; } - public long? Created { get; set; } - public long? Expirary { get; set; } - } - -} diff --git a/SharpbinV2.Server/Controllers/PasteController.cs b/SharpbinV2.Server/Controllers/PasteController.cs index 8d22b5e..16f2d72 100644 --- a/SharpbinV2.Server/Controllers/PasteController.cs +++ b/SharpbinV2.Server/Controllers/PasteController.cs @@ -445,5 +445,83 @@ namespace SharpbinV2.Server.Controllers return StatusCode(500, new { error = "An error occurred while changing the paste ID." }); } } + + [HttpPatch("{id}")] + [EnableRateLimiting("general")] + [Consumes("text/plain")] + public async Task EditPaste(string id, string title) + { + var requestDetails = HelperService.GetRequestDetails(HttpContext); + if (string.IsNullOrWhiteSpace(requestDetails.Token) || await _databaseService.GetSession(requestDetails.Token) == null) + return Unauthorized(new { success = false, message = "Invalid or missing authentication token." }); + + User? user = await _databaseService.UserFromToken(requestDetails.Token ?? ""); + if (user == null || user.UUID == "0") + return Unauthorized(new { success = false, message = "User not found." }); + if (string.IsNullOrWhiteSpace(id)) + return BadRequest(new { success = false, message = "Invalid paste ID." }); + + var paste = await _databaseService.GetPasteFromID(id); + if (paste == null || string.IsNullOrWhiteSpace(paste.FilePath)) + return NotFound(new { success = false, message = "Paste not found." }); + + if (paste.AuthorUUID != user.UUID && user.Type != 255) + return Unauthorized(new { success = false, message = "You do not have permission to edit this paste." }); + + if (title.Length > 500) + return BadRequest(new { success = false, message = "Title cannot exceed 500 characters." }); + + try + { + string content = await new StreamReader(HttpContext.Request.Body, Encoding.UTF8).ReadToEndAsync(); + if (string.IsNullOrWhiteSpace(content)) + return BadRequest(new { success = false, message = "Content cannot be empty." }); + + if (content.Length > Program.MaxFileSize) + return BadRequest(new { success = false, message = $"Content exceeds maximum size of {Program.MaxFileSize} bytes." }); + + bool shouldCompress = await CompressionService.ShouldCompress(content); + byte[] compressedContent = shouldCompress + ? await CompressionService.CompressString(content) + : Encoding.UTF8.GetBytes(content); + + paste.Edited = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + paste.TrueSize = content.Length; + paste.Size = compressedContent.Length; + paste.FilePath = shouldCompress ? paste.FilePath.Split('.').First() + ".txt.gz" : paste.FilePath.Split('.').First() + ".txt"; + paste.Title = !string.IsNullOrWhiteSpace(title) ? title : paste.Title; + + await IOFile.WriteAllBytesAsync(paste.FilePath, compressedContent); + + Console.WriteLine($"Wrote {content} to {paste.FilePath}"); + + var updatedPaste = await _databaseService.UpdatePaste(paste, _logger); + if (updatedPaste == null) + return StatusCode(500, new { success = false, message = "An error occurred while editing the paste." }); + + return Ok(new + { + success = true, + paste = new + { + updatedPaste.ID, + updatedPaste.UUID, + updatedPaste.Title, + updatedPaste.Syntax, + updatedPaste.Visibility, + updatedPaste.Created, + updatedPaste.Size, + updatedPaste.TrueSize, + updatedPaste.AuthorUUID, + updatedPaste.Edited + } + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error editing paste"); + return StatusCode(500, new { error = "An error occurred while editing the paste." }); + } + } } } diff --git a/SharpbinV2.Server/Program.cs b/SharpbinV2.Server/Program.cs index 83ab0f3..8353bd2 100644 --- a/SharpbinV2.Server/Program.cs +++ b/SharpbinV2.Server/Program.cs @@ -1,6 +1,5 @@ -using Microsoft.Data.Sqlite; using Microsoft.AspNetCore.RateLimiting; -using SharpbinV2.Server.Models; +using Microsoft.Data.Sqlite; using SharpbinV2.Server.Services; using System.Security.Cryptography; @@ -11,7 +10,7 @@ namespace SharpbinV2.Server public static string MainDatabaseConnection = "Data Source=data.db"; public static long MaxFileSize = 1_048_576; // 1MB public static string[] ValidSyntaxLanguages = - { + [ "none", "autoHotkey", "autoIt", @@ -36,7 +35,7 @@ namespace SharpbinV2.Server "typescript", "toml", "xml" - }; + ]; public static string SHA256Salt = RandomNumberGenerator.GetHexString(32); static async Task Main(string[] args) @@ -155,7 +154,6 @@ namespace SharpbinV2.Server //app.UseHttpsRedirection(); app.UseRouting(); app.UseStaticFiles(); - app.UseRateLimiter(); app.UseCors(builder =>