Skip to content

Commit dfe785c

Browse files
author
Meyn
committed
Expose necessary properties.
Version bump
1 parent c82592c commit dfe785c

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

DownloadAssistant/DownloadAssistant.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageProjectUrl></PackageProjectUrl>
1111
<PackageIcon>logo.png</PackageIcon>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
13-
<Version>1.0.2</Version>
13+
<Version>1.0.3</Version>
1414
<GenerateDocumentationFile>True</GenerateDocumentationFile>
1515
<Description>A free to use library as a download manager.
1616
Includes retry, priority, cancel, etc.. function.
@@ -27,7 +27,8 @@ Features:
2727
<Company>Shard</Company>
2828
<Authors>Meyn</Authors>
2929
<PackageReleaseNotes>Implement WriteMode.AppendOrTruncate
30-
Implement RequestFailed to LoadRequest</PackageReleaseNotes>
30+
Implement RequestFailed to LoadRequest
31+
Implement Download Speed Measurement</PackageReleaseNotes>
3132
<RepositoryUrl>https://github.com/TypNull/DownloadAssistant</RepositoryUrl>
3233
</PropertyGroup>
3334

DownloadAssistant/Requests/GetRequest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class GetRequest : WebRequest<GetRequestOptions, string>, IProgressableRe
4242
/// </summary>
4343
public long? PartialContentLength { get; private set; }
4444

45+
/// <summary>
46+
/// Gets the current transfer rate in bytes per second.
47+
/// </summary>
48+
public long CurrentBytesPerSecond => _responseStream?.CurrentBytesPerSecond ?? 0;
49+
4550
/// <summary>
4651
/// Gets the name of the file that should be downloaded.
4752
/// </summary>

DownloadAssistant/Requests/LoadRequest.cs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ public class LoadRequest : WebRequest<LoadRequestOptions, string>, IProgressable
2222
private readonly ChunkHandler _chunkHandler = new();
2323

2424
/// <summary>
25-
/// Represents the current <see cref="IRequest"/> instance.
25+
/// This property exposes the inner <see cref="IRequest"/> instance, which can be either a <see cref="GetRequest"/>
26+
/// or an <see cref="ExtendedContainer{GetRequest}"/> with <see cref="GetRequest"/>'s.
2627
/// </summary>
27-
private IRequest _request = null!;
28+
/// <remarks>
29+
/// <para>
30+
/// <b>WARNING:</b> Modifying this object can lead
31+
/// to unexpected behavior and is not recommended. Incorrect usage can result in data corruption, incomplete downloads,
32+
/// or other issues! For most use cases, use higher-level methods and properties
33+
/// provided by the <see cref="LoadRequest"/> class to avoid potential pitfalls.
34+
/// </para>
35+
/// </remarks>
36+
public IRequest InnerRequest { get; protected set; } = null!;
2837

2938
/// <summary>
3039
/// Specifies the mode of writing to the file.
@@ -42,22 +51,27 @@ public class LoadRequest : WebRequest<LoadRequestOptions, string>, IProgressable
4251
public LoadRange Range => Options.Range;
4352

4453
/// <inheritdoc/>
45-
public override Task Task => _request.Task;
54+
public override Task Task => InnerRequest.Task;
4655

4756
/// <summary>
4857
/// Gets the filename of the content that is being downloaded.
4958
/// </summary>
5059
public string? Filename { get; private set; }
5160

61+
/// <summary>
62+
/// Gets the current transfer rate in bytes per second.
63+
/// </summary>
64+
public long CurrentBytesPerSecond => IsChunked ? _chunkHandler.RequestContainer.Sum(x => x.CurrentBytesPerSecond) : ((GetRequest)InnerRequest).CurrentBytesPerSecond;
65+
5266
/// <summary>
5367
/// Gets the number of bytes that have been written to the temporary file.
5468
/// </summary>
55-
public long BytesWritten => IsChunked ? _chunkHandler.BytesWritten : ((GetRequest)_request).BytesWritten;
69+
public long BytesWritten => IsChunked ? _chunkHandler.BytesWritten : ((GetRequest)InnerRequest).BytesWritten;
5670

5771
/// <summary>
5872
/// Gets the number of bytes that have been downloaded.
5973
/// </summary>
60-
public long BytesDownloaded => IsChunked ? _chunkHandler.BytesDownloaded : ((GetRequest)_request).BytesWritten;
74+
public long BytesDownloaded => IsChunked ? _chunkHandler.BytesDownloaded : ((GetRequest)InnerRequest).BytesWritten;
6175

6276
/// <summary>
6377
/// Gets the <see cref="AggregateException"/> that contains the exceptions thrown during the request.
@@ -66,11 +80,11 @@ public override AggregateException? Exception
6680
{
6781
get
6882
{
69-
if (base.Exception != null && _request.Exception != null)
70-
return new(base.Exception!, _request.Exception!);
83+
if (base.Exception != null && InnerRequest.Exception != null)
84+
return new(base.Exception!, InnerRequest.Exception!);
7185
else if (base.Exception != null)
7286
return base.Exception;
73-
return _request.Exception;
87+
return InnerRequest.Exception;
7488
}
7589
}
7690

@@ -84,12 +98,12 @@ public long ContentLength
8498
if (IsChunked)
8599
return _chunkHandler.Requests.Sum(x => x.PartialContentLength ?? 0);
86100
else
87-
return ((GetRequest)_request).ContentLength;
101+
return ((GetRequest)InnerRequest).ContentLength;
88102
}
89103
}
90104

91105
/// <inheritdoc/>
92-
public override int AttemptCounter => _chunkHandler.Requests.Max(x => x.AttemptCounter);
106+
public override int AttemptCounter => IsChunked ? _chunkHandler.Requests.Max(x => x.AttemptCounter) : ((GetRequest)InnerRequest).AttemptCounter;
93107

94108
/// <summary>
95109
/// Gets a value indicating whether this <see cref="LoadRequest"/> downloads in parts.
@@ -109,12 +123,12 @@ public long ContentLength
109123
/// <summary>
110124
/// Retrieves the progress updates for the download process, enabling real-time monitoring of the download's advancement.
111125
/// </summary>
112-
public Progress<float> Progress => ((IProgressableRequest)_request).Progress;
126+
public Progress<float> Progress => ((IProgressableRequest)InnerRequest).Progress;
113127

114128
/// <summary>
115129
/// Retrieves the speed reporter for the download process, providing real-time metrics on the download speed. This property is only not null when <see cref="LoadRequestOptions.CreateSpeedReporter"/> is true.
116130
/// </summary>
117-
public SpeedReporter<long>? SpeedReporter => ((ISpeedReportable)_request).SpeedReporter;
131+
public SpeedReporter<long>? SpeedReporter => ((ISpeedReportable)InnerRequest).SpeedReporter;
118132

119133
/// <summary>
120134
/// Initializes a new instance of the <see cref="LoadRequest"/> class.
@@ -160,8 +174,8 @@ private void CreateRequest()
160174

161175
if (IsChunked)
162176
{
163-
_request = _chunkHandler.RequestContainer;
164-
_request.StateChanged += OnStateChanged;
177+
InnerRequest = _chunkHandler.RequestContainer;
178+
InnerRequest.StateChanged += OnStateChanged;
165179
_chunkHandler.RequestContainer.SpeedReporter.Timeout = Options.SpeedReporterTimeout;
166180
_chunkHandler.Add(CreateChunk(0, options));
167181

@@ -183,8 +197,8 @@ private void CreateRequest()
183197
RequestCompleated = OnCompletion,
184198
};
185199

186-
_request = new GetRequest(Url, options);
187-
_request.StateChanged += OnStateChanged;
200+
InnerRequest = new GetRequest(Url, options);
201+
InnerRequest.StateChanged += OnStateChanged;
188202
AutoStart();
189203
}
190204

@@ -217,10 +231,10 @@ private GetRequest CreateChunk(int index, GetRequestOptions options)
217231
/// <param name="state">The new state of the request, represented by the <see cref="RequestState"/> enumeration.</param>
218232
private void OnStateChanged(object? sender, RequestState state)
219233
{
220-
if (IsChunked && _request.State == RequestState.Compleated)
234+
if (IsChunked && InnerRequest.State == RequestState.Compleated)
221235
State = RequestState.Running;
222236
else if (State == RequestState.Idle)
223-
State = _request.State;
237+
State = InnerRequest.State;
224238
}
225239

226240
/// <summary>
@@ -344,7 +358,7 @@ private async void CreatePlaceholderFiles(GetRequest request)
344358
IOManager.Create(TempDestination);
345359
break;
346360
case WriteMode.Append:
347-
if (File.Exists(Destination) && new FileInfo(TempDestination).Length > 0)
361+
if (File.Exists(Destination) && new FileInfo(Destination).Length > 0)
348362
{
349363
AddException(new FileLoadException($"The file {Filename} at {Destination} already exists. Please change the WriteMode or delete the file."));
350364
OnFailure(this, null);
@@ -475,7 +489,7 @@ protected override async Task<RequestReturn> RunRequestAsync()
475489
public override void Start()
476490
{
477491
State = RequestState.Idle;
478-
_request.Start();
492+
InnerRequest.Start();
479493
}
480494

481495
/// <summary>
@@ -484,7 +498,7 @@ public override void Start()
484498
public override void Pause()
485499
{
486500
base.Pause();
487-
_request.Pause();
501+
InnerRequest.Pause();
488502
}
489503

490504
/// <summary>
@@ -493,7 +507,7 @@ public override void Pause()
493507
public override void Cancel()
494508
{
495509
base.Cancel();
496-
_request.Cancel();
510+
InnerRequest.Cancel();
497511
}
498512

499513
/// <summary>
@@ -508,7 +522,7 @@ public override void Cancel()
508522
public override void Dispose()
509523
#pragma warning restore CA1816
510524
{
511-
_request.Dispose();
525+
InnerRequest.Dispose();
512526
base.Dispose();
513527
}
514528
}

0 commit comments

Comments
 (0)