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 kB

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