DownloadUtils.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SharpGLTF
  5. {
  6. static class DownloadUtils
  7. {
  8. private static readonly Object _DownloadMutex = new object();
  9. public static void SyncronizeGitRepository(string remoteUrl, string localDirectoryPath)
  10. {
  11. Console.WriteLine($"Sync with {remoteUrl}...");
  12. if (!System.IO.Path.IsPathRooted(localDirectoryPath)) throw new ArgumentException(nameof(localDirectoryPath));
  13. lock (_DownloadMutex)
  14. {
  15. if (LibGit2Sharp.Repository.Discover(localDirectoryPath) == null)
  16. {
  17. Console.WriteLine($"Cloning {remoteUrl} can take several minutes; Please wait...");
  18. LibGit2Sharp.Repository.Clone(remoteUrl, localDirectoryPath);
  19. Console.WriteLine("... Clone Completed");
  20. return;
  21. }
  22. using (var repo = new LibGit2Sharp.Repository(localDirectoryPath))
  23. {
  24. var options = new LibGit2Sharp.PullOptions
  25. {
  26. FetchOptions = new LibGit2Sharp.FetchOptions()
  27. };
  28. var r = LibGit2Sharp.Commands.Pull(repo, new LibGit2Sharp.Signature("Anonymous", "[email protected]", new DateTimeOffset(DateTime.Now)), options);
  29. Console.WriteLine($"{remoteUrl} is {r.Status}");
  30. }
  31. }
  32. }
  33. public static string DownloadFile(string remoteUri, string localFilePath)
  34. {
  35. if (!System.IO.Path.IsPathRooted(localFilePath)) throw new ArgumentException(nameof(localFilePath));
  36. lock (_DownloadMutex)
  37. {
  38. if (System.IO.File.Exists(localFilePath)) return localFilePath; // we check again because we could have downloaded the file while waiting.
  39. Console.WriteLine($"Downloading {remoteUri}... Please Wait...");
  40. var dir = System.IO.Path.GetDirectoryName(localFilePath);
  41. System.IO.Directory.CreateDirectory(dir);
  42. using (var wc = new System.Net.WebClient())
  43. {
  44. wc.DownloadFile(remoteUri, localFilePath);
  45. }
  46. if (localFilePath.ToUpperInvariant().EndsWith(".ZIP"))
  47. {
  48. Console.WriteLine($"Extracting {localFilePath}...");
  49. var extractPath = System.IO.Path.Combine(dir, System.IO.Path.GetFileNameWithoutExtension(localFilePath));
  50. System.IO.Compression.ZipFile.ExtractToDirectory(localFilePath, extractPath);
  51. }
  52. return localFilePath;
  53. }
  54. }
  55. }
  56. }