Skip to content

Commit

Permalink
Merge pull request #142 from bezzad/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
bezzad authored Jun 6, 2023
2 parents f97c9d7 + e97cbea commit 03e72c9
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 25 deletions.
14 changes: 9 additions & 5 deletions .licrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# IMPORTANT! ALL SECTIONS ARE MANDATORY
# IMPORTANT!: ALL SECTIONS ARE MANDATORY
[licenses]
# This indicates which are the only licenses that Licensebat will accept.
# The rest will be flagged as not allowed.
accepted = [MIT, MSC, BSD]
accepted = ["MIT", "MSC", "BSD"]
# This will indicate which licenses are not accepted.
# The rest will be accepted, except for the unknown licenses or dependencies without licenses.
# unaccepted = [LGPL]
# unaccepted = ["LGPL"]
# Note that only one of the previous options can be enabled at once.
# If both of them are informed, only accepted will be considered.

[dependencies]
# This will allow users to flag some dependencies so that Licensebat will not check for their license.
# ignored=[ignored_dep1, ignored_dep2]
ignored=["ignored_dep1", "ignored_dep2"]
# If set to true, Licensebat will ignore the dev dependencies.
ignore_dev_dependencies = true
# If set to true, Licensebat will ignore the optional dependencies.
ignore_optional_dependencies = true

[behavior]
# False by default, if true, it will only run the checks when one of the dependency files or the .licrc file has been modified.
run_only_on_dependency_modification = true
# False by default, if true, it will never block the build.
do_not_block_pr = true
do_not_block_pr = false
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[![Generic badge](https://img.shields.io/badge/support-.Net_Framework-blue.svg)](https://github.com/bezzad/Downloader)
[![Generic badge](https://img.shields.io/badge/support-.Net_Core-blue.svg)](https://github.com/bezzad/Downloader)
[![Generic badge](https://img.shields.io/badge/support-.Net_Standard-blue.svg)](https://github.com/bezzad/Downloader)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fbezzad%2FDownloader.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fbezzad%2FDownloader?ref=badge_shield)

# Downloader

Expand Down
33 changes: 33 additions & 0 deletions src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,5 +692,38 @@ public async Task TestStopDownloadWithCancellationToken()
Assert.IsTrue(downloader.Status == DownloadStatus.Stopped);
Assert.IsTrue(downloadProgress > 10);
}

[TestMethod]
public async Task TestResumeDownloadWithAnotherUrl()
{
// arrange
var url1 = DummyFileHelper.GetFileWithNameUrl("file1.dat", DummyFileHelper.FileSize16Kb);
var url2 = DummyFileHelper.GetFileWithNameUrl("file2.dat", DummyFileHelper.FileSize16Kb);
var canStopDownload = true;
var totalDownloadSize = 0L;
var config = (DownloadConfiguration)Config.Clone();
config.BufferBlockSize = 1024;
config.ChunkCount = 4;
var downloader = new DownloadService(config);
downloader.DownloadProgressChanged += (s, e) => {
totalDownloadSize = e.ReceivedBytesSize;
if (canStopDownload && totalDownloadSize > DummyFileHelper.FileSize16Kb / 2)
{
// Stopping after start of downloading
downloader.CancelAsync();
canStopDownload = false;
}
};

// act
await downloader.DownloadFileTaskAsync(url1).ConfigureAwait(false);
await downloader.DownloadFileTaskAsync(downloader.Package, url2); // resume download with new url2.

// assert
Assert.AreEqual(DummyFileHelper.FileSize16Kb, downloader.Package.TotalFileSize);
Assert.AreEqual(DummyFileHelper.FileSize16Kb, totalDownloadSize);
Assert.AreEqual(downloader.Package.Storage.Length, DummyFileHelper.FileSize16Kb);
Assert.AreEqual(100.0, downloader.Package.SaveProgress);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ public async Task TestSerializePackageAfterCancel(bool onMemory)
Cleanup();
}


[TestMethod]
public async Task TestResumeFromSerializedPackageOnMemory()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader.Test/UnitTests/DownloadBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void TestPackageWhenResume()
Address = url,
IsSupportDownloadInRange = true
};
IDownload download = DownloadBuilder.Build(package);
IDownload download = DownloadBuilder.New().Build(package);
DownloadPackage beforeStartPackage = download.Package;

// act
Expand Down
13 changes: 12 additions & 1 deletion src/Downloader/Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public Download(DownloadPackage package, DownloadConfiguration configuration)
Package = package;
}

public Download(DownloadPackage package, string address, DownloadConfiguration configuration)
{
downloadService = new DownloadService(configuration);
Package = package;
Url = address;
}

public async Task<Stream> StartAsync(CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(Package?.Address))
Expand All @@ -76,10 +83,14 @@ public async Task<Stream> StartAsync(CancellationToken cancellationToken = defau
return null;
}
}
else
else if(string.IsNullOrWhiteSpace(Url))
{
return await downloadService.DownloadFileTaskAsync(Package, cancellationToken);
}
else
{
return await downloadService.DownloadFileTaskAsync(Package, Url, cancellationToken);
}
}

public void Stop()
Expand Down
25 changes: 11 additions & 14 deletions src/Downloader/DownloadBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,9 @@ public class DownloadBuilder

public static DownloadBuilder New()
{
DownloadBuilder builder = new();
return builder;
return new DownloadBuilder();
}

public static IDownload Build(DownloadPackage package)
{
return Build(package, new DownloadConfiguration());
}

public static IDownload Build(DownloadPackage package, DownloadConfiguration downloadConfiguration)
{
return new Download(package, downloadConfiguration);
}

private DownloadBuilder() { }

public DownloadBuilder WithUrl(string url)
{
this.url = url;
Expand Down Expand Up @@ -101,5 +88,15 @@ public IDownload Build()

return new Download(url, directoryPath, filename, downloadConfiguration);
}

public IDownload Build(DownloadPackage package)
{
return new Download(package, url, downloadConfiguration);
}

public IDownload Build(DownloadPackage package, DownloadConfiguration downloadConfiguration)
{
return new Download(package, url, downloadConfiguration);
}
}
}
7 changes: 7 additions & 0 deletions src/Downloader/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public async Task<Stream> DownloadFileTaskAsync(DownloadPackage package, Cancell
return await StartDownload().ConfigureAwait(false);
}

public async Task<Stream> DownloadFileTaskAsync(DownloadPackage package, string address, CancellationToken cancellationToken = default)
{
Package = package;
await InitialDownloader(address, cancellationToken);
return await StartDownload().ConfigureAwait(false);
}

public async Task<Stream> DownloadFileTaskAsync(string address, CancellationToken cancellationToken = default)
{
await InitialDownloader(address, cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions src/Downloader/Downloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net452;net6.0;</TargetFrameworks>
<LangVersion>latestMajor</LangVersion>
<Version>3.0.5</Version>
<Version>3.0.6</Version>
<Title>Downloader</Title>
<Authors>Behzad Khosravifar</Authors>
<Company>bezzad</Company>
<Description>Fast and reliable multipart downloader with asynchronous progress events for .NET</Description>
<PackageProjectUrl>https://github.com/bezzad/Downloader</PackageProjectUrl>
<RepositoryUrl>https://github.com/bezzad/Downloader</RepositoryUrl>
<PackageTags>download-manager, downloader, download, idm, internet, streaming, download-file, stream-downloader, multipart-download</PackageTags>
<PackageReleaseNotes>added memory buffering limitation to prevent memory performance issue #126</PackageReleaseNotes>
<PackageReleaseNotes>resume downloads with a different URL #136</PackageReleaseNotes>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Downloader.snk</AssemblyOriginatorKeyFile>
<Copyright>Copyright (C) 2019-2023 Behzad Khosravifar</Copyright>
Expand Down
1 change: 1 addition & 0 deletions src/Downloader/IDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface IDownloadService
event EventHandler<DownloadStartedEventArgs> DownloadStarted;

Task<Stream> DownloadFileTaskAsync(DownloadPackage package, CancellationToken cancellationToken = default);
Task<Stream> DownloadFileTaskAsync(DownloadPackage package, string address, CancellationToken cancellationToken = default);
Task<Stream> DownloadFileTaskAsync(string address, CancellationToken cancellationToken = default);
Task DownloadFileTaskAsync(string address, string fileName, CancellationToken cancellationToken = default);
Task DownloadFileTaskAsync(string address, DirectoryInfo folder, CancellationToken cancellationToken = default);
Expand Down

0 comments on commit 03e72c9

Please sign in to comment.