re-initial support for Zip

This commit is contained in:
Collin M. Barrett 2019-06-30 20:35:29 -05:00
parent 76a973ecb9
commit 4c436d031e
2 changed files with 55 additions and 2 deletions

View file

@ -53,8 +53,8 @@ private static readonly Dictionary<string, Func<ListInfo, IRequest>> 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<Handler> _logger;

View file

@ -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<Command>
{
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});
}
}
}
}
}
}