aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2013-06-03 22:02:49 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2013-06-03 22:02:49 -0400
commit02fedead11f738c09e503c3bdc74e2dd98e21cc8 (patch)
tree5e32fb80c23fa910dbdd0cc6a8be6bf105abd631 /MediaBrowser.Controller
parent08d9004d8f361aaf13756cab70fc659e5fbb775c (diff)
re-factored some file system access
Diffstat (limited to 'MediaBrowser.Controller')
-rw-r--r--MediaBrowser.Controller/Drawing/ImageManager.cs112
-rw-r--r--MediaBrowser.Controller/Entities/BaseItem.cs1
-rw-r--r--MediaBrowser.Controller/Entities/User.cs26
-rw-r--r--MediaBrowser.Controller/MediaInfo/FFMpegManager.cs57
-rw-r--r--MediaBrowser.Controller/Providers/MediaInfo/AudioImageProvider.cs14
5 files changed, 110 insertions, 100 deletions
diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs
index b728fe71f..b8406438c 100644
--- a/MediaBrowser.Controller/Drawing/ImageManager.cs
+++ b/MediaBrowser.Controller/Drawing/ImageManager.cs
@@ -31,7 +31,7 @@ namespace MediaBrowser.Controller.Drawing
/// </summary>
/// <value>The image enhancers.</value>
public IEnumerable<IImageEnhancer> ImageEnhancers { get; set; }
-
+
/// <summary>
/// Gets the image size cache.
/// </summary>
@@ -106,9 +106,10 @@ namespace MediaBrowser.Controller.Drawing
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
+ /// <param name="enhancers">The enhancers.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">entity</exception>
- 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<IImageEnhancer> 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
/// <param name="bytes">The bytes.</param>
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");
-
+
/// <summary>
/// Gets the size of the image.
/// </summary>
@@ -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 ef1efd8af..f5aed6d21 100644
--- a/MediaBrowser.Controller/Entities/BaseItem.cs
+++ b/MediaBrowser.Controller/Entities/BaseItem.cs
@@ -304,7 +304,6 @@ namespace MediaBrowser.Controller.Entities
/// <summary>
/// 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
/// </summary>
/// <value>The resolve args.</value>
[IgnoreDataMember]
diff --git a/MediaBrowser.Controller/Entities/User.cs b/MediaBrowser.Controller/Entities/User.cs
index e186119d6..65884332a 100644
--- a/MediaBrowser.Controller/Entities/User.cs
+++ b/MediaBrowser.Controller/Entities/User.cs
@@ -20,10 +20,6 @@ namespace MediaBrowser.Controller.Entities
public static IXmlSerializer XmlSerializer { get; set; }
/// <summary>
- /// The _root folder path
- /// </summary>
- private string _rootFolderPath;
- /// <summary>
/// Gets the root folder path.
/// </summary>
/// <value>The root folder path.</value>
@@ -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 111f5aff8..91359cd29 100644
--- a/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs
+++ b/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs
@@ -56,10 +56,6 @@ namespace MediaBrowser.Controller.MediaInfo
}
/// <summary>
- /// The _video images data path
- /// </summary>
- private string _videoImagesDataPath;
- /// <summary>
/// Gets the video images data path.
/// </summary>
/// <value>The video images data path.</value>
@@ -67,25 +63,11 @@ 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");
}
}
/// <summary>
- /// The _audio images data path
- /// </summary>
- private string _audioImagesDataPath;
- /// <summary>
/// Gets the audio images data path.
/// </summary>
/// <value>The audio images data path.</value>
@@ -93,25 +75,11 @@ 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");
}
}
/// <summary>
- /// The _subtitle cache path
- /// </summary>
- private string _subtitleCachePath;
- /// <summary>
/// Gets the subtitle cache path.
/// </summary>
/// <value>The subtitle cache path.</value>
@@ -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 881065ea6..9fd67f477 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