Refactor for async operations and add new features

Updated Compression.cs to support asynchronous operations, including new methods for decompressing byte arrays and checking compression status.
Enhanced Logging.cs with asynchronous logging methods.
Refactored Program.cs to replace synchronous calls with asynchronous ones, added a new endpoint for editing pastes, and implemented rate limiting.
Removed outdated logging configuration.
This commit is contained in:
whoswhip
2024-10-19 00:05:59 -04:00
parent c28b9b92b1
commit bdcc3a1dc6
5 changed files with 403 additions and 117 deletions
+26 -4
View File
@@ -1,11 +1,12 @@
using System.IO.Compression;
using System.Text;
using System.Threading.Tasks;
namespace Sharpbin
{
public class Compression
{
public static byte[] CompressString(string text)
public static async Task<byte[]> CompressString(string text)
{
byte[] compressedBytes;
using (var ms = new MemoryStream())
@@ -13,13 +14,14 @@ namespace Sharpbin
using (var cs = new GZipStream(ms, CompressionMode.Compress))
{
byte[] inputBytes = Encoding.UTF8.GetBytes(text);
cs.Write(inputBytes, 0, inputBytes.Length);
await cs.WriteAsync(inputBytes, 0, inputBytes.Length);
}
compressedBytes = ms.ToArray();
}
return compressedBytes;
}
public static string DecompressString(byte[] compressedBytes)
public static async Task<string> DecompressByteArrayToString(byte[] compressedBytes)
{
string decompressedText;
using (var ms = new MemoryStream(compressedBytes))
@@ -28,13 +30,33 @@ namespace Sharpbin
{
using (var sr = new StreamReader(cs))
{
decompressedText = sr.ReadToEnd();
decompressedText = await sr.ReadToEndAsync();
}
}
}
return decompressedText;
}
public static async Task<byte[]> DecompressByteArray(byte[] compressedBytes)
{
byte[] decompressedBytes;
using (var ms = new MemoryStream(compressedBytes))
{
using (var cs = new GZipStream(ms, CompressionMode.Decompress))
{
using (var msDecompressed = new MemoryStream())
{
await cs.CopyToAsync(msDecompressed);
decompressedBytes = msDecompressed.ToArray();
}
}
}
return decompressedBytes;
}
public static bool IsCompressed(byte[] data)
{
return data.Length >= 2 && data[0] == 0x1F && data[1] == 0x8B;
}
}
}