/// <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="options">The options.</param> private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem owner, DtoOptions options) { if (options.ContainsField(ItemFields.DateCreated)) { dto.DateCreated = item.DateCreated; } if (options.ContainsField(ItemFields.Settings)) { dto.LockedFields = item.LockedFields; dto.LockData = item.IsLocked; dto.ForcedSortName = item.ForcedSortName; } dto.Container = item.Container; dto.EndDate = item.EndDate; if (options.ContainsField(ItemFields.ExternalUrls)) { dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray(); } if (options.ContainsField(ItemFields.Tags)) { dto.Tags = item.Tags; } var hasAspectRatio = item as IHasAspectRatio; if (hasAspectRatio != null) { dto.AspectRatio = hasAspectRatio.AspectRatio; } var backdropLimit = options.GetImageLimit(ImageType.Backdrop); if (backdropLimit > 0) { dto.BackdropImageTags = GetImageTags(item, item.GetImages(ImageType.Backdrop).Take(backdropLimit).ToList()); } if (options.ContainsField(ItemFields.ScreenshotImageTags)) { var screenshotLimit = options.GetImageLimit(ImageType.Screenshot); if (screenshotLimit > 0) { dto.ScreenshotImageTags = GetImageTags(item, item.GetImages(ImageType.Screenshot).Take(screenshotLimit).ToList()); } } if (options.ContainsField(ItemFields.Genres)) { dto.Genres = item.Genres; AttachGenreItems(dto, item); } if (options.EnableImages) { dto.ImageTags = new Dictionary <ImageType, string>(); // Prevent implicitly captured closure var currentItem = item; foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type)) .ToList()) { if (options.GetImageLimit(image.Type) > 0) { var tag = GetImageCacheTag(item, image); if (tag != null) { dto.ImageTags[image.Type] = tag; } } } } dto.Id = item.Id; dto.IndexNumber = item.IndexNumber; dto.ParentIndexNumber = item.ParentIndexNumber; if (item.IsFolder) { dto.IsFolder = true; } else if (item is IHasMediaSources) { dto.IsFolder = false; } dto.MediaType = item.MediaType; if (!(item is LiveTvProgram)) { dto.LocationType = item.LocationType; } dto.Audio = item.Audio; if (options.ContainsField(ItemFields.Settings)) { dto.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode; dto.PreferredMetadataLanguage = item.PreferredMetadataLanguage; } dto.CriticRating = item.CriticRating; var hasDisplayOrder = item as IHasDisplayOrder; if (hasDisplayOrder != null) { dto.DisplayOrder = hasDisplayOrder.DisplayOrder; } var hasCollectionType = item as IHasCollectionType; if (hasCollectionType != null) { dto.CollectionType = hasCollectionType.CollectionType; } if (options.ContainsField(ItemFields.RemoteTrailers)) { dto.RemoteTrailers = item.RemoteTrailers; } dto.Name = item.Name; dto.OfficialRating = item.OfficialRating; if (options.ContainsField(ItemFields.Overview)) { dto.Overview = item.Overview; } if (options.ContainsField(ItemFields.OriginalTitle)) { dto.OriginalTitle = item.OriginalTitle; } if (options.ContainsField(ItemFields.ParentId)) { dto.ParentId = item.DisplayParentId; } AddInheritedImages(dto, item, options, owner); if (options.ContainsField(ItemFields.Path)) { dto.Path = GetMappedPath(item, owner); } if (options.ContainsField(ItemFields.EnableMediaSourceDisplay)) { dto.EnableMediaSourceDisplay = item.EnableMediaSourceDisplay; } dto.PremiereDate = item.PremiereDate; dto.ProductionYear = item.ProductionYear; if (options.ContainsField(ItemFields.ProviderIds)) { dto.ProviderIds = item.ProviderIds; } dto.RunTimeTicks = item.RunTimeTicks; if (options.ContainsField(ItemFields.SortName)) { dto.SortName = item.SortName; } if (options.ContainsField(ItemFields.CustomRating)) { dto.CustomRating = item.CustomRating; } if (options.ContainsField(ItemFields.Taglines)) { if (!string.IsNullOrEmpty(item.Tagline)) { dto.Taglines = new string[] { item.Tagline }; } if (dto.Taglines == null) { dto.Taglines = Array.Empty <string>(); } } dto.Type = item.GetClientTypeName(); if ((item.CommunityRating ?? 0) > 0) { dto.CommunityRating = item.CommunityRating; } var supportsPlaceHolders = item as ISupportsPlaceHolders; if (supportsPlaceHolders != null && supportsPlaceHolders.IsPlaceHolder) { dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder; } // Add audio info var audio = item as Audio; if (audio != null) { dto.Album = audio.Album; if (audio.ExtraType.HasValue) { dto.ExtraType = audio.ExtraType.Value.ToString(); } var albumParent = audio.AlbumEntity; if (albumParent != null) { dto.AlbumId = albumParent.Id; dto.AlbumPrimaryImageTag = GetImageCacheTag(albumParent, ImageType.Primary); } //if (options.ContainsField(ItemFields.MediaSourceCount)) //{ // Songs always have one //} } var hasArtist = item as IHasArtist; if (hasArtist != null) { dto.Artists = hasArtist.Artists; //var artistItems = _libraryManager.GetArtists(new InternalItemsQuery //{ // EnableTotalRecordCount = false, // ItemIds = new[] { item.Id.ToString("N") } //}); //dto.ArtistItems = artistItems.Items // .Select(i => // { // var artist = i.Item1; // return new NameIdPair // { // Name = artist.Name, // Id = artist.Id.ToString("N") // }; // }) // .ToList(); // Include artists that are not in the database yet, e.g., just added via metadata editor //var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList(); dto.ArtistItems = hasArtist.Artists //.Except(foundArtists, new DistinctNameComparer()) .Select(i => { // This should not be necessary but we're seeing some cases of it if (string.IsNullOrEmpty(i)) { return(null); } var artist = _libraryManager.GetArtist(i, new DtoOptions(false) { EnableImages = false }); if (artist != null) { return(new NameGuidPair { Name = artist.Name, Id = artist.Id }); } return(null); }).Where(i => i != null).ToArray(); } var hasAlbumArtist = item as IHasAlbumArtist; if (hasAlbumArtist != null) { dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault(); //var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery //{ // EnableTotalRecordCount = false, // ItemIds = new[] { item.Id.ToString("N") } //}); //dto.AlbumArtists = artistItems.Items // .Select(i => // { // var artist = i.Item1; // return new NameIdPair // { // Name = artist.Name, // Id = artist.Id.ToString("N") // }; // }) // .ToList(); dto.AlbumArtists = hasAlbumArtist.AlbumArtists //.Except(foundArtists, new DistinctNameComparer()) .Select(i => { // This should not be necessary but we're seeing some cases of it if (string.IsNullOrEmpty(i)) { return(null); } var artist = _libraryManager.GetArtist(i, new DtoOptions(false) { EnableImages = false }); if (artist != null) { return(new NameGuidPair { Name = artist.Name, Id = artist.Id }); } return(null); }).Where(i => i != null).ToArray(); } // Add video info var video = item as Video; if (video != null) { dto.VideoType = video.VideoType; dto.Video3DFormat = video.Video3DFormat; dto.IsoType = video.IsoType; if (video.HasSubtitles) { dto.HasSubtitles = video.HasSubtitles; } if (video.AdditionalParts.Length != 0) { dto.PartCount = video.AdditionalParts.Length + 1; } if (options.ContainsField(ItemFields.MediaSourceCount)) { var mediaSourceCount = video.MediaSourceCount; if (mediaSourceCount != 1) { dto.MediaSourceCount = mediaSourceCount; } } if (options.ContainsField(ItemFields.Chapters)) { dto.Chapters = _itemRepo.GetChapters(item); } if (video.ExtraType.HasValue) { dto.ExtraType = video.ExtraType.Value.ToString(); } } if (options.ContainsField(ItemFields.MediaStreams)) { // Add VideoInfo var iHasMediaSources = item as IHasMediaSources; if (iHasMediaSources != null) { MediaStream[] mediaStreams; if (dto.MediaSources != null && dto.MediaSources.Length > 0) { if (item.SourceType == SourceType.Channel) { mediaStreams = dto.MediaSources[0].MediaStreams.ToArray(); } else { mediaStreams = dto.MediaSources.Where(i => string.Equals(i.Id, item.Id.ToString("N"), StringComparison.OrdinalIgnoreCase)) .SelectMany(i => i.MediaStreams) .ToArray(); } } else { mediaStreams = _mediaSourceManager().GetStaticMediaSources(item, true).First().MediaStreams.ToArray(); } dto.MediaStreams = mediaStreams; } } BaseItem[] allExtras = null; if (options.ContainsField(ItemFields.SpecialFeatureCount)) { if (allExtras == null) { allExtras = item.GetExtras().ToArray(); } dto.SpecialFeatureCount = allExtras.Count(i => i.ExtraType.HasValue && BaseItem.DisplayExtraTypes.Contains(i.ExtraType.Value)); } if (options.ContainsField(ItemFields.LocalTrailerCount)) { if (allExtras == null) { allExtras = item.GetExtras().ToArray(); } dto.LocalTrailerCount = allExtras.Count(i => i.ExtraType.HasValue && i.ExtraType.Value == ExtraType.Trailer); } // Add EpisodeInfo var episode = item as Episode; if (episode != null) { dto.IndexNumberEnd = episode.IndexNumberEnd; dto.SeriesName = episode.SeriesName; if (options.ContainsField(ItemFields.SpecialEpisodeNumbers)) { dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber; dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber; dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber; } dto.SeasonName = episode.SeasonName; dto.SeasonId = episode.SeasonId; dto.SeriesId = episode.SeriesId; Series episodeSeries = null; //if (options.ContainsField(ItemFields.SeriesPrimaryImage)) { episodeSeries = episodeSeries ?? episode.Series; if (episodeSeries != null) { dto.SeriesPrimaryImageTag = GetImageCacheTag(episodeSeries, ImageType.Primary); } } if (options.ContainsField(ItemFields.SeriesStudio)) { episodeSeries = episodeSeries ?? episode.Series; if (episodeSeries != null) { dto.SeriesStudio = episodeSeries.Studios.FirstOrDefault(); } } } // Add SeriesInfo var series = item as Series; if (series != null) { dto.AirDays = series.AirDays; dto.AirTime = series.AirTime; dto.Status = series.Status.HasValue ? series.Status.Value.ToString() : null; } // Add SeasonInfo var season = item as Season; if (season != null) { dto.SeriesName = season.SeriesName; dto.SeriesId = season.SeriesId; series = null; if (options.ContainsField(ItemFields.SeriesStudio)) { series = series ?? season.Series; if (series != null) { dto.SeriesStudio = series.Studios.FirstOrDefault(); } } //if (options.ContainsField(ItemFields.SeriesPrimaryImage)) { series = series ?? season.Series; if (series != null) { dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary); } } } var musicVideo = item as MusicVideo; if (musicVideo != null) { SetMusicVideoProperties(dto, musicVideo); } var book = item as Book; if (book != null) { SetBookProperties(dto, book); } if (options.ContainsField(ItemFields.ProductionLocations)) { if (item.ProductionLocations.Length > 0 || item is Movie) { dto.ProductionLocations = item.ProductionLocations; } } if (options.ContainsField(ItemFields.Width)) { var width = item.Width; if (width > 0) { dto.Width = width; } } if (options.ContainsField(ItemFields.Height)) { var height = item.Height; if (height > 0) { dto.Height = height; } } if (options.ContainsField(ItemFields.IsHD)) { // Compatibility if (item.IsHD) { dto.IsHD = true; } } var photo = item as Photo; if (photo != null) { SetPhotoProperties(dto, photo); } dto.ChannelId = item.ChannelId; if (item.SourceType == SourceType.Channel) { var channel = _libraryManager.GetItemById(item.ChannelId); if (channel != null) { dto.ChannelName = channel.Name; } } }
private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem owner) { if (!item.SupportsInheritedParentImages) { return; } var logoLimit = options.GetImageLimit(ImageType.Logo); var artLimit = options.GetImageLimit(ImageType.Art); var thumbLimit = options.GetImageLimit(ImageType.Thumb); var backdropLimit = options.GetImageLimit(ImageType.Backdrop); // For now. Emby apps are not using this artLimit = 0; if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0) { return; } BaseItem parent = null; var isFirst = true; var imageTags = dto.ImageTags; while (((!(imageTags != null && imageTags.ContainsKey(ImageType.Logo)) && logoLimit > 0) || (!(imageTags != null && imageTags.ContainsKey(ImageType.Art)) && artLimit > 0) || (!(imageTags != null && imageTags.ContainsKey(ImageType.Thumb)) && thumbLimit > 0) || parent is Series) && (parent = parent ?? (isFirst ? GetImageDisplayParent(item, item) ?? owner : parent)) != null) { if (parent == null) { break; } var allImages = parent.ImageInfos; if (logoLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Logo)) && dto.ParentLogoItemId == null) { var image = allImages.FirstOrDefault(i => i.Type == ImageType.Logo); if (image != null) { dto.ParentLogoItemId = GetDtoId(parent); dto.ParentLogoImageTag = GetImageCacheTag(parent, image); } } if (artLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtItemId == null) { var image = allImages.FirstOrDefault(i => i.Type == ImageType.Art); if (image != null) { dto.ParentArtItemId = GetDtoId(parent); dto.ParentArtImageTag = GetImageCacheTag(parent, image); } } if (thumbLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentThumbItemId == null || parent is Series) && !(parent is ICollectionFolder) && !(parent is UserView)) { var image = allImages.FirstOrDefault(i => i.Type == ImageType.Thumb); if (image != null) { dto.ParentThumbItemId = GetDtoId(parent); dto.ParentThumbImageTag = GetImageCacheTag(parent, image); } } if (backdropLimit > 0 && !((dto.BackdropImageTags != null && dto.BackdropImageTags.Length > 0) || (dto.ParentBackdropImageTags != null && dto.ParentBackdropImageTags.Length > 0))) { var images = allImages.Where(i => i.Type == ImageType.Backdrop).Take(backdropLimit).ToList(); if (images.Count > 0) { dto.ParentBackdropItemId = GetDtoId(parent); dto.ParentBackdropImageTags = GetImageTags(parent, images); } } isFirst = false; if (!parent.SupportsInheritedParentImages) { break; } parent = GetImageDisplayParent(parent, item); } }
/// <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="options">The options.</param> private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem owner, DtoOptions options) { var fields = options.Fields; if (fields.Contains(ItemFields.DateCreated)) { dto.DateCreated = item.DateCreated; } if (fields.Contains(ItemFields.DisplayMediaType)) { dto.DisplayMediaType = item.DisplayMediaType; } if (fields.Contains(ItemFields.Settings)) { dto.LockedFields = item.LockedFields; dto.LockData = item.IsLocked; dto.ForcedSortName = item.ForcedSortName; } var hasBudget = item as IHasBudget; if (hasBudget != null) { if (fields.Contains(ItemFields.Budget)) { dto.Budget = hasBudget.Budget; } if (fields.Contains(ItemFields.Revenue)) { dto.Revenue = hasBudget.Revenue; } } dto.EndDate = item.EndDate; if (fields.Contains(ItemFields.HomePageUrl)) { dto.HomePageUrl = item.HomePageUrl; } if (fields.Contains(ItemFields.ExternalUrls)) { dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray(); } if (fields.Contains(ItemFields.Tags)) { var hasTags = item as IHasTags; if (hasTags != null) { dto.Tags = hasTags.Tags; } if (dto.Tags == null) { dto.Tags = new List<string>(); } } if (fields.Contains(ItemFields.Keywords)) { var hasTags = item as IHasKeywords; if (hasTags != null) { dto.Keywords = hasTags.Keywords; } if (dto.Keywords == null) { dto.Keywords = new List<string>(); } } if (fields.Contains(ItemFields.ProductionLocations)) { SetProductionLocations(item, dto); } var hasAspectRatio = item as IHasAspectRatio; if (hasAspectRatio != null) { dto.AspectRatio = hasAspectRatio.AspectRatio; } if (fields.Contains(ItemFields.Metascore)) { var hasMetascore = item as IHasMetascore; if (hasMetascore != null) { dto.Metascore = hasMetascore.Metascore; } } if (fields.Contains(ItemFields.AwardSummary)) { var hasAwards = item as IHasAwards; if (hasAwards != null) { dto.AwardSummary = hasAwards.AwardSummary; } } var backdropLimit = options.GetImageLimit(ImageType.Backdrop); if (backdropLimit > 0) { dto.BackdropImageTags = GetBackdropImageTags(item, backdropLimit); } if (fields.Contains(ItemFields.ScreenshotImageTags)) { var screenshotLimit = options.GetImageLimit(ImageType.Screenshot); if (screenshotLimit > 0) { dto.ScreenshotImageTags = GetScreenshotImageTags(item, screenshotLimit); } } if (fields.Contains(ItemFields.Genres)) { dto.Genres = item.Genres; } dto.ImageTags = new Dictionary<ImageType, string>(); // Prevent implicitly captured closure var currentItem = item; foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type)) .ToList()) { if (options.GetImageLimit(image.Type) > 0) { var tag = GetImageCacheTag(item, image); if (tag != null) { dto.ImageTags[image.Type] = tag; } } } dto.Id = GetDtoId(item); dto.IndexNumber = item.IndexNumber; dto.IsFolder = item.IsFolder; dto.MediaType = item.MediaType; dto.LocationType = item.LocationType; dto.IsHD = item.IsHD; dto.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode; dto.PreferredMetadataLanguage = item.PreferredMetadataLanguage; var hasCriticRating = item as IHasCriticRating; if (hasCriticRating != null) { dto.CriticRating = hasCriticRating.CriticRating; if (fields.Contains(ItemFields.CriticRatingSummary)) { dto.CriticRatingSummary = hasCriticRating.CriticRatingSummary; } } var hasTrailers = item as IHasTrailers; if (hasTrailers != null) { dto.LocalTrailerCount = hasTrailers.GetTrailerIds().Count; } var hasDisplayOrder = item as IHasDisplayOrder; if (hasDisplayOrder != null) { dto.DisplayOrder = hasDisplayOrder.DisplayOrder; } var userView = item as UserView; if (userView != null) { dto.CollectionType = userView.ViewType; } if (fields.Contains(ItemFields.RemoteTrailers)) { dto.RemoteTrailers = hasTrailers != null ? hasTrailers.RemoteTrailers : new List<MediaUrl>(); } dto.Name = item.Name; dto.OfficialRating = item.OfficialRating; if (fields.Contains(ItemFields.Overview)) { dto.Overview = item.Overview; } if (fields.Contains(ItemFields.ShortOverview)) { var hasShortOverview = item as IHasShortOverview; if (hasShortOverview != null) { dto.ShortOverview = hasShortOverview.ShortOverview; } } // If there are no backdrops, indicate what parent has them in case the Ui wants to allow inheritance if (backdropLimit > 0 && dto.BackdropImageTags.Count == 0) { var parentWithBackdrop = GetParentBackdropItem(item, owner); if (parentWithBackdrop != null) { dto.ParentBackdropItemId = GetDtoId(parentWithBackdrop); dto.ParentBackdropImageTags = GetBackdropImageTags(parentWithBackdrop, backdropLimit); } } if (fields.Contains(ItemFields.ParentId)) { var displayParent = item.DisplayParent; if (displayParent != null) { dto.ParentId = GetDtoId(displayParent); } } dto.ParentIndexNumber = item.ParentIndexNumber; // If there is no logo, indicate what parent has one in case the Ui wants to allow inheritance if (!dto.HasLogo && options.GetImageLimit(ImageType.Logo) > 0) { var parentWithLogo = GetParentImageItem(item, ImageType.Logo, owner); if (parentWithLogo != null) { dto.ParentLogoItemId = GetDtoId(parentWithLogo); dto.ParentLogoImageTag = GetImageCacheTag(parentWithLogo, ImageType.Logo); } } // If there is no art, indicate what parent has one in case the Ui wants to allow inheritance if (!dto.HasArtImage && options.GetImageLimit(ImageType.Art) > 0) { var parentWithImage = GetParentImageItem(item, ImageType.Art, owner); if (parentWithImage != null) { dto.ParentArtItemId = GetDtoId(parentWithImage); dto.ParentArtImageTag = GetImageCacheTag(parentWithImage, ImageType.Art); } } // If there is no thumb, indicate what parent has one in case the Ui wants to allow inheritance if (!dto.HasThumb && options.GetImageLimit(ImageType.Thumb) > 0) { var parentWithImage = GetParentImageItem(item, ImageType.Thumb, owner); if (parentWithImage != null) { dto.ParentThumbItemId = GetDtoId(parentWithImage); dto.ParentThumbImageTag = GetImageCacheTag(parentWithImage, ImageType.Thumb); } } if (fields.Contains(ItemFields.Path)) { dto.Path = GetMappedPath(item); } dto.PremiereDate = item.PremiereDate; dto.ProductionYear = item.ProductionYear; if (fields.Contains(ItemFields.ProviderIds)) { dto.ProviderIds = item.ProviderIds; } dto.RunTimeTicks = item.RunTimeTicks; if (fields.Contains(ItemFields.SortName)) { dto.SortName = item.SortName; } if (fields.Contains(ItemFields.CustomRating)) { dto.CustomRating = item.CustomRating; } if (fields.Contains(ItemFields.Taglines)) { var hasTagline = item as IHasTaglines; if (hasTagline != null) { dto.Taglines = hasTagline.Taglines; } if (dto.Taglines == null) { dto.Taglines = new List<string>(); } } dto.Type = item.GetClientTypeName(); dto.CommunityRating = item.CommunityRating; if (fields.Contains(ItemFields.VoteCount)) { dto.VoteCount = item.VoteCount; } if (item.IsFolder) { var folder = (Folder)item; if (fields.Contains(ItemFields.IndexOptions)) { dto.IndexOptions = folder.IndexByOptionStrings.ToArray(); } } var supportsPlaceHolders = item as ISupportsPlaceHolders; if (supportsPlaceHolders != null) { dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder; } // Add audio info var audio = item as Audio; if (audio != null) { dto.Album = audio.Album; var albumParent = audio.AlbumEntity; if (albumParent != null) { dto.AlbumId = GetDtoId(albumParent); dto.AlbumPrimaryImageTag = GetImageCacheTag(albumParent, ImageType.Primary); } //if (fields.Contains(ItemFields.MediaSourceCount)) //{ // Songs always have one //} } var hasArtist = item as IHasArtist; if (hasArtist != null) { dto.Artists = hasArtist.Artists; dto.ArtistItems = hasArtist .Artists .Select(i => { try { var artist = _libraryManager.GetArtist(i); return new NameIdPair { Name = artist.Name, Id = artist.Id.ToString("N") }; } catch (Exception ex) { _logger.ErrorException("Error getting artist", ex); return null; } }) .Where(i => i != null) .ToList(); } var hasAlbumArtist = item as IHasAlbumArtist; if (hasAlbumArtist != null) { dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault(); dto.AlbumArtists = hasAlbumArtist .AlbumArtists .Select(i => { try { var artist = _libraryManager.GetArtist(i); return new NameIdPair { Name = artist.Name, Id = artist.Id.ToString("N") }; } catch (Exception ex) { _logger.ErrorException("Error getting album artist", ex); return null; } }) .Where(i => i != null) .ToList(); } // Add video info var video = item as Video; if (video != null) { dto.VideoType = video.VideoType; dto.Video3DFormat = video.Video3DFormat; dto.IsoType = video.IsoType; if (video.HasSubtitles) { dto.HasSubtitles = video.HasSubtitles; } if (video.AdditionalParts.Count != 0) { dto.PartCount = video.AdditionalParts.Count + 1; } if (fields.Contains(ItemFields.MediaSourceCount)) { if (video.MediaSourceCount != 1) { dto.MediaSourceCount = video.MediaSourceCount; } } if (fields.Contains(ItemFields.Chapters)) { dto.Chapters = GetChapterInfoDtos(item); } } if (fields.Contains(ItemFields.MediaStreams)) { // Add VideoInfo var iHasMediaSources = item as IHasMediaSources; if (iHasMediaSources != null) { List<MediaStream> mediaStreams; if (dto.MediaSources != null && dto.MediaSources.Count > 0) { mediaStreams = dto.MediaSources.Where(i => new Guid(i.Id) == item.Id) .SelectMany(i => i.MediaStreams) .ToList(); } else { mediaStreams = _mediaSourceManager().GetStaticMediaSources(iHasMediaSources, true).First().MediaStreams; } dto.MediaStreams = mediaStreams; } } // Add MovieInfo var movie = item as Movie; if (movie != null) { if (fields.Contains(ItemFields.TmdbCollectionName)) { dto.TmdbCollectionName = movie.TmdbCollectionName; } } var hasSpecialFeatures = item as IHasSpecialFeatures; if (hasSpecialFeatures != null) { var specialFeatureCount = hasSpecialFeatures.SpecialFeatureIds.Count; if (specialFeatureCount > 0) { dto.SpecialFeatureCount = specialFeatureCount; } } // Add EpisodeInfo var episode = item as Episode; if (episode != null) { dto.IndexNumberEnd = episode.IndexNumberEnd; if (fields.Contains(ItemFields.AlternateEpisodeNumbers)) { dto.DvdSeasonNumber = episode.DvdSeasonNumber; dto.DvdEpisodeNumber = episode.DvdEpisodeNumber; dto.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber; } if (fields.Contains(ItemFields.SpecialEpisodeNumbers)) { dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber; dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber; dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber; } var episodeSeason = episode.Season; if (episodeSeason != null) { dto.SeasonId = episodeSeason.Id.ToString("N"); if (fields.Contains(ItemFields.SeasonName)) { dto.SeasonName = episodeSeason.Name; } } if (fields.Contains(ItemFields.SeriesGenres)) { var episodeseries = episode.Series; if (episodeseries != null) { dto.SeriesGenres = episodeseries.Genres.ToList(); } } } // Add SeriesInfo var series = item as Series; if (series != null) { dto.AirDays = series.AirDays; dto.AirTime = series.AirTime; dto.SeriesStatus = series.Status; if (fields.Contains(ItemFields.Settings)) { dto.DisplaySpecialsWithSeasons = series.DisplaySpecialsWithSeasons; } dto.AnimeSeriesIndex = series.AnimeSeriesIndex; } if (episode != null) { series = episode.Series; if (series != null) { dto.SeriesId = GetDtoId(series); dto.SeriesName = series.Name; if (fields.Contains(ItemFields.AirTime)) { dto.AirTime = series.AirTime; } if (options.GetImageLimit(ImageType.Thumb) > 0) { dto.SeriesThumbImageTag = GetImageCacheTag(series, ImageType.Thumb); } if (options.GetImageLimit(ImageType.Primary) > 0) { dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary); } if (fields.Contains(ItemFields.SeriesStudio)) { dto.SeriesStudio = series.Studios.FirstOrDefault(); } } } // Add SeasonInfo var season = item as Season; if (season != null) { series = season.Series; if (series != null) { dto.SeriesId = GetDtoId(series); dto.SeriesName = series.Name; dto.AirTime = series.AirTime; dto.SeriesStudio = series.Studios.FirstOrDefault(); if (options.GetImageLimit(ImageType.Primary) > 0) { dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary); } } } var game = item as Game; if (game != null) { SetGameProperties(dto, game); } var gameSystem = item as GameSystem; if (gameSystem != null) { SetGameSystemProperties(dto, gameSystem); } var musicVideo = item as MusicVideo; if (musicVideo != null) { SetMusicVideoProperties(dto, musicVideo); } var book = item as Book; if (book != null) { SetBookProperties(dto, book); } var photo = item as Photo; if (photo != null) { SetPhotoProperties(dto, photo); } dto.ChannelId = item.ChannelId; var channelItem = item as IChannelItem; if (channelItem != null) { dto.ChannelName = _channelManagerFactory().GetChannel(channelItem.ChannelId).Name; } var channelMediaItem = item as IChannelMediaItem; if (channelMediaItem != null) { dto.ExtraType = channelMediaItem.ExtraType; } }
private void AttachLinkedChildImages(BaseItemDto dto, Folder folder, User user, DtoOptions options) { List<BaseItem> linkedChildren = null; var backdropLimit = options.GetImageLimit(ImageType.Backdrop); if (backdropLimit > 0 && dto.BackdropImageTags.Count == 0) { linkedChildren = user == null ? folder.GetRecursiveChildren().ToList() : folder.GetRecursiveChildren(user).ToList(); var parentWithBackdrop = linkedChildren.FirstOrDefault(i => i.GetImages(ImageType.Backdrop).Any()); if (parentWithBackdrop != null) { dto.ParentBackdropItemId = GetDtoId(parentWithBackdrop); dto.ParentBackdropImageTags = GetBackdropImageTags(parentWithBackdrop, backdropLimit); } } if (!dto.ImageTags.ContainsKey(ImageType.Primary) && options.GetImageLimit(ImageType.Primary) > 0) { if (linkedChildren == null) { linkedChildren = user == null ? folder.GetRecursiveChildren().ToList() : folder.GetRecursiveChildren(user).ToList(); } var parentWithImage = linkedChildren.FirstOrDefault(i => i.GetImages(ImageType.Primary).Any()); if (parentWithImage != null) { dto.ParentPrimaryImageItemId = GetDtoId(parentWithImage); dto.ParentPrimaryImageTag = GetImageCacheTag(parentWithImage, ImageType.Primary); } } }
/// <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="options">The options.</param> private void AttachBasicFields(BaseItemDto dto, BaseItem item, BaseItem owner, DtoOptions options) { var fields = options.Fields; if (fields.Contains(ItemFields.DateCreated)) { dto.DateCreated = item.DateCreated; } if (fields.Contains(ItemFields.DisplayMediaType)) { dto.DisplayMediaType = item.DisplayMediaType; } if (fields.Contains(ItemFields.Settings)) { dto.LockedFields = item.LockedFields; dto.LockData = item.IsLocked; dto.ForcedSortName = item.ForcedSortName; } dto.Container = item.Container; var hasBudget = item as IHasBudget; if (hasBudget != null) { if (fields.Contains(ItemFields.Budget)) { dto.Budget = hasBudget.Budget; } if (fields.Contains(ItemFields.Revenue)) { dto.Revenue = hasBudget.Revenue; } } dto.EndDate = item.EndDate; if (fields.Contains(ItemFields.HomePageUrl)) { dto.HomePageUrl = item.HomePageUrl; } if (fields.Contains(ItemFields.ExternalUrls)) { dto.ExternalUrls = _providerManager.GetExternalUrls(item).ToArray(); } if (fields.Contains(ItemFields.Tags)) { dto.Tags = item.Tags; } if (fields.Contains(ItemFields.Keywords)) { dto.Keywords = item.Keywords; } var hasAspectRatio = item as IHasAspectRatio; if (hasAspectRatio != null) { dto.AspectRatio = hasAspectRatio.AspectRatio; } if (fields.Contains(ItemFields.Metascore)) { var hasMetascore = item as IHasMetascore; if (hasMetascore != null) { dto.Metascore = hasMetascore.Metascore; } } if (fields.Contains(ItemFields.AwardSummary)) { var hasAwards = item as IHasAwards; if (hasAwards != null) { dto.AwardSummary = hasAwards.AwardSummary; } } var backdropLimit = options.GetImageLimit(ImageType.Backdrop); if (backdropLimit > 0) { dto.BackdropImageTags = GetImageTags(item, item.GetImages(ImageType.Backdrop).Take(backdropLimit).ToList()); } if (fields.Contains(ItemFields.ScreenshotImageTags)) { var screenshotLimit = options.GetImageLimit(ImageType.Screenshot); if (screenshotLimit > 0) { dto.ScreenshotImageTags = GetImageTags(item, item.GetImages(ImageType.Screenshot).Take(screenshotLimit).ToList()); } } if (fields.Contains(ItemFields.Genres)) { dto.Genres = item.Genres; } if (options.EnableImages) { dto.ImageTags = new Dictionary<ImageType, string>(); // Prevent implicitly captured closure var currentItem = item; foreach (var image in currentItem.ImageInfos.Where(i => !currentItem.AllowsMultipleImages(i.Type)) .ToList()) { if (options.GetImageLimit(image.Type) > 0) { var tag = GetImageCacheTag(item, image); if (tag != null) { dto.ImageTags[image.Type] = tag; } } } } dto.Id = GetDtoId(item); dto.IndexNumber = item.IndexNumber; dto.ParentIndexNumber = item.ParentIndexNumber; if (item.IsFolder) { dto.IsFolder = true; } else if (item is IHasMediaSources) { dto.IsFolder = false; } dto.MediaType = item.MediaType; dto.LocationType = item.LocationType; if (item.IsHD.HasValue && item.IsHD.Value) { dto.IsHD = item.IsHD; } dto.Audio = item.Audio; if (fields.Contains(ItemFields.Settings)) { dto.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode; dto.PreferredMetadataLanguage = item.PreferredMetadataLanguage; } dto.CriticRating = item.CriticRating; if (fields.Contains(ItemFields.CriticRatingSummary)) { dto.CriticRatingSummary = item.CriticRatingSummary; } var hasTrailers = item as IHasTrailers; if (hasTrailers != null) { dto.LocalTrailerCount = hasTrailers.GetTrailerIds().Count; } var hasDisplayOrder = item as IHasDisplayOrder; if (hasDisplayOrder != null) { dto.DisplayOrder = hasDisplayOrder.DisplayOrder; } var userView = item as UserView; if (userView != null) { dto.CollectionType = userView.ViewType; } if (fields.Contains(ItemFields.RemoteTrailers)) { dto.RemoteTrailers = hasTrailers != null ? hasTrailers.RemoteTrailers : new List<MediaUrl>(); } dto.Name = item.Name; dto.OfficialRating = item.OfficialRating; if (fields.Contains(ItemFields.Overview)) { dto.Overview = item.Overview; } if (fields.Contains(ItemFields.OriginalTitle)) { dto.OriginalTitle = item.OriginalTitle; } if (fields.Contains(ItemFields.ShortOverview)) { dto.ShortOverview = item.ShortOverview; } if (fields.Contains(ItemFields.ParentId)) { var displayParentId = item.DisplayParentId; if (displayParentId.HasValue) { dto.ParentId = displayParentId.Value.ToString("N"); } } AddInheritedImages(dto, item, options, owner); if (fields.Contains(ItemFields.Path)) { dto.Path = GetMappedPath(item); } dto.PremiereDate = item.PremiereDate; dto.ProductionYear = item.ProductionYear; if (fields.Contains(ItemFields.ProviderIds)) { dto.ProviderIds = item.ProviderIds; } dto.RunTimeTicks = item.RunTimeTicks; if (fields.Contains(ItemFields.SortName)) { dto.SortName = item.SortName; } if (fields.Contains(ItemFields.CustomRating)) { dto.CustomRating = item.CustomRating; } if (fields.Contains(ItemFields.Taglines)) { if (!string.IsNullOrWhiteSpace(item.Tagline)) { dto.Taglines = new List<string> { item.Tagline }; } if (dto.Taglines == null) { dto.Taglines = new List<string>(); } } dto.Type = item.GetClientTypeName(); dto.CommunityRating = item.CommunityRating; if (fields.Contains(ItemFields.VoteCount)) { dto.VoteCount = item.VoteCount; } //if (item.IsFolder) //{ // var folder = (Folder)item; // if (fields.Contains(ItemFields.IndexOptions)) // { // dto.IndexOptions = folder.IndexByOptionStrings.ToArray(); // } //} var supportsPlaceHolders = item as ISupportsPlaceHolders; if (supportsPlaceHolders != null) { dto.IsPlaceHolder = supportsPlaceHolders.IsPlaceHolder; } // Add audio info var audio = item as Audio; if (audio != null) { dto.Album = audio.Album; dto.ExtraType = audio.ExtraType; var albumParent = audio.AlbumEntity; if (albumParent != null) { dto.AlbumId = GetDtoId(albumParent); dto.AlbumPrimaryImageTag = GetImageCacheTag(albumParent, ImageType.Primary); } //if (fields.Contains(ItemFields.MediaSourceCount)) //{ // Songs always have one //} } var hasArtist = item as IHasArtist; if (hasArtist != null) { dto.Artists = hasArtist.Artists; var artistItems = _libraryManager.GetArtists(new InternalItemsQuery { EnableTotalRecordCount = false, ItemIds = new[] { item.Id.ToString("N") } }); dto.ArtistItems = artistItems.Items .Select(i => { var artist = i.Item1; return new NameIdPair { Name = artist.Name, Id = artist.Id.ToString("N") }; }) .ToList(); // Include artists that are not in the database yet, e.g., just added via metadata editor var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList(); dto.ArtistItems.AddRange(hasArtist.Artists .Except(foundArtists, new DistinctNameComparer()) .Select(i => { // This should not be necessary but we're seeing some cases of it if (string.IsNullOrWhiteSpace(i)) { return null; } var artist = _libraryManager.GetArtist(i); if (artist != null) { return new NameIdPair { Name = artist.Name, Id = artist.Id.ToString("N") }; } return null; }).Where(i => i != null)); } var hasAlbumArtist = item as IHasAlbumArtist; if (hasAlbumArtist != null) { dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault(); var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery { EnableTotalRecordCount = false, ItemIds = new[] { item.Id.ToString("N") } }); dto.AlbumArtists = artistItems.Items .Select(i => { var artist = i.Item1; return new NameIdPair { Name = artist.Name, Id = artist.Id.ToString("N") }; }) .ToList(); } // Add video info var video = item as Video; if (video != null) { dto.VideoType = video.VideoType; dto.Video3DFormat = video.Video3DFormat; dto.IsoType = video.IsoType; if (video.HasSubtitles) { dto.HasSubtitles = video.HasSubtitles; } if (video.AdditionalParts.Count != 0) { dto.PartCount = video.AdditionalParts.Count + 1; } if (fields.Contains(ItemFields.MediaSourceCount)) { var mediaSourceCount = video.MediaSourceCount; if (mediaSourceCount != 1) { dto.MediaSourceCount = mediaSourceCount; } } if (fields.Contains(ItemFields.Chapters)) { dto.Chapters = GetChapterInfoDtos(item); } dto.ExtraType = video.ExtraType; } if (fields.Contains(ItemFields.MediaStreams)) { // Add VideoInfo var iHasMediaSources = item as IHasMediaSources; if (iHasMediaSources != null) { List<MediaStream> mediaStreams; if (dto.MediaSources != null && dto.MediaSources.Count > 0) { mediaStreams = dto.MediaSources.Where(i => new Guid(i.Id) == item.Id) .SelectMany(i => i.MediaStreams) .ToList(); } else { mediaStreams = _mediaSourceManager().GetStaticMediaSources(iHasMediaSources, true).First().MediaStreams; } dto.MediaStreams = mediaStreams; } } var hasSpecialFeatures = item as IHasSpecialFeatures; if (hasSpecialFeatures != null) { var specialFeatureCount = hasSpecialFeatures.SpecialFeatureIds.Count; if (specialFeatureCount > 0) { dto.SpecialFeatureCount = specialFeatureCount; } } // Add EpisodeInfo var episode = item as Episode; if (episode != null) { dto.IndexNumberEnd = episode.IndexNumberEnd; dto.SeriesName = episode.SeriesName; if (fields.Contains(ItemFields.AlternateEpisodeNumbers)) { dto.DvdSeasonNumber = episode.DvdSeasonNumber; dto.DvdEpisodeNumber = episode.DvdEpisodeNumber; dto.AbsoluteEpisodeNumber = episode.AbsoluteEpisodeNumber; } if (fields.Contains(ItemFields.SpecialEpisodeNumbers)) { dto.AirsAfterSeasonNumber = episode.AirsAfterSeasonNumber; dto.AirsBeforeEpisodeNumber = episode.AirsBeforeEpisodeNumber; dto.AirsBeforeSeasonNumber = episode.AirsBeforeSeasonNumber; } var seasonId = episode.SeasonId; if (seasonId.HasValue) { dto.SeasonId = seasonId.Value.ToString("N"); } dto.SeasonName = episode.SeasonName; var seriesId = episode.SeriesId; if (seriesId.HasValue) { dto.SeriesId = seriesId.Value.ToString("N"); } Series episodeSeries = null; if (fields.Contains(ItemFields.SeriesGenres)) { episodeSeries = episodeSeries ?? episode.Series; if (episodeSeries != null) { dto.SeriesGenres = episodeSeries.Genres.ToList(); } } //if (fields.Contains(ItemFields.SeriesPrimaryImage)) { episodeSeries = episodeSeries ?? episode.Series; if (episodeSeries != null) { dto.SeriesPrimaryImageTag = GetImageCacheTag(episodeSeries, ImageType.Primary); } } if (fields.Contains(ItemFields.SeriesStudio)) { episodeSeries = episodeSeries ?? episode.Series; if (episodeSeries != null) { dto.SeriesStudio = episodeSeries.Studios.FirstOrDefault(); } } } // Add SeriesInfo var series = item as Series; if (series != null) { dto.AirDays = series.AirDays; dto.AirTime = series.AirTime; dto.SeriesStatus = series.Status; dto.AnimeSeriesIndex = series.AnimeSeriesIndex; } // Add SeasonInfo var season = item as Season; if (season != null) { dto.SeriesName = season.SeriesName; var seriesId = season.SeriesId; if (seriesId.HasValue) { dto.SeriesId = seriesId.Value.ToString("N"); } series = null; if (fields.Contains(ItemFields.SeriesStudio)) { series = series ?? season.Series; if (series != null) { dto.SeriesStudio = series.Studios.FirstOrDefault(); } } if (fields.Contains(ItemFields.SeriesPrimaryImage)) { series = series ?? season.Series; if (series != null) { dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary); } } } var game = item as Game; if (game != null) { SetGameProperties(dto, game); } var gameSystem = item as GameSystem; if (gameSystem != null) { SetGameSystemProperties(dto, gameSystem); } var musicVideo = item as MusicVideo; if (musicVideo != null) { SetMusicVideoProperties(dto, musicVideo); } var book = item as Book; if (book != null) { SetBookProperties(dto, book); } if (item.ProductionLocations.Count > 0 || item is Movie) { dto.ProductionLocations = item.ProductionLocations.ToArray(); } var photo = item as Photo; if (photo != null) { SetPhotoProperties(dto, photo); } dto.ChannelId = item.ChannelId; if (item.SourceType == SourceType.Channel && !string.IsNullOrWhiteSpace(item.ChannelId)) { var channel = _libraryManager.GetItemById(item.ChannelId); if (channel != null) { dto.ChannelName = channel.Name; } } }
private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem owner) { var logoLimit = options.GetImageLimit(ImageType.Logo); var artLimit = options.GetImageLimit(ImageType.Art); var thumbLimit = options.GetImageLimit(ImageType.Thumb); var backdropLimit = options.GetImageLimit(ImageType.Backdrop); if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0) { return; } BaseItem parent = null; var isFirst = true; while (((!dto.HasLogo && logoLimit > 0) || (!dto.HasArtImage && artLimit > 0) || (!dto.HasThumb && thumbLimit > 0) || parent is Series) && (parent = parent ?? (isFirst ? item.GetParent() ?? owner : parent)) != null) { if (parent == null) { break; } var allImages = parent.ImageInfos; if (logoLimit > 0 && !dto.HasLogo && dto.ParentLogoItemId == null) { var image = allImages.FirstOrDefault(i => i.Type == ImageType.Logo); if (image != null) { dto.ParentLogoItemId = GetDtoId(parent); dto.ParentLogoImageTag = GetImageCacheTag(parent, image); } } if (artLimit > 0 && !dto.HasArtImage && dto.ParentArtItemId == null) { var image = allImages.FirstOrDefault(i => i.Type == ImageType.Art); if (image != null) { dto.ParentArtItemId = GetDtoId(parent); dto.ParentArtImageTag = GetImageCacheTag(parent, image); } } if (thumbLimit > 0 && !dto.HasThumb && (dto.ParentThumbItemId == null || parent is Series)) { var image = allImages.FirstOrDefault(i => i.Type == ImageType.Thumb); if (image != null) { dto.ParentThumbItemId = GetDtoId(parent); dto.ParentThumbImageTag = GetImageCacheTag(parent, image); } } if (backdropLimit > 0 && !dto.HasBackdrop) { var images = allImages.Where(i => i.Type == ImageType.Backdrop).Take(backdropLimit).ToList(); if (images.Count > 0) { dto.ParentBackdropItemId = GetDtoId(parent); dto.ParentBackdropImageTags = GetImageTags(parent, images); } } isFirst = false; parent = parent.GetParent(); } }