From 8ea02ee02068f7e7361e143301b51ca57e45615a Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 30 Oct 2015 12:45:22 -0400 Subject: [PATCH] update dynamic images --- .../Images/CollectionFolderImageProvider.cs | 2 +- .../Images/ImagesByNameImageProvider.cs | 2 +- .../InternalMetadataFolderImageProvider.cs | 4 +- .../Images/LocalImageProvider.cs | 35 ++------- MediaBrowser.Providers/Manager/ImageSaver.cs | 16 +---- .../Photos/BaseDynamicImageProvider.cs | 72 +++---------------- .../Photos/PhotoAlbumImageProvider.cs | 2 +- 7 files changed, 20 insertions(+), 113 deletions(-) diff --git a/MediaBrowser.LocalMetadata/Images/CollectionFolderImageProvider.cs b/MediaBrowser.LocalMetadata/Images/CollectionFolderImageProvider.cs index de2f55161..0e04fd9ec 100644 --- a/MediaBrowser.LocalMetadata/Images/CollectionFolderImageProvider.cs +++ b/MediaBrowser.LocalMetadata/Images/CollectionFolderImageProvider.cs @@ -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); } } } diff --git a/MediaBrowser.LocalMetadata/Images/ImagesByNameImageProvider.cs b/MediaBrowser.LocalMetadata/Images/ImagesByNameImageProvider.cs index dc6f2ae93..833a8a985 100644 --- a/MediaBrowser.LocalMetadata/Images/ImagesByNameImageProvider.cs +++ b/MediaBrowser.LocalMetadata/Images/ImagesByNameImageProvider.cs @@ -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) { diff --git a/MediaBrowser.LocalMetadata/Images/InternalMetadataFolderImageProvider.cs b/MediaBrowser.LocalMetadata/Images/InternalMetadataFolderImageProvider.cs index 664603315..cdf29a5fe 100644 --- a/MediaBrowser.LocalMetadata/Images/InternalMetadataFolderImageProvider.cs +++ b/MediaBrowser.LocalMetadata/Images/InternalMetadataFolderImageProvider.cs @@ -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) { diff --git a/MediaBrowser.LocalMetadata/Images/LocalImageProvider.cs b/MediaBrowser.LocalMetadata/Images/LocalImageProvider.cs index 3c9736429..7e28484a2 100644 --- a/MediaBrowser.LocalMetadata/Images/LocalImageProvider.cs +++ b/MediaBrowser.LocalMetadata/Images/LocalImageProvider.cs @@ -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 GetImages(IHasImages item, string path, bool checkForCacheKeyFiles, IDirectoryService directoryService) + public List GetImages(IHasImages item, string path, IDirectoryService directoryService) { - return GetImages(item, new[] { path }, checkForCacheKeyFiles, directoryService); + return GetImages(item, new[] { path }, directoryService); } - public List GetImages(IHasImages item, IEnumerable paths, bool checkForCacheKeyFiles, IDirectoryService directoryService) + public List GetImages(IHasImages item, IEnumerable 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 files, List 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 files, string name) { return files.FirstOrDefault(i => ((i.Attributes & FileAttributes.Directory) != FileAttributes.Directory) && string.Equals(name, _fileSystem.GetFileNameWithoutExtension(i), StringComparison.OrdinalIgnoreCase)); diff --git a/MediaBrowser.Providers/Manager/ImageSaver.cs b/MediaBrowser.Providers/Manager/ImageSaver.cs index 9d183c880..e8eece299 100644 --- a/MediaBrowser.Providers/Manager/ImageSaver.cs +++ b/MediaBrowser.Providers/Manager/ImageSaver.cs @@ -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); - } - /// /// Saves the image to location. /// diff --git a/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs b/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs index 2402d3ec1..fd34e3248 100644 --- a/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs +++ b/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs @@ -76,20 +76,13 @@ namespace MediaBrowser.Server.Implementations.Photos protected async Task 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 FetchToFileInternal(IHasImages item, List 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> GetItemsWithImages(IHasImages item); - private const string Version = "32"; - protected string GetConfigurationCacheKey(List 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 CreateThumbCollage(IHasImages primaryItem, List 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); diff --git a/MediaBrowser.Server.Implementations/Photos/PhotoAlbumImageProvider.cs b/MediaBrowser.Server.Implementations/Photos/PhotoAlbumImageProvider.cs index 32b046d3a..3abb7e70e 100644 --- a/MediaBrowser.Server.Implementations/Photos/PhotoAlbumImageProvider.cs +++ b/MediaBrowser.Server.Implementations/Photos/PhotoAlbumImageProvider.cs @@ -21,7 +21,7 @@ namespace MediaBrowser.Server.Implementations.Photos protected override Task> GetItemsWithImages(IHasImages item) { var photoAlbum = (PhotoAlbum)item; - var items = GetFinalItems(photoAlbum.Children.ToList(), 1); + var items = GetFinalItems(photoAlbum.Children.ToList()); return Task.FromResult(items); }