mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: introduce paste editing api
This commit is contained in:
@@ -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; }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -445,5 +445,83 @@ namespace SharpbinV2.Server.Controllers
|
|||||||
return StatusCode(500, new { error = "An error occurred while changing the paste ID." });
|
return StatusCode(500, new { error = "An error occurred while changing the paste ID." });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPatch("{id}")]
|
||||||
|
[EnableRateLimiting("general")]
|
||||||
|
[Consumes("text/plain")]
|
||||||
|
public async Task<IActionResult> 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." });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using Microsoft.Data.Sqlite;
|
|
||||||
using Microsoft.AspNetCore.RateLimiting;
|
using Microsoft.AspNetCore.RateLimiting;
|
||||||
using SharpbinV2.Server.Models;
|
using Microsoft.Data.Sqlite;
|
||||||
using SharpbinV2.Server.Services;
|
using SharpbinV2.Server.Services;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ namespace SharpbinV2.Server
|
|||||||
public static string MainDatabaseConnection = "Data Source=data.db";
|
public static string MainDatabaseConnection = "Data Source=data.db";
|
||||||
public static long MaxFileSize = 1_048_576; // 1MB
|
public static long MaxFileSize = 1_048_576; // 1MB
|
||||||
public static string[] ValidSyntaxLanguages =
|
public static string[] ValidSyntaxLanguages =
|
||||||
{
|
[
|
||||||
"none",
|
"none",
|
||||||
"autoHotkey",
|
"autoHotkey",
|
||||||
"autoIt",
|
"autoIt",
|
||||||
@@ -36,7 +35,7 @@ namespace SharpbinV2.Server
|
|||||||
"typescript",
|
"typescript",
|
||||||
"toml",
|
"toml",
|
||||||
"xml"
|
"xml"
|
||||||
};
|
];
|
||||||
public static string SHA256Salt = RandomNumberGenerator.GetHexString(32);
|
public static string SHA256Salt = RandomNumberGenerator.GetHexString(32);
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
@@ -155,7 +154,6 @@ namespace SharpbinV2.Server
|
|||||||
//app.UseHttpsRedirection();
|
//app.UseHttpsRedirection();
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
app.UseRateLimiter();
|
app.UseRateLimiter();
|
||||||
|
|
||||||
app.UseCors(builder =>
|
app.UseCors(builder =>
|
||||||
|
|||||||
Reference in New Issue
Block a user