-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from YPermitin/develop
Улучшение операций скачивания, распаковки и очистки файлов ФИАС + реф…
- Loading branch information
Showing
12 changed files
with
598 additions
and
68 deletions.
There are no files selected for viewing
68 changes: 38 additions & 30 deletions
68
Apps/YPermitin.FIASToolSet.DistributionDownloader/Program.cs
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,47 +1,55 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using YPermitin.FIASToolSet.DistributionBrowser; | ||
using YPermitin.FIASToolSet.DistributionBrowser.Enums; | ||
using YPermitin.FIASToolSet.DistributionBrowser.Models; | ||
|
||
Console.WriteLine("Тестовая загрузка полного дистрибутива ФИАС."); | ||
|
||
IFIASDistributionBrowser loader = new FIASDistributionBrowser(); | ||
IConfiguration configuration = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) | ||
.Build(); | ||
|
||
var options = new FIASDistributionBrowserOptions( | ||
workingDirectory: configuration.GetValue("FIASToolSet:WorkingDirectory", string.Empty)); | ||
IFIASDistributionBrowser loader = new FIASDistributionBrowser(options); | ||
var lastInfo = await loader.GetLastDistributionInfo(); | ||
var xmlFullUrl = lastInfo?.GetUrlByFileType(DistributionFileType.GARFIASXmlComplete); | ||
|
||
var tempFileToDownload = Path.Combine( | ||
Path.GetTempPath(), | ||
"FIAS_FULL.zip"); | ||
|
||
DateTime downloadStarted = DateTime.UtcNow; | ||
DateTime lastProgressShow = DateTime.MinValue; | ||
double lastProgressPercentage = 0; | ||
await lastInfo?.DownloadDistributionByFileTypeAsync( | ||
DistributionFileType.GARFIASXmlComplete, | ||
tempFileToDownload, | ||
(args) => | ||
{ | ||
double progress = Math.Round(args.ProgressPercentage, 2); | ||
double progressChanged = lastProgressPercentage - progress; | ||
var showProgressTimeLeft = DateTime.UtcNow - lastProgressShow; | ||
if (args.State != DownloadDistributionFileProgressChangedEventType.Downloading | ||
|| showProgressTimeLeft.TotalSeconds > 10 | ||
|| progressChanged >= 1) | ||
if (lastInfo != null) | ||
{ | ||
await lastInfo.DownloadDistributionByFileTypeAsync( | ||
DistributionFileType.GARFIASXmlComplete, | ||
(args) => | ||
{ | ||
var downloadDuration = DateTime.UtcNow - downloadStarted; | ||
Console.WriteLine($"[{DateTime.UtcNow}] {args.State}. Progress: {progress} %. Seconds left: {downloadDuration.TotalSeconds}"); | ||
if (args.ErrorInfo != null) | ||
double progress = Math.Round(args.ProgressPercentage, 2); | ||
double progressChanged = lastProgressPercentage - progress; | ||
var showProgressTimeLeft = DateTime.UtcNow - lastProgressShow; | ||
if (args.State != DownloadDistributionFileProgressChangedEventType.Downloading | ||
|| showProgressTimeLeft.TotalSeconds > 10 | ||
|| progressChanged >= 1) | ||
{ | ||
Console.WriteLine(args.ErrorInfo.GetBaseException().Message); | ||
} | ||
lastProgressPercentage = progress; | ||
lastProgressShow = DateTime.UtcNow; | ||
lastProgressPercentage = progress; | ||
lastProgressShow = DateTime.UtcNow; | ||
} | ||
}); | ||
var downloadDuration = DateTime.UtcNow - downloadStarted; | ||
Console.WriteLine( | ||
$"[{DateTime.UtcNow}] {args.State}. Progress: {progress} %. Seconds left: {Math.Round(downloadDuration.TotalSeconds, 2)}"); | ||
if (args.ErrorInfo != null) | ||
{ | ||
Console.WriteLine(args.ErrorInfo.GetBaseException().Message); | ||
} | ||
} | ||
}); | ||
|
||
lastInfo.ExtractDistributionFile( | ||
DistributionFileType.GARFIASXmlComplete, | ||
true); | ||
} | ||
|
||
Console.WriteLine("Для выхода нажмите любую клавишу..."); | ||
Console.ReadKey(); |
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
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
2 changes: 1 addition & 1 deletion
2
...SToolSet.DistributionBrowser/ZipHelper.cs → ....DistributionBrowser/Helpers/ZipHelper.cs
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
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
49 changes: 49 additions & 0 deletions
49
Libs/YPermitin.FIASToolSet.DistributionBrowser/Models/FIASDistributionBrowserOptions.cs
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.IO; | ||
|
||
namespace YPermitin.FIASToolSet.DistributionBrowser.Models; | ||
|
||
/// <summary> | ||
/// Параметры обозревателя дистрибутивов ФИАС | ||
/// </summary> | ||
public class FIASDistributionBrowserOptions | ||
{ | ||
public static readonly FIASDistributionBrowserOptions Default; | ||
|
||
static FIASDistributionBrowserOptions() | ||
{ | ||
string workingDirectory = Path.Combine( | ||
Path.GetTempPath(), | ||
"FIASToolSet"); | ||
if (!Directory.Exists(workingDirectory)) | ||
{ | ||
Directory.CreateDirectory(workingDirectory); | ||
} | ||
|
||
Default = new FIASDistributionBrowserOptions( | ||
workingDirectory: workingDirectory); | ||
} | ||
|
||
/// <summary> | ||
/// Рабочий каталог | ||
/// | ||
/// Используется для хранения служебных и временных файлов. | ||
/// </summary> | ||
public readonly string WorkingDirectory; | ||
|
||
/// <summary> | ||
/// Максимальная скороть загрузки дистрибутивов ФИАС (байт в секунду). | ||
/// | ||
/// Значение 0 означает отсутствие ограничений. Используется по умолчанию. | ||
/// </summary> | ||
public readonly long MaximumDownloadSpeedBytesPerSecond; | ||
|
||
public FIASDistributionBrowserOptions( | ||
string workingDirectory = null, | ||
long maximumDownloadSpeedBytesPerSecond = long.MinValue) | ||
{ | ||
workingDirectory ??= Default.WorkingDirectory; | ||
|
||
WorkingDirectory = workingDirectory; | ||
MaximumDownloadSpeedBytesPerSecond = maximumDownloadSpeedBytesPerSecond; | ||
} | ||
} |
Oops, something went wrong.