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:
@@ -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) =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user