diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index c47de7cb8..d7ffcf79a 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -1673,13 +1673,13 @@ namespace MediaBrowser.Server.Implementations.Library throw new ArgumentNullException("viewType"); } - var id = GetNewItemId("27_namedview_" + name + user.Id.ToString("N") + (parentId ?? string.Empty), typeof(UserView)); + var id = GetNewItemId("30_namedview_" + name + user.Id.ToString("N") + (parentId ?? string.Empty), typeof(UserView)); var path = Path.Combine(ConfigurationManager.ApplicationPaths.InternalMetadataPath, "views", id.ToString("N")); var item = GetItemById(id) as UserView; - var refresh = false; + var isNew = false; if (item == null) { @@ -1703,17 +1703,13 @@ namespace MediaBrowser.Server.Implementations.Library await CreateItem(item, cancellationToken).ConfigureAwait(false); - refresh = true; + isNew = true; } - if (!refresh) - { - refresh = (DateTime.UtcNow - item.DateLastSaved).TotalHours >= 24; - } + var refresh = isNew || (DateTime.UtcNow - item.DateLastSaved).TotalHours >= 24; if (refresh) { - await item.UpdateToRepository(ItemUpdateType.MetadataImport, CancellationToken.None).ConfigureAwait(false); _providerManagerFactory().QueueRefresh(item.Id, new MetadataRefreshOptions()); } diff --git a/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs b/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs index 9cae36283..b3b79ae7e 100644 --- a/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs +++ b/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs @@ -2,6 +2,7 @@ using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Drawing; using MediaBrowser.Model.Entities; @@ -15,7 +16,7 @@ using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.Photos { - public abstract class BaseDynamicImageProvider : IHasChangeMonitor, IForcedProvider, IDynamicImageProvider, IHasOrder + public abstract class BaseDynamicImageProvider : IHasChangeMonitor, IForcedProvider, ICustomMetadataProvider, IHasOrder where T : IHasMetadata { protected IFileSystem FileSystem { get; private set; } @@ -31,7 +32,7 @@ namespace MediaBrowser.Server.Implementations.Photos public virtual bool Supports(IHasImages item) { - return item is T; + return true; } public virtual IEnumerable GetSupportedImages(IHasImages item) @@ -43,12 +44,75 @@ namespace MediaBrowser.Server.Implementations.Photos }; } + public async Task FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken) + { + if (!Supports(item)) + { + return ItemUpdateType.None; + } + + var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false); + var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false); + + return primaryResult | thumbResult; + } + + protected async Task FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken) + { + var items = await GetItemsWithImages(item).ConfigureAwait(false); + var cacheKey = GetConfigurationCacheKey(items); + + if (!HasChanged(item, imageType, cacheKey)) + { + return ItemUpdateType.None; + } + + return await FetchToFileInternal(item, items, imageType, cacheKey, cancellationToken).ConfigureAwait(false); + } + + protected async Task FetchToFileInternal(IHasImages item, + List itemsWithImages, + ImageType imageType, + string cacheKey, + CancellationToken cancellationToken) + { + var stream = await CreateImageAsync(item, itemsWithImages, imageType, 0).ConfigureAwait(false); + + if (stream == null) + { + return ItemUpdateType.None; + } + + if (stream is MemoryStream) + { + using (stream) + { + stream.Position = 0; + + await ProviderManager.SaveImage(item, stream, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false); + } + } + else + { + using (var ms = new MemoryStream()) + { + await stream.CopyToAsync(ms).ConfigureAwait(false); + + ms.Position = 0; + + await ProviderManager.SaveImage(item, ms, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false); + } + } + + return ItemUpdateType.ImageUpdate; + } + public async Task GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken) { var items = await GetItemsWithImages(item).ConfigureAwait(false); var cacheKey = GetConfigurationCacheKey(items); - var result = await FetchAsyncInternal(item, items, type, cacheKey, cancellationToken).ConfigureAwait(false); + var result = await CreateImageAsync(item, items, type, 0).ConfigureAwait(false); return new DynamicImageResponse { @@ -67,15 +131,6 @@ namespace MediaBrowser.Server.Implementations.Photos return (Version + "_" + string.Join(",", items.Select(i => i.Id.ToString("N")).ToArray())).GetMD5().ToString("N"); } - protected Task FetchAsyncInternal(IHasImages item, - List itemsWithImages, - ImageType imageType, - string cacheKey, - CancellationToken cancellationToken) - { - return CreateImageAsync(item, itemsWithImages, imageType, 0); - } - protected Task GetThumbCollage(List items) { var files = items @@ -163,7 +218,7 @@ namespace MediaBrowser.Server.Implementations.Photos return GetFinalItems(items, 4); } - protected List GetFinalItems(List items, int limit) + protected virtual List GetFinalItems(List items, int limit) { // Rotate the images no more than once per week var random = new Random(GetWeekOfYear()).Next(); diff --git a/MediaBrowser.Server.Implementations/UserViews/DynamicImageProvider.cs b/MediaBrowser.Server.Implementations/UserViews/DynamicImageProvider.cs index 6cb7a0339..c47a116ca 100644 --- a/MediaBrowser.Server.Implementations/UserViews/DynamicImageProvider.cs +++ b/MediaBrowser.Server.Implementations/UserViews/DynamicImageProvider.cs @@ -1,5 +1,4 @@ -using System.Globalization; -using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.IO; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; @@ -11,13 +10,14 @@ using MediaBrowser.Server.Implementations.Photos; using MoreLinq; using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.UserViews { - public class DynamicImageProvider : BaseDynamicImageProvider + public class DynamicImageProvider : BaseDynamicImageProvider, IPreRefreshProvider { private readonly IUserManager _userManager; private readonly ILibraryManager _libraryManager;