DownloadUtils.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if (!System.IO.Path.IsPathRooted(localDirectoryPath)) throw new ArgumentException(nameof(localDirectoryPath));
  12. lock (_DownloadMutex)
  13. {
  14. if (LibGit2Sharp.Repository.Discover(localDirectoryPath) == null)
  15. {
  16. Console.WriteLine($"Cloning {remoteUrl} can take several minutes; Please wait...");
  17. LibGit2Sharp.Repository.Clone(remoteUrl, localDirectoryPath);
  18. Console.WriteLine($"... Clone Completed");
  19. return;
  20. }
  21. using (var repo = new LibGit2Sharp.Repository(localDirectoryPath))
  22. {
  23. var options = new LibGit2Sharp.PullOptions
  24. {
  25. FetchOptions = new LibGit2Sharp.FetchOptions()
  26. };
  27. var r = LibGit2Sharp.Commands.Pull(repo, new LibGit2Sharp.Signature("Anonymous", "[email protected]", new DateTimeOffset(DateTime.Now)), options);
  28. Console.WriteLine($"{remoteUrl} is {r.Status}");
  29. }
  30. }
  31. }
  32. public static string DownloadFile(string remoteUri, string localFilePath)
  33. {
  34. if (!System.IO.Path.IsPathRooted(localFilePath)) throw new ArgumentException(nameof(localFilePath));
  35. lock (_DownloadMutex)
  36. {
  37. if (System.IO.File.Exists(localFilePath)) return localFilePath; // we check again because we could have downloaded the file while waiting.
  38. Console.WriteLine($"Downloading {remoteUri}... Please Wait...");
  39. var dir = System.IO.Path.GetDirectoryName(localFilePath);
  40. System.IO.Directory.CreateDirectory(dir);
  41. using (var wc = new System.Net.WebClient())
  42. {
  43. wc.DownloadFile(remoteUri, localFilePath);
  44. }
  45. if (localFilePath.ToLower().EndsWith(".zip"))
  46. {
  47. Console.WriteLine($"Extracting {localFilePath}...");
  48. var extractPath = System.IO.Path.Combine(dir, System.IO.Path.GetFileNameWithoutExtension(localFilePath));
  49. System.IO.Compression.ZipFile.ExtractToDirectory(localFilePath, extractPath);
  50. }
  51. return localFilePath;
  52. }
  53. }
  54. }
  55. }