jellyfin-server/MediaBrowser.Controller/Entities/TV/Episode.cs

337 lines
9.5 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2018-12-27 23:27:57 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2019-10-15 15:49:49 +00:00
using System.Text.Json.Serialization;
2020-05-13 02:10:35 +00:00
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2018-12-27 23:27:57 +00:00
using MediaBrowser.Model.IO;
2018-12-28 00:11:04 +00:00
using Microsoft.Extensions.Logging;
2018-12-27 23:27:57 +00:00
namespace MediaBrowser.Controller.Entities.TV
{
/// <summary>
/// Class Episode.
2018-12-27 23:27:57 +00:00
/// </summary>
public class Episode : Video, IHasTrailers, IHasLookupInfo<EpisodeInfo>, IHasSeries
{
public Episode()
{
RemoteTrailers = Array.Empty<MediaUrl>();
2018-12-27 21:43:48 +00:00
LocalTrailerIds = Array.Empty<Guid>();
RemoteTrailerIds = Array.Empty<Guid>();
2018-12-27 23:27:57 +00:00
}
/// <inheritdoc />
public IReadOnlyList<Guid> LocalTrailerIds { get; set; }
/// <inheritdoc />
public IReadOnlyList<Guid> RemoteTrailerIds { get; set; }
2018-12-27 23:27:57 +00:00
/// <summary>
/// Gets the season in which it aired.
/// </summary>
/// <value>The aired season.</value>
public int? AirsBeforeSeasonNumber { get; set; }
2020-06-15 21:43:52 +00:00
2018-12-27 23:27:57 +00:00
public int? AirsAfterSeasonNumber { get; set; }
2020-06-15 21:43:52 +00:00
2018-12-27 23:27:57 +00:00
public int? AirsBeforeEpisodeNumber { get; set; }
/// <summary>
/// This is the ending episode number for double episodes.
/// </summary>
/// <value>The index number.</value>
public int? IndexNumberEnd { get; set; }
public string FindSeriesSortName()
{
var series = Series;
return series == null ? SeriesName : series.SortName;
}
2019-10-15 15:49:49 +00:00
[JsonIgnore]
protected override bool SupportsOwnedItems => IsStacked || MediaSourceCount > 1;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
public override bool SupportsInheritedParentImages => true;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
public override bool SupportsPeople => true;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
public int? AiredSeasonNumber => AirsAfterSeasonNumber ?? AirsBeforeSeasonNumber ?? ParentIndexNumber;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
public override Folder LatestItemsIndexContainer => Series;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
public override Guid DisplayParentId => SeasonId;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
protected override bool EnableDefaultVideoUserDataKeys => false;
2018-12-27 23:27:57 +00:00
public override double GetDefaultPrimaryImageAspectRatio()
{
// hack for tv plugins
if (SourceType == SourceType.Channel)
{
return 0;
}
return 16.0 / 9;
2018-12-27 23:27:57 +00:00
}
public override List<string> GetUserDataKeys()
{
var list = base.GetUserDataKeys();
var series = Series;
if (series != null && ParentIndexNumber.HasValue && IndexNumber.HasValue)
{
var seriesUserDataKeys = series.GetUserDataKeys();
var take = seriesUserDataKeys.Count;
if (seriesUserDataKeys.Count > 1)
{
take--;
}
2020-06-15 21:43:52 +00:00
2018-12-27 23:27:57 +00:00
list.InsertRange(0, seriesUserDataKeys.Take(take).Select(i => i + ParentIndexNumber.Value.ToString("000") + IndexNumber.Value.ToString("000")));
}
return list;
}
/// <summary>
/// This Episode's Series Instance.
2018-12-27 23:27:57 +00:00
/// </summary>
/// <value>The series.</value>
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public Series Series
{
get
{
var seriesId = SeriesId;
if (seriesId.Equals(Guid.Empty))
{
2018-12-27 23:27:57 +00:00
seriesId = FindSeriesId();
}
2020-06-15 21:43:52 +00:00
2018-12-27 23:27:57 +00:00
return !seriesId.Equals(Guid.Empty) ? (LibraryManager.GetItemById(seriesId) as Series) : null;
}
}
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public Season Season
{
get
{
var seasonId = SeasonId;
if (seasonId.Equals(Guid.Empty))
{
2018-12-27 23:27:57 +00:00
seasonId = FindSeasonId();
}
2020-06-15 21:43:52 +00:00
2018-12-27 23:27:57 +00:00
return !seasonId.Equals(Guid.Empty) ? (LibraryManager.GetItemById(seasonId) as Season) : null;
}
}
2019-10-15 15:49:49 +00:00
[JsonIgnore]
public bool IsInSeasonFolder => FindParent<Season>() != null;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public string SeriesPresentationUniqueKey { get; set; }
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public string SeriesName { get; set; }
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public string SeasonName { get; set; }
public string FindSeriesPresentationUniqueKey()
{
var series = Series;
return series == null ? null : series.PresentationUniqueKey;
}
public string FindSeasonName()
{
var season = Season;
if (season == null)
{
if (ParentIndexNumber.HasValue)
{
return "Season " + ParentIndexNumber.Value.ToString(CultureInfo.InvariantCulture);
}
2020-06-15 21:43:52 +00:00
2018-12-27 23:27:57 +00:00
return "Season Unknown";
}
return season.Name;
}
public string FindSeriesName()
{
var series = Series;
return series == null ? SeriesName : series.Name;
}
public Guid FindSeasonId()
{
var season = FindParent<Season>();
// Episodes directly in series folder
if (season == null)
{
var series = Series;
if (series != null && ParentIndexNumber.HasValue)
{
var findNumber = ParentIndexNumber.Value;
season = series.Children
.OfType<Season>()
.FirstOrDefault(i => i.IndexNumber.HasValue && i.IndexNumber.Value == findNumber);
}
}
return season == null ? Guid.Empty : season.Id;
}
/// <summary>
/// Creates the name of the sort.
/// </summary>
/// <returns>System.String.</returns>
protected override string CreateSortName()
{
return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("000 - ") : "")
+ (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
}
/// <summary>
/// Determines whether [contains episode number] [the specified number].
/// </summary>
/// <param name="number">The number.</param>
/// <returns><c>true</c> if [contains episode number] [the specified number]; otherwise, <c>false</c>.</returns>
public bool ContainsEpisodeNumber(int number)
{
if (IndexNumber.HasValue)
{
if (IndexNumberEnd.HasValue)
{
return number >= IndexNumber.Value && number <= IndexNumberEnd.Value;
}
return IndexNumber.Value == number;
}
return false;
}
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public override bool SupportsRemoteImageDownloading
{
get
{
if (IsMissingEpisode)
{
return false;
}
return true;
}
}
2019-10-15 15:49:49 +00:00
[JsonIgnore]
public bool IsMissingEpisode => LocationType == LocationType.Virtual;
2018-12-27 23:27:57 +00:00
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public Guid SeasonId { get; set; }
2019-10-15 15:49:49 +00:00
[JsonIgnore]
2018-12-27 23:27:57 +00:00
public Guid SeriesId { get; set; }
public Guid FindSeriesId()
{
var series = FindParent<Series>();
return series == null ? Guid.Empty : series.Id;
}
public override IEnumerable<Guid> GetAncestorIds()
{
var list = base.GetAncestorIds().ToList();
var seasonId = SeasonId;
if (!seasonId.Equals(Guid.Empty) && !list.Contains(seasonId))
{
list.Add(seasonId);
}
return list;
}
public override IEnumerable<FileSystemMetadata> GetDeletePaths()
{
return new[] {
new FileSystemMetadata
{
FullName = Path,
IsDirectory = IsFolder
}
}.Concat(GetLocalMetadataFilesToDelete());
}
public override UnratedItem GetBlockUnratedType()
{
return UnratedItem.Series;
}
public EpisodeInfo GetLookupInfo()
{
var id = GetItemLookupInfo<EpisodeInfo>();
var series = Series;
if (series != null)
{
id.SeriesProviderIds = series.ProviderIds;
id.SeriesDisplayOrder = series.DisplayOrder;
}
id.IsMissingEpisode = IsMissingEpisode;
id.IndexNumberEnd = IndexNumberEnd;
return id;
}
public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
{
var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
if (!IsLocked)
{
if (SourceType == SourceType.Library)
{
try
{
if (LibraryManager.FillMissingEpisodeNumbersFromPath(this, replaceAllMetdata))
{
hasChanges = true;
}
}
catch (Exception ex)
{
Logger.LogError(ex, "Error in FillMissingEpisodeNumbersFromPath. Episode: {Episode}", Path ?? Name ?? Id.ToString());
2018-12-27 23:27:57 +00:00
}
}
}
return hasChanges;
}
}
}