Merge pull request #1241 from MediaBrowser/master

update dynamic images
This commit is contained in:
Luke 2015-10-30 12:47:42 -04:00
commit 0b211b0604
7 changed files with 20 additions and 113 deletions

View File

@ -38,7 +38,7 @@ namespace MediaBrowser.LocalMetadata.Images
{
var collectionFolder = (CollectionFolder)item;
return new LocalImageProvider(_fileSystem).GetImages(item, collectionFolder.PhysicalLocations, false, directoryService);
return new LocalImageProvider(_fileSystem).GetImages(item, collectionFolder.PhysicalLocations, directoryService);
}
}
}

View File

@ -46,7 +46,7 @@ namespace MediaBrowser.LocalMetadata.Images
try
{
return new LocalImageProvider(_fileSystem).GetImages(item, path, false, directoryService);
return new LocalImageProvider(_fileSystem).GetImages(item, path, directoryService);
}
catch (DirectoryNotFoundException)
{

View File

@ -47,7 +47,7 @@ namespace MediaBrowser.LocalMetadata.Images
{
return false;
}
return true;
}
@ -66,7 +66,7 @@ namespace MediaBrowser.LocalMetadata.Images
try
{
return new LocalImageProvider(_fileSystem).GetImages(item, path, true, directoryService);
return new LocalImageProvider(_fileSystem).GetImages(item, path, directoryService);
}
catch (DirectoryNotFoundException)
{

View File

@ -1,5 +1,4 @@
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
@ -97,12 +96,12 @@ namespace MediaBrowser.LocalMetadata.Images
return list;
}
public List<LocalImageInfo> GetImages(IHasImages item, string path, bool checkForCacheKeyFiles, IDirectoryService directoryService)
public List<LocalImageInfo> GetImages(IHasImages item, string path, IDirectoryService directoryService)
{
return GetImages(item, new[] { path }, checkForCacheKeyFiles, directoryService);
return GetImages(item, new[] { path }, directoryService);
}
public List<LocalImageInfo> GetImages(IHasImages item, IEnumerable<string> paths, bool checkForCacheKeyFiles, IDirectoryService directoryService)
public List<LocalImageInfo> GetImages(IHasImages item, IEnumerable<string> paths, IDirectoryService directoryService)
{
var files = paths.SelectMany(directoryService.GetFiles)
.Where(i =>
@ -119,12 +118,6 @@ namespace MediaBrowser.LocalMetadata.Images
PopulateImages(item, list, files, false, directoryService);
if (checkForCacheKeyFiles)
{
AddCacheKeyImage(files, list, ImageType.Primary);
AddCacheKeyImage(files, list, ImageType.Thumb);
}
return list;
}
@ -383,26 +376,6 @@ namespace MediaBrowser.LocalMetadata.Images
return false;
}
private void AddCacheKeyImage(IEnumerable<FileSystemMetadata> files, List<LocalImageInfo> images, ImageType type)
{
var candidates = files
.Where(i => _fileSystem.GetFileNameWithoutExtension(i).StartsWith(type.ToString() + "_key_", StringComparison.OrdinalIgnoreCase))
.ToList();
var image = BaseItem.SupportedImageExtensions
.Select(i => candidates.FirstOrDefault(c => string.Equals(c.Extension, i, StringComparison.OrdinalIgnoreCase)))
.FirstOrDefault(i => i != null);
if (image != null)
{
images.Add(new LocalImageInfo
{
FileInfo = image,
Type = type
});
}
}
private FileSystemMetadata GetImage(IEnumerable<FileSystemMetadata> files, string name)
{
return files.FirstOrDefault(i => ((i.Attributes & FileAttributes.Directory) != FileAttributes.Directory) && string.Equals(name, _fileSystem.GetFileNameWithoutExtension(i), StringComparison.OrdinalIgnoreCase));

View File

@ -119,13 +119,9 @@ namespace MediaBrowser.Providers.Manager
var index = imageIndex ?? 0;
var paths = !string.IsNullOrEmpty(internalCacheKey) ?
new[] { GetCacheKeyPath(item, type, mimeType, internalCacheKey) } :
GetSavePaths(item, type, imageIndex, mimeType, saveLocally);
var paths = GetSavePaths(item, type, imageIndex, mimeType, saveLocally);
var retryPaths = !string.IsNullOrEmpty(internalCacheKey) ?
new[] { GetCacheKeyPath(item, type, mimeType, internalCacheKey) } :
GetSavePaths(item, type, imageIndex, mimeType, false);
var retryPaths = GetSavePaths(item, type, imageIndex, mimeType, false);
// If there are more than one output paths, the stream will need to be seekable
var memoryStream = new MemoryStream();
@ -196,7 +192,7 @@ namespace MediaBrowser.Providers.Manager
}
catch (UnauthorizedAccessException)
{
var retry = !string.IsNullOrWhiteSpace(retryPath) &&
var retry = !string.IsNullOrWhiteSpace(retryPath) &&
!string.Equals(path, retryPath, StringComparison.OrdinalIgnoreCase);
if (retry)
@ -213,12 +209,6 @@ namespace MediaBrowser.Providers.Manager
await SaveImageToLocation(source, retryPath, cancellationToken).ConfigureAwait(false);
}
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);
}
/// <summary>
/// Saves the image to location.
/// </summary>

View File

@ -76,20 +76,13 @@ namespace MediaBrowser.Server.Implementations.Photos
protected async Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
var items = await GetItemsWithImages(item).ConfigureAwait(false);
var cacheKey = GetConfigurationCacheKey(items, item.Name);
if (!HasChanged(item, imageType, cacheKey))
{
return ItemUpdateType.None;
}
return await FetchToFileInternal(item, items, imageType, cacheKey, cancellationToken).ConfigureAwait(false);
return await FetchToFileInternal(item, items, imageType, cancellationToken).ConfigureAwait(false);
}
protected async Task<ItemUpdateType> FetchToFileInternal(IHasImages item,
List<BaseItem> itemsWithImages,
ImageType imageType,
string cacheKey,
CancellationToken cancellationToken)
{
var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
@ -101,22 +94,13 @@ namespace MediaBrowser.Server.Implementations.Photos
return ItemUpdateType.None;
}
await ProviderManager.SaveImage(item, outputPath, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
await ProviderManager.SaveImage(item, outputPath, "image/png", imageType, null, Guid.NewGuid().ToString("N"), cancellationToken).ConfigureAwait(false);
return ItemUpdateType.ImageUpdate;
}
protected abstract Task<List<BaseItem>> GetItemsWithImages(IHasImages item);
private const string Version = "32";
protected string GetConfigurationCacheKey(List<BaseItem> items, string itemName)
{
var parts = Version + "_" + (itemName ?? string.Empty) + "_" +
string.Join(",", items.Select(i => i.Id.ToString("N")).ToArray());
return parts.GetMD5().ToString("N");
}
protected Task<string> CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
{
return CreateCollage(primaryItem, items, outputPath, 640, 360);
@ -224,7 +208,10 @@ namespace MediaBrowser.Server.Implementations.Photos
throw new ArgumentException("Unexpected image type");
}
private const int MaxImageAgeDays = 7;
protected virtual int MaxImageAgeDays
{
get { return 7; }
}
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
{
@ -235,28 +222,11 @@ namespace MediaBrowser.Server.Implementations.Photos
var supportedImages = GetSupportedImages(item).ToList();
if (item is UserView || item is ICollectionFolder)
{
if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
{
return true;
}
if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
{
return true;
}
return false;
}
var items = GetItemsWithImages(item).Result;
var cacheKey = GetConfigurationCacheKey(items, item.Name);
if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary, cacheKey))
if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
{
return true;
}
if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb, cacheKey))
if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
{
return true;
}
@ -264,32 +234,6 @@ namespace MediaBrowser.Server.Implementations.Photos
return false;
}
protected bool HasChanged(IHasImages item, ImageType type, string cacheKey)
{
var image = item.GetImageInfo(type, 0);
if (image != null)
{
if (!image.IsLocalFile)
{
return false;
}
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
{
return false;
}
var currentPathCacheKey = (Path.GetFileNameWithoutExtension(image.Path) ?? string.Empty).Split('_').LastOrDefault();
if (string.Equals(cacheKey, currentPathCacheKey, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
return true;
}
protected bool HasChanged(IHasImages item, ImageType type)
{
var image = item.GetImageInfo(type, 0);

View File

@ -21,7 +21,7 @@ namespace MediaBrowser.Server.Implementations.Photos
protected override Task<List<BaseItem>> GetItemsWithImages(IHasImages item)
{
var photoAlbum = (PhotoAlbum)item;
var items = GetFinalItems(photoAlbum.Children.ToList(), 1);
var items = GetFinalItems(photoAlbum.Children.ToList());
return Task.FromResult(items);
}