From 02fedead11f738c09e503c3bdc74e2dd98e21cc8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 3 Jun 2013 22:02:49 -0400 Subject: re-factored some file system access --- MediaBrowser.Controller/Drawing/ImageManager.cs | 112 +++++++++++++++------ MediaBrowser.Controller/Entities/BaseItem.cs | 1 - MediaBrowser.Controller/Entities/User.cs | 26 ++--- MediaBrowser.Controller/MediaInfo/FFMpegManager.cs | 57 ++--------- .../Providers/MediaInfo/AudioImageProvider.cs | 14 ++- 5 files changed, 110 insertions(+), 100 deletions(-) (limited to 'MediaBrowser.Controller') diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index b728fe71f6..b8406438c9 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -31,7 +31,7 @@ namespace MediaBrowser.Controller.Drawing /// /// The image enhancers. public IEnumerable ImageEnhancers { get; set; } - + /// /// Gets the image size cache. /// @@ -106,9 +106,10 @@ namespace MediaBrowser.Controller.Drawing /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. + /// The enhancers. /// Task. /// entity - public async Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, bool cropWhitespace, DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality) + public async Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, bool cropWhitespace, DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality, List enhancers) { if (entity == null) { @@ -127,28 +128,13 @@ namespace MediaBrowser.Controller.Drawing originalImagePath = await GetCroppedImage(originalImagePath, dateModified).ConfigureAwait(false); } - var supportedEnhancers = ImageEnhancers.Where(i => - { - try - { - return i.Supports(entity, imageType); - } - catch (Exception ex) - { - _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name); - - return false; - } - - }).ToList(); - // No enhancement - don't cache - if (supportedEnhancers.Count > 0) + if (enhancers.Count > 0) { try { // Enhance if we have enhancers - var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex, supportedEnhancers).ConfigureAwait(false); + var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex, enhancers).ConfigureAwait(false); // If the path changed update dateModified if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase)) @@ -175,6 +161,19 @@ namespace MediaBrowser.Controller.Drawing var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality.Value, dateModified); + try + { + using (var fileStream = new FileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + { + await fileStream.CopyToAsync(toStream).ConfigureAwait(false); + return; + } + } + catch (IOException) + { + // Cache file doesn't exist or is currently being written ro + } + var semaphore = GetLock(cacheFilePath); await semaphore.WaitAsync().ConfigureAwait(false); @@ -262,6 +261,13 @@ namespace MediaBrowser.Controller.Drawing /// The bytes. private async Task CacheResizedImage(string cacheFilePath, byte[] bytes) { + var parentPath = Path.GetDirectoryName(cacheFilePath); + + if (!Directory.Exists(parentPath)) + { + Directory.CreateDirectory(parentPath); + } + // Save to the cache location using (var cacheFileStream = new FileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) { @@ -323,7 +329,7 @@ namespace MediaBrowser.Controller.Drawing } protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); - + /// /// Gets the size of the image. /// @@ -335,25 +341,53 @@ namespace MediaBrowser.Controller.Drawing // Now check the file system cache var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".txt"); + try + { + var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray(); + + return new ImageSize { Width = result[0], Height = result[1] }; + } + catch (IOException) + { + // Cache file doesn't exist or is currently being written to + } + var semaphore = GetLock(fullCachePath); await semaphore.WaitAsync().ConfigureAwait(false); try { - try - { - var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray(); + var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray(); - return new ImageSize { Width = result[0], Height = result[1] }; - } - catch (FileNotFoundException) - { - // Cache file doesn't exist no biggie - } + return new ImageSize { Width = result[0], Height = result[1] }; + } + catch (FileNotFoundException) + { + // Cache file doesn't exist no biggie + } + catch (DirectoryNotFoundException) + { + // Cache file doesn't exist no biggie + } + catch + { + semaphore.Release(); + + throw; + } + try + { var size = await ImageHeader.GetDimensions(imagePath, _logger).ConfigureAwait(false); + var parentPath = Path.GetDirectoryName(fullCachePath); + + if (!Directory.Exists(parentPath)) + { + Directory.CreateDirectory(parentPath); + } + // Update the file system cache File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture)); @@ -490,12 +524,12 @@ namespace MediaBrowser.Controller.Drawing await semaphore.WaitAsync().ConfigureAwait(false); // Check again in case of contention - if (CroppedImageCache.ContainsFilePath(croppedImagePath)) + if (File.Exists(croppedImagePath)) { semaphore.Release(); return croppedImagePath; } - + try { using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true)) @@ -511,6 +545,13 @@ namespace MediaBrowser.Controller.Drawing using (var croppedImage = originalImage.CropWhitespace()) { + var parentPath = Path.GetDirectoryName(croppedImagePath); + + if (!Directory.Exists(parentPath)) + { + Directory.CreateDirectory(parentPath); + } + using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { croppedImage.Save(outputFormat, outputStream, 100); @@ -568,7 +609,7 @@ namespace MediaBrowser.Controller.Drawing await semaphore.WaitAsync().ConfigureAwait(false); // Check again in case of contention - if (EnhancedImageCache.ContainsFilePath(enhancedImagePath)) + if (File.Exists(enhancedImagePath)) { semaphore.Release(); return enhancedImagePath; @@ -588,6 +629,13 @@ namespace MediaBrowser.Controller.Drawing //Pass the image through registered enhancers using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false)) { + var parentDirectory = Path.GetDirectoryName(enhancedImagePath); + + if (!Directory.Exists(parentDirectory)) + { + Directory.CreateDirectory(parentDirectory); + } + //And then save it in the cache using (var outputStream = new FileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index ef1efd8af9..f5aed6d214 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -304,7 +304,6 @@ namespace MediaBrowser.Controller.Entities /// /// We attach these to the item so that we only ever have to hit the file system once /// (this includes the children of the containing folder) - /// Use ResolveArgs.FileSystemDictionary to check for the existence of files instead of File.Exists /// /// The resolve args. [IgnoreDataMember] diff --git a/MediaBrowser.Controller/Entities/User.cs b/MediaBrowser.Controller/Entities/User.cs index e186119d6a..65884332aa 100644 --- a/MediaBrowser.Controller/Entities/User.cs +++ b/MediaBrowser.Controller/Entities/User.cs @@ -19,10 +19,6 @@ namespace MediaBrowser.Controller.Entities public static IUserManager UserManager { get; set; } public static IXmlSerializer XmlSerializer { get; set; } - /// - /// The _root folder path - /// - private string _rootFolderPath; /// /// Gets the root folder path. /// @@ -32,23 +28,19 @@ namespace MediaBrowser.Controller.Entities { get { - if (_rootFolderPath == null) + if (Configuration.UseCustomLibrary) { - if (Configuration.UseCustomLibrary) - { - _rootFolderPath = GetRootFolderPath(Name); + var rootFolderPath = GetRootFolderPath(Name); - if (!Directory.Exists(_rootFolderPath)) - { - Directory.CreateDirectory(_rootFolderPath); - } - } - else + if (!Directory.Exists(rootFolderPath)) { - _rootFolderPath = ConfigurationManager.ApplicationPaths.DefaultUserViewsPath; + Directory.CreateDirectory(rootFolderPath); } + + return rootFolderPath; } - return _rootFolderPath; + + return ConfigurationManager.ApplicationPaths.DefaultUserViewsPath; } } @@ -261,7 +253,6 @@ namespace MediaBrowser.Controller.Entities // Force these to be lazy loaded again _configurationDirectoryPath = null; - _rootFolderPath = null; RootFolder = null; // Kick off a task to validate the media library @@ -378,7 +369,6 @@ namespace MediaBrowser.Controller.Entities // Force these to be lazy loaded again if (customLibraryChanged) { - _rootFolderPath = null; RootFolder = null; } } diff --git a/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs b/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs index 111f5aff89..91359cd290 100644 --- a/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs +++ b/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs @@ -55,10 +55,6 @@ namespace MediaBrowser.Controller.MediaInfo SubtitleCache = new FileSystemRepository(SubtitleCachePath); } - /// - /// The _video images data path - /// - private string _videoImagesDataPath; /// /// Gets the video images data path. /// @@ -67,24 +63,10 @@ namespace MediaBrowser.Controller.MediaInfo { get { - if (_videoImagesDataPath == null) - { - _videoImagesDataPath = Path.Combine(_appPaths.DataPath, "extracted-video-images"); - - if (!Directory.Exists(_videoImagesDataPath)) - { - Directory.CreateDirectory(_videoImagesDataPath); - } - } - - return _videoImagesDataPath; + return Path.Combine(_appPaths.DataPath, "extracted-video-images"); } } - /// - /// The _audio images data path - /// - private string _audioImagesDataPath; /// /// Gets the audio images data path. /// @@ -93,24 +75,10 @@ namespace MediaBrowser.Controller.MediaInfo { get { - if (_audioImagesDataPath == null) - { - _audioImagesDataPath = Path.Combine(_appPaths.DataPath, "extracted-audio-images"); - - if (!Directory.Exists(_audioImagesDataPath)) - { - Directory.CreateDirectory(_audioImagesDataPath); - } - } - - return _audioImagesDataPath; + return Path.Combine(_appPaths.DataPath, "extracted-audio-images"); } } - /// - /// The _subtitle cache path - /// - private string _subtitleCachePath; /// /// Gets the subtitle cache path. /// @@ -119,17 +87,7 @@ namespace MediaBrowser.Controller.MediaInfo { get { - if (_subtitleCachePath == null) - { - _subtitleCachePath = Path.Combine(_appPaths.CachePath, "subtitles"); - - if (!Directory.Exists(_subtitleCachePath)) - { - Directory.CreateDirectory(_subtitleCachePath); - } - } - - return _subtitleCachePath; + return Path.Combine(_appPaths.CachePath, "subtitles"); } } @@ -177,7 +135,7 @@ namespace MediaBrowser.Controller.MediaInfo var path = VideoImageCache.GetResourcePath(filename, ".jpg"); - if (!VideoImageCache.ContainsFilePath(path)) + if (!File.Exists(path)) { if (extractImages) { @@ -204,6 +162,13 @@ namespace MediaBrowser.Controller.MediaInfo try { + var parentPath = Path.GetDirectoryName(path); + + if (!Directory.Exists(parentPath)) + { + Directory.CreateDirectory(parentPath); + } + await _encoder.ExtractImage(inputPath, type, time, path, cancellationToken).ConfigureAwait(false); chapter.ImagePath = path; changesMade = true; diff --git a/MediaBrowser.Controller/Providers/MediaInfo/AudioImageProvider.cs b/MediaBrowser.Controller/Providers/MediaInfo/AudioImageProvider.cs index 881065ea61..9fd67f4777 100644 --- a/MediaBrowser.Controller/Providers/MediaInfo/AudioImageProvider.cs +++ b/MediaBrowser.Controller/Providers/MediaInfo/AudioImageProvider.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.IO; +using System.IO; +using MediaBrowser.Common.IO; using MediaBrowser.Common.MediaInfo; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; @@ -156,7 +157,7 @@ namespace MediaBrowser.Controller.Providers.MediaInfo var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg"); - if (!ImageCache.ContainsFilePath(path)) + if (!File.Exists(path)) { var semaphore = GetLock(path); @@ -164,10 +165,17 @@ namespace MediaBrowser.Controller.Providers.MediaInfo await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); // Check again - if (!ImageCache.ContainsFilePath(path)) + if (!File.Exists(path)) { try { + var parentPath = Path.GetDirectoryName(path); + + if (!Directory.Exists(parentPath)) + { + Directory.CreateDirectory(parentPath); + } + await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false); } finally -- cgit v1.2.3