mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
refactor: make unnecessary asynchronous methods synchronous
This commit is contained in:
@@ -31,13 +31,13 @@ namespace SharpbinV3.Server.Controllers
|
|||||||
[EnableRateLimiting("Strict")]
|
[EnableRateLimiting("Strict")]
|
||||||
public async Task<IActionResult> CreatePaste(string title = "", string syntax = "plaintext", int visibility = 0, long expiresAt = 0, string? token = null)
|
public async Task<IActionResult> CreatePaste(string title = "", string syntax = "plaintext", int visibility = 0, long expiresAt = 0, string? token = null)
|
||||||
{
|
{
|
||||||
if (!await _pasteService.ValidateExpiresAt(expiresAt))
|
if (!_pasteService.ValidateExpiresAt(expiresAt))
|
||||||
return BadRequest(new { success = false, message = "Invalid expiration time." });
|
return BadRequest(new { success = false, message = "Invalid expiration time." });
|
||||||
if (!await _pasteService.ValidateVisibility(visibility))
|
if (!_pasteService.ValidateVisibility(visibility))
|
||||||
return BadRequest(new { success = false, message = "Invalid visibility level. Must be between 0 and 2." });
|
return BadRequest(new { success = false, message = "Invalid visibility level. Must be between 0 and 2." });
|
||||||
if (!await _pasteService.ValidateSyntax(syntax))
|
if (!_pasteService.ValidateSyntax(syntax))
|
||||||
return BadRequest(new { success = false, message = "Invalid syntax." });
|
return BadRequest(new { success = false, message = "Invalid syntax." });
|
||||||
if (!await _pasteService.ValidateTitle(title))
|
if (!_pasteService.ValidateTitle(title))
|
||||||
return BadRequest(new { success = false, message = $"Invalid title. Must be less than {_pasteSettings.MaxTitleLength} characters." });
|
return BadRequest(new { success = false, message = $"Invalid title. Must be less than {_pasteSettings.MaxTitleLength} characters." });
|
||||||
|
|
||||||
string content = await new StreamReader(Request.Body).ReadToEndAsync();
|
string content = await new StreamReader(Request.Body).ReadToEndAsync();
|
||||||
@@ -185,13 +185,13 @@ namespace SharpbinV3.Server.Controllers
|
|||||||
if (request.ExpiresAt.HasValue)
|
if (request.ExpiresAt.HasValue)
|
||||||
paste.ExpiresAt = request.ExpiresAt.Value;
|
paste.ExpiresAt = request.ExpiresAt.Value;
|
||||||
|
|
||||||
if (!await _pasteService.ValidateExpiresAt(paste.ExpiresAt))
|
if (!_pasteService.ValidateExpiresAt(paste.ExpiresAt))
|
||||||
return BadRequest(new { success = false, message = "Invalid expiration time." });
|
return BadRequest(new { success = false, message = "Invalid expiration time." });
|
||||||
if (!await _pasteService.ValidateVisibility(paste.Visibility))
|
if (!_pasteService.ValidateVisibility(paste.Visibility))
|
||||||
return BadRequest(new { success = false, message = "Invalid visibility level. Must be between 0 and 2." });
|
return BadRequest(new { success = false, message = "Invalid visibility level. Must be between 0 and 2." });
|
||||||
if (!await _pasteService.ValidateSyntax(paste.Syntax ?? "plaintext"))
|
if (!_pasteService.ValidateSyntax(paste.Syntax ?? "plaintext"))
|
||||||
return BadRequest(new { success = false, message = "Invalid syntax." });
|
return BadRequest(new { success = false, message = "Invalid syntax." });
|
||||||
if (!await _pasteService.ValidateTitle(paste.Title ?? ""))
|
if (!_pasteService.ValidateTitle(paste.Title ?? ""))
|
||||||
return BadRequest(new { success = false, message = $"Invalid title. Must be less than {_pasteSettings.MaxTitleLength} characters." });
|
return BadRequest(new { success = false, message = $"Invalid title. Must be less than {_pasteSettings.MaxTitleLength} characters." });
|
||||||
|
|
||||||
var newPaste = await _pasteService.Edit(paste);
|
var newPaste = await _pasteService.Edit(paste);
|
||||||
|
|||||||
@@ -130,23 +130,27 @@ namespace SharpbinV3.Server.Services
|
|||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> ValidateSyntax(string syntax)
|
public bool ValidateSyntax(string syntax)
|
||||||
{
|
{
|
||||||
return await Task.FromResult(_pasteSettings.ValidSyntaxLanguages.Contains(syntax));
|
return _pasteSettings.ValidSyntaxLanguages.Contains(syntax);
|
||||||
}
|
}
|
||||||
public async Task<bool> ValidateTitle(string title)
|
|
||||||
|
public bool ValidateTitle(string title)
|
||||||
{
|
{
|
||||||
return await Task.FromResult(title.Length <= _pasteSettings.MaxTitleLength);
|
return title.Length <= _pasteSettings.MaxTitleLength;
|
||||||
}
|
}
|
||||||
public async Task<bool> ValidateVisibility(int visibility)
|
|
||||||
|
public bool ValidateVisibility(int visibility)
|
||||||
{
|
{
|
||||||
return await Task.FromResult(visibility >= 0 && visibility <= 2);
|
return visibility >= 0 && visibility <= 2;
|
||||||
}
|
}
|
||||||
public async Task<bool> ValidateExpiresAt(long expiresAt)
|
|
||||||
|
public bool ValidateExpiresAt(long expiresAt)
|
||||||
{
|
{
|
||||||
if (expiresAt == 0) return true;
|
if (expiresAt == 0) return true;
|
||||||
var currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
var currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||||
return await Task.FromResult(expiresAt > currentTime - 1000);
|
return expiresAt > currentTime - 1000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user