rework dynamic image provider
This commit is contained in:
parent
fc115a0c2b
commit
cf0456360b
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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<T> : IHasChangeMonitor, IForcedProvider, IDynamicImageProvider, IHasOrder
|
||||
public abstract class BaseDynamicImageProvider<T> : IHasChangeMonitor, IForcedProvider, ICustomMetadataProvider<T>, 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<ImageType> GetSupportedImages(IHasImages item)
|
||||
|
@ -43,12 +44,75 @@ namespace MediaBrowser.Server.Implementations.Photos
|
|||
};
|
||||
}
|
||||
|
||||
public async Task<ItemUpdateType> 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<ItemUpdateType> 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<ItemUpdateType> FetchToFileInternal(IHasImages item,
|
||||
List<BaseItem> 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<DynamicImageResponse> 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<Stream> FetchAsyncInternal(IHasImages item,
|
||||
List<BaseItem> itemsWithImages,
|
||||
ImageType imageType,
|
||||
string cacheKey,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return CreateImageAsync(item, itemsWithImages, imageType, 0);
|
||||
}
|
||||
|
||||
protected Task<Stream> GetThumbCollage(List<BaseItem> items)
|
||||
{
|
||||
var files = items
|
||||
|
@ -163,7 +218,7 @@ namespace MediaBrowser.Server.Implementations.Photos
|
|||
return GetFinalItems(items, 4);
|
||||
}
|
||||
|
||||
protected List<BaseItem> GetFinalItems(List<BaseItem> items, int limit)
|
||||
protected virtual List<BaseItem> GetFinalItems(List<BaseItem> items, int limit)
|
||||
{
|
||||
// Rotate the images no more than once per week
|
||||
var random = new Random(GetWeekOfYear()).Next();
|
||||
|
|
|
@ -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<UserView>
|
||||
public class DynamicImageProvider : BaseDynamicImageProvider<UserView>, IPreRefreshProvider
|
||||
{
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
|
|
Loading…
Reference in New Issue
Block a user