migration

This commit is contained in:
2026-02-13 21:24:55 +01:00
Unverified
parent 84286c2161
commit 3ae545ea8b
10 changed files with 574 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleToolkit.Extensions
{
public static class HttpClientExtensions
{
public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, CancellationToken cancellationToken = default, IProgress<double> progress = null)
{
using (HttpResponseMessage response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead))
{
long? contentLength = response.Content.Headers.ContentLength;
using (Stream download = await response.Content.ReadAsStreamAsync(cancellationToken))
{
if (progress == null || !contentLength.HasValue)
{
await download.CopyToAsync(destination);
return;
}
var relativeProgress = new Progress<long>(totalBytes => progress.Report((double)totalBytes * 100 / contentLength.Value));
await download.CopyToAsync(destination, 81920, relativeProgress, cancellationToken);
progress.Report(100);
}
}
}
}
}

View File

@@ -0,0 +1,16 @@
namespace SimpleToolkit.Extensions;
public static class IEnumerableExtensions
{
public static T? Random<T>(this IEnumerable<T> enumerable)
{
if (!enumerable.Any())
{
return default;
}
Random random = new Random();
int size = enumerable.Count();
int index = random.Next(size);
return enumerable.ElementAt(index);
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version />
<Title>SimpleToolkit.Extensions</Title>
<Authors>Mateusz Skoczek</Authors>
<Copyright>Mateusz Skoczek</Copyright>
<PackageProjectUrl>https://repos.mateuszskoczek.com/SimpleToolkit/</PackageProjectUrl>
<PackageLicenseUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.Extensions/src/branch/main/LICENSE</PackageLicenseUrl>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.Extensions</RepositoryUrl>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleToolkit.Extensions
{
public static class StreamExtensions
{
public static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<long> progress = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(source);
if (!source.CanRead)
{
throw new ArgumentException("Has to be readable", nameof(source));
}
ArgumentNullException.ThrowIfNull(destination);
if (!destination.CanWrite)
{
throw new ArgumentException("Has to be writable", nameof(destination));
}
ArgumentOutOfRangeException.ThrowIfNegative(bufferSize);
var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead);
}
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleToolkit.Extensions
{
public static class StringExtensions
{
#region STATIC
public static string CreateRandom(int length) => CreateRandom(length, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890`~!@#$%^&*()-_=+[{]};:'\"\\|,<.>/?");
public static string CreateRandom(int length, IEnumerable<char> characters) => new string(Enumerable.Repeat(characters, length).Select(s => s.ElementAt(Random.Shared.Next(s.Count()))).ToArray());
#endregion
#region INSTANCE
public static string Shuffle(this string instance)
{
char[] array = instance.ToCharArray();
Random rng = Random.Shared;
int n = array.Length;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
char value = array[k];
array[k] = array[n];
array[n] = value;
}
return new string(array);
}
#endregion
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB