Advertisement
Lauda

Untitled

Apr 22nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1.  
  2.     public class DownloadService : IDownloadService, IDisposable
  3.     {
  4.         private readonly HttpClient _httpClient;
  5.         private readonly ILogger _logger;
  6.         private bool _disposed;
  7.  
  8.         public DownloadService(ILogger logger)
  9.         {
  10.             _logger = logger;
  11.             _httpClient = new HttpClient();
  12.         }
  13.  
  14.         public async Task DownloadAsync(Uri source, string destinationDirectory)
  15.         {
  16.             _logger.LogDebug($"Initiating async file download for {source.AbsoluteUri}");
  17.             try
  18.             {
  19.                 using (var request = new HttpRequestMessage(HttpMethod.Get, source))
  20.                 {
  21.                     using (Stream contentStream = await (await _httpClient.SendAsync(request)).Content.ReadAsStreamAsync(),
  22.                         stream = new FileStream($@"{destinationDirectory}\{Path.GetFileName(source.LocalPath)}", FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
  23.                     {
  24.                         await contentStream.CopyToAsync(stream);
  25.                     }
  26.                 }
  27.             }
  28.             catch (Exception ex)
  29.             {
  30.                 _logger.LogError($"Error occurred while async downloading {source.AbsoluteUri}", ex);
  31.                 throw;
  32.             }
  33.         }
  34.  
  35.         public void Download(Uri source, string destinationDirectory)
  36.         {
  37.             _logger.LogDebug($"Initiating file download for {source.AbsoluteUri}");
  38.             try
  39.             {
  40.                 AsyncHelper.RunSync(() => DownloadAsync(source, destinationDirectory));
  41.             }
  42.             catch (Exception ex)
  43.             {
  44.                 _logger.LogError($"Error occurred while downloading {source.AbsoluteUri}", ex);
  45.                 throw;
  46.             }
  47.         }
  48.  
  49.         public void BatchDownload(List<Uri> sources, string destinationDirectory)
  50.         {
  51.             sources.ForEach(source => Download(source, destinationDirectory));
  52.         }
  53.  
  54.         public Task BatchDownloadAsync(List<Uri> sources, string destinationDirectory)
  55.         {
  56.             Parallel.ForEach(sources, new ParallelOptions { MaxDegreeOfParallelism =  2 }, source =>
  57.             {
  58.                 Download(source, destinationDirectory);
  59.             });
  60.  
  61.             return Task.CompletedTask;
  62.         }
  63.  
  64.         public void Dispose()
  65.         {
  66.             Dispose(true);
  67.             GC.SuppressFinalize(this);
  68.         }
  69.  
  70.         protected virtual void Dispose(bool disposing)
  71.         {
  72.             if (_disposed)
  73.                 return;
  74.  
  75.             if (disposing)
  76.             {
  77.                 _httpClient?.Dispose();
  78.             }
  79.  
  80.             _disposed = true;
  81.         }
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement