jellyfin/MediaBrowser.Server.Implementations/Dto/DtoService.cs

1175 lines
38 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Extensions;
2013-09-04 19:02:19 +02:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
2013-09-04 19:02:19 +02:00
using MediaBrowser.Controller.Session;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.Entities;
2013-02-21 07:38:23 +01:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
2013-09-04 19:02:19 +02:00
using MediaBrowser.Model.Session;
using MoreLinq;
2013-02-21 02:33:05 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2013-09-04 19:02:19 +02:00
namespace MediaBrowser.Server.Implementations.Dto
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
public class DtoService : IDtoService
2013-02-21 02:33:05 +01:00
{
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
2013-09-04 19:02:19 +02:00
private readonly IUserManager _userManager;
private readonly IUserDataRepository _userDataRepository;
2013-06-18 21:16:27 +02:00
private readonly IItemRepository _itemRepo;
2013-09-04 19:02:19 +02:00
public DtoService(ILogger logger, ILibraryManager libraryManager, IUserManager userManager, IUserDataRepository userDataRepository, IItemRepository itemRepo)
{
_logger = logger;
_libraryManager = libraryManager;
2013-09-04 19:02:19 +02:00
_userManager = userManager;
_userDataRepository = userDataRepository;
2013-06-18 21:16:27 +02:00
_itemRepo = itemRepo;
2013-09-04 19:02:19 +02:00
}
2013-02-21 02:33:05 +01:00
/// <summary>
2013-05-15 18:56:38 +02:00
/// Converts a BaseItem to a DTOBaseItem
2013-02-21 02:33:05 +01:00
/// </summary>
/// <param name="item">The item.</param>
/// <param name="fields">The fields.</param>
2013-05-15 18:56:38 +02:00
/// <param name="user">The user.</param>
/// <param name="owner">The owner.</param>
2013-02-21 02:33:05 +01:00
/// <returns>Task{DtoBaseItem}.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
public async Task<BaseItemDto> GetBaseItemDto(BaseItem item, List<ItemFields> fields, User user = null, BaseItem owner = null)
2013-02-21 02:33:05 +01:00
{
if (item == null)
{
throw new ArgumentNullException("item");
}
2013-05-15 18:56:38 +02:00
2013-02-21 02:33:05 +01:00
if (fields == null)
{
throw new ArgumentNullException("fields");
}
var dto = new BaseItemDto();
2013-02-21 02:33:05 +01:00
var tasks = new List<Task>();
2013-04-03 04:59:27 +02:00
if (fields.Contains(ItemFields.Studios))
{
2013-05-04 05:13:28 +02:00
tasks.Add(AttachStudios(dto, item));
2013-04-03 04:59:27 +02:00
}
2013-04-26 05:31:10 +02:00
2013-04-03 04:59:27 +02:00
if (fields.Contains(ItemFields.People))
{
tasks.Add(AttachPeople(dto, item));
}
if (fields.Contains(ItemFields.PrimaryImageAspectRatio))
{
try
{
2013-09-04 19:02:19 +02:00
await AttachPrimaryImageAspectRatio(dto, item).ConfigureAwait(false);
2013-04-03 04:59:27 +02:00
}
catch (Exception ex)
{
// Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
_logger.ErrorException("Error generating PrimaryImageAspectRatio for {0}", ex, item.Name);
}
}
if (fields.Contains(ItemFields.DisplayPreferencesId))
{
dto.DisplayPreferencesId = item.DisplayPreferencesId.ToString("N");
}
2013-06-17 22:35:43 +02:00
if (user != null)
{
AttachUserSpecificInfo(dto, item, user, fields);
}
AttachBasicFields(dto, item, owner, fields);
2013-02-21 02:33:05 +01:00
2013-07-16 18:03:28 +02:00
if (fields.Contains(ItemFields.SoundtrackIds))
{
2013-08-31 01:55:17 +02:00
dto.SoundtrackIds = item.SoundtrackIds
.Select(i => i.ToString("N"))
.ToArray();
2013-07-16 18:03:28 +02:00
}
2013-02-21 02:33:05 +01:00
// Make sure all the tasks we kicked off have completed.
if (tasks.Count > 0)
{
await Task.WhenAll(tasks).ConfigureAwait(false);
}
return dto;
}
/// <summary>
/// Attaches the user specific info.
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <param name="user">The user.</param>
/// <param name="fields">The fields.</param>
2013-06-17 22:35:43 +02:00
private void AttachUserSpecificInfo(BaseItemDto dto, BaseItem item, User user, List<ItemFields> fields)
2013-02-21 02:33:05 +01:00
{
if (item.IsFolder)
{
2013-07-25 21:17:44 +02:00
var hasItemCounts = fields.Contains(ItemFields.ItemCounts);
if (hasItemCounts || fields.Contains(ItemFields.CumulativeRunTimeTicks))
2013-02-21 02:33:05 +01:00
{
var folder = (Folder)item;
2013-07-25 21:17:44 +02:00
if (hasItemCounts)
{
dto.ChildCount = folder.GetChildren(user, true).Count();
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
SetSpecialCounts(folder, user, dto);
2013-02-21 02:33:05 +01:00
}
}
2013-05-08 03:17:41 +02:00
2013-07-16 19:18:32 +02:00
var userData = _userDataRepository.GetUserData(user.Id, item.GetUserDataKey());
2013-05-08 03:17:41 +02:00
2013-07-16 19:18:32 +02:00
dto.UserData = GetUserItemDataDto(userData);
2013-05-08 03:17:41 +02:00
2013-07-16 19:18:32 +02:00
if (item.IsFolder)
{
dto.UserData.Played = dto.PlayedPercentage.HasValue && dto.PlayedPercentage.Value >= 100;
2013-05-08 03:17:41 +02:00
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
public async Task<UserDto> GetUserDto(User user)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
if (user == null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
throw new ArgumentNullException("user");
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
var dto = new UserDto
2013-03-20 15:06:22 +01:00
{
2013-09-04 19:02:19 +02:00
Id = user.Id.ToString("N"),
Name = user.Name,
HasPassword = !String.IsNullOrEmpty(user.Password),
LastActivityDate = user.LastActivityDate,
LastLoginDate = user.LastLoginDate,
Configuration = user.Configuration
};
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var image = user.PrimaryImagePath;
2013-09-04 19:02:19 +02:00
if (!string.IsNullOrEmpty(image))
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.PrimaryImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(user, ImageType.Primary, image);
try
{
2013-09-04 19:02:19 +02:00
await AttachPrimaryImageAspectRatio(dto, user).ConfigureAwait(false);
}
catch (Exception ex)
{
2013-09-04 19:02:19 +02:00
// Have to use a catch-all unfortunately because some .net image methods throw plain Exceptions
_logger.ErrorException("Error generating PrimaryImageAspectRatio for {0}", ex, user.Name);
}
2013-09-04 19:02:19 +02:00
}
2013-09-04 19:02:19 +02:00
return dto;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
public SessionInfoDto GetSessionInfoDto(SessionInfo session)
{
var dto = new SessionInfoDto
{
Client = session.Client,
DeviceId = session.DeviceId,
DeviceName = session.DeviceName,
Id = session.Id.ToString("N"),
LastActivityDate = session.LastActivityDate,
NowPlayingPositionTicks = session.NowPlayingPositionTicks,
SupportsRemoteControl = session.SupportsRemoteControl,
IsPaused = session.IsPaused,
IsMuted = session.IsMuted,
NowViewingContext = session.NowViewingContext,
NowViewingItemId = session.NowViewingItemId,
NowViewingItemName = session.NowViewingItemName,
NowViewingItemType = session.NowViewingItemType,
ApplicationVersion = session.ApplicationVersion
};
2013-09-04 19:02:19 +02:00
if (session.NowPlayingItem != null)
{
2013-09-04 19:02:19 +02:00
dto.NowPlayingItem = GetBaseItemInfo(session.NowPlayingItem);
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
if (session.User != null)
{
dto.UserId = session.User.Id.ToString("N");
dto.UserName = session.User.Name;
}
return dto;
2013-02-21 02:33:05 +01:00
}
/// <summary>
2013-09-04 19:02:19 +02:00
/// Converts a BaseItem to a BaseItemInfo
2013-02-21 02:33:05 +01:00
/// </summary>
/// <param name="item">The item.</param>
2013-09-04 19:02:19 +02:00
/// <returns>BaseItemInfo.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
public BaseItemInfo GetBaseItemInfo(BaseItem item)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
if (item == null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
throw new ArgumentNullException("item");
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
var info = new BaseItemInfo
{
2013-09-04 19:02:19 +02:00
Id = GetDtoId(item),
Name = item.Name,
MediaType = item.MediaType,
Type = item.GetType().Name,
IsFolder = item.IsFolder,
RunTimeTicks = item.RunTimeTicks
};
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var imagePath = item.PrimaryImagePath;
2013-09-04 19:02:19 +02:00
if (!string.IsNullOrEmpty(imagePath))
{
2013-09-04 19:02:19 +02:00
try
{
info.PrimaryImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Primary, imagePath);
}
catch (IOException)
{
}
}
2013-09-04 19:02:19 +02:00
return info;
}
2013-09-04 19:02:19 +02:00
const string IndexFolderDelimeter = "-index-";
2013-09-04 19:02:19 +02:00
/// <summary>
/// Gets client-side Id of a server-side BaseItem
/// </summary>
/// <param name="item">The item.</param>
/// <returns>System.String.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
public string GetDtoId(BaseItem item)
{
if (item == null)
{
2013-09-04 19:02:19 +02:00
throw new ArgumentNullException("item");
}
2013-09-04 19:02:19 +02:00
var indexFolder = item as IndexFolder;
2013-09-04 19:02:19 +02:00
if (indexFolder != null)
{
2013-09-04 19:02:19 +02:00
return GetDtoId(indexFolder.Parent) + IndexFolderDelimeter + (indexFolder.IndexName ?? string.Empty) + IndexFolderDelimeter + indexFolder.Id;
}
2013-04-26 05:31:10 +02:00
2013-09-04 19:02:19 +02:00
return item.Id.ToString("N");
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Converts a UserItemData to a DTOUserItemData
/// </summary>
/// <param name="data">The data.</param>
/// <returns>DtoUserItemData.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public UserItemDataDto GetUserItemDataDto(UserItemData data)
{
if (data == null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
throw new ArgumentNullException("data");
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
return new UserItemDataDto
{
2013-09-04 19:02:19 +02:00
IsFavorite = data.IsFavorite,
Likes = data.Likes,
PlaybackPositionTicks = data.PlaybackPositionTicks,
PlayCount = data.PlayCount,
Rating = data.Rating,
Played = data.Played,
LastPlayedDate = data.LastPlayedDate
};
}
private void SetBookProperties(BaseItemDto dto, Book item)
{
dto.SeriesName = item.SeriesName;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
private void SetMusicVideoProperties(BaseItemDto dto, MusicVideo item)
{
if (!string.IsNullOrEmpty(item.Album))
{
var parentAlbum = _libraryManager.RootFolder
.RecursiveChildren
.OfType<MusicAlbum>()
.FirstOrDefault(i => string.Equals(i.Name, item.Album, StringComparison.OrdinalIgnoreCase));
2013-09-04 19:02:19 +02:00
if (parentAlbum != null)
{
2013-09-04 19:02:19 +02:00
dto.AlbumId = GetDtoId(parentAlbum);
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
dto.Album = item.Album;
dto.Artists = string.IsNullOrEmpty(item.Artist) ? new string[] { } : new[] { item.Artist };
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
private void SetGameProperties(BaseItemDto dto, Game item)
{
dto.Players = item.PlayersSupported;
dto.GameSystem = item.GameSystem;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Gets the backdrop image tags.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>List{System.String}.</returns>
private List<Guid> GetBackdropImageTags(BaseItem item)
{
return item.BackdropImagePaths
.Select(p => GetImageCacheTag(item, ImageType.Backdrop, p))
.Where(i => i.HasValue)
.Select(i => i.Value)
.ToList();
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Gets the screenshot image tags.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>List{Guid}.</returns>
private List<Guid> GetScreenshotImageTags(BaseItem item)
{
return item.ScreenshotImagePaths
.Select(p => GetImageCacheTag(item, ImageType.Screenshot, p))
.Where(i => i.HasValue)
.Select(i => i.Value)
.ToList();
}
2013-09-04 19:02:19 +02:00
private Guid? GetImageCacheTag(BaseItem item, ImageType type, string path)
{
try
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
return Kernel.Instance.ImageManager.GetImageCacheTag(item, type, path);
}
2013-09-04 19:02:19 +02:00
catch (IOException ex)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
_logger.ErrorException("Error getting {0} image info for {1}", ex, type, path);
return null;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
} /// <summary>
/// Attaches People DTO's to a DTOBaseItem
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <returns>Task.</returns>
private async Task AttachPeople(BaseItemDto dto, BaseItem item)
{
// Ordering by person type to ensure actors and artists are at the front.
// This is taking advantage of the fact that they both begin with A
// This should be improved in the future
var people = item.People.OrderBy(i => i.Type).ToList();
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Attach People by transforming them into BaseItemPerson (DTO)
dto.People = new BaseItemPerson[people.Count];
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var entities = await Task.WhenAll(people.Select(p => p.Name)
.Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
Task.Run(async () =>
{
try
{
return await _libraryManager.GetPerson(c).ConfigureAwait(false);
}
catch (IOException ex)
{
_logger.ErrorException("Error getting person {0}", ex, c);
return null;
}
})
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
)).ConfigureAwait(false);
var dictionary = entities.Where(i => i != null)
.DistinctBy(i => i.Name)
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < people.Count; i++)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var person = people[i];
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var baseItemPerson = new BaseItemPerson
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
Name = person.Name,
Role = person.Role,
Type = person.Type
};
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
Person entity;
if (dictionary.TryGetValue(person.Name, out entity))
{
var primaryImagePath = entity.PrimaryImagePath;
if (!string.IsNullOrEmpty(primaryImagePath))
{
baseItemPerson.PrimaryImageTag = GetImageCacheTag(entity, ImageType.Primary, primaryImagePath);
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
dto.People[i] = baseItemPerson;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Attaches the studios.
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <returns>Task.</returns>
private async Task AttachStudios(BaseItemDto dto, BaseItem item)
{
var studios = item.Studios.ToList();
dto.Studios = new StudioDto[studios.Count];
var entities = await Task.WhenAll(studios.Distinct(StringComparer.OrdinalIgnoreCase).Select(c =>
Task.Run(async () =>
{
try
{
return await _libraryManager.GetStudio(c).ConfigureAwait(false);
}
catch (IOException ex)
{
_logger.ErrorException("Error getting studio {0}", ex, c);
return null;
}
})
)).ConfigureAwait(false);
var dictionary = entities
.Where(i => i != null)
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < studios.Count; i++)
2013-07-08 21:31:45 +02:00
{
2013-09-04 19:02:19 +02:00
var studio = studios[i];
2013-07-08 21:31:45 +02:00
2013-09-04 19:02:19 +02:00
var studioDto = new StudioDto
2013-07-08 21:31:45 +02:00
{
2013-09-04 19:02:19 +02:00
Name = studio
};
2013-07-08 21:31:45 +02:00
2013-09-04 19:02:19 +02:00
Studio entity;
if (dictionary.TryGetValue(studio, out entity))
{
var primaryImagePath = entity.PrimaryImagePath;
if (!string.IsNullOrEmpty(primaryImagePath))
{
studioDto.PrimaryImageTag = GetImageCacheTag(entity, ImageType.Primary, primaryImagePath);
}
2013-07-08 21:31:45 +02:00
}
2013-09-04 19:02:19 +02:00
dto.Studios[i] = studioDto;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// If an item does not any backdrops, this can be used to find the first parent that does have one
/// </summary>
/// <param name="item">The item.</param>
/// <param name="owner">The owner.</param>
/// <returns>BaseItem.</returns>
private BaseItem GetParentBackdropItem(BaseItem item, BaseItem owner)
{
var parent = item.Parent ?? owner;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
while (parent != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Count > 0)
{
return parent;
}
parent = parent.Parent;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
return null;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// If an item does not have a logo, this can be used to find the first parent that does have one
/// </summary>
/// <param name="item">The item.</param>
/// <param name="type">The type.</param>
/// <param name="owner">The owner.</param>
/// <returns>BaseItem.</returns>
private BaseItem GetParentImageItem(BaseItem item, ImageType type, BaseItem owner)
{
var parent = item.Parent ?? owner;
while (parent != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
if (parent.HasImage(type))
{
return parent;
}
parent = parent.Parent;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
return null;
}
/// <summary>
/// Gets the chapter info dto.
/// </summary>
/// <param name="chapterInfo">The chapter info.</param>
/// <param name="item">The item.</param>
/// <returns>ChapterInfoDto.</returns>
private ChapterInfoDto GetChapterInfoDto(ChapterInfo chapterInfo, BaseItem item)
{
var dto = new ChapterInfoDto
{
2013-09-04 19:02:19 +02:00
Name = chapterInfo.Name,
StartPositionTicks = chapterInfo.StartPositionTicks
};
2013-09-04 19:02:19 +02:00
if (!string.IsNullOrEmpty(chapterInfo.ImagePath))
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.ImageTag = GetImageCacheTag(item, ImageType.Chapter, chapterInfo.ImagePath);
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
return dto;
}
/// <summary>
/// Gets a BaseItem based upon it's client-side item id
/// </summary>
/// <param name="id">The id.</param>
/// <param name="userId">The user id.</param>
/// <returns>BaseItem.</returns>
public BaseItem GetItemByDtoId(string id, Guid? userId = null)
{
if (string.IsNullOrEmpty(id))
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
throw new ArgumentNullException("id");
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
// If the item is an indexed folder we have to do a special routine to get it
var isIndexFolder = id.IndexOf(IndexFolderDelimeter, StringComparison.OrdinalIgnoreCase) != -1;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (isIndexFolder)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
if (userId.HasValue)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
return GetIndexFolder(id, userId.Value);
2013-02-21 02:33:05 +01:00
}
}
2013-09-04 19:02:19 +02:00
BaseItem item = null;
2013-07-16 21:10:57 +02:00
2013-09-04 19:02:19 +02:00
if (userId.HasValue || !isIndexFolder)
{
item = _libraryManager.GetItemById(new Guid(id));
}
2013-07-16 21:10:57 +02:00
2013-09-04 19:02:19 +02:00
// If we still don't find it, look within individual user views
if (item == null && !userId.HasValue && isIndexFolder)
{
foreach (var user in _userManager.Users)
2013-07-16 21:10:57 +02:00
{
2013-09-04 19:02:19 +02:00
item = GetItemByDtoId(id, user.Id);
2013-07-16 21:10:57 +02:00
2013-09-04 19:02:19 +02:00
if (item != null)
2013-07-16 21:10:57 +02:00
{
2013-09-04 19:02:19 +02:00
break;
2013-07-16 21:10:57 +02:00
}
}
2013-07-16 18:03:28 +02:00
}
2013-04-25 00:34:38 +02:00
2013-09-04 19:02:19 +02:00
return item;
}
2013-04-25 00:34:38 +02:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Finds an index folder based on an Id and userId
/// </summary>
/// <param name="id">The id.</param>
/// <param name="userId">The user id.</param>
/// <returns>BaseItem.</returns>
private BaseItem GetIndexFolder(string id, Guid userId)
{
var user = _userManager.GetUserById(userId);
2013-04-25 00:34:38 +02:00
2013-09-04 19:02:19 +02:00
var stringSeparators = new[] { IndexFolderDelimeter };
2013-04-26 05:31:10 +02:00
2013-09-04 19:02:19 +02:00
// Split using the delimeter
var values = id.Split(stringSeparators, StringSplitOptions.None).ToList();
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Get the top folder normally using the first id
var folder = GetItemByDtoId(values[0], userId) as Folder;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
values.RemoveAt(0);
2013-09-04 19:02:19 +02:00
// Get indexed folders using the remaining values in the id string
return GetIndexFolder(values, folder, user);
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Gets indexed folders based on a list of index names and folder id's
/// </summary>
/// <param name="values">The values.</param>
/// <param name="parentFolder">The parent folder.</param>
/// <param name="user">The user.</param>
/// <returns>BaseItem.</returns>
private BaseItem GetIndexFolder(List<string> values, Folder parentFolder, User user)
{
// The index name is first
var indexBy = values[0];
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// The index folder id is next
var indexFolderId = new Guid(values[1]);
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Remove them from the lst
values.RemoveRange(0, 2);
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Get the IndexFolder
var indexFolder = parentFolder.GetChildren(user, false, indexBy).FirstOrDefault(i => i.Id == indexFolderId) as Folder;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Nested index folder
if (values.Count > 0)
{
return GetIndexFolder(values, indexFolder, user);
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
return indexFolder;
}
2013-05-24 06:02:42 +02:00
2013-09-04 19:02:19 +02:00
/// <summary>
/// Sets simple property values on a DTOBaseItem
/// </summary>
/// <param name="dto">The dto.</param>
/// <param name="item">The item.</param>
/// <param name="owner">The owner.</param>
/// <param name="fields">The fields.</param>
private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem owner, List<ItemFields> fields)
{
if (fields.Contains(ItemFields.DateCreated))
2013-05-24 06:02:42 +02:00
{
2013-09-04 19:02:19 +02:00
dto.DateCreated = item.DateCreated;
2013-05-24 06:02:42 +02:00
}
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.OriginalRunTimeTicks))
2013-07-16 18:03:28 +02:00
{
2013-09-04 19:02:19 +02:00
dto.OriginalRunTimeTicks = item.OriginalRunTimeTicks;
}
2013-09-04 19:02:19 +02:00
dto.DisplayMediaType = item.DisplayMediaType;
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.MetadataSettings))
{
dto.LockedFields = item.LockedFields;
dto.EnableInternetProviders = !item.DontFetchMeta;
2013-07-16 18:03:28 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.Budget))
2013-07-16 18:03:28 +02:00
{
2013-09-04 19:02:19 +02:00
dto.Budget = item.Budget;
2013-07-16 18:03:28 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.Revenue))
2013-07-16 18:03:28 +02:00
{
2013-09-04 19:02:19 +02:00
dto.Revenue = item.Revenue;
2013-02-21 02:33:05 +01:00
}
2013-04-25 00:34:38 +02:00
2013-09-04 19:02:19 +02:00
dto.EndDate = item.EndDate;
2013-04-25 00:34:38 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.HomePageUrl))
2013-04-25 00:34:38 +02:00
{
2013-09-04 19:02:19 +02:00
dto.HomePageUrl = item.HomePageUrl;
2013-04-25 00:34:38 +02:00
}
2013-07-12 21:56:40 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.Tags))
{
dto.Tags = item.Tags;
}
2013-07-12 21:56:40 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.ProductionLocations))
2013-07-12 21:56:40 +02:00
{
2013-09-04 19:02:19 +02:00
dto.ProductionLocations = item.ProductionLocations;
2013-07-12 21:56:40 +02:00
}
2013-08-31 01:55:17 +02:00
2013-09-04 19:02:19 +02:00
dto.AspectRatio = item.AspectRatio;
2013-08-31 01:55:17 +02:00
2013-09-04 19:02:19 +02:00
dto.BackdropImageTags = GetBackdropImageTags(item);
dto.ScreenshotImageTags = GetScreenshotImageTags(item);
if (fields.Contains(ItemFields.Genres))
2013-08-31 01:55:17 +02:00
{
2013-09-04 19:02:19 +02:00
dto.Genres = item.Genres;
2013-08-31 01:55:17 +02:00
}
2013-07-12 21:56:40 +02:00
2013-09-04 19:02:19 +02:00
dto.ImageTags = new Dictionary<ImageType, Guid>();
2013-09-04 19:02:19 +02:00
foreach (var image in item.Images)
{
2013-09-04 19:02:19 +02:00
var type = image.Key;
2013-09-04 19:02:19 +02:00
var tag = GetImageCacheTag(item, type, image.Value);
if (tag.HasValue)
{
2013-09-04 19:02:19 +02:00
dto.ImageTags[type] = tag.Value;
}
}
2013-09-04 19:02:19 +02:00
dto.Id = GetDtoId(item);
dto.IndexNumber = item.IndexNumber;
dto.IsFolder = item.IsFolder;
dto.Language = item.Language;
dto.MediaType = item.MediaType;
dto.LocationType = item.LocationType;
dto.CriticRating = item.CriticRating;
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.CriticRatingSummary))
{
dto.CriticRatingSummary = item.CriticRatingSummary;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var localTrailerCount = item.LocalTrailerIds.Count;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (localTrailerCount > 0)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.LocalTrailerCount = localTrailerCount;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.Name = item.Name;
dto.OfficialRating = item.OfficialRating;
2013-03-09 06:15:51 +01:00
2013-09-04 19:02:19 +02:00
var hasOverview = fields.Contains(ItemFields.Overview);
var hasHtmlOverview = fields.Contains(ItemFields.OverviewHtml);
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (hasOverview || hasHtmlOverview)
{
var strippedOverview = string.IsNullOrEmpty(item.Overview) ? item.Overview : item.Overview.StripHtml();
2013-06-08 19:04:17 +02:00
2013-09-04 19:02:19 +02:00
if (hasOverview)
2013-03-09 06:15:51 +01:00
{
2013-09-04 19:02:19 +02:00
dto.Overview = strippedOverview;
2013-02-21 02:33:05 +01:00
}
2013-06-08 19:04:17 +02:00
2013-09-04 19:02:19 +02:00
// Only supply the html version if there was actually html content
if (hasHtmlOverview)
2013-06-08 19:04:17 +02:00
{
2013-09-04 19:02:19 +02:00
dto.OverviewHtml = item.Overview;
2013-06-08 19:04:17 +02:00
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
// If there are no backdrops, indicate what parent has them in case the Ui wants to allow inheritance
if (dto.BackdropImageTags.Count == 0)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var parentWithBackdrop = GetParentBackdropItem(item, owner);
if (parentWithBackdrop != null)
{
dto.ParentBackdropItemId = GetDtoId(parentWithBackdrop);
dto.ParentBackdropImageTags = GetBackdropImageTags(parentWithBackdrop);
}
2013-02-21 02:33:05 +01:00
}
2013-07-25 21:17:44 +02:00
2013-09-04 19:02:19 +02:00
if (item.Parent != null && fields.Contains(ItemFields.ParentId))
2013-07-25 21:17:44 +02:00
{
2013-09-04 19:02:19 +02:00
dto.ParentId = GetDtoId(item.Parent);
2013-07-25 21:17:44 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.ParentIndexNumber = item.ParentIndexNumber;
2013-09-04 19:02:19 +02:00
// If there is no logo, indicate what parent has one in case the Ui wants to allow inheritance
if (!dto.HasLogo)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var parentWithLogo = GetParentImageItem(item, ImageType.Logo, owner);
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (parentWithLogo != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.ParentLogoItemId = GetDtoId(parentWithLogo);
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.ParentLogoImageTag = GetImageCacheTag(parentWithLogo, ImageType.Logo, parentWithLogo.GetImage(ImageType.Logo));
}
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// If there is no art, indicate what parent has one in case the Ui wants to allow inheritance
if (!dto.HasArtImage)
{
var parentWithImage = GetParentImageItem(item, ImageType.Art, owner);
if (parentWithImage != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.ParentArtItemId = GetDtoId(parentWithImage);
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.ParentArtImageTag = GetImageCacheTag(parentWithImage, ImageType.Art, parentWithImage.GetImage(ImageType.Art));
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.Path))
{
dto.Path = item.Path;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
dto.PremiereDate = item.PremiereDate;
dto.ProductionYear = item.ProductionYear;
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.ProviderIds))
{
dto.ProviderIds = item.ProviderIds;
}
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
dto.RunTimeTicks = item.RunTimeTicks;
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.SortName))
{
dto.SortName = item.SortName;
}
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.CustomRating))
{
dto.CustomRating = item.CustomRating;
}
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.Taglines))
{
dto.Taglines = item.Taglines;
}
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.RemoteTrailers))
2013-05-04 05:13:28 +02:00
{
2013-09-04 19:02:19 +02:00
dto.RemoteTrailers = item.RemoteTrailers;
}
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
dto.Type = item.GetType().Name;
dto.CommunityRating = item.CommunityRating;
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
if (item.IsFolder)
{
var folder = (Folder)item;
2013-05-04 05:13:28 +02:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.IndexOptions))
2013-05-04 05:13:28 +02:00
{
2013-09-04 19:02:19 +02:00
dto.IndexOptions = folder.IndexByOptionStrings.ToArray();
2013-05-04 05:13:28 +02:00
}
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Add audio info
var audio = item as Audio;
if (audio != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.Album = audio.Album;
dto.AlbumArtist = audio.AlbumArtist;
dto.Artists = audio.Artists.ToArray();
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var albumParent = audio.FindParent<MusicAlbum>();
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (albumParent != null)
{
dto.AlbumId = GetDtoId(albumParent);
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var imagePath = albumParent.PrimaryImagePath;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (!string.IsNullOrEmpty(imagePath))
{
dto.AlbumPrimaryImageTag = GetImageCacheTag(albumParent, ImageType.Primary, imagePath);
}
2013-02-21 02:33:05 +01:00
}
}
2013-09-04 19:02:19 +02:00
var album = item as MusicAlbum;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (album != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var songs = album.RecursiveChildren.OfType<Audio>().ToList();
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.AlbumArtist = songs.Select(i => i.AlbumArtist).FirstOrDefault(i => !string.IsNullOrEmpty(i));
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.Artists =
songs.SelectMany(i => i.Artists)
2013-09-04 19:02:19 +02:00
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Add video info
var video = item as Video;
if (video != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.VideoType = video.VideoType;
dto.Video3DFormat = video.Video3DFormat;
dto.IsoType = video.IsoType;
dto.MainFeaturePlaylistName = video.MainFeaturePlaylistName;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.PartCount = video.AdditionalPartIds.Count + 1;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.Chapters))
{
dto.Chapters = _itemRepo.GetChapters(video.Id).Select(c => GetChapterInfoDto(c, item)).ToList();
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
if (fields.Contains(ItemFields.MediaStreams))
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
// Add VideoInfo
var iHasMediaStreams = item as IHasMediaStreams;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (iHasMediaStreams != null)
{
dto.MediaStreams = iHasMediaStreams.MediaStreams;
}
}
// Add MovieInfo
var movie = item as Movie;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (movie != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var specialFeatureCount = movie.SpecialFeatureIds.Count;
if (specialFeatureCount > 0)
{
2013-09-04 19:02:19 +02:00
dto.SpecialFeatureCount = specialFeatureCount;
}
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
// Add EpisodeInfo
var episode = item as Episode;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (episode != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.IndexNumberEnd = episode.IndexNumberEnd;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
// Add SeriesInfo
var series = item as Series;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (series != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.AirDays = series.AirDays;
dto.AirTime = series.AirTime;
dto.Status = series.Status;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.SpecialFeatureCount = series.SpecialFeatureIds.Count;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.SeasonCount = series.SeasonCount;
}
if (episode != null)
{
2013-09-04 19:02:19 +02:00
series = item.FindParent<Series>();
dto.SeriesId = GetDtoId(series);
dto.SeriesName = series.Name;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Add SeasonInfo
var season = item as Season;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (season != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
series = item.FindParent<Series>();
dto.SeriesId = GetDtoId(series);
dto.SeriesName = series.Name;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
var game = item as Game;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (game != null)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
SetGameProperties(dto, game);
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
var musicVideo = item as MusicVideo;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (musicVideo != null)
{
SetMusicVideoProperties(dto, musicVideo);
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
var book = item as Book;
if (book != null)
{
SetBookProperties(dto, book);
}
2013-02-21 02:33:05 +01:00
}
/// <summary>
2013-09-04 19:02:19 +02:00
/// Since it can be slow to make all of these calculations independently, this method will provide a way to do them all at once
2013-02-21 02:33:05 +01:00
/// </summary>
2013-09-04 19:02:19 +02:00
/// <param name="folder">The folder.</param>
/// <param name="user">The user.</param>
/// <param name="dto">The dto.</param>
/// <returns>Task.</returns>
private void SetSpecialCounts(Folder folder, User user, BaseItemDto dto)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var rcentlyAddedItemCount = 0;
var recursiveItemCount = 0;
var unplayed = 0;
long runtime = 0;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
double totalPercentPlayed = 0;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Loop through each recursive child
foreach (var child in folder.GetRecursiveChildren(user, true).Where(i => !i.IsFolder).ToList())
{
var userdata = _userDataRepository.GetUserData(user.Id, child.GetUserDataKey());
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
recursiveItemCount++;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Check is recently added
if (child.IsRecentlyAdded())
{
rcentlyAddedItemCount++;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
var isUnplayed = true;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
// Incrememt totalPercentPlayed
if (userdata != null)
{
if (userdata.Played)
{
totalPercentPlayed += 100;
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
isUnplayed = false;
}
else if (userdata.PlaybackPositionTicks > 0 && child.RunTimeTicks.HasValue && child.RunTimeTicks.Value > 0)
{
double itemPercent = userdata.PlaybackPositionTicks;
itemPercent /= child.RunTimeTicks.Value;
totalPercentPlayed += itemPercent;
}
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
if (isUnplayed)
{
unplayed++;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
runtime += child.RunTimeTicks ?? 0;
}
2013-02-21 02:33:05 +01:00
2013-09-04 19:02:19 +02:00
dto.RecursiveItemCount = recursiveItemCount;
dto.RecentlyAddedItemCount = rcentlyAddedItemCount;
dto.RecursiveUnplayedItemCount = unplayed;
if (recursiveItemCount > 0)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
dto.PlayedPercentage = totalPercentPlayed / recursiveItemCount;
2013-02-21 02:33:05 +01:00
}
2013-09-04 19:02:19 +02:00
if (runtime > 0)
{
dto.CumulativeRunTimeTicks = runtime;
}
2013-02-21 02:33:05 +01:00
}
/// <summary>
2013-09-04 19:02:19 +02:00
/// Attaches the primary image aspect ratio.
2013-02-21 02:33:05 +01:00
/// </summary>
2013-09-04 19:02:19 +02:00
/// <param name="dto">The dto.</param>
2013-02-21 02:33:05 +01:00
/// <param name="item">The item.</param>
2013-09-04 19:02:19 +02:00
/// <param name="logger">The _logger.</param>
/// <returns>Task.</returns>
private async Task AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item)
2013-02-21 02:33:05 +01:00
{
2013-09-04 19:02:19 +02:00
var path = item.PrimaryImagePath;
2013-05-05 06:49:49 +02:00
2013-09-04 19:02:19 +02:00
if (string.IsNullOrEmpty(path))
{
return;
}
var metaFileEntry = item.ResolveArgs.GetMetaFileByPath(path);
// See if we can avoid a file system lookup by looking for the file in ResolveArgs
var dateModified = metaFileEntry == null ? File.GetLastWriteTimeUtc(path) : metaFileEntry.LastWriteTimeUtc;
ImageSize size;
try
2013-05-05 06:49:49 +02:00
{
2013-09-04 19:02:19 +02:00
size = await Kernel.Instance.ImageManager.GetImageSize(path, dateModified).ConfigureAwait(false);
}
2013-09-04 19:02:19 +02:00
catch (FileNotFoundException)
{
2013-09-04 19:02:19 +02:00
_logger.Error("Image file does not exist: {0}", path);
return;
}
catch (Exception ex)
{
_logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path);
return;
}
dto.OriginalPrimaryImageAspectRatio = size.Width / size.Height;
var supportedEnhancers = Kernel.Instance.ImageManager.ImageEnhancers.Where(i =>
{
try
{
return i.Supports(item, ImageType.Primary);
}
catch (Exception ex)
{
_logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
return false;
}
}).ToList();
foreach (var enhancer in supportedEnhancers)
{
try
{
size = enhancer.GetEnhancedImageSize(item, ImageType.Primary, 0, size);
}
catch (Exception ex)
{
_logger.ErrorException("Error in image enhancer: {0}", ex, enhancer.GetType().Name);
}
2013-05-05 06:49:49 +02:00
}
2013-09-04 19:02:19 +02:00
dto.PrimaryImageAspectRatio = size.Width / size.Height;
2013-05-05 06:49:49 +02:00
}
2013-09-04 19:02:19 +02:00
2013-02-21 02:33:05 +01:00
}
}