2
0

FileSystemManager.cs 4.1 KB

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