diff --git a/src/FilterLists.Agent/Features/Archiver/DownloadSevenZip.cs b/src/FilterLists.Agent/Features/Archiver/DownloadSevenZip.cs new file mode 100644 index 000000000..e05bbe02f --- /dev/null +++ b/src/FilterLists.Agent/Features/Archiver/DownloadSevenZip.cs @@ -0,0 +1,54 @@ +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.Archives; +using SharpCompress.Archives.SevenZip; +using SharpCompress.Common; + +namespace FilterLists.Agent.Features.Archiver +{ + public static class DownloadSevenZip + { + 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 archive = SevenZipArchive.Open(input)) + { + foreach (var entry in archive.Entries) + if (!entry.IsDirectory) + entry.WriteToDirectory(destinationDirectoryPath, + new ExtractionOptions {ExtractFullPath = true, Overwrite = true}); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/Features/Archiver/StreamExtensions.cs b/src/FilterLists.Agent/Features/Archiver/StreamExtensions.cs deleted file mode 100644 index cbebe8a17..000000000 --- a/src/FilterLists.Agent/Features/Archiver/StreamExtensions.cs +++ /dev/null @@ -1,46 +0,0 @@ -//using System.IO; -//using System.Threading; -//using System.Threading.Tasks; -//using SharpCompress.Archives; -//using SharpCompress.Archives.SevenZip; -//using SharpCompress.Readers; - -//namespace FilterLists.Agent.Features.Archiver -//{ -// public static class StreamExtensions -// { -// /// -// /// Downloads compressed streams that support forward-only reading. -// /// e.g. Zip, GZip, BZip2, Tar, Rar, LZip, XZ -// /// https://github.com/adamhathcock/sharpcompress/blob/master/FORMATS.md -// /// -// public static async Task CopyToWithCompressedReaderApi(this Stream input, Stream output, -// CancellationToken cancellationToken) -// { -// using (var reader = ReaderFactory.Open(input)) -// { -// while (reader.MoveToNextEntry()) -// if (!reader.Entry.IsDirectory) -// using (var entryStream = reader.OpenEntryStream()) -// { -// await entryStream.CopyToAsync(output, cancellationToken); -// } -// } -// } - -// /// -// /// Downloads compressed streams that only support the Archive API. -// /// e.g. 7zip -// /// https://github.com/adamhathcock/sharpcompress/blob/master/FORMATS.md -// /// -// public static void CopyToWithCompressedArchiveApi(this Stream input, Stream output) -// { -// using (var archive = SevenZipArchive.Open(input)) -// { -// foreach (var entry in archive.Entries) -// if (!entry.IsDirectory) -// entry.WriteTo(output); -// } -// } -// } -//} \ No newline at end of file