Advertisement
TechGeek

Untitled

Jul 19th, 2020
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1.     public class FileHelper
  2.     {
  3.         private static HttpClient _httpClient = new HttpClient()
  4.         {
  5.             Timeout = TimeSpan.FromSeconds(5)
  6.         };
  7.  
  8.         public FileHelper() { }
  9.  
  10.         public async Task<bool> Download(string url, string path)
  11.         {
  12.             var policyResult = await Policy
  13.                .Handle<TaskCanceledException>()
  14.                .WaitAndRetryAsync(retryCount: 3, sleepDurationProvider: i => TimeSpan.FromMilliseconds(300))
  15.                .ExecuteAndCaptureAsync(async () =>
  16.                {
  17.                    using (var httpResponse = await _httpClient.GetAsync(url).ConfigureAwait(false))
  18.                    {
  19.                        httpResponse.EnsureSuccessStatusCode();
  20.                        return await httpResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
  21.                    }
  22.                }).ConfigureAwait(false);
  23.  
  24.             if (policyResult.Outcome == OutcomeType.Failure || policyResult.Result == null)
  25.                 return false;
  26.             try
  27.             {
  28.                 File.WriteAllBytes(path, policyResult.Result);
  29.                 return true;
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 // log error
  34.                 return false;
  35.             }
  36.         }
  37.  
  38.         public bool ExtractTarGz(string fileName, string directory)
  39.         {
  40.             try
  41.             {
  42.                 using (var inStream = File.OpenRead(fileName))
  43.                 using (var gzipStream = new GZipInputStream(inStream))
  44.                 using (var tarArchive = TarArchive.CreateInputTarArchive(gzipStream))
  45.                 {
  46.                     tarArchive.ExtractContents(directory);
  47.                 }
  48.             }
  49.             catch (Exception ex)
  50.             {
  51.                 // log error
  52.                 return false;
  53.             }
  54.             return true;
  55.         }
  56.  
  57.         public IList<string> GetFiles(string path)
  58.         {
  59.             var allFiles = new List<string>();
  60.             try
  61.             {
  62.                 var localJsonFiles = Directory.GetFiles(path, "*.json", SearchOption.AllDirectories);
  63.                 var localTextFiles = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
  64.                 allFiles.AddRange(localJsonFiles);
  65.                 allFiles.AddRange(localTextFiles);
  66.             }
  67.             catch (UnauthorizedAccessException ex)
  68.             {
  69.                 // log error
  70.             }
  71.             return allFiles;
  72.         }
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement