jellyfin/Emby.Server.Implementations/UserViews/DynamicImageProvider.cs
Erwin de Haan ec1f5dc317 Mayor code cleanup
Add Argument*Exceptions now use proper nameof operators.

Added exception messages to quite a few Argument*Exceptions.

Fixed rethorwing to be proper syntax.

Added a ton of null checkes. (This is only a start, there are about 500 places that need proper null handling)

Added some TODOs to log certain exceptions.

Fix sln again.

Fixed all AssemblyInfo's and added proper copyright (where I could find them)

We live in *current year*.

Fixed the use of braces.

Fixed a ton of properties, and made a fair amount of functions static that should be and can be static.

Made more Methods that should be static static.

You can now use static to find bad functions!

Removed unused variable. And added one more proper XML comment.
2019-01-10 20:38:53 +01:00

140 lines
4.6 KiB
C#

using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Emby.Server.Implementations.Images;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Model.IO;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Extensions;
namespace Emby.Server.Implementations.UserViews
{
public class DynamicImageProvider : BaseDynamicImageProvider<UserView>
{
private readonly IUserManager _userManager;
private readonly ILibraryManager _libraryManager;
public DynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor, IUserManager userManager, ILibraryManager libraryManager)
: base(fileSystem, providerManager, applicationPaths, imageProcessor)
{
_userManager = userManager;
_libraryManager = libraryManager;
}
protected override List<BaseItem> GetItemsWithImages(BaseItem item)
{
var view = (UserView)item;
var isUsingCollectionStrip = IsUsingCollectionStrip(view);
var recursive = isUsingCollectionStrip && !new[] { CollectionType.BoxSets, CollectionType.Playlists }.Contains(view.ViewType ?? string.Empty, StringComparer.OrdinalIgnoreCase);
var result = view.GetItemList(new InternalItemsQuery
{
User = view.UserId.HasValue ? _userManager.GetUserById(view.UserId.Value) : null,
CollapseBoxSetItems = false,
Recursive = recursive,
ExcludeItemTypes = new[] { "UserView", "CollectionFolder", "Person" },
DtoOptions = new DtoOptions(false)
});
var items = result.Select(i =>
{
var episode = i as Episode;
if (episode != null)
{
var series = episode.Series;
if (series != null)
{
return series;
}
return episode;
}
var season = i as Season;
if (season != null)
{
var series = season.Series;
if (series != null)
{
return series;
}
return season;
}
var audio = i as Audio;
if (audio != null)
{
var album = audio.AlbumEntity;
if (album != null && album.HasImage(ImageType.Primary))
{
return album;
}
}
return i;
}).DistinctBy(i => i.Id);
if (isUsingCollectionStrip)
{
return items
.Where(i => i.HasImage(ImageType.Primary) || i.HasImage(ImageType.Thumb))
.OrderBy(i => Guid.NewGuid())
.ToList();
}
return items
.Where(i => i.HasImage(ImageType.Primary))
.OrderBy(i => Guid.NewGuid())
.ToList();
}
protected override bool Supports(BaseItem item)
{
var view = item as UserView;
if (view != null)
{
return IsUsingCollectionStrip(view);
}
return false;
}
private static bool IsUsingCollectionStrip(UserView view)
{
string[] collectionStripViewTypes =
{
CollectionType.Movies,
CollectionType.TvShows,
CollectionType.Playlists
};
return collectionStripViewTypes.Contains(view.ViewType ?? string.Empty);
}
protected override string CreateImage(BaseItem item, List<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType, int imageIndex)
{
if (itemsWithImages.Count == 0)
{
return null;
}
var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
return CreateThumbCollage(item, itemsWithImages, outputPath, 960, 540);
}
}
}