diff --git a/src/FilterLists.Agent/Features/Archiver/DownloadLists.cs b/src/FilterLists.Agent/Features/Archiver/DownloadLists.cs index 60f02e949..6b227f354 100644 --- a/src/FilterLists.Agent/Features/Archiver/DownloadLists.cs +++ b/src/FilterLists.Agent/Features/Archiver/DownloadLists.cs @@ -53,8 +53,8 @@ private static readonly Dictionary> CommandsByE {".p2p", l => new DownloadRawText.Command(l)}, {".php", l => new DownloadRawText.Command(l)}, {".tpl", l => new DownloadRawText.Command(l)}, - {".txt", l => new DownloadRawText.Command(l)} - //{".zip", l => new DownloadRawText.Command(l)} + {".txt", l => new DownloadRawText.Command(l)}, + {".zip", l => new DownloadZip.Command(l)} }; private readonly ILogger _logger; diff --git a/src/FilterLists.Agent/Features/Archiver/DownloadZip.cs b/src/FilterLists.Agent/Features/Archiver/DownloadZip.cs new file mode 100644 index 000000000..a1a1ed2b2 --- /dev/null +++ b/src/FilterLists.Agent/Features/Archiver/DownloadZip.cs @@ -0,0 +1,53 @@ +using System.IO; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using FilterLists.Agent.Core.Entities; +using FilterLists.Agent.Infrastructure.Clients; +using MediatR; +using SharpCompress.Common; +using SharpCompress.Readers; + +namespace FilterLists.Agent.Features.Archiver +{ + public static class DownloadZip + { + public class Command : IRequest + { + public Command(ListInfo listInfo) + { + ListInfo = listInfo; + } + + public ListInfo ListInfo { get; } + } + + public class Handler : AsyncRequestHandler + { + private const string RepoDirectory = @"archives"; + private readonly HttpClient _httpClient; + + public Handler(AgentHttpClient httpClient) + { + _httpClient = httpClient.Client; + } + + protected override async Task Handle(Command request, CancellationToken cancellationToken) + { + var destinationDirectoryPath = Path.Combine(RepoDirectory, $"{request.ListInfo.Id}"); + using (var response = await _httpClient.GetAsync(request.ListInfo.ViewUrl, cancellationToken)) + { + if (response.IsSuccessStatusCode) + using (var input = await response.Content.ReadAsStreamAsync()) + using (var reader = ReaderFactory.Open(input)) + { + while (reader.MoveToNextEntry()) + if (!reader.Entry.IsDirectory) + reader.WriteEntryToDirectory(destinationDirectoryPath, + new ExtractionOptions {ExtractFullPath = true, Overwrite = true}); + } + } + } + } + } +} \ No newline at end of file