Skip to content

Commit

Permalink
refactor samples to scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Nov 18, 2023
1 parent 554b5fe commit cca2cbd
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 282 deletions.
13 changes: 6 additions & 7 deletions src/Samples/Downloader.Sample/DownloadItem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace Downloader.Sample
namespace Downloader.Sample;

public class DownloadItem
{
public class DownloadItem
{
public string FolderPath { get; set; }
public string FileName { get; set; }
public string Url { get; set; }
}
public string FolderPath { get; set; }
public string FileName { get; set; }
public string Url { get; set; }
}
1 change: 1 addition & 0 deletions src/Samples/Downloader.Sample/Downloader.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>latestMajor</LangVersion>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net6.0;</TargetFrameworks>
</PropertyGroup>
Expand Down
99 changes: 49 additions & 50 deletions src/Samples/Downloader.Sample/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
using System;

namespace Downloader.Sample
namespace Downloader.Sample;

public static class Helper
{
public static class Helper
public static string CalcMemoryMensurableUnit(this long bytes)
{
public static string CalcMemoryMensurableUnit(this long bytes)
{
return CalcMemoryMensurableUnit((double)bytes);
}
return CalcMemoryMensurableUnit((double)bytes);
}

public static string CalcMemoryMensurableUnit(this double bytes)
public static string CalcMemoryMensurableUnit(this double bytes)
{
double kb = bytes / 1024; // · 1024 Bytes = 1 Kilobyte
double mb = kb / 1024; // · 1024 Kilobytes = 1 Megabyte
double gb = mb / 1024; // · 1024 Megabytes = 1 Gigabyte
double tb = gb / 1024; // · 1024 Gigabytes = 1 Terabyte

string result =
tb > 1 ? $"{tb:0.##}TB" :
gb > 1 ? $"{gb:0.##}GB" :
mb > 1 ? $"{mb:0.##}MB" :
kb > 1 ? $"{kb:0.##}KB" :
$"{bytes:0.##}B";

result = result.Replace("/", ".");
return result;
}

public static void UpdateTitleInfo(this DownloadProgressChangedEventArgs e, bool isPaused)
{
int estimateTime = (int)Math.Ceiling((e.TotalBytesToReceive - e.ReceivedBytesSize) / e.AverageBytesPerSecondSpeed);
string timeLeftUnit = "seconds";

if (estimateTime >= 60) // isMinutes
{
double kb = bytes / 1024; // · 1024 Bytes = 1 Kilobyte
double mb = kb / 1024; // · 1024 Kilobytes = 1 Megabyte
double gb = mb / 1024; // · 1024 Megabytes = 1 Gigabyte
double tb = gb / 1024; // · 1024 Gigabytes = 1 Terabyte

string result =
tb > 1 ? $"{tb:0.##}TB" :
gb > 1 ? $"{gb:0.##}GB" :
mb > 1 ? $"{mb:0.##}MB" :
kb > 1 ? $"{kb:0.##}KB" :
$"{bytes:0.##}B";

result = result.Replace("/", ".");
return result;
timeLeftUnit = "minutes";
estimateTime /= 60;
}

public static void UpdateTitleInfo(this DownloadProgressChangedEventArgs e, bool isPaused)
else if (estimateTime < 0)
{
int estimateTime = (int)Math.Ceiling((e.TotalBytesToReceive - e.ReceivedBytesSize) / e.AverageBytesPerSecondSpeed);
string timeLeftUnit = "seconds";

if (estimateTime >= 60) // isMinutes
{
timeLeftUnit = "minutes";
estimateTime /= 60;
}
else if (estimateTime < 0)
{
estimateTime = 0;
}

string avgSpeed = e.AverageBytesPerSecondSpeed.CalcMemoryMensurableUnit();
string speed = e.BytesPerSecondSpeed.CalcMemoryMensurableUnit();
string bytesReceived = e.ReceivedBytesSize.CalcMemoryMensurableUnit();
string totalBytesToReceive = e.TotalBytesToReceive.CalcMemoryMensurableUnit();
string progressPercentage = $"{e.ProgressPercentage:F3}".Replace("/", ".");
string usedMemory = GC.GetTotalMemory(false).CalcMemoryMensurableUnit();

Console.Title = $"{estimateTime} {timeLeftUnit} left - " +
$"{speed}/s (avg: {avgSpeed}/s) - " +
$"{progressPercentage}% - " +
$"[{bytesReceived} of {totalBytesToReceive}] " +
$"Active Chunks: {e.ActiveChunks} - " +
$"[{usedMemory} memory] " +
(isPaused ? " - Paused" : "");
estimateTime = 0;
}

string avgSpeed = e.AverageBytesPerSecondSpeed.CalcMemoryMensurableUnit();
string speed = e.BytesPerSecondSpeed.CalcMemoryMensurableUnit();
string bytesReceived = e.ReceivedBytesSize.CalcMemoryMensurableUnit();
string totalBytesToReceive = e.TotalBytesToReceive.CalcMemoryMensurableUnit();
string progressPercentage = $"{e.ProgressPercentage:F3}".Replace("/", ".");
string usedMemory = GC.GetTotalMemory(false).CalcMemoryMensurableUnit();

Console.Title = $"{estimateTime} {timeLeftUnit} left - " +
$"{speed}/s (avg: {avgSpeed}/s) - " +
$"{progressPercentage}% - " +
$"[{bytesReceived} of {totalBytesToReceive}] " +
$"Active Chunks: {e.ActiveChunks} - " +
$"[{usedMemory} memory] " +
(isPaused ? " - Paused" : "");
}
}
49 changes: 49 additions & 0 deletions src/Samples/Downloader.Sample/Program.Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Net;
using System.Reflection;

namespace Downloader.Sample;

public partial class Program
{
private static DownloadConfiguration GetDownloadConfiguration()
{
var cookies = new CookieContainer();
cookies.Add(new Cookie("download-type", "test") { Domain = "domain.com" });

return new DownloadConfiguration {
BufferBlockSize = 10240, // usually, hosts support max to 8000 bytes, default values is 8000
ChunkCount = 8, // file parts to download, default value is 1
MaximumBytesPerSecond = 0, // download speed limited to 10MB/s, default values is zero or unlimited
MaxTryAgainOnFailover = 5, // the maximum number of times to fail
MaximumMemoryBufferBytes = 1024 * 1024 * 1024, // release memory buffer after each 1GB
ParallelDownload = true, // download parts of file as parallel or not. Default value is false
ParallelCount = 16, // number of parallel downloads. The default value is the same as the chunk count
Timeout = 3000, // timeout (millisecond) per stream block reader, default value is 1000
RangeDownload = false, // set true if you want to download just a specific range of bytes of a large file
RangeLow = 0, // floor offset of download range of a large file
RangeHigh = 0, // ceiling offset of download range of a large file
ClearPackageOnCompletionWithFailure = true, // Clear package and downloaded data when download completed with failure, default value is false
MinimumSizeOfChunking = 1024, // minimum size of chunking to download a file in multiple parts, default value is 512
ReserveStorageSpaceBeforeStartingDownload = true, // Before starting the download, reserve the storage space of the file as file size, default value is false
RequestConfiguration =
{
// config and customize request headers
Accept = "*/*",
CookieContainer = cookies,
Headers = new WebHeaderCollection(), // { your custom headers }
KeepAlive = true, // default value is false
ProtocolVersion = HttpVersion.Version11, // default value is HTTP 1.1
UseDefaultCredentials = false,
// your custom user agent or your_app_name/app_version.
UserAgent = $"DownloaderSample/{Assembly.GetExecutingAssembly().GetName().Version?.ToString(3)}"
// Proxy = new WebProxy(new Uri($"socks5://127.0.0.1:9050"))
// Proxy = new WebProxy() {
// Address = new Uri("http://YourProxyServer/proxy.pac"),
// UseDefaultCredentials = false,
// Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
// BypassProxyOnLocal = true
// }
}
};
}
}
Loading

0 comments on commit cca2cbd

Please sign in to comment.