FileSystemManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Kernel;
  3. using MediaBrowser.Common.ScheduledTasks;
  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 : BaseManager<Kernel>
  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. /// The _logger
  27. /// </summary>
  28. private readonly ILogger _logger;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="FileSystemManager" /> class.
  31. /// </summary>
  32. /// <param name="kernel">The kernel.</param>
  33. /// <param name="logger">The logger.</param>
  34. /// <param name="taskManager">The task manager.</param>
  35. /// <param name="libraryManager">The library manager.</param>
  36. public FileSystemManager(Kernel kernel, ILogger logger, ITaskManager taskManager, ILibraryManager libraryManager)
  37. : base(kernel)
  38. {
  39. _logger = logger;
  40. DirectoryWatchers = new DirectoryWatchers(logger, taskManager, libraryManager);
  41. }
  42. /// <summary>
  43. /// Start the directory watchers on our library folders
  44. /// </summary>
  45. public void StartWatchers()
  46. {
  47. DirectoryWatchers.Start();
  48. }
  49. /// <summary>
  50. /// Saves to library filesystem.
  51. /// </summary>
  52. /// <param name="item">The item.</param>
  53. /// <param name="path">The path.</param>
  54. /// <param name="dataToSave">The data to save.</param>
  55. /// <param name="cancellationToken">The cancellation token.</param>
  56. /// <returns>Task.</returns>
  57. /// <exception cref="System.ArgumentNullException"></exception>
  58. public async Task SaveToLibraryFilesystem(BaseItem item, string path, Stream dataToSave, CancellationToken cancellationToken)
  59. {
  60. if (item == null)
  61. {
  62. throw new ArgumentNullException();
  63. }
  64. if (string.IsNullOrEmpty(path))
  65. {
  66. throw new ArgumentNullException();
  67. }
  68. if (dataToSave == null)
  69. {
  70. throw new ArgumentNullException();
  71. }
  72. if (cancellationToken == null)
  73. {
  74. throw new ArgumentNullException();
  75. }
  76. cancellationToken.ThrowIfCancellationRequested();
  77. //Tell the watchers to ignore
  78. DirectoryWatchers.TemporarilyIgnore(path);
  79. //Make the mod
  80. dataToSave.Position = 0;
  81. try
  82. {
  83. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  84. {
  85. await dataToSave.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  86. dataToSave.Dispose();
  87. // If this is ever used for something other than metadata we can add a file type param
  88. item.ResolveArgs.AddMetadataFile(path);
  89. }
  90. }
  91. finally
  92. {
  93. //Remove the ignore
  94. DirectoryWatchers.RemoveTempIgnore(path);
  95. }
  96. }
  97. /// <summary>
  98. /// Releases unmanaged and - optionally - managed resources.
  99. /// </summary>
  100. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  101. protected override void Dispose(bool dispose)
  102. {
  103. if (dispose)
  104. {
  105. DirectoryWatchers.Dispose();
  106. }
  107. base.Dispose(dispose);
  108. }
  109. }
  110. }