2014-07-04 02:22:57 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-10-20 03:04:45 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-01-28 21:25:10 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-10-15 15:29:19 +00:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-12-04 14:52:38 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
using System;
|
2013-11-07 15:57:12 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-09-18 02:43:34 +00:00
|
|
|
|
using System.Globalization;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2014-12-26 17:45:06 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class ImageSaver
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ImageSaver
|
|
|
|
|
{
|
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _config
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _directory watchers
|
|
|
|
|
/// </summary>
|
2014-01-28 21:25:10 +00:00
|
|
|
|
private readonly ILibraryMonitor _libraryMonitor;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-11-08 15:35:11 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-08 20:02:35 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="ImageSaver" /> class.
|
2013-06-28 20:25:58 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="config">The config.</param>
|
2014-01-28 21:25:10 +00:00
|
|
|
|
/// <param name="libraryMonitor">The directory watchers.</param>
|
2014-02-08 20:02:35 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2014-01-28 21:25:10 +00:00
|
|
|
|
public ImageSaver(IServerConfigurationManager config, ILibraryMonitor libraryMonitor, IFileSystem fileSystem, ILogger logger)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
2014-01-28 21:25:10 +00:00
|
|
|
|
_libraryMonitor = libraryMonitor;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-11-08 15:35:11 +00:00
|
|
|
|
_logger = logger;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="source">The source.</param>
|
|
|
|
|
/// <param name="mimeType">Type of the MIME.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-10-22 19:03:21 +00:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">mimeType</exception>
|
2014-10-20 03:04:45 +00:00
|
|
|
|
public Task SaveImage(IHasImages item, Stream source, string mimeType, ImageType type, int? imageIndex, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return SaveImage(item, source, mimeType, type, imageIndex, null, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SaveImage(IHasImages item, Stream source, string mimeType, ImageType type, int? imageIndex, string internalCacheKey, CancellationToken cancellationToken)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(mimeType))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("mimeType");
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 22:42:06 +00:00
|
|
|
|
var saveLocally = item.SupportsLocalMetadata && item.IsSaveLocalMetadataEnabled() && !item.IsOwnedItem && !(item is Audio);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2014-04-13 17:27:13 +00:00
|
|
|
|
if (item is User)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
2013-10-31 21:03:24 +00:00
|
|
|
|
saveLocally = true;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
2014-04-13 17:27:13 +00:00
|
|
|
|
if (item is IItemByName)
|
|
|
|
|
{
|
|
|
|
|
var hasDualAccess = item as IHasDualAccess;
|
|
|
|
|
if (hasDualAccess == null || hasDualAccess.IsAccessedByName)
|
|
|
|
|
{
|
|
|
|
|
saveLocally = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-10-25 20:58:31 +00:00
|
|
|
|
if (type != ImageType.Primary && item is Episode)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
2013-10-25 20:58:31 +00:00
|
|
|
|
saveLocally = false;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-25 20:58:31 +00:00
|
|
|
|
var locationType = item.LocationType;
|
|
|
|
|
if (locationType == LocationType.Remote || locationType == LocationType.Virtual)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
|
|
|
|
saveLocally = false;
|
2013-12-04 14:52:38 +00:00
|
|
|
|
|
|
|
|
|
var season = item as Season;
|
|
|
|
|
|
|
|
|
|
// If season is virtual under a physical series, save locally if using compatible convention
|
|
|
|
|
if (season != null && _config.Configuration.ImageSavingConvention == ImageSavingConvention.Compatible)
|
|
|
|
|
{
|
|
|
|
|
var series = season.Series;
|
|
|
|
|
|
2014-02-15 22:42:06 +00:00
|
|
|
|
if (series != null && series.SupportsLocalMetadata && series.IsSaveLocalMetadataEnabled())
|
2013-12-04 14:52:38 +00:00
|
|
|
|
{
|
2014-02-10 20:11:46 +00:00
|
|
|
|
saveLocally = true;
|
2013-12-04 14:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
2014-10-20 03:04:45 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(internalCacheKey))
|
|
|
|
|
{
|
|
|
|
|
saveLocally = false;
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
|
if (!imageIndex.HasValue && item.AllowsMultipleImages(type))
|
2013-11-07 18:05:14 +00:00
|
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
|
imageIndex = item.GetImages(type).Count();
|
2013-11-07 18:05:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 21:51:32 +00:00
|
|
|
|
var index = imageIndex ?? 0;
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
var paths = !string.IsNullOrEmpty(internalCacheKey) ?
|
|
|
|
|
new[] { GetCacheKeyPath(item, type, mimeType, internalCacheKey) } :
|
|
|
|
|
GetSavePaths(item, type, imageIndex, mimeType, saveLocally);
|
2013-10-15 15:29:19 +00:00
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
// If there are more than one output paths, the stream will need to be seekable
|
|
|
|
|
if (paths.Length > 1 && !source.CanSeek)
|
|
|
|
|
{
|
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
|
using (source)
|
|
|
|
|
{
|
|
|
|
|
await source.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
source = memoryStream;
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-12-19 21:51:32 +00:00
|
|
|
|
var currentPath = GetCurrentImagePath(item, type, index);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
using (source)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
2013-10-15 22:17:08 +00:00
|
|
|
|
var isFirst = true;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
foreach (var path in paths)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
2013-10-15 22:17:08 +00:00
|
|
|
|
// Seek back to the beginning
|
|
|
|
|
if (!isFirst)
|
2013-09-24 00:04:18 +00:00
|
|
|
|
{
|
2013-10-15 22:17:08 +00:00
|
|
|
|
source.Position = 0;
|
2013-10-15 15:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
await SaveImageToLocation(source, path, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
isFirst = false;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
2013-10-15 22:17:08 +00:00
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-12-19 21:51:32 +00:00
|
|
|
|
// Set the path into the item
|
2014-02-03 17:44:13 +00:00
|
|
|
|
SetImagePath(item, type, imageIndex, paths[0]);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
// Delete the current path
|
|
|
|
|
if (!string.IsNullOrEmpty(currentPath) && !paths.Contains(currentPath, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2014-01-28 21:25:10 +00:00
|
|
|
|
_libraryMonitor.ReportFileSystemChangeBeginning(currentPath);
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
|
|
|
|
try
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
2013-10-22 19:03:21 +00:00
|
|
|
|
var currentFile = new FileInfo(currentPath);
|
|
|
|
|
|
|
|
|
|
// This will fail if the file is hidden
|
|
|
|
|
if (currentFile.Exists)
|
|
|
|
|
{
|
|
|
|
|
if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
|
|
|
|
{
|
|
|
|
|
currentFile.Attributes &= ~FileAttributes.Hidden;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-13 03:46:44 +00:00
|
|
|
|
_fileSystem.DeleteFile(currentFile.FullName);
|
2013-10-22 19:03:21 +00:00
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
2013-10-15 22:17:08 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
2014-01-28 21:25:10 +00:00
|
|
|
|
_libraryMonitor.ReportFileSystemChangeComplete(currentPath, false);
|
2013-10-15 22:17:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
private string GetCacheKeyPath(IHasImages item, ImageType type, string mimeType, string key)
|
|
|
|
|
{
|
|
|
|
|
var extension = MimeTypes.ToExtension(mimeType);
|
|
|
|
|
return Path.Combine(item.GetInternalMetadataPath(), type.ToString().ToLower() + "_key_" + key + extension);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the image to location.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">The source.</param>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task SaveImageToLocation(Stream source, string path, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2013-11-08 15:35:11 +00:00
|
|
|
|
_logger.Debug("Saving image to {0}", path);
|
|
|
|
|
|
|
|
|
|
var parentFolder = Path.GetDirectoryName(path);
|
|
|
|
|
|
2014-01-28 21:25:10 +00:00
|
|
|
|
_libraryMonitor.ReportFileSystemChangeBeginning(path);
|
|
|
|
|
_libraryMonitor.ReportFileSystemChangeBeginning(parentFolder);
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
|
|
|
|
|
// If the file is currently hidden we'll have to remove that or the save will fail
|
|
|
|
|
var file = new FileInfo(path);
|
|
|
|
|
|
|
|
|
|
// This will fail if the file is hidden
|
|
|
|
|
if (file.Exists)
|
|
|
|
|
{
|
|
|
|
|
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
|
|
|
|
{
|
|
|
|
|
file.Attributes &= ~FileAttributes.Hidden;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
2013-10-15 22:17:08 +00:00
|
|
|
|
{
|
2015-01-13 05:40:27 +00:00
|
|
|
|
await source.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2013-10-15 22:17:08 +00:00
|
|
|
|
}
|
2014-09-06 04:21:23 +00:00
|
|
|
|
|
|
|
|
|
if (_config.Configuration.SaveMetadataHidden)
|
|
|
|
|
{
|
|
|
|
|
file.Refresh();
|
|
|
|
|
|
|
|
|
|
// Add back the attribute
|
|
|
|
|
file.Attributes |= FileAttributes.Hidden;
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
2015-01-13 05:40:27 +00:00
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("Error saving image to {0}", ex, path);
|
|
|
|
|
throw new Exception(string.Format("Error saving image to {0}", path), ex);
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
2014-01-28 21:25:10 +00:00
|
|
|
|
_libraryMonitor.ReportFileSystemChangeComplete(path, false);
|
|
|
|
|
_libraryMonitor.ReportFileSystemChangeComplete(parentFolder, false);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
2013-10-15 22:17:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the save paths.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <param name="mimeType">Type of the MIME.</param>
|
|
|
|
|
/// <param name="saveLocally">if set to <c>true</c> [save locally].</param>
|
|
|
|
|
/// <returns>IEnumerable{System.String}.</returns>
|
2014-02-15 22:42:06 +00:00
|
|
|
|
private string[] GetSavePaths(IHasImages item, ImageType type, int? imageIndex, string mimeType, bool saveLocally)
|
2013-10-15 22:17:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (_config.Configuration.ImageSavingConvention == ImageSavingConvention.Legacy || !saveLocally)
|
|
|
|
|
{
|
2013-10-17 15:35:39 +00:00
|
|
|
|
return new[] { GetStandardSavePath(item, type, imageIndex, mimeType, saveLocally) };
|
2013-10-15 22:17:08 +00:00
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
return GetCompatibleSavePaths(item, type, imageIndex, mimeType);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current image path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// imageIndex
|
|
|
|
|
/// or
|
|
|
|
|
/// imageIndex
|
|
|
|
|
/// </exception>
|
2013-12-19 21:51:32 +00:00
|
|
|
|
private string GetCurrentImagePath(IHasImages item, ImageType type, int imageIndex)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
2013-12-19 21:51:32 +00:00
|
|
|
|
return item.GetImagePath(type, imageIndex);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the image path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <param name="path">The path.</param>
|
2013-10-22 19:03:21 +00:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">imageIndex
|
2013-10-15 22:17:08 +00:00
|
|
|
|
/// or
|
2013-10-22 19:03:21 +00:00
|
|
|
|
/// imageIndex</exception>
|
2014-02-15 22:42:06 +00:00
|
|
|
|
private void SetImagePath(IHasImages item, ImageType type, int? imageIndex, string path)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
|
item.SetImagePath(type, imageIndex ?? 0, new FileInfo(path));
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the save path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <param name="mimeType">Type of the MIME.</param>
|
|
|
|
|
/// <param name="saveLocally">if set to <c>true</c> [save locally].</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// imageIndex
|
|
|
|
|
/// or
|
|
|
|
|
/// imageIndex
|
|
|
|
|
/// </exception>
|
2014-02-15 22:42:06 +00:00
|
|
|
|
private string GetStandardSavePath(IHasImages item, ImageType type, int? imageIndex, string mimeType, bool saveLocally)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
{
|
|
|
|
|
string filename;
|
2014-10-20 03:04:45 +00:00
|
|
|
|
|
2013-06-28 20:25:58 +00:00
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case ImageType.Art:
|
|
|
|
|
filename = "clearart";
|
|
|
|
|
break;
|
2014-01-29 01:47:47 +00:00
|
|
|
|
case ImageType.BoxRear:
|
|
|
|
|
filename = "back";
|
|
|
|
|
break;
|
2013-11-19 16:44:53 +00:00
|
|
|
|
case ImageType.Disc:
|
|
|
|
|
filename = item is MusicAlbum ? "cdart" : "disc";
|
|
|
|
|
break;
|
2013-06-28 20:25:58 +00:00
|
|
|
|
case ImageType.Primary:
|
2014-07-26 17:30:15 +00:00
|
|
|
|
filename = item is Episode ? _fileSystem.GetFileNameWithoutExtension(item.Path) : "folder";
|
2013-06-28 20:25:58 +00:00
|
|
|
|
break;
|
|
|
|
|
case ImageType.Backdrop:
|
2014-02-07 20:30:41 +00:00
|
|
|
|
filename = GetBackdropSaveFilename(item.GetImages(type), "backdrop", "backdrop", imageIndex);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
break;
|
|
|
|
|
case ImageType.Screenshot:
|
2014-02-07 20:30:41 +00:00
|
|
|
|
filename = GetBackdropSaveFilename(item.GetImages(type), "screenshot", "screenshot", imageIndex);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
filename = type.ToString().ToLower();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var extension = mimeType.Split('/').Last();
|
|
|
|
|
|
|
|
|
|
if (string.Equals(extension, "jpeg", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
extension = "jpg";
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 01:11:56 +00:00
|
|
|
|
extension = "." + extension.ToLower();
|
|
|
|
|
|
2013-08-15 16:00:39 +00:00
|
|
|
|
string path = null;
|
|
|
|
|
|
|
|
|
|
if (saveLocally)
|
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
if (item is Episode)
|
|
|
|
|
{
|
|
|
|
|
path = Path.Combine(Path.GetDirectoryName(item.Path), "metadata", filename + extension);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (item.IsInMixedFolder)
|
2013-08-15 16:00:39 +00:00
|
|
|
|
{
|
2013-08-15 19:09:52 +00:00
|
|
|
|
path = GetSavePathForItemInMixedFolder(item, type, filename, extension);
|
2013-08-15 16:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-25 20:58:31 +00:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
2013-08-15 16:00:39 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
path = Path.Combine(item.ContainingFolderPath, filename + extension);
|
2013-08-15 16:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-15 19:09:52 +00:00
|
|
|
|
// None of the save local conditions passed, so store it in our internal folders
|
2013-08-15 16:00:39 +00:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
2014-02-09 04:52:52 +00:00
|
|
|
|
if (string.IsNullOrEmpty(filename))
|
|
|
|
|
{
|
|
|
|
|
filename = "folder";
|
|
|
|
|
}
|
2014-09-28 15:27:26 +00:00
|
|
|
|
path = Path.Combine(item.GetInternalMetadataPath(), filename + extension);
|
2013-08-15 16:00:39 +00:00
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-10-15 15:29:19 +00:00
|
|
|
|
return path;
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
|
private string GetBackdropSaveFilename(IEnumerable<ItemImageInfo> images, string zeroIndexFilename, string numberedIndexPrefix, int? index)
|
2013-11-02 19:30:29 +00:00
|
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
|
if (index.HasValue && index.Value == 0)
|
2013-11-02 19:30:29 +00:00
|
|
|
|
{
|
|
|
|
|
return zeroIndexFilename;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 17:30:15 +00:00
|
|
|
|
var filenames = images.Select(i => _fileSystem.GetFileNameWithoutExtension(i.Path)).ToList();
|
2013-11-03 01:58:56 +00:00
|
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
|
var current = 1;
|
2013-11-03 01:58:56 +00:00
|
|
|
|
while (filenames.Contains(numberedIndexPrefix + current.ToString(UsCulture), StringComparer.OrdinalIgnoreCase))
|
2013-11-02 19:30:29 +00:00
|
|
|
|
{
|
|
|
|
|
current++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return numberedIndexPrefix + current.ToString(UsCulture);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 15:29:19 +00:00
|
|
|
|
/// <summary>
|
2013-10-15 22:17:08 +00:00
|
|
|
|
/// Gets the compatible save paths.
|
2013-10-15 15:29:19 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <param name="mimeType">Type of the MIME.</param>
|
2013-10-15 22:17:08 +00:00
|
|
|
|
/// <returns>IEnumerable{System.String}.</returns>
|
2013-10-15 15:29:19 +00:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">imageIndex</exception>
|
2014-02-15 22:42:06 +00:00
|
|
|
|
private string[] GetCompatibleSavePaths(IHasImages item, ImageType type, int? imageIndex, string mimeType)
|
2013-10-15 15:29:19 +00:00
|
|
|
|
{
|
2013-12-04 14:52:38 +00:00
|
|
|
|
var season = item as Season;
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
var extension = MimeTypes.ToExtension(mimeType);
|
2013-10-15 15:29:19 +00:00
|
|
|
|
|
|
|
|
|
// Backdrop paths
|
|
|
|
|
if (type == ImageType.Backdrop)
|
|
|
|
|
{
|
|
|
|
|
if (!imageIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("imageIndex");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imageIndex.Value == 0)
|
|
|
|
|
{
|
2013-12-11 23:30:41 +00:00
|
|
|
|
if (item.IsInMixedFolder)
|
|
|
|
|
{
|
|
|
|
|
return new[] { GetSavePathForItemInMixedFolder(item, type, "fanart", extension) };
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 22:42:06 +00:00
|
|
|
|
if (season != null && season.IndexNumber.HasValue)
|
2013-10-16 01:44:23 +00:00
|
|
|
|
{
|
2013-12-04 14:52:38 +00:00
|
|
|
|
var seriesFolder = season.SeriesPath;
|
2013-10-16 01:44:23 +00:00
|
|
|
|
|
2014-02-15 22:42:06 +00:00
|
|
|
|
var seasonMarker = season.IndexNumber.Value == 0
|
2013-10-16 01:44:23 +00:00
|
|
|
|
? "-specials"
|
2014-02-15 22:42:06 +00:00
|
|
|
|
: season.IndexNumber.Value.ToString("00", UsCulture);
|
2013-10-16 01:44:23 +00:00
|
|
|
|
|
|
|
|
|
var imageFilename = "season" + seasonMarker + "-fanart" + extension;
|
|
|
|
|
|
|
|
|
|
return new[] { Path.Combine(seriesFolder, imageFilename) };
|
|
|
|
|
}
|
2013-10-17 20:59:46 +00:00
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
return new[]
|
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
Path.Combine(item.ContainingFolderPath, "fanart" + extension)
|
2013-10-15 22:17:08 +00:00
|
|
|
|
};
|
2013-10-15 15:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
var outputIndex = imageIndex.Value;
|
|
|
|
|
|
2013-12-11 23:30:41 +00:00
|
|
|
|
if (item.IsInMixedFolder)
|
|
|
|
|
{
|
|
|
|
|
return new[] { GetSavePathForItemInMixedFolder(item, type, "fanart" + outputIndex.ToString(UsCulture), extension) };
|
|
|
|
|
}
|
2013-12-27 00:24:23 +00:00
|
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
|
var extraFanartFilename = GetBackdropSaveFilename(item.GetImages(ImageType.Backdrop), "fanart", "fanart", outputIndex);
|
2013-11-03 01:58:56 +00:00
|
|
|
|
|
2014-07-04 02:22:57 +00:00
|
|
|
|
var list = new List<string>
|
|
|
|
|
{
|
|
|
|
|
Path.Combine(item.ContainingFolderPath, "extrafanart", extraFanartFilename + extension)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (EnableExtraThumbsDuplication)
|
|
|
|
|
{
|
|
|
|
|
list.Add(Path.Combine(item.ContainingFolderPath, "extrathumbs", "thumb" + outputIndex.ToString(UsCulture) + extension));
|
|
|
|
|
}
|
|
|
|
|
return list.ToArray();
|
2013-10-15 15:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == ImageType.Primary)
|
|
|
|
|
{
|
2014-02-15 22:42:06 +00:00
|
|
|
|
if (season != null && season.IndexNumber.HasValue)
|
2013-10-15 22:17:08 +00:00
|
|
|
|
{
|
2013-12-04 14:52:38 +00:00
|
|
|
|
var seriesFolder = season.SeriesPath;
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
2014-02-15 22:42:06 +00:00
|
|
|
|
var seasonMarker = season.IndexNumber.Value == 0
|
2013-10-15 22:17:08 +00:00
|
|
|
|
? "-specials"
|
2014-02-15 22:42:06 +00:00
|
|
|
|
: season.IndexNumber.Value.ToString("00", UsCulture);
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
|
|
|
|
var imageFilename = "season" + seasonMarker + "-poster" + extension;
|
|
|
|
|
|
|
|
|
|
return new[] { Path.Combine(seriesFolder, imageFilename) };
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 15:29:19 +00:00
|
|
|
|
if (item is Episode)
|
|
|
|
|
{
|
2013-10-15 22:17:08 +00:00
|
|
|
|
var seasonFolder = Path.GetDirectoryName(item.Path);
|
|
|
|
|
|
2014-07-26 17:30:15 +00:00
|
|
|
|
var imageFilename = _fileSystem.GetFileNameWithoutExtension(item.Path) + "-thumb" + extension;
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
|
|
|
|
return new[] { Path.Combine(seasonFolder, imageFilename) };
|
2013-10-15 15:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 23:30:41 +00:00
|
|
|
|
if (item.IsInMixedFolder || item is MusicVideo)
|
2013-10-15 15:29:19 +00:00
|
|
|
|
{
|
2013-10-15 22:17:08 +00:00
|
|
|
|
return new[] { GetSavePathForItemInMixedFolder(item, type, string.Empty, extension) };
|
2013-10-15 15:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 20:48:26 +00:00
|
|
|
|
if (item is MusicAlbum || item is MusicArtist)
|
2013-11-19 17:17:14 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return new[] { Path.Combine(item.ContainingFolderPath, "folder" + extension) };
|
2013-11-19 17:17:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return new[] { Path.Combine(item.ContainingFolderPath, "poster" + extension) };
|
2013-10-15 22:17:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == ImageType.Banner)
|
|
|
|
|
{
|
2014-02-15 22:42:06 +00:00
|
|
|
|
if (season != null && season.IndexNumber.HasValue)
|
2013-10-15 22:17:08 +00:00
|
|
|
|
{
|
2013-12-04 14:52:38 +00:00
|
|
|
|
var seriesFolder = season.SeriesPath;
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
2014-02-15 22:42:06 +00:00
|
|
|
|
var seasonMarker = season.IndexNumber.Value == 0
|
2013-10-15 22:17:08 +00:00
|
|
|
|
? "-specials"
|
2014-02-15 22:42:06 +00:00
|
|
|
|
: season.IndexNumber.Value.ToString("00", UsCulture);
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
|
|
|
|
var imageFilename = "season" + seasonMarker + "-banner" + extension;
|
|
|
|
|
|
|
|
|
|
return new[] { Path.Combine(seriesFolder, imageFilename) };
|
|
|
|
|
}
|
2013-10-15 15:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 22:17:08 +00:00
|
|
|
|
if (type == ImageType.Thumb)
|
|
|
|
|
{
|
2014-02-15 22:42:06 +00:00
|
|
|
|
if (season != null && season.IndexNumber.HasValue)
|
2013-10-15 22:17:08 +00:00
|
|
|
|
{
|
2013-12-04 14:52:38 +00:00
|
|
|
|
var seriesFolder = season.SeriesPath;
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
2014-02-15 22:42:06 +00:00
|
|
|
|
var seasonMarker = season.IndexNumber.Value == 0
|
2013-10-15 22:17:08 +00:00
|
|
|
|
? "-specials"
|
2014-02-15 22:42:06 +00:00
|
|
|
|
: season.IndexNumber.Value.ToString("00", UsCulture);
|
2013-10-15 22:17:08 +00:00
|
|
|
|
|
|
|
|
|
var imageFilename = "season" + seasonMarker + "-landscape" + extension;
|
|
|
|
|
|
|
|
|
|
return new[] { Path.Combine(seriesFolder, imageFilename) };
|
|
|
|
|
}
|
2013-12-27 00:24:23 +00:00
|
|
|
|
|
|
|
|
|
if (item.IsInMixedFolder)
|
|
|
|
|
{
|
|
|
|
|
return new[] { GetSavePathForItemInMixedFolder(item, type, "landscape", extension) };
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return new[] { Path.Combine(item.ContainingFolderPath, "landscape" + extension) };
|
2013-10-15 22:17:08 +00:00
|
|
|
|
}
|
2013-10-16 01:44:23 +00:00
|
|
|
|
|
2013-10-15 15:29:19 +00:00
|
|
|
|
// All other paths are the same
|
2013-10-17 15:35:39 +00:00
|
|
|
|
return new[] { GetStandardSavePath(item, type, imageIndex, mimeType, true) };
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
2013-08-15 19:09:52 +00:00
|
|
|
|
|
2014-07-04 02:22:57 +00:00
|
|
|
|
private bool EnableExtraThumbsDuplication
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var config = _config.GetConfiguration<XbmcMetadataOptions>("xbmcmetadata");
|
|
|
|
|
|
|
|
|
|
return config.EnableExtraThumbsDuplication;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-15 15:29:19 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the save path for item in mixed folder.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="imageFilename">The image filename.</param>
|
|
|
|
|
/// <param name="extension">The extension.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
2013-12-19 21:51:32 +00:00
|
|
|
|
private string GetSavePathForItemInMixedFolder(IHasImages item, ImageType type, string imageFilename, string extension)
|
2013-08-15 19:09:52 +00:00
|
|
|
|
{
|
|
|
|
|
if (type == ImageType.Primary)
|
|
|
|
|
{
|
2013-10-15 15:29:19 +00:00
|
|
|
|
imageFilename = "poster";
|
2013-08-15 19:09:52 +00:00
|
|
|
|
}
|
|
|
|
|
var folder = Path.GetDirectoryName(item.Path);
|
|
|
|
|
|
2014-07-26 17:30:15 +00:00
|
|
|
|
return Path.Combine(folder, _fileSystem.GetFileNameWithoutExtension(item.Path) + "-" + imageFilename + extension);
|
2013-08-15 19:09:52 +00:00
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|