FileSystemManager.cs 4.3 KB

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