From 767cdc1f6f6a63ce997fc9476911e2c361f9d402 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Wed, 20 Feb 2013 20:33:05 -0500 Subject: Pushing missing changes --- MediaBrowser.Controller/IO/FileSystemManager.cs | 112 ++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 MediaBrowser.Controller/IO/FileSystemManager.cs (limited to 'MediaBrowser.Controller/IO/FileSystemManager.cs') diff --git a/MediaBrowser.Controller/IO/FileSystemManager.cs b/MediaBrowser.Controller/IO/FileSystemManager.cs new file mode 100644 index 000000000..e33983489 --- /dev/null +++ b/MediaBrowser.Controller/IO/FileSystemManager.cs @@ -0,0 +1,112 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.Common.Kernel; +using MediaBrowser.Controller.Entities; +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Controller.IO +{ + /// + /// This class will manage our file system watching and modifications. Any process that needs to + /// modify the directories that the system is watching for changes should use the methods of + /// this class to do so. This way we can have the watchers correctly respond to only external changes. + /// + public class FileSystemManager : BaseManager + { + /// + /// Gets or sets the directory watchers. + /// + /// The directory watchers. + private DirectoryWatchers DirectoryWatchers { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The kernel. + public FileSystemManager(Kernel kernel) + : base(kernel) + { + DirectoryWatchers = new DirectoryWatchers(); + } + + /// + /// Start the directory watchers on our library folders + /// + public void StartWatchers() + { + DirectoryWatchers.Start(); + } + + /// + /// Saves to library filesystem. + /// + /// The item. + /// The path. + /// The data to save. + /// The cancellation token. + /// Task. + /// + public async Task SaveToLibraryFilesystem(BaseItem item, string path, Stream dataToSave, CancellationToken cancellationToken) + { + if (item == null) + { + throw new ArgumentNullException(); + } + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentNullException(); + } + if (dataToSave == null) + { + throw new ArgumentNullException(); + } + if (cancellationToken == null) + { + throw new ArgumentNullException(); + } + + cancellationToken.ThrowIfCancellationRequested(); + + //Tell the watchers to ignore + DirectoryWatchers.TemporarilyIgnore(path); + + //Make the mod + + dataToSave.Position = 0; + + try + { + using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + { + await dataToSave.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false); + + dataToSave.Dispose(); + + // If this is ever used for something other than metadata we can add a file type param + item.ResolveArgs.AddMetadataFile(path); + } + } + finally + { + //Remove the ignore + DirectoryWatchers.RemoveTempIgnore(path); + } + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected override void Dispose(bool dispose) + { + if (dispose) + { + DirectoryWatchers.Dispose(); + } + + base.Dispose(dispose); + } + } +} -- cgit v1.2.3