mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-20 00:55:13 +01:00
improve user view images
This commit is contained in:
@@ -530,8 +530,8 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
return item;
|
||||
}
|
||||
|
||||
public BaseItem ResolvePath(FileSystemInfo fileInfo,
|
||||
Folder parent = null,
|
||||
public BaseItem ResolvePath(FileSystemInfo fileInfo,
|
||||
Folder parent = null,
|
||||
string collectionType = null)
|
||||
{
|
||||
return ResolvePath(fileInfo, new DirectoryService(_logger), parent, collectionType);
|
||||
@@ -1190,7 +1190,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the intros.
|
||||
/// </summary>
|
||||
@@ -1508,27 +1508,22 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
return collectionTypes.Count == 1 ? collectionTypes[0] : null;
|
||||
}
|
||||
|
||||
public Task<UserView> GetNamedView(string name, string type, string sortName, CancellationToken cancellationToken)
|
||||
{
|
||||
return GetNamedView(name, null, type, sortName, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<UserView> GetNamedView(string name, string category, string type, string sortName, CancellationToken cancellationToken)
|
||||
public async Task<UserView> GetNamedView(string name,
|
||||
string type,
|
||||
string sortName,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var path = Path.Combine(ConfigurationManager.ApplicationPaths.ItemsByNamePath,
|
||||
"views");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(category))
|
||||
{
|
||||
path = Path.Combine(path, _fileSystem.GetValidFilename(category));
|
||||
}
|
||||
|
||||
path = Path.Combine(path, _fileSystem.GetValidFilename(type));
|
||||
|
||||
var id = (path + "_namedview_" + name).GetMBId(typeof(UserView));
|
||||
|
||||
var item = GetItemById(id) as UserView;
|
||||
|
||||
var refresh = false;
|
||||
|
||||
if (item == null ||
|
||||
!string.Equals(item.Path, path, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
@@ -1546,7 +1541,89 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
|
||||
await CreateItem(item, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
|
||||
refresh = true;
|
||||
}
|
||||
|
||||
if (!refresh && item != null)
|
||||
{
|
||||
refresh = (DateTime.UtcNow - item.DateLastSaved).TotalHours >= 24;
|
||||
}
|
||||
|
||||
if (refresh)
|
||||
{
|
||||
await item.RefreshMetadata(new MetadataRefreshOptions
|
||||
{
|
||||
ForceSave = true
|
||||
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<UserView> GetSpecialFolder(User user,
|
||||
string name,
|
||||
string parentId,
|
||||
string viewType,
|
||||
string sortName,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
throw new ArgumentNullException("name");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(parentId))
|
||||
{
|
||||
throw new ArgumentNullException("parentId");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(viewType))
|
||||
{
|
||||
throw new ArgumentNullException("viewType");
|
||||
}
|
||||
|
||||
var id = ("7_namedview_" + name + user.Id.ToString("N") + parentId).GetMBId(typeof(UserView));
|
||||
|
||||
var path = BaseItem.GetInternalMetadataPathForId(id);
|
||||
|
||||
var item = GetItemById(id) as UserView;
|
||||
|
||||
var refresh = false;
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
|
||||
item = new UserView
|
||||
{
|
||||
Path = path,
|
||||
Id = id,
|
||||
DateCreated = DateTime.UtcNow,
|
||||
Name = name,
|
||||
ViewType = viewType,
|
||||
ForcedSortName = sortName,
|
||||
UserId = user.Id,
|
||||
ParentId = new Guid(parentId)
|
||||
};
|
||||
|
||||
await CreateItem(item, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
refresh = true;
|
||||
}
|
||||
|
||||
if (!refresh && item != null)
|
||||
{
|
||||
refresh = (DateTime.UtcNow - item.DateLastSaved).TotalHours >= 24;
|
||||
}
|
||||
|
||||
if (refresh)
|
||||
{
|
||||
await item.RefreshMetadata(new MetadataRefreshOptions
|
||||
{
|
||||
ForceSave = true
|
||||
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return item;
|
||||
|
||||
@@ -68,24 +68,24 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase)) ||
|
||||
foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
|
||||
{
|
||||
list.Add(await GetUserView(CollectionType.TvShows, user, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
list.Add(await GetUserView(CollectionType.TvShows, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)) ||
|
||||
foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
list.Add(await GetUserView(CollectionType.Music, user, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
list.Add(await GetUserView(CollectionType.Music, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase)) ||
|
||||
foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
|
||||
{
|
||||
list.Add(await GetUserView(CollectionType.Movies, user, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
list.Add(await GetUserView(CollectionType.Movies, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Games, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
list.Add(await GetUserView(CollectionType.Games, user, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
list.Add(await GetUserView(CollectionType.Games, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
if (user.Configuration.DisplayCollectionsView &&
|
||||
@@ -93,7 +93,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
.Except(standaloneFolders)
|
||||
.SelectMany(i => i.GetRecursiveChildren(user, false)).OfType<BoxSet>().Any())
|
||||
{
|
||||
list.Add(await GetUserView(CollectionType.BoxSets, user, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
list.Add(await GetUserView(CollectionType.BoxSets, string.Empty, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Playlists, StringComparison.OrdinalIgnoreCase)))
|
||||
@@ -103,7 +103,7 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
|
||||
if (user.Configuration.DisplayFoldersView)
|
||||
{
|
||||
list.Add(await GetUserView(CollectionType.Folders, user, "zz_" + CollectionType.Folders, cancellationToken).ConfigureAwait(false));
|
||||
list.Add(await GetUserView(CollectionType.Folders, "zz_" + CollectionType.Folders, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
if (query.IncludeExternalContent)
|
||||
@@ -148,16 +148,18 @@ namespace MediaBrowser.Server.Implementations.Library
|
||||
.ThenBy(i => i.SortName);
|
||||
}
|
||||
|
||||
public Task<UserView> GetUserView(string category, string type, User user, string sortName, CancellationToken cancellationToken)
|
||||
public Task<UserView> GetUserView(string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
|
||||
{
|
||||
var name = _localizationManager.GetLocalizedString("ViewType" + type);
|
||||
|
||||
return _libraryManager.GetNamedView(name, category, type, sortName, cancellationToken);
|
||||
return _libraryManager.GetSpecialFolder(user, name, parentId, type, sortName, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<UserView> GetUserView(string type, User user, string sortName, CancellationToken cancellationToken)
|
||||
public Task<UserView> GetUserView(string type, string sortName, CancellationToken cancellationToken)
|
||||
{
|
||||
return GetUserView(null, type, user, sortName, cancellationToken);
|
||||
var name = _localizationManager.GetLocalizedString("ViewType" + type);
|
||||
|
||||
return _libraryManager.GetNamedView(name, type, sortName, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -830,7 +830,7 @@
|
||||
"HeaderWelcomeToMediaBrowserWebClient": "Welcome to the Media Browser Web Client",
|
||||
"ButtonDismiss": "Dismiss",
|
||||
"ButtonTakeTheTour": "Take the tour",
|
||||
"ButtonEditOtherUserPreferences": "Edit this user's profile and personal preferences.",
|
||||
"ButtonEditOtherUserPreferences": "Edit this user's profile, password and personal preferences.",
|
||||
"LabelChannelStreamQuality": "Preferred internet stream quality:",
|
||||
"LabelChannelStreamQualityHelp": "In a low bandwidth environment, limiting quality can help ensure a smooth streaming experience.",
|
||||
"OptionBestAvailableStreamQuality": "Best available",
|
||||
@@ -1260,5 +1260,8 @@
|
||||
"OptionDisableUserPreferences": "Disable access to user preferences",
|
||||
"OptionDisableUserPreferencesHelp": "If enabled, only administrators will be able to configure user profile images, passwords, and language preferences.",
|
||||
"HeaderSelectServer": "Select Server",
|
||||
"MessageNoServersAvailableToConnect": "No servers are available to connect to. If you've been invited to share a server, make sure to confirm it by clicking the link in the email."
|
||||
"MessageNoServersAvailableToConnect": "No servers are available to connect to. If you've been invited to share a server, make sure to confirm it by clicking the link in the email.",
|
||||
"TitleNewUser": "New User",
|
||||
"ButtonConfigurePassword": "Configure Password",
|
||||
"HeaderDashboardUserPassword": "User passwords are managed within each user's personal profile settings."
|
||||
}
|
||||
|
||||
@@ -226,6 +226,7 @@
|
||||
<Compile Include="LiveTv\RefreshChannelsScheduledTask.cs" />
|
||||
<Compile Include="Localization\LocalizationManager.cs" />
|
||||
<Compile Include="MediaEncoder\EncodingManager.cs" />
|
||||
<Compile Include="Music\MusicDynamicImageProvider.cs" />
|
||||
<Compile Include="News\NewsEntryPoint.cs" />
|
||||
<Compile Include="News\NewsService.cs" />
|
||||
<Compile Include="Notifications\CoreNotificationTypes.cs" />
|
||||
@@ -240,6 +241,8 @@
|
||||
<Compile Include="Persistence\SqliteProviderInfoRepository.cs" />
|
||||
<Compile Include="Persistence\SqliteShrinkMemoryTimer.cs" />
|
||||
<Compile Include="Persistence\TypeMapper.cs" />
|
||||
<Compile Include="Photos\BaseDynamicImageProvider.cs" />
|
||||
<Compile Include="Photos\DynamicImageHelpers.cs" />
|
||||
<Compile Include="Playlists\ManualPlaylistsFolder.cs" />
|
||||
<Compile Include="Photos\PhotoAlbumImageProvider.cs" />
|
||||
<Compile Include="Playlists\PlaylistImageProvider.cs" />
|
||||
@@ -515,6 +518,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
|
||||
</Target>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
using MediaBrowser.Common.IO;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Server.Implementations.Photos;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.Music
|
||||
{
|
||||
public class MusicDynamicImageProvider : BaseDynamicImageProvider<UserView>, ICustomMetadataProvider<UserView>
|
||||
{
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
public MusicDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IUserManager userManager)
|
||||
: base(fileSystem, providerManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
protected override async Task<List<BaseItem>> GetItemsWithImages(IHasImages item)
|
||||
{
|
||||
var view = (UserView)item;
|
||||
|
||||
if (!view.UserId.HasValue)
|
||||
{
|
||||
return new List<BaseItem>();
|
||||
}
|
||||
|
||||
var result = await view.GetItems(new InternalItemsQuery
|
||||
{
|
||||
User = _userManager.GetUserById(view.UserId.Value)
|
||||
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
return GetFinalItems(result.Items.Where(i => i.HasImage(ImageType.Primary)).ToList());
|
||||
}
|
||||
|
||||
protected override bool Supports(IHasImages item)
|
||||
{
|
||||
var view = item as UserView;
|
||||
|
||||
if (view != null && view.UserId.HasValue)
|
||||
{
|
||||
var supported = new[]
|
||||
{
|
||||
SpecialFolder.TvFavoriteEpisodes,
|
||||
SpecialFolder.TvFavoriteSeries,
|
||||
SpecialFolder.TvGenres,
|
||||
SpecialFolder.TvLatest,
|
||||
SpecialFolder.TvNextUp,
|
||||
SpecialFolder.TvResume,
|
||||
SpecialFolder.TvShowSeries,
|
||||
|
||||
SpecialFolder.MovieCollections,
|
||||
SpecialFolder.MovieFavorites,
|
||||
SpecialFolder.MovieGenres,
|
||||
SpecialFolder.MovieLatest,
|
||||
SpecialFolder.MovieMovies,
|
||||
SpecialFolder.MovieResume,
|
||||
|
||||
SpecialFolder.GameFavorites,
|
||||
SpecialFolder.GameGenres,
|
||||
SpecialFolder.GameSystems,
|
||||
SpecialFolder.LatestGames,
|
||||
SpecialFolder.RecentlyPlayedGames,
|
||||
|
||||
SpecialFolder.MusicArtists,
|
||||
SpecialFolder.MusicAlbumArtists,
|
||||
SpecialFolder.MusicAlbums,
|
||||
SpecialFolder.MusicGenres,
|
||||
SpecialFolder.MusicLatest,
|
||||
SpecialFolder.MusicSongs,
|
||||
SpecialFolder.MusicFavorites,
|
||||
SpecialFolder.MusicFavoriteArtists,
|
||||
SpecialFolder.MusicFavoriteAlbums,
|
||||
SpecialFolder.MusicFavoriteSongs
|
||||
};
|
||||
|
||||
return supported.Contains(view.ViewType, StringComparer.OrdinalIgnoreCase) &&
|
||||
_userManager.GetUserById(view.UserId.Value) != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Common.IO;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.Photos
|
||||
{
|
||||
public abstract class BaseDynamicImageProvider<T> : IHasChangeMonitor
|
||||
where T : IHasImages
|
||||
{
|
||||
protected IFileSystem FileSystem { get; private set; }
|
||||
protected IProviderManager ProviderManager { get; private set; }
|
||||
|
||||
protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager)
|
||||
{
|
||||
ProviderManager = providerManager;
|
||||
FileSystem = fileSystem;
|
||||
}
|
||||
|
||||
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 virtual bool Supports(IHasImages item)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected abstract Task<List<BaseItem>> GetItemsWithImages(IHasImages item);
|
||||
|
||||
private const string Version = "3";
|
||||
protected string GetConfigurationCacheKey(List<BaseItem> items)
|
||||
{
|
||||
return (Version + "_" + string.Join(",", items.Select(i => i.Id.ToString("N")).ToArray())).GetMD5().ToString("N");
|
||||
}
|
||||
|
||||
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 FetchAsyncInternal(item, items, imageType, cacheKey, options, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected async Task<ItemUpdateType> FetchAsyncInternal(IHasImages item,
|
||||
List<BaseItem> itemsWithImages,
|
||||
ImageType imageType,
|
||||
string cacheKey,
|
||||
MetadataRefreshOptions options,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var img = await CreateImageAsync(item, itemsWithImages, imageType, 0).ConfigureAwait(false);
|
||||
|
||||
if (img == null)
|
||||
{
|
||||
return ItemUpdateType.None;
|
||||
}
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
img.Save(ms, ImageFormat.Png);
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
await ProviderManager.SaveImage(item, ms, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return ItemUpdateType.ImageUpdate;
|
||||
}
|
||||
|
||||
protected Task<Image> GetThumbCollage(List<BaseItem> items)
|
||||
{
|
||||
return DynamicImageHelpers.GetThumbCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList(),
|
||||
FileSystem,
|
||||
1600,
|
||||
900);
|
||||
}
|
||||
|
||||
protected Task<Image> GetSquareCollage(List<BaseItem> items)
|
||||
{
|
||||
return DynamicImageHelpers.GetSquareCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList(),
|
||||
FileSystem,
|
||||
800);
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return "Dynamic Image Provider"; }
|
||||
}
|
||||
|
||||
public async Task<Image> CreateImageAsync(IHasImages item,
|
||||
List<BaseItem> itemsWithImages,
|
||||
ImageType imageType,
|
||||
int imageIndex)
|
||||
{
|
||||
if (itemsWithImages.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return imageType == ImageType.Thumb ?
|
||||
await GetThumbCollage(itemsWithImages).ConfigureAwait(false) :
|
||||
await GetSquareCollage(itemsWithImages).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
||||
{
|
||||
if (!Supports(item))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var items = GetItemsWithImages(item).Result;
|
||||
var cacheKey = GetConfigurationCacheKey(items);
|
||||
|
||||
return HasChanged(item, ImageType.Primary, cacheKey) || HasChanged(item, ImageType.Thumb, cacheKey);
|
||||
}
|
||||
|
||||
protected bool HasChanged(IHasImages item, ImageType type, string cacheKey)
|
||||
{
|
||||
var image = item.GetImageInfo(type, 0);
|
||||
|
||||
if (image != null)
|
||||
{
|
||||
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 List<BaseItem> GetFinalItems(List<BaseItem> items)
|
||||
{
|
||||
// Rotate the images no more than once per week
|
||||
var random = new Random(GetWeekOfYear()).Next();
|
||||
|
||||
return items
|
||||
.OrderBy(i => random - items.IndexOf(i))
|
||||
.Take(4)
|
||||
.OrderBy(i => i.Name)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private int GetWeekOfYear()
|
||||
{
|
||||
var usCulture = new CultureInfo("en-US");
|
||||
var weekNo = usCulture.Calendar.GetWeekOfYear(
|
||||
DateTime.Now,
|
||||
usCulture.DateTimeFormat.CalendarWeekRule,
|
||||
usCulture.DateTimeFormat.FirstDayOfWeek);
|
||||
|
||||
return weekNo;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
using MediaBrowser.Common.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.Photos
|
||||
{
|
||||
public static class DynamicImageHelpers
|
||||
{
|
||||
public static async Task<Image> GetThumbCollage(List<string> files,
|
||||
IFileSystem fileSystem,
|
||||
int width,
|
||||
int height)
|
||||
{
|
||||
if (files.Count < 3)
|
||||
{
|
||||
return await GetSingleImage(files, fileSystem).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
const int rows = 1;
|
||||
const int cols = 3;
|
||||
|
||||
int cellWidth = 2 * (width / 3);
|
||||
int cellHeight = height;
|
||||
var index = 0;
|
||||
|
||||
var img = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
|
||||
|
||||
using (var graphics = Graphics.FromImage(img))
|
||||
{
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.CompositingMode = CompositingMode.SourceCopy;
|
||||
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
for (var col = 0; col < cols; col++)
|
||||
{
|
||||
var x = col * (cellWidth / 2);
|
||||
var y = row * cellHeight;
|
||||
|
||||
if (files.Count > index)
|
||||
{
|
||||
using (var fileStream = fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
||||
{
|
||||
graphics.DrawImage(imgtemp, x, y, cellWidth, cellHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
public static async Task<Image> GetSquareCollage(List<string> files,
|
||||
IFileSystem fileSystem,
|
||||
int size)
|
||||
{
|
||||
if (files.Count < 4)
|
||||
{
|
||||
return await GetSingleImage(files, fileSystem).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
const int rows = 2;
|
||||
const int cols = 2;
|
||||
|
||||
int singleSize = size / 2;
|
||||
var index = 0;
|
||||
|
||||
var img = new Bitmap(size, size, PixelFormat.Format32bppPArgb);
|
||||
|
||||
using (var graphics = Graphics.FromImage(img))
|
||||
{
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.CompositingMode = CompositingMode.SourceCopy;
|
||||
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
for (var col = 0; col < cols; col++)
|
||||
{
|
||||
var x = col * singleSize;
|
||||
var y = row * singleSize;
|
||||
|
||||
using (var fileStream = fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
||||
{
|
||||
graphics.DrawImage(imgtemp, x, y, singleSize, singleSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
private static Task<Image> GetSingleImage(List<string> files, IFileSystem fileSystem)
|
||||
{
|
||||
return GetImage(files[0], fileSystem);
|
||||
}
|
||||
|
||||
private static async Task<Image> GetImage(string file, IFileSystem fileSystem)
|
||||
{
|
||||
using (var fileStream = fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
var memoryStream = new MemoryStream();
|
||||
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
return Image.FromStream(memoryStream, true, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,337 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Common.IO;
|
||||
using MediaBrowser.Common.IO;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Audio;
|
||||
using MediaBrowser.Controller.Entities.TV;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Playlists;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MoreLinq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.Photos
|
||||
{
|
||||
public class PhotoAlbumImageProvider : ICustomMetadataProvider<PhotoAlbum>, IHasChangeMonitor
|
||||
public class PhotoAlbumImageProvider : BaseDynamicImageProvider<PhotoAlbum>, ICustomMetadataProvider<PhotoAlbum>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly IProviderManager _provider;
|
||||
|
||||
public PhotoAlbumImageProvider(IFileSystem fileSystem, IProviderManager provider)
|
||||
public PhotoAlbumImageProvider(IFileSystem fileSystem, IProviderManager providerManager)
|
||||
: base(fileSystem, providerManager)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public async Task<ItemUpdateType> FetchAsync(PhotoAlbum item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
protected override Task<List<BaseItem>> GetItemsWithImages(IHasImages item)
|
||||
{
|
||||
var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
|
||||
var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
|
||||
var photoAlbum = (PhotoAlbum)item;
|
||||
var items = GetFinalItems(photoAlbum.RecursiveChildren.Where(i => i is Photo).ToList());
|
||||
|
||||
return primaryResult | thumbResult;
|
||||
}
|
||||
|
||||
private Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
var items = GetItemsWithImages(item);
|
||||
var cacheKey = GetConfigurationCacheKey(items);
|
||||
|
||||
if (!HasChanged(item, imageType, cacheKey))
|
||||
{
|
||||
return Task.FromResult(ItemUpdateType.None);
|
||||
}
|
||||
|
||||
return FetchAsyncInternal(item, imageType, cacheKey, options, cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<ItemUpdateType> FetchAsyncInternal(IHasImages item, ImageType imageType, string cacheKey, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
var img = await CreateImageAsync(item, imageType, 0).ConfigureAwait(false);
|
||||
|
||||
if (img == null)
|
||||
{
|
||||
return ItemUpdateType.None;
|
||||
}
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
img.Save(ms, ImageFormat.Png);
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
await _provider.SaveImage(item, ms, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return ItemUpdateType.ImageUpdate;
|
||||
}
|
||||
|
||||
private bool HasChanged(IHasImages item, ImageType type, string cacheKey)
|
||||
{
|
||||
var image = item.GetImageInfo(type, 0);
|
||||
|
||||
if (image != null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
private const string Version = "3";
|
||||
|
||||
public string GetConfigurationCacheKey(List<BaseItem> items)
|
||||
{
|
||||
return (Version + "_" + string.Join(",", items.Select(i => i.Id.ToString("N")).ToArray())).GetMD5().ToString("N");
|
||||
}
|
||||
|
||||
private List<BaseItem> GetItemsWithImages(IHasImages item)
|
||||
{
|
||||
var photoAlbum = item as PhotoAlbum;
|
||||
if (photoAlbum != null)
|
||||
{
|
||||
return GetFinalItems(photoAlbum.RecursiveChildren.Where(i => i is Photo).ToList());
|
||||
}
|
||||
|
||||
var playlist = (Playlist)item;
|
||||
|
||||
var items = playlist.GetManageableItems()
|
||||
.Select(i =>
|
||||
{
|
||||
var subItem = i.Item2;
|
||||
|
||||
var episode = subItem as Episode;
|
||||
|
||||
if (episode != null)
|
||||
{
|
||||
var series = episode.Series;
|
||||
if (series != null && series.HasImage(ImageType.Primary))
|
||||
{
|
||||
return series;
|
||||
}
|
||||
}
|
||||
|
||||
if (subItem.HasImage(ImageType.Primary))
|
||||
{
|
||||
return subItem;
|
||||
}
|
||||
|
||||
var parent = subItem.Parent;
|
||||
|
||||
if (parent != null && parent.HasImage(ImageType.Primary))
|
||||
{
|
||||
if (parent is MusicAlbum)
|
||||
{
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
})
|
||||
.Where(i => i != null)
|
||||
.DistinctBy(i => i.Id)
|
||||
.ToList();
|
||||
|
||||
return GetFinalItems(items);
|
||||
}
|
||||
|
||||
private List<BaseItem> GetFinalItems(List<BaseItem> items)
|
||||
{
|
||||
// Rotate the images no more than once per day
|
||||
var random = new Random(DateTime.Now.DayOfYear).Next();
|
||||
|
||||
return items
|
||||
.OrderBy(i => random - items.IndexOf(i))
|
||||
.Take(4)
|
||||
.OrderBy(i => i.Name)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<Image> CreateImageAsync(IHasImages item, ImageType imageType, int imageIndex)
|
||||
{
|
||||
var items = GetItemsWithImages(item);
|
||||
|
||||
if (items.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return imageType == ImageType.Thumb ?
|
||||
await GetThumbCollage(items).ConfigureAwait(false) :
|
||||
await GetSquareCollage(items).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private Task<Image> GetThumbCollage(List<BaseItem> items)
|
||||
{
|
||||
return GetThumbCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList());
|
||||
}
|
||||
|
||||
private Task<Image> GetSquareCollage(List<BaseItem> items)
|
||||
{
|
||||
return GetSquareCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList());
|
||||
}
|
||||
|
||||
private async Task<Image> GetThumbCollage(List<string> files)
|
||||
{
|
||||
if (files.Count < 3)
|
||||
{
|
||||
return await GetSingleImage(files).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
const int rows = 1;
|
||||
const int cols = 3;
|
||||
|
||||
const int cellWidth = 2 * (ThumbImageWidth / 3);
|
||||
const int cellHeight = ThumbImageHeight;
|
||||
var index = 0;
|
||||
|
||||
var img = new Bitmap(ThumbImageWidth, ThumbImageHeight, PixelFormat.Format32bppPArgb);
|
||||
|
||||
using (var graphics = Graphics.FromImage(img))
|
||||
{
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.CompositingMode = CompositingMode.SourceCopy;
|
||||
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
for (var col = 0; col < cols; col++)
|
||||
{
|
||||
var x = col * (cellWidth / 2);
|
||||
var y = row * cellHeight;
|
||||
|
||||
if (files.Count > index)
|
||||
{
|
||||
using (var fileStream = _fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
||||
{
|
||||
graphics.DrawImage(imgtemp, x, y, cellWidth, cellHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
private const int SquareImageSize = 800;
|
||||
private const int ThumbImageWidth = 1600;
|
||||
private const int ThumbImageHeight = 900;
|
||||
|
||||
private async Task<Image> GetSquareCollage(List<string> files)
|
||||
{
|
||||
if (files.Count < 4)
|
||||
{
|
||||
return await GetSingleImage(files).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
const int rows = 2;
|
||||
const int cols = 2;
|
||||
|
||||
const int singleSize = SquareImageSize / 2;
|
||||
var index = 0;
|
||||
|
||||
var img = new Bitmap(SquareImageSize, SquareImageSize, PixelFormat.Format32bppPArgb);
|
||||
|
||||
using (var graphics = Graphics.FromImage(img))
|
||||
{
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.CompositingMode = CompositingMode.SourceCopy;
|
||||
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
for (var col = 0; col < cols; col++)
|
||||
{
|
||||
var x = col * singleSize;
|
||||
var y = row * singleSize;
|
||||
|
||||
using (var fileStream = _fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
||||
{
|
||||
graphics.DrawImage(imgtemp, x, y, singleSize, singleSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
private Task<Image> GetSingleImage(List<string> files)
|
||||
{
|
||||
return GetImage(files[0]);
|
||||
}
|
||||
|
||||
private async Task<Image> GetImage(string file)
|
||||
{
|
||||
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
var memoryStream = new MemoryStream();
|
||||
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
return Image.FromStream(memoryStream, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return "Dynamic Image Provider"; }
|
||||
}
|
||||
|
||||
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
||||
{
|
||||
var items = GetItemsWithImages(item);
|
||||
var cacheKey = GetConfigurationCacheKey(items);
|
||||
|
||||
return HasChanged(item, ImageType.Primary, cacheKey) || HasChanged(item, ImageType.Thumb, cacheKey);
|
||||
return Task.FromResult(items);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,123 +1,26 @@
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Common.IO;
|
||||
using MediaBrowser.Common.IO;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Audio;
|
||||
using MediaBrowser.Controller.Entities.TV;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Playlists;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Server.Implementations.Photos;
|
||||
using MoreLinq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.Playlists
|
||||
{
|
||||
public class PlaylistImageProvider : ICustomMetadataProvider<Playlist>, IHasChangeMonitor
|
||||
public class PlaylistImageProvider : BaseDynamicImageProvider<Playlist>, ICustomMetadataProvider<Playlist>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly IProviderManager _provider;
|
||||
|
||||
public PlaylistImageProvider(IFileSystem fileSystem, IProviderManager provider)
|
||||
public PlaylistImageProvider(IFileSystem fileSystem, IProviderManager providerManager) : base(fileSystem, providerManager)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public async Task<ItemUpdateType> FetchAsync(Playlist item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
protected override Task<List<BaseItem>> GetItemsWithImages(IHasImages item)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task<ItemUpdateType> FetchAsync(PhotoAlbum item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
private Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
var items = GetItemsWithImages(item);
|
||||
var cacheKey = GetConfigurationCacheKey(items);
|
||||
|
||||
if (!HasChanged(item, imageType, cacheKey))
|
||||
{
|
||||
return Task.FromResult(ItemUpdateType.None);
|
||||
}
|
||||
|
||||
return FetchAsyncInternal(item, imageType, cacheKey, options, cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<ItemUpdateType> FetchAsyncInternal(IHasImages item, ImageType imageType, string cacheKey, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
var img = await CreateImageAsync(item, imageType, 0).ConfigureAwait(false);
|
||||
|
||||
if (img == null)
|
||||
{
|
||||
return ItemUpdateType.None;
|
||||
}
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
img.Save(ms, ImageFormat.Png);
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
await _provider.SaveImage(item, ms, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return ItemUpdateType.ImageUpdate;
|
||||
}
|
||||
|
||||
private bool HasChanged(IHasImages item, ImageType type, string cacheKey)
|
||||
{
|
||||
var image = item.GetImageInfo(type, 0);
|
||||
|
||||
if (image != null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
private const string Version = "3";
|
||||
|
||||
public string GetConfigurationCacheKey(List<BaseItem> items)
|
||||
{
|
||||
return (Version + "_" + string.Join(",", items.Select(i => i.Id.ToString("N")).ToArray())).GetMD5().ToString("N");
|
||||
}
|
||||
|
||||
private List<BaseItem> GetItemsWithImages(IHasImages item)
|
||||
{
|
||||
var photoAlbum = item as PhotoAlbum;
|
||||
if (photoAlbum != null)
|
||||
{
|
||||
return GetFinalItems(photoAlbum.RecursiveChildren.Where(i => i is Photo).ToList());
|
||||
}
|
||||
|
||||
var playlist = (Playlist)item;
|
||||
|
||||
var items = playlist.GetManageableItems()
|
||||
@@ -157,189 +60,7 @@ namespace MediaBrowser.Server.Implementations.Playlists
|
||||
.DistinctBy(i => i.Id)
|
||||
.ToList();
|
||||
|
||||
return GetFinalItems(items);
|
||||
}
|
||||
|
||||
private List<BaseItem> GetFinalItems(List<BaseItem> items)
|
||||
{
|
||||
// Rotate the images no more than once per day
|
||||
var random = new Random(DateTime.Now.DayOfYear).Next();
|
||||
|
||||
return items
|
||||
.OrderBy(i => random - items.IndexOf(i))
|
||||
.Take(4)
|
||||
.OrderBy(i => i.Name)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<Image> CreateImageAsync(IHasImages item, ImageType imageType, int imageIndex)
|
||||
{
|
||||
var items = GetItemsWithImages(item);
|
||||
|
||||
if (items.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return imageType == ImageType.Thumb ?
|
||||
await GetThumbCollage(items).ConfigureAwait(false) :
|
||||
await GetSquareCollage(items).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private Task<Image> GetThumbCollage(List<BaseItem> items)
|
||||
{
|
||||
return GetThumbCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList());
|
||||
}
|
||||
|
||||
private Task<Image> GetSquareCollage(List<BaseItem> items)
|
||||
{
|
||||
return GetSquareCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList());
|
||||
}
|
||||
|
||||
private async Task<Image> GetThumbCollage(List<string> files)
|
||||
{
|
||||
if (files.Count < 3)
|
||||
{
|
||||
return await GetSingleImage(files).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
const int rows = 1;
|
||||
const int cols = 3;
|
||||
|
||||
const int cellWidth = 2 * (ThumbImageWidth / 3);
|
||||
const int cellHeight = ThumbImageHeight;
|
||||
var index = 0;
|
||||
|
||||
var img = new Bitmap(ThumbImageWidth, ThumbImageHeight, PixelFormat.Format32bppPArgb);
|
||||
|
||||
using (var graphics = Graphics.FromImage(img))
|
||||
{
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.CompositingMode = CompositingMode.SourceCopy;
|
||||
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
for (var col = 0; col < cols; col++)
|
||||
{
|
||||
var x = col * (cellWidth / 2);
|
||||
var y = row * cellHeight;
|
||||
|
||||
if (files.Count > index)
|
||||
{
|
||||
using (var fileStream = _fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
||||
{
|
||||
graphics.DrawImage(imgtemp, x, y, cellWidth, cellHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
private const int SquareImageSize = 800;
|
||||
private const int ThumbImageWidth = 1600;
|
||||
private const int ThumbImageHeight = 900;
|
||||
|
||||
private async Task<Image> GetSquareCollage(List<string> files)
|
||||
{
|
||||
if (files.Count < 4)
|
||||
{
|
||||
return await GetSingleImage(files).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
const int rows = 2;
|
||||
const int cols = 2;
|
||||
|
||||
const int singleSize = SquareImageSize / 2;
|
||||
var index = 0;
|
||||
|
||||
var img = new Bitmap(SquareImageSize, SquareImageSize, PixelFormat.Format32bppPArgb);
|
||||
|
||||
using (var graphics = Graphics.FromImage(img))
|
||||
{
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.CompositingMode = CompositingMode.SourceCopy;
|
||||
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
for (var col = 0; col < cols; col++)
|
||||
{
|
||||
var x = col * singleSize;
|
||||
var y = row * singleSize;
|
||||
|
||||
using (var fileStream = _fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
||||
{
|
||||
graphics.DrawImage(imgtemp, x, y, singleSize, singleSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
private Task<Image> GetSingleImage(List<string> files)
|
||||
{
|
||||
return GetImage(files[0]);
|
||||
}
|
||||
|
||||
private async Task<Image> GetImage(string file)
|
||||
{
|
||||
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||
{
|
||||
var memoryStream = new MemoryStream();
|
||||
|
||||
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
return Image.FromStream(memoryStream, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return "Dynamic Image Provider"; }
|
||||
}
|
||||
|
||||
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
||||
{
|
||||
var items = GetItemsWithImages(item);
|
||||
var cacheKey = GetConfigurationCacheKey(items);
|
||||
|
||||
return HasChanged(item, ImageType.Primary, cacheKey) || HasChanged(item, ImageType.Thumb, cacheKey);
|
||||
return Task.FromResult(GetFinalItems(items));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user