migration
This commit is contained in:
32
SimpleToolkit.Extensions/HttpClientExtensions.cs
Normal file
32
SimpleToolkit.Extensions/HttpClientExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
SimpleToolkit.Extensions/IEnumerableExtensions.cs
Normal file
16
SimpleToolkit.Extensions/IEnumerableExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
17
SimpleToolkit.Extensions/SimpleToolkit.Extensions.csproj
Normal file
17
SimpleToolkit.Extensions/SimpleToolkit.Extensions.csproj
Normal 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>
|
||||
36
SimpleToolkit.Extensions/StreamExtensions.cs
Normal file
36
SimpleToolkit.Extensions/StreamExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
SimpleToolkit.Extensions/StringExtensions.cs
Normal file
40
SimpleToolkit.Extensions/StringExtensions.cs
Normal 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
|
||||
}
|
||||
}
|
||||
BIN
SimpleToolkit.Extensions/icon.png
Normal file
BIN
SimpleToolkit.Extensions/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.7 KiB |
Reference in New Issue
Block a user