FileSystemManager.cs 4.6 KB

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