-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor samples to scoped namespaces
- Loading branch information
Showing
5 changed files
with
286 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" : ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// } | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.