FileSystemManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Kernel;
  3. using MediaBrowser.Controller.Entities;
  4. using System;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Controller.IO
  9. {
  10. /// <summary>
  11. /// This class will manage our file system watching and modifications. Any process that needs to
  12. /// modify the directories that the system is watching for changes should use the methods of
  13. /// this class to do so. This way we can have the watchers correctly respond to only external changes.
  14. /// </summary>
  15. public class FileSystemManager : BaseManager<Kernel>
  16. {
  17. /// <summary>
  18. /// Gets or sets the directory watchers.
  19. /// </summary>
  20. /// <value>The directory watchers.</value>
  21. private DirectoryWatchers DirectoryWatchers { get; set; }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="FileSystemManager" /> class.
  24. /// </summary>
  25. /// <param name="kernel">The kernel.</param>
  26. public FileSystemManager(Kernel kernel)
  27. : base(kernel)
  28. {
  29. DirectoryWatchers = new DirectoryWatchers(Logger);
  30. }
  31. /// <summary>
  32. /// Start the directory watchers on our library folders
  33. /// </summary>
  34. public void StartWatchers()
  35. {
  36. DirectoryWatchers.Start();
  37. }
  38. /// <summary>
  39. /// Saves to library filesystem.
  40. /// </summary>
  41. /// <param name="item">The item.</param>
  42. /// <param name="path">The path.</param>
  43. /// <param name="dataToSave">The data to save.</param>
  44. /// <param name="cancellationToken">The cancellation token.</param>
  45. /// <returns>Task.</returns>
  46. /// <exception cref="System.ArgumentNullException"></exception>
  47. public async Task SaveToLibraryFilesystem(BaseItem item, string path, Stream dataToSave, CancellationToken cancellationToken)
  48. {
  49. if (item == null)
  50. {
  51. throw new ArgumentNullException();
  52. }
  53. if (string.IsNullOrEmpty(path))
  54. {
  55. throw new ArgumentNullException();
  56. }
  57. if (dataToSave == null)
  58. {
  59. throw new ArgumentNullException();
  60. }
  61. if (cancellationToken == null)
  62. {
  63. throw new ArgumentNullException();
  64. }
  65. cancellationToken.ThrowIfCancellationRequested();
  66. //Tell the watchers to ignore
  67. DirectoryWatchers.TemporarilyIgnore(path);
  68. //Make the mod
  69. dataToSave.Position = 0;
  70. try
  71. {
  72. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  73. {
  74. await dataToSave.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  75. dataToSave.Dispose();
  76. // If this is ever used for something other than metadata we can add a file type param
  77. item.ResolveArgs.AddMetadataFile(path);
  78. }
  79. }
  80. finally
  81. {
  82. //Remove the ignore
  83. DirectoryWatchers.RemoveTempIgnore(path);
  84. }
  85. }
  86. /// <summary>
  87. /// Releases unmanaged and - optionally - managed resources.
  88. /// </summary>
  89. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  90. protected override void Dispose(bool dispose)
  91. {
  92. if (dispose)
  93. {
  94. DirectoryWatchers.Dispose();
  95. }
  96. base.Dispose(dispose);
  97. }
  98. }
  99. }