You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilesystemProvider.cs 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //From https://github.com/akavache/Akavache
  2. //Copyright (c) 2012 GitHub
  3. //TODO: Remove once netstandard support is added
  4. using Akavache;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reactive;
  11. using System.Reactive.Concurrency;
  12. using System.Reactive.Linq;
  13. using System.Reactive.Subjects;
  14. using System.Reflection;
  15. namespace Discord
  16. {
  17. public class FilesystemProvider : IFilesystemProvider
  18. {
  19. public IObservable<Stream> OpenFileForReadAsync(string path, IScheduler scheduler)
  20. {
  21. return SafeOpenFileAsync(path, FileMode.Open, FileAccess.Read, FileShare.Read, scheduler);
  22. }
  23. public IObservable<Stream> OpenFileForWriteAsync(string path, IScheduler scheduler)
  24. {
  25. return SafeOpenFileAsync(path, FileMode.Create, FileAccess.Write, FileShare.None, scheduler);
  26. }
  27. public IObservable<Unit> CreateRecursive(string path)
  28. {
  29. CreateRecursive(new DirectoryInfo(path));
  30. return Observable.Return(Unit.Default);
  31. }
  32. public IObservable<Unit> Delete(string path)
  33. {
  34. return Observable.Start(() => File.Delete(path), Scheduler.Default);
  35. }
  36. public string GetDefaultRoamingCacheDirectory()
  37. {
  38. throw new NotSupportedException();
  39. }
  40. public string GetDefaultSecretCacheDirectory()
  41. {
  42. throw new NotSupportedException();
  43. }
  44. public string GetDefaultLocalMachineCacheDirectory()
  45. {
  46. throw new NotSupportedException();
  47. }
  48. protected static string GetAssemblyDirectoryName()
  49. {
  50. var assemblyDirectoryName = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
  51. Debug.Assert(assemblyDirectoryName != null, "The directory name of the assembly location is null");
  52. return assemblyDirectoryName;
  53. }
  54. private static IObservable<Stream> SafeOpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share, IScheduler scheduler = null)
  55. {
  56. scheduler = scheduler ?? Scheduler.Default;
  57. var ret = new AsyncSubject<Stream>();
  58. Observable.Start(() =>
  59. {
  60. try
  61. {
  62. var createModes = new[]
  63. {
  64. FileMode.Create,
  65. FileMode.CreateNew,
  66. FileMode.OpenOrCreate,
  67. };
  68. // NB: We do this (even though it's incorrect!) because
  69. // throwing lots of 1st chance exceptions makes debugging
  70. // obnoxious, as well as a bug in VS where it detects
  71. // exceptions caught by Observable.Start as Unhandled.
  72. if (!createModes.Contains(mode) && !File.Exists(path))
  73. {
  74. ret.OnError(new FileNotFoundException());
  75. return;
  76. }
  77. Observable.Start(() => new FileStream(path, mode, access, share, 4096, false), scheduler).Cast<Stream>().Subscribe(ret);
  78. }
  79. catch (Exception ex)
  80. {
  81. ret.OnError(ex);
  82. }
  83. }, scheduler);
  84. return ret;
  85. }
  86. private static void CreateRecursive(DirectoryInfo info)
  87. {
  88. SplitFullPath(info).Aggregate((parent, dir) =>
  89. {
  90. var path = Path.Combine(parent, dir);
  91. if (!Directory.Exists(path))
  92. Directory.CreateDirectory(path);
  93. return path;
  94. });
  95. }
  96. private static IEnumerable<string> SplitFullPath(DirectoryInfo info)
  97. {
  98. var root = Path.GetPathRoot(info.FullName);
  99. var components = new List<string>();
  100. for (var path = info.FullName; path != root && path != null; path = Path.GetDirectoryName(path))
  101. {
  102. var filename = Path.GetFileName(path);
  103. if (String.IsNullOrEmpty(filename))
  104. continue;
  105. components.Add(filename);
  106. }
  107. components.Add(root);
  108. components.Reverse();
  109. return components;
  110. }
  111. }
  112. }