mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Convert more apis to controllers
- pastes/id/info - pastes/id - pastes/id/views
This commit is contained in:
@@ -284,6 +284,8 @@ namespace SharpbinV2.Server.Controllers
|
|||||||
return StatusCode(500, "Internal server error");
|
return StatusCode(500, "Internal server error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public class AuthRequest
|
public class AuthRequest
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ using Microsoft.AspNetCore.RateLimiting;
|
|||||||
using SharpbinV2.Server.Services;
|
using SharpbinV2.Server.Services;
|
||||||
using SharpbinV2.Server.Models;
|
using SharpbinV2.Server.Models;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.IO;
|
using IOFile = System.IO.File;
|
||||||
|
using UAParser;
|
||||||
|
|
||||||
namespace SharpbinV2.Server.Controllers
|
namespace SharpbinV2.Server.Controllers
|
||||||
{
|
{
|
||||||
@@ -73,7 +74,7 @@ namespace SharpbinV2.Server.Controllers
|
|||||||
Syntax = syntax.ToLower()
|
Syntax = syntax.ToLower()
|
||||||
};
|
};
|
||||||
|
|
||||||
await System.IO.File.WriteAllBytesAsync(paste.FilePath, compressedContent);
|
await IOFile.WriteAllBytesAsync(paste.FilePath, compressedContent);
|
||||||
bool success = await _databaseService.CreatePaste(paste, _logger);
|
bool success = await _databaseService.CreatePaste(paste, _logger);
|
||||||
|
|
||||||
if (!success)
|
if (!success)
|
||||||
@@ -101,7 +102,134 @@ namespace SharpbinV2.Server.Controllers
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error creating paste");
|
_logger.LogError(ex, "Error creating paste");
|
||||||
return StatusCode(500, new { error = "An error occurred while creating the paste."});
|
return StatusCode(500, new { error = "An error occurred while creating the paste." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
[EnableRateLimiting("general")]
|
||||||
|
public async Task<IActionResult> GetPaste(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
return BadRequest(new { success = false, message = "Invalid paste ID." });
|
||||||
|
Paste? paste = await _databaseService.GetPasteFromID(id);
|
||||||
|
|
||||||
|
if (paste == null || paste.FilePath == null || !IOFile.Exists(paste.FilePath))
|
||||||
|
return NotFound(new { success = false, message = "Paste not found." });
|
||||||
|
|
||||||
|
if (paste.FilePath.EndsWith(".gz"))
|
||||||
|
Response.Headers.Append("Content-Encoding", "gzip");
|
||||||
|
|
||||||
|
Response.Headers.Append("Content-Type", "text/plain; charset=utf-8");
|
||||||
|
Response.Headers.Append("Cache-Control", "public, max-age=3600");
|
||||||
|
|
||||||
|
return PhysicalFile(Path.GetFullPath(paste.FilePath), "text/plain");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error retrieving paste");
|
||||||
|
return StatusCode(500, new { error = "An error occurred while retrieving the paste." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}/info")]
|
||||||
|
[EnableRateLimiting("general")]
|
||||||
|
public async Task<IActionResult> GetPasteInfo(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
return BadRequest(new { success = false, message = "Invalid paste ID." });
|
||||||
|
Paste? paste = await _databaseService.GetPasteFromID(id);
|
||||||
|
if (paste == null || string.IsNullOrWhiteSpace(paste.UUID))
|
||||||
|
return NotFound(new { success = false, message = "Paste not found." });
|
||||||
|
User? author = null;
|
||||||
|
if (!string.IsNullOrWhiteSpace(paste.AuthorUUID))
|
||||||
|
author = await _databaseService.UserFromUUID(paste.AuthorUUID ?? "");
|
||||||
|
return Ok(new
|
||||||
|
{
|
||||||
|
success = true,
|
||||||
|
paste = new
|
||||||
|
{
|
||||||
|
paste.ID,
|
||||||
|
paste.UUID,
|
||||||
|
paste.Title,
|
||||||
|
paste.Syntax,
|
||||||
|
paste.Views,
|
||||||
|
paste.Visibility,
|
||||||
|
paste.Created,
|
||||||
|
paste.Size,
|
||||||
|
paste.TrueSize,
|
||||||
|
paste.AuthorUUID,
|
||||||
|
username = author?.Username ?? "Anonymous",
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error retrieving paste info");
|
||||||
|
return StatusCode(500, new { error = "An error occurred while retrieving the paste info." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}/views")]
|
||||||
|
[EnableRateLimiting("general")]
|
||||||
|
public async Task<IActionResult> GetPasteViews(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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." });
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
return BadRequest(new { success = false, message = "Invalid paste ID." });
|
||||||
|
Paste? paste = await _databaseService.GetPasteFromID(id);
|
||||||
|
User? user = await _databaseService.UserFromToken(requestDetails.Token ?? "");
|
||||||
|
if (paste == null)
|
||||||
|
return NotFound(new { success = false, message = "Paste not found." });
|
||||||
|
if (paste.AuthorUUID != user?.UUID)
|
||||||
|
return Unauthorized(new { success = false, message = "You do not have permission to view this paste's views." });
|
||||||
|
|
||||||
|
List<View?>? views = await _databaseService.GetViewsFromPaste(paste.UUID ?? "", _logger);
|
||||||
|
|
||||||
|
if (views == null || views.Count == 0)
|
||||||
|
return BadRequest(new { success = false, message = "No views found for this paste." });
|
||||||
|
|
||||||
|
var details = new List<object>();
|
||||||
|
|
||||||
|
foreach (var view in views)
|
||||||
|
{
|
||||||
|
if (view == null)
|
||||||
|
continue;
|
||||||
|
var uaParser = Parser.GetDefault();
|
||||||
|
ClientInfo clientInfo = uaParser.Parse(view.UserAgent ?? "");
|
||||||
|
details.Add(new
|
||||||
|
{
|
||||||
|
view.PasteUUID,
|
||||||
|
view.Created,
|
||||||
|
UserAgent = new
|
||||||
|
{
|
||||||
|
Browser = clientInfo.UA.Family,
|
||||||
|
Platform = clientInfo.OS.Family,
|
||||||
|
Device = clientInfo.Device.Family
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(new
|
||||||
|
{
|
||||||
|
success = true,
|
||||||
|
pasteID = paste.ID,
|
||||||
|
pasteUUID = paste.UUID,
|
||||||
|
views = details
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error retrieving paste views");
|
||||||
|
return StatusCode(500, new { error = "An error occurred while retrieving the paste views." });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -563,157 +563,6 @@ namespace SharpbinV2.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
app.MapGet("/api/pastes/{pasteid}/info", async (HttpContext context) =>
|
|
||||||
{
|
|
||||||
var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
|
|
||||||
if (string.IsNullOrEmpty(pasteid))
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Paste ID." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var paste = await Database.GetPasteFromID(pasteid);
|
|
||||||
string authorUsername = null;
|
|
||||||
if (paste == null)
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Paste not found." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (paste.AuthorUUID != "0")
|
|
||||||
{
|
|
||||||
var user = await Database.UserFromUUID(paste.AuthorUUID);
|
|
||||||
authorUsername = user?.Username;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
paste.AuthorUUID = "Anonymous";
|
|
||||||
authorUsername = "Anonymous";
|
|
||||||
}
|
|
||||||
context.Response.StatusCode = 200;
|
|
||||||
await context.Response.WriteAsJsonAsync(new
|
|
||||||
{
|
|
||||||
success = true,
|
|
||||||
paste = new
|
|
||||||
{
|
|
||||||
paste.UUID,
|
|
||||||
paste.ID,
|
|
||||||
paste.Visibility,
|
|
||||||
paste.Title,
|
|
||||||
paste.AuthorUUID,
|
|
||||||
username = authorUsername,
|
|
||||||
paste.Created,
|
|
||||||
paste.Edited,
|
|
||||||
paste.Size,
|
|
||||||
paste.TrueSize,
|
|
||||||
paste.Views,
|
|
||||||
paste.Syntax
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}).RequireRateLimiting("general");
|
|
||||||
app.MapGet("/api/pastes/{pasteid}", async (HttpContext context) =>
|
|
||||||
{
|
|
||||||
var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
|
|
||||||
if (string.IsNullOrEmpty(pasteid))
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Paste ID." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var paste = await Database.GetPasteFromID(pasteid);
|
|
||||||
context.Response.StatusCode = 200;
|
|
||||||
context.Response.Headers.Add("Content-Type", "text/plain");
|
|
||||||
if (paste.FilePath.EndsWith(".gz"))
|
|
||||||
{
|
|
||||||
context.Response.Headers.Add("Content-Encoding", "gzip");
|
|
||||||
await context.Response.SendFileAsync(paste.FilePath);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
await context.Response.SendFileAsync(paste.FilePath);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}).RequireRateLimiting("general");
|
|
||||||
app.MapGet("/api/pastes/{pasteid}/views", async (HttpContext context) =>
|
|
||||||
{
|
|
||||||
var requestdetails = GetRequestDetails(context);
|
|
||||||
var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
|
|
||||||
if (string.IsNullOrEmpty(pasteid))
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Paste ID." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (string.IsNullOrEmpty(requestdetails.Token))
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Not authorized. Please provide a token." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var user = await Database.UserFromToken(requestdetails.Token);
|
|
||||||
if (user == null)
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid token." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var paste = await Database.GetPasteFromID(pasteid);
|
|
||||||
if (paste == null)
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Paste not found." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (paste.AuthorUUID != user.UUID)
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "You are not the author of this paste." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
|
||||||
{
|
|
||||||
await connection.OpenAsync();
|
|
||||||
using (var command = connection.CreateCommand())
|
|
||||||
{
|
|
||||||
command.CommandText = "SELECT * FROM views WHERE PasteUUID = @PasteUUID;";
|
|
||||||
command.Parameters.AddWithValue("@PasteUUID", paste.UUID);
|
|
||||||
using (var reader = await command.ExecuteReaderAsync())
|
|
||||||
{
|
|
||||||
if (!reader.HasRows)
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 400;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "No views found." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var views = new List<object>();
|
|
||||||
var uaparser = Parser.GetDefault();
|
|
||||||
int viewcount = 0;
|
|
||||||
|
|
||||||
while (await reader.ReadAsync())
|
|
||||||
{
|
|
||||||
viewcount++;
|
|
||||||
string useragent = reader.GetString(3);
|
|
||||||
var clientInfo = uaparser.Parse(useragent);
|
|
||||||
views.Add(new
|
|
||||||
{
|
|
||||||
PasteUUID = reader.GetString(1),
|
|
||||||
Created = reader.GetInt64(4),
|
|
||||||
UserAgent = new
|
|
||||||
{
|
|
||||||
browser = clientInfo.UA.Family,
|
|
||||||
platform = clientInfo.OS.Family,
|
|
||||||
device = clientInfo.Device.Family,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
context.Response.StatusCode = 200;
|
|
||||||
await context.Response.WriteAsJsonAsync(new { success = true, views, viewcount });
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}).RequireRateLimiting("general");
|
|
||||||
|
|
||||||
app.MapGet("/api/users/uuid/{uuid}", async (HttpContext context) =>
|
app.MapGet("/api/users/uuid/{uuid}", async (HttpContext context) =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -529,5 +529,45 @@ namespace SharpbinV2.Server.Services
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public async Task<List<View?>?> GetViewsFromPaste(string uuid, ILogger logger)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(uuid))
|
||||||
|
return null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection(_connectionString))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM views WHERE PasteUUID = @PasteUUID;";
|
||||||
|
command.Parameters.AddWithValue("@PasteUUID", uuid);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
if (!reader.HasRows)
|
||||||
|
return null;
|
||||||
|
var views = new List<View>();
|
||||||
|
while (await reader.ReadAsync())
|
||||||
|
{
|
||||||
|
views.Add(new View
|
||||||
|
{
|
||||||
|
UserUUID = reader.GetString(0),
|
||||||
|
PasteUUID = reader.GetString(1),
|
||||||
|
IP = reader.GetString(2),
|
||||||
|
UserAgent = reader.GetString(3),
|
||||||
|
Created = reader.GetInt64(4)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return views;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "Failed to get views from paste in database.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user