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.2 kB

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