| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591, SA1401 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Collections.Immutable; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.IO; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Text; |
| | | 12 | | using System.Text.Json.Serialization; |
| | | 13 | | using System.Threading; |
| | | 14 | | using System.Threading.Tasks; |
| | | 15 | | using Jellyfin.Data; |
| | | 16 | | using Jellyfin.Data.Enums; |
| | | 17 | | using Jellyfin.Database.Implementations.Entities; |
| | | 18 | | using Jellyfin.Database.Implementations.Enums; |
| | | 19 | | using Jellyfin.Extensions; |
| | | 20 | | using MediaBrowser.Common.Extensions; |
| | | 21 | | using MediaBrowser.Controller.Channels; |
| | | 22 | | using MediaBrowser.Controller.Chapters; |
| | | 23 | | using MediaBrowser.Controller.Configuration; |
| | | 24 | | using MediaBrowser.Controller.Dto; |
| | | 25 | | using MediaBrowser.Controller.Entities.TV; |
| | | 26 | | using MediaBrowser.Controller.Library; |
| | | 27 | | using MediaBrowser.Controller.MediaSegments; |
| | | 28 | | using MediaBrowser.Controller.Persistence; |
| | | 29 | | using MediaBrowser.Controller.Providers; |
| | | 30 | | using MediaBrowser.Model.Configuration; |
| | | 31 | | using MediaBrowser.Model.Dto; |
| | | 32 | | using MediaBrowser.Model.Entities; |
| | | 33 | | using MediaBrowser.Model.Globalization; |
| | | 34 | | using MediaBrowser.Model.IO; |
| | | 35 | | using MediaBrowser.Model.Library; |
| | | 36 | | using MediaBrowser.Model.LiveTv; |
| | | 37 | | using MediaBrowser.Model.MediaInfo; |
| | | 38 | | using Microsoft.Extensions.Logging; |
| | | 39 | | |
| | | 40 | | namespace MediaBrowser.Controller.Entities |
| | | 41 | | { |
| | | 42 | | /// <summary> |
| | | 43 | | /// Class BaseItem. |
| | | 44 | | /// </summary> |
| | | 45 | | public abstract class BaseItem : IHasProviderIds, IHasLookupInfo<ItemLookupInfo>, IEquatable<BaseItem> |
| | | 46 | | { |
| | | 47 | | private BaseItemKind? _baseItemKind; |
| | | 48 | | |
| | | 49 | | public const string ThemeSongFileName = "theme"; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// The supported image extensions. |
| | | 53 | | /// </summary> |
| | 5 | 54 | | public static readonly string[] SupportedImageExtensions |
| | 5 | 55 | | = [".png", ".jpg", ".jpeg", ".webp", ".tbn", ".gif", ".svg"]; |
| | | 56 | | |
| | 5 | 57 | | private static readonly List<string> _supportedExtensions = new List<string>(SupportedImageExtensions) |
| | 5 | 58 | | { |
| | 5 | 59 | | ".nfo", |
| | 5 | 60 | | ".xml", |
| | 5 | 61 | | ".srt", |
| | 5 | 62 | | ".vtt", |
| | 5 | 63 | | ".sub", |
| | 5 | 64 | | ".sup", |
| | 5 | 65 | | ".idx", |
| | 5 | 66 | | ".txt", |
| | 5 | 67 | | ".edl", |
| | 5 | 68 | | ".bif", |
| | 5 | 69 | | ".smi", |
| | 5 | 70 | | ".ttml", |
| | 5 | 71 | | ".lrc", |
| | 5 | 72 | | ".elrc" |
| | 5 | 73 | | }; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Extra types that should be counted and displayed as "Special Features" in the UI. |
| | | 77 | | /// </summary> |
| | 5 | 78 | | public static readonly IReadOnlyCollection<ExtraType> DisplayExtraTypes = new HashSet<ExtraType> |
| | 5 | 79 | | { |
| | 5 | 80 | | Model.Entities.ExtraType.Unknown, |
| | 5 | 81 | | Model.Entities.ExtraType.BehindTheScenes, |
| | 5 | 82 | | Model.Entities.ExtraType.Clip, |
| | 5 | 83 | | Model.Entities.ExtraType.DeletedScene, |
| | 5 | 84 | | Model.Entities.ExtraType.Interview, |
| | 5 | 85 | | Model.Entities.ExtraType.Sample, |
| | 5 | 86 | | Model.Entities.ExtraType.Scene, |
| | 5 | 87 | | Model.Entities.ExtraType.Featurette, |
| | 5 | 88 | | Model.Entities.ExtraType.Short |
| | 5 | 89 | | }; |
| | | 90 | | |
| | 5 | 91 | | private static readonly char[] VersionDelimiters = ['-', '_', '.']; |
| | | 92 | | |
| | | 93 | | private string _sortName; |
| | | 94 | | |
| | | 95 | | private string _forcedSortName; |
| | | 96 | | |
| | | 97 | | private string _name; |
| | | 98 | | |
| | | 99 | | private string _originalLanguage; |
| | | 100 | | |
| | | 101 | | public const char SlugChar = '-'; |
| | | 102 | | |
| | | 103 | | protected BaseItem() |
| | | 104 | | { |
| | 1243 | 105 | | Tags = Array.Empty<string>(); |
| | 1243 | 106 | | Genres = Array.Empty<string>(); |
| | 1243 | 107 | | Studios = Array.Empty<string>(); |
| | 1243 | 108 | | ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 1243 | 109 | | LockedFields = Array.Empty<MetadataField>(); |
| | 1243 | 110 | | ImageInfos = Array.Empty<ItemImageInfo>(); |
| | 1243 | 111 | | ProductionLocations = Array.Empty<string>(); |
| | 1243 | 112 | | RemoteTrailers = Array.Empty<MediaUrl>(); |
| | 1243 | 113 | | UserData = []; |
| | 1243 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Gets or Sets the user data collection as cached from the last Db query. |
| | | 118 | | /// </summary> |
| | | 119 | | [JsonIgnore] |
| | | 120 | | public ICollection<UserData> UserData { get; set; } |
| | | 121 | | |
| | | 122 | | [JsonIgnore] |
| | | 123 | | public string PreferredMetadataCountryCode { get; set; } |
| | | 124 | | |
| | | 125 | | [JsonIgnore] |
| | | 126 | | public string PreferredMetadataLanguage { get; set; } |
| | | 127 | | |
| | | 128 | | public long? Size { get; set; } |
| | | 129 | | |
| | | 130 | | public string Container { get; set; } |
| | | 131 | | |
| | | 132 | | [JsonIgnore] |
| | | 133 | | public string Tagline { get; set; } |
| | | 134 | | |
| | | 135 | | [JsonIgnore] |
| | | 136 | | public virtual ItemImageInfo[] ImageInfos { get; set; } |
| | | 137 | | |
| | | 138 | | [JsonIgnore] |
| | | 139 | | public bool IsVirtualItem { get; set; } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Gets or sets the album. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <value>The album.</value> |
| | | 145 | | [JsonIgnore] |
| | | 146 | | public string Album { get; set; } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Gets or sets the LUFS value. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <value>The LUFS Value.</value> |
| | | 152 | | [JsonIgnore] |
| | | 153 | | public float? LUFS { get; set; } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets or sets the gain required for audio normalization. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <value>The gain required for audio normalization.</value> |
| | | 159 | | [JsonIgnore] |
| | | 160 | | public float? NormalizationGain { get; set; } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets or sets the channel identifier. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <value>The channel identifier.</value> |
| | | 166 | | [JsonIgnore] |
| | | 167 | | public Guid ChannelId { get; set; } |
| | | 168 | | |
| | | 169 | | [JsonIgnore] |
| | 0 | 170 | | public virtual bool SupportsAddingToPlaylist => false; |
| | | 171 | | |
| | | 172 | | [JsonIgnore] |
| | 17 | 173 | | public virtual bool AlwaysScanInternalMetadataPath => false; |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Gets or sets a value indicating whether this instance is in mixed folder. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <value><c>true</c> if this instance is in mixed folder; otherwise, <c>false</c>.</value> |
| | | 179 | | [JsonIgnore] |
| | | 180 | | public bool IsInMixedFolder { get; set; } |
| | | 181 | | |
| | | 182 | | [JsonIgnore] |
| | 0 | 183 | | public virtual bool SupportsPlayedStatus => false; |
| | | 184 | | |
| | | 185 | | [JsonIgnore] |
| | 0 | 186 | | public virtual bool SupportsPositionTicksResume => false; |
| | | 187 | | |
| | | 188 | | [JsonIgnore] |
| | 17 | 189 | | public virtual bool SupportsRemoteImageDownloading => true; |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Gets or sets the name. |
| | | 193 | | /// </summary> |
| | | 194 | | /// <value>The name.</value> |
| | | 195 | | [JsonIgnore] |
| | | 196 | | public virtual string Name |
| | | 197 | | { |
| | 956 | 198 | | get => _name; |
| | | 199 | | set |
| | | 200 | | { |
| | 441 | 201 | | _name = value; |
| | | 202 | | |
| | | 203 | | // lazy load this again |
| | 441 | 204 | | _sortName = null; |
| | 441 | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | [JsonIgnore] |
| | 0 | 209 | | public bool IsUnaired => PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date; |
| | | 210 | | |
| | | 211 | | [JsonIgnore] |
| | | 212 | | public int? TotalBitrate { get; set; } |
| | | 213 | | |
| | | 214 | | [JsonIgnore] |
| | | 215 | | public ExtraType? ExtraType { get; set; } |
| | | 216 | | |
| | | 217 | | [JsonIgnore] |
| | 0 | 218 | | public bool IsThemeMedia => ExtraType.HasValue && (ExtraType.Value == Model.Entities.ExtraType.ThemeSong || Extr |
| | | 219 | | |
| | | 220 | | [JsonIgnore] |
| | | 221 | | public string OriginalTitle { get; set; } |
| | | 222 | | |
| | | 223 | | [JsonIgnore] |
| | | 224 | | public string OriginalLanguage |
| | | 225 | | { |
| | 423 | 226 | | get => _originalLanguage; |
| | 234 | 227 | | set => _originalLanguage = LocalizationManager?.FindLanguageInfo(value)?.TwoLetterISOLanguageName ?? value; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Gets or sets the id. |
| | | 232 | | /// </summary> |
| | | 233 | | /// <value>The id.</value> |
| | | 234 | | [JsonIgnore] |
| | | 235 | | public Guid Id { get; set; } |
| | | 236 | | |
| | | 237 | | [JsonIgnore] |
| | | 238 | | public Guid OwnerId { get; set; } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Gets or sets the audio. |
| | | 242 | | /// </summary> |
| | | 243 | | /// <value>The audio.</value> |
| | | 244 | | [JsonIgnore] |
| | | 245 | | public ProgramAudio? Audio { get; set; } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Gets the id that should be used to key display prefs for this item. |
| | | 249 | | /// Default is based on the type for everything except actual generic folders. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <value>The display prefs id.</value> |
| | | 252 | | [JsonIgnore] |
| | | 253 | | public virtual Guid DisplayPreferencesId |
| | | 254 | | { |
| | | 255 | | get |
| | | 256 | | { |
| | 6 | 257 | | var thisType = GetType(); |
| | 6 | 258 | | return thisType == typeof(Folder) ? Id : thisType.FullName.GetMD5(); |
| | | 259 | | } |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Gets or sets the path. |
| | | 264 | | /// </summary> |
| | | 265 | | /// <value>The path.</value> |
| | | 266 | | [JsonIgnore] |
| | | 267 | | public virtual string Path { get; set; } |
| | | 268 | | |
| | | 269 | | [JsonIgnore] |
| | | 270 | | public virtual SourceType SourceType |
| | | 271 | | { |
| | | 272 | | get |
| | | 273 | | { |
| | 2394 | 274 | | if (!ChannelId.IsEmpty()) |
| | | 275 | | { |
| | 0 | 276 | | return SourceType.Channel; |
| | | 277 | | } |
| | | 278 | | |
| | 2394 | 279 | | return SourceType.Library; |
| | | 280 | | } |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <summary> |
| | | 284 | | /// Gets the folder containing the item. |
| | | 285 | | /// If the item is a folder, it returns the folder itself. |
| | | 286 | | /// </summary> |
| | | 287 | | [JsonIgnore] |
| | | 288 | | public virtual string ContainingFolderPath |
| | | 289 | | { |
| | | 290 | | get |
| | | 291 | | { |
| | 482 | 292 | | if (IsFolder) |
| | | 293 | | { |
| | 417 | 294 | | return Path; |
| | | 295 | | } |
| | | 296 | | |
| | 65 | 297 | | return System.IO.Path.GetDirectoryName(Path); |
| | | 298 | | } |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Gets or sets the name of the service. |
| | | 303 | | /// </summary> |
| | | 304 | | /// <value>The name of the service.</value> |
| | | 305 | | [JsonIgnore] |
| | | 306 | | public string ServiceName { get; set; } |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Gets or sets the external id. |
| | | 310 | | /// </summary> |
| | | 311 | | /// <remarks> |
| | | 312 | | /// If this content came from an external service, the id of the content on that service. |
| | | 313 | | /// </remarks> |
| | | 314 | | [JsonIgnore] |
| | | 315 | | public string ExternalId { get; set; } |
| | | 316 | | |
| | | 317 | | [JsonIgnore] |
| | | 318 | | public string ExternalSeriesId { get; set; } |
| | | 319 | | |
| | | 320 | | [JsonIgnore] |
| | 0 | 321 | | public virtual bool IsHidden => false; |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// Gets the type of the location. |
| | | 325 | | /// </summary> |
| | | 326 | | /// <value>The type of the location.</value> |
| | | 327 | | [JsonIgnore] |
| | | 328 | | public virtual LocationType LocationType |
| | | 329 | | { |
| | | 330 | | get |
| | | 331 | | { |
| | 16 | 332 | | var path = Path; |
| | 16 | 333 | | if (string.IsNullOrEmpty(path)) |
| | | 334 | | { |
| | 7 | 335 | | if (SourceType == SourceType.Channel) |
| | | 336 | | { |
| | 0 | 337 | | return LocationType.Remote; |
| | | 338 | | } |
| | | 339 | | |
| | 7 | 340 | | return LocationType.Virtual; |
| | | 341 | | } |
| | | 342 | | |
| | 9 | 343 | | return FileSystem.IsPathFile(path) ? LocationType.FileSystem : LocationType.Remote; |
| | | 344 | | } |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | [JsonIgnore] |
| | | 348 | | public MediaProtocol? PathProtocol |
| | | 349 | | { |
| | | 350 | | get |
| | | 351 | | { |
| | 2658 | 352 | | var path = Path; |
| | | 353 | | |
| | 2658 | 354 | | if (string.IsNullOrEmpty(path)) |
| | | 355 | | { |
| | 116 | 356 | | return null; |
| | | 357 | | } |
| | | 358 | | |
| | 2542 | 359 | | return MediaSourceManager.GetPathProtocol(path); |
| | | 360 | | } |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | [JsonIgnore] |
| | 2619 | 364 | | public bool IsFileProtocol => PathProtocol == MediaProtocol.File; |
| | | 365 | | |
| | | 366 | | [JsonIgnore] |
| | 0 | 367 | | public bool HasPathProtocol => PathProtocol.HasValue; |
| | | 368 | | |
| | | 369 | | [JsonIgnore] |
| | | 370 | | public virtual bool SupportsLocalMetadata |
| | | 371 | | { |
| | | 372 | | get |
| | | 373 | | { |
| | 1364 | 374 | | if (SourceType == SourceType.Channel) |
| | | 375 | | { |
| | 0 | 376 | | return false; |
| | | 377 | | } |
| | | 378 | | |
| | 1364 | 379 | | return IsFileProtocol; |
| | | 380 | | } |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | [JsonIgnore] |
| | | 384 | | public virtual string FileNameWithoutExtension |
| | | 385 | | { |
| | | 386 | | get |
| | | 387 | | { |
| | 0 | 388 | | if (IsFileProtocol) |
| | | 389 | | { |
| | 0 | 390 | | return System.IO.Path.GetFileNameWithoutExtension(Path); |
| | | 391 | | } |
| | | 392 | | |
| | 0 | 393 | | return null; |
| | | 394 | | } |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | [JsonIgnore] |
| | 107 | 398 | | public virtual bool EnableAlphaNumericSorting => true; |
| | | 399 | | |
| | 146 | 400 | | public virtual bool IsHD => Height >= 720; |
| | | 401 | | |
| | | 402 | | public bool IsShortcut { get; set; } |
| | | 403 | | |
| | | 404 | | public string ShortcutPath { get; set; } |
| | | 405 | | |
| | | 406 | | public int Width { get; set; } |
| | | 407 | | |
| | | 408 | | public int Height { get; set; } |
| | | 409 | | |
| | | 410 | | /// <summary> |
| | | 411 | | /// Gets the primary image path. |
| | | 412 | | /// </summary> |
| | | 413 | | /// <remarks> |
| | | 414 | | /// This is just a helper for convenience. |
| | | 415 | | /// </remarks> |
| | | 416 | | /// <value>The primary image path.</value> |
| | | 417 | | [JsonIgnore] |
| | 0 | 418 | | public string PrimaryImagePath => this.GetImagePath(ImageType.Primary); |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Gets or sets the date created. |
| | | 422 | | /// </summary> |
| | | 423 | | /// <value>The date created.</value> |
| | | 424 | | [JsonIgnore] |
| | | 425 | | public DateTime DateCreated { get; set; } |
| | | 426 | | |
| | | 427 | | /// <summary> |
| | | 428 | | /// Gets or sets the date modified. |
| | | 429 | | /// </summary> |
| | | 430 | | /// <value>The date modified.</value> |
| | | 431 | | [JsonIgnore] |
| | | 432 | | public DateTime DateModified { get; set; } |
| | | 433 | | |
| | | 434 | | public DateTime DateLastSaved { get; set; } |
| | | 435 | | |
| | | 436 | | [JsonIgnore] |
| | | 437 | | public DateTime DateLastRefreshed { get; set; } |
| | | 438 | | |
| | | 439 | | [JsonIgnore] |
| | | 440 | | public bool IsLocked { get; set; } |
| | | 441 | | |
| | | 442 | | /// <summary> |
| | | 443 | | /// Gets or sets the locked fields. |
| | | 444 | | /// </summary> |
| | | 445 | | /// <value>The locked fields.</value> |
| | | 446 | | [JsonIgnore] |
| | | 447 | | public MetadataField[] LockedFields { get; set; } |
| | | 448 | | |
| | | 449 | | /// <summary> |
| | | 450 | | /// Gets the type of the media. |
| | | 451 | | /// </summary> |
| | | 452 | | /// <value>The type of the media.</value> |
| | | 453 | | [JsonIgnore] |
| | 140 | 454 | | public virtual MediaType MediaType => MediaType.Unknown; |
| | | 455 | | |
| | | 456 | | [JsonIgnore] |
| | | 457 | | public virtual string[] PhysicalLocations |
| | | 458 | | { |
| | | 459 | | get |
| | | 460 | | { |
| | 0 | 461 | | if (!IsFileProtocol) |
| | | 462 | | { |
| | 0 | 463 | | return Array.Empty<string>(); |
| | | 464 | | } |
| | | 465 | | |
| | 0 | 466 | | return [Path]; |
| | | 467 | | } |
| | | 468 | | } |
| | | 469 | | |
| | | 470 | | [JsonIgnore] |
| | | 471 | | public bool EnableMediaSourceDisplay |
| | | 472 | | { |
| | | 473 | | get |
| | | 474 | | { |
| | 6 | 475 | | if (SourceType == SourceType.Channel) |
| | | 476 | | { |
| | 0 | 477 | | return ChannelManager.EnableMediaSourceDisplay(this); |
| | | 478 | | } |
| | | 479 | | |
| | 6 | 480 | | return true; |
| | | 481 | | } |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | [JsonIgnore] |
| | | 485 | | public Guid ParentId { get; set; } |
| | | 486 | | |
| | | 487 | | /// <summary> |
| | | 488 | | /// Gets or sets the logger. |
| | | 489 | | /// </summary> |
| | | 490 | | public static ILogger<BaseItem> Logger { get; set; } |
| | | 491 | | |
| | | 492 | | public static ILibraryManager LibraryManager { get; set; } |
| | | 493 | | |
| | | 494 | | public static IServerConfigurationManager ConfigurationManager { get; set; } |
| | | 495 | | |
| | | 496 | | public static IProviderManager ProviderManager { get; set; } |
| | | 497 | | |
| | | 498 | | public static ILocalizationManager LocalizationManager { get; set; } |
| | | 499 | | |
| | | 500 | | public static IItemRepository ItemRepository { get; set; } |
| | | 501 | | |
| | | 502 | | public static IItemCountService ItemCountService { get; set; } |
| | | 503 | | |
| | | 504 | | public static IChapterManager ChapterManager { get; set; } |
| | | 505 | | |
| | | 506 | | public static IFileSystem FileSystem { get; set; } |
| | | 507 | | |
| | | 508 | | public static IUserDataManager UserDataManager { get; set; } |
| | | 509 | | |
| | | 510 | | public static IChannelManager ChannelManager { get; set; } |
| | | 511 | | |
| | | 512 | | public static IMediaSourceManager MediaSourceManager { get; set; } |
| | | 513 | | |
| | | 514 | | public static IMediaSegmentManager MediaSegmentManager { get; set; } |
| | | 515 | | |
| | | 516 | | /// <summary> |
| | | 517 | | /// Gets or sets the name of the forced sort. |
| | | 518 | | /// </summary> |
| | | 519 | | /// <value>The name of the forced sort.</value> |
| | | 520 | | [JsonIgnore] |
| | | 521 | | public string ForcedSortName |
| | | 522 | | { |
| | 494 | 523 | | get => _forcedSortName; |
| | | 524 | | set |
| | | 525 | | { |
| | 95 | 526 | | _forcedSortName = value; |
| | 95 | 527 | | _sortName = null; |
| | 95 | 528 | | } |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <summary> |
| | | 532 | | /// Gets or sets the name of the sort. |
| | | 533 | | /// </summary> |
| | | 534 | | /// <value>The name of the sort.</value> |
| | | 535 | | [JsonIgnore] |
| | | 536 | | public string SortName |
| | | 537 | | { |
| | | 538 | | get |
| | | 539 | | { |
| | 181 | 540 | | if (_sortName is null) |
| | | 541 | | { |
| | 107 | 542 | | if (!string.IsNullOrEmpty(ForcedSortName)) |
| | | 543 | | { |
| | | 544 | | // Run the forced sort name through the same cleaning as auto-generated sort names. |
| | 1 | 545 | | _sortName = GetSortName(ForcedSortName, EnableAlphaNumericSorting, ConfigurationManager.Configur |
| | | 546 | | } |
| | | 547 | | else |
| | | 548 | | { |
| | 106 | 549 | | _sortName = CreateSortName(); |
| | | 550 | | } |
| | | 551 | | } |
| | | 552 | | |
| | 181 | 553 | | return _sortName; |
| | | 554 | | } |
| | | 555 | | |
| | 135 | 556 | | set => _sortName = value; |
| | | 557 | | } |
| | | 558 | | |
| | | 559 | | [JsonIgnore] |
| | 358 | 560 | | public virtual Guid DisplayParentId => ParentId; |
| | | 561 | | |
| | | 562 | | [JsonIgnore] |
| | | 563 | | public BaseItem DisplayParent |
| | | 564 | | { |
| | | 565 | | get |
| | | 566 | | { |
| | 358 | 567 | | var id = DisplayParentId; |
| | 358 | 568 | | if (id.IsEmpty()) |
| | | 569 | | { |
| | 264 | 570 | | return null; |
| | | 571 | | } |
| | | 572 | | |
| | 94 | 573 | | return LibraryManager.GetItemById(id); |
| | | 574 | | } |
| | | 575 | | } |
| | | 576 | | |
| | | 577 | | /// <summary> |
| | | 578 | | /// Gets or sets the date that the item first debuted. For movies this could be premiere date, episodes would be |
| | | 579 | | /// </summary> |
| | | 580 | | /// <value>The premiere date.</value> |
| | | 581 | | [JsonIgnore] |
| | | 582 | | public DateTime? PremiereDate { get; set; } |
| | | 583 | | |
| | | 584 | | /// <summary> |
| | | 585 | | /// Gets or sets the end date. |
| | | 586 | | /// </summary> |
| | | 587 | | /// <value>The end date.</value> |
| | | 588 | | [JsonIgnore] |
| | | 589 | | public DateTime? EndDate { get; set; } |
| | | 590 | | |
| | | 591 | | /// <summary> |
| | | 592 | | /// Gets or sets the official rating. |
| | | 593 | | /// </summary> |
| | | 594 | | /// <value>The official rating.</value> |
| | | 595 | | [JsonIgnore] |
| | | 596 | | public string OfficialRating { get; set; } |
| | | 597 | | |
| | | 598 | | [JsonIgnore] |
| | | 599 | | public int? InheritedParentalRatingValue { get; set; } |
| | | 600 | | |
| | | 601 | | [JsonIgnore] |
| | | 602 | | public int? InheritedParentalRatingSubValue { get; set; } |
| | | 603 | | |
| | | 604 | | /// <summary> |
| | | 605 | | /// Gets or sets the critic rating. |
| | | 606 | | /// </summary> |
| | | 607 | | /// <value>The critic rating.</value> |
| | | 608 | | [JsonIgnore] |
| | | 609 | | public float? CriticRating { get; set; } |
| | | 610 | | |
| | | 611 | | /// <summary> |
| | | 612 | | /// Gets or sets the custom rating. |
| | | 613 | | /// </summary> |
| | | 614 | | /// <value>The custom rating.</value> |
| | | 615 | | [JsonIgnore] |
| | | 616 | | public string CustomRating { get; set; } |
| | | 617 | | |
| | | 618 | | /// <summary> |
| | | 619 | | /// Gets or sets the overview. |
| | | 620 | | /// </summary> |
| | | 621 | | /// <value>The overview.</value> |
| | | 622 | | [JsonIgnore] |
| | | 623 | | public string Overview { get; set; } |
| | | 624 | | |
| | | 625 | | /// <summary> |
| | | 626 | | /// Gets or sets the studios. |
| | | 627 | | /// </summary> |
| | | 628 | | /// <value>The studios.</value> |
| | | 629 | | [JsonIgnore] |
| | | 630 | | public string[] Studios { get; set; } |
| | | 631 | | |
| | | 632 | | /// <summary> |
| | | 633 | | /// Gets or sets the genres. |
| | | 634 | | /// </summary> |
| | | 635 | | /// <value>The genres.</value> |
| | | 636 | | [JsonIgnore] |
| | | 637 | | public string[] Genres { get; set; } |
| | | 638 | | |
| | | 639 | | /// <summary> |
| | | 640 | | /// Gets or sets the tags. |
| | | 641 | | /// </summary> |
| | | 642 | | /// <value>The tags.</value> |
| | | 643 | | [JsonIgnore] |
| | | 644 | | public string[] Tags { get; set; } |
| | | 645 | | |
| | | 646 | | [JsonIgnore] |
| | | 647 | | public string[] ProductionLocations { get; set; } |
| | | 648 | | |
| | | 649 | | /// <summary> |
| | | 650 | | /// Gets or sets the home page URL. |
| | | 651 | | /// </summary> |
| | | 652 | | /// <value>The home page URL.</value> |
| | | 653 | | [JsonIgnore] |
| | | 654 | | public string HomePageUrl { get; set; } |
| | | 655 | | |
| | | 656 | | /// <summary> |
| | | 657 | | /// Gets or sets the community rating. |
| | | 658 | | /// </summary> |
| | | 659 | | /// <value>The community rating.</value> |
| | | 660 | | [JsonIgnore] |
| | | 661 | | public float? CommunityRating { get; set; } |
| | | 662 | | |
| | | 663 | | /// <summary> |
| | | 664 | | /// Gets or sets the run time ticks. |
| | | 665 | | /// </summary> |
| | | 666 | | /// <value>The run time ticks.</value> |
| | | 667 | | [JsonIgnore] |
| | | 668 | | public long? RunTimeTicks { get; set; } |
| | | 669 | | |
| | | 670 | | /// <summary> |
| | | 671 | | /// Gets or sets the production year. |
| | | 672 | | /// </summary> |
| | | 673 | | /// <value>The production year.</value> |
| | | 674 | | [JsonIgnore] |
| | | 675 | | public int? ProductionYear { get; set; } |
| | | 676 | | |
| | | 677 | | /// <summary> |
| | | 678 | | /// Gets or sets the index number. If the item is part of a series, this is it's number in the series. |
| | | 679 | | /// This could be episode number, album track number, etc. |
| | | 680 | | /// </summary> |
| | | 681 | | /// <value>The index number.</value> |
| | | 682 | | [JsonIgnore] |
| | | 683 | | public int? IndexNumber { get; set; } |
| | | 684 | | |
| | | 685 | | /// <summary> |
| | | 686 | | /// Gets or sets the parent index number. For an episode this could be the season number, or for a song this cou |
| | | 687 | | /// </summary> |
| | | 688 | | /// <value>The parent index number.</value> |
| | | 689 | | [JsonIgnore] |
| | | 690 | | public int? ParentIndexNumber { get; set; } |
| | | 691 | | |
| | | 692 | | [JsonIgnore] |
| | 0 | 693 | | public virtual bool HasLocalAlternateVersions => false; |
| | | 694 | | |
| | | 695 | | [JsonIgnore] |
| | | 696 | | public string OfficialRatingForComparison |
| | | 697 | | { |
| | | 698 | | get |
| | | 699 | | { |
| | 176 | 700 | | var officialRating = OfficialRating; |
| | 176 | 701 | | if (!string.IsNullOrEmpty(officialRating)) |
| | | 702 | | { |
| | 0 | 703 | | return officialRating; |
| | | 704 | | } |
| | | 705 | | |
| | 176 | 706 | | var parent = DisplayParent; |
| | 176 | 707 | | if (parent is not null) |
| | | 708 | | { |
| | 44 | 709 | | return parent.OfficialRatingForComparison; |
| | | 710 | | } |
| | | 711 | | |
| | 132 | 712 | | return null; |
| | | 713 | | } |
| | | 714 | | } |
| | | 715 | | |
| | | 716 | | [JsonIgnore] |
| | | 717 | | public string CustomRatingForComparison |
| | | 718 | | { |
| | | 719 | | get |
| | | 720 | | { |
| | 132 | 721 | | return GetCustomRatingForComparision(); |
| | | 722 | | } |
| | | 723 | | } |
| | | 724 | | |
| | | 725 | | /// <summary> |
| | | 726 | | /// Gets or sets the provider ids. |
| | | 727 | | /// </summary> |
| | | 728 | | /// <value>The provider ids.</value> |
| | | 729 | | [JsonIgnore] |
| | | 730 | | public Dictionary<string, string> ProviderIds { get; set; } |
| | | 731 | | |
| | | 732 | | [JsonIgnore] |
| | 0 | 733 | | public virtual Folder LatestItemsIndexContainer => null; |
| | | 734 | | |
| | | 735 | | [JsonIgnore] |
| | | 736 | | public string PresentationUniqueKey { get; set; } |
| | | 737 | | |
| | | 738 | | [JsonIgnore] |
| | 31 | 739 | | public virtual bool EnableRememberingTrackSelections => true; |
| | | 740 | | |
| | | 741 | | [JsonIgnore] |
| | | 742 | | public virtual bool IsTopParent |
| | | 743 | | { |
| | | 744 | | get |
| | | 745 | | { |
| | 359 | 746 | | if (this is BasePluginFolder || this is Channel) |
| | | 747 | | { |
| | 61 | 748 | | return true; |
| | | 749 | | } |
| | | 750 | | |
| | 298 | 751 | | if (this is IHasCollectionType view) |
| | | 752 | | { |
| | 13 | 753 | | if (view.CollectionType == CollectionType.livetv) |
| | | 754 | | { |
| | 0 | 755 | | return true; |
| | | 756 | | } |
| | | 757 | | } |
| | | 758 | | |
| | 298 | 759 | | if (GetParent() is AggregateFolder) |
| | | 760 | | { |
| | 0 | 761 | | return true; |
| | | 762 | | } |
| | | 763 | | |
| | 298 | 764 | | return false; |
| | | 765 | | } |
| | | 766 | | } |
| | | 767 | | |
| | | 768 | | [JsonIgnore] |
| | 468 | 769 | | public virtual bool SupportsAncestors => true; |
| | | 770 | | |
| | | 771 | | [JsonIgnore] |
| | 99 | 772 | | protected virtual bool SupportsOwnedItems => !ParentId.IsEmpty() && IsFileProtocol; |
| | | 773 | | |
| | | 774 | | [JsonIgnore] |
| | 72 | 775 | | public virtual bool SupportsPeople => false; |
| | | 776 | | |
| | | 777 | | [JsonIgnore] |
| | 0 | 778 | | public virtual bool SupportsThemeMedia => false; |
| | | 779 | | |
| | | 780 | | [JsonIgnore] |
| | 0 | 781 | | public virtual bool SupportsInheritedParentImages => false; |
| | | 782 | | |
| | | 783 | | /// <summary> |
| | | 784 | | /// Gets a value indicating whether this instance is folder. |
| | | 785 | | /// </summary> |
| | | 786 | | /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value> |
| | | 787 | | [JsonIgnore] |
| | 92 | 788 | | public virtual bool IsFolder => false; |
| | | 789 | | |
| | | 790 | | [JsonIgnore] |
| | 0 | 791 | | public virtual bool IsDisplayedAsFolder => false; |
| | | 792 | | |
| | | 793 | | /// <summary> |
| | | 794 | | /// Gets or sets the remote trailers. |
| | | 795 | | /// </summary> |
| | | 796 | | /// <value>The remote trailers.</value> |
| | | 797 | | public IReadOnlyList<MediaUrl> RemoteTrailers { get; set; } |
| | | 798 | | |
| | | 799 | | private string GetCustomRatingForComparision(HashSet<Guid> callstack = null) |
| | | 800 | | { |
| | 176 | 801 | | callstack ??= new(); |
| | 176 | 802 | | var customRating = CustomRating; |
| | 176 | 803 | | if (!string.IsNullOrEmpty(customRating)) |
| | | 804 | | { |
| | 0 | 805 | | return customRating; |
| | | 806 | | } |
| | | 807 | | |
| | 176 | 808 | | callstack.Add(Id); |
| | | 809 | | |
| | 176 | 810 | | var parent = DisplayParent; |
| | 176 | 811 | | if (parent is not null && !callstack.Contains(parent.Id)) |
| | | 812 | | { |
| | 44 | 813 | | return parent.GetCustomRatingForComparision(callstack); |
| | | 814 | | } |
| | | 815 | | |
| | 132 | 816 | | return null; |
| | | 817 | | } |
| | | 818 | | |
| | | 819 | | public virtual double GetDefaultPrimaryImageAspectRatio() |
| | | 820 | | { |
| | 0 | 821 | | return 0; |
| | | 822 | | } |
| | | 823 | | |
| | | 824 | | public virtual string CreatePresentationUniqueKey() |
| | | 825 | | { |
| | 61 | 826 | | return Id.ToString("N", CultureInfo.InvariantCulture); |
| | | 827 | | } |
| | | 828 | | |
| | | 829 | | public virtual bool CanDelete() |
| | | 830 | | { |
| | 0 | 831 | | if (SourceType == SourceType.Channel) |
| | | 832 | | { |
| | 0 | 833 | | return ChannelManager.CanDelete(this); |
| | | 834 | | } |
| | | 835 | | |
| | 0 | 836 | | return IsFileProtocol; |
| | | 837 | | } |
| | | 838 | | |
| | | 839 | | public virtual bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders) |
| | | 840 | | { |
| | 0 | 841 | | if (user.HasPermission(PermissionKind.EnableContentDeletion)) |
| | | 842 | | { |
| | 0 | 843 | | return true; |
| | | 844 | | } |
| | | 845 | | |
| | 0 | 846 | | var allowed = user.GetPreferenceValues<Guid>(PreferenceKind.EnableContentDeletionFromFolders); |
| | | 847 | | |
| | 0 | 848 | | if (SourceType == SourceType.Channel) |
| | | 849 | | { |
| | 0 | 850 | | return allowed.Contains(ChannelId); |
| | | 851 | | } |
| | | 852 | | |
| | 0 | 853 | | var collectionFolders = LibraryManager.GetCollectionFolders(this, allCollectionFolders); |
| | | 854 | | |
| | 0 | 855 | | foreach (var folder in collectionFolders) |
| | | 856 | | { |
| | 0 | 857 | | if (allowed.Contains(folder.Id)) |
| | | 858 | | { |
| | 0 | 859 | | return true; |
| | | 860 | | } |
| | | 861 | | } |
| | | 862 | | |
| | 0 | 863 | | return false; |
| | 0 | 864 | | } |
| | | 865 | | |
| | | 866 | | public BaseItem GetOwner() |
| | | 867 | | { |
| | 667 | 868 | | var ownerId = OwnerId; |
| | 667 | 869 | | return ownerId.IsEmpty() ? null : LibraryManager.GetItemById(ownerId); |
| | | 870 | | } |
| | | 871 | | |
| | | 872 | | public bool CanDelete(User user, List<Folder> allCollectionFolders) |
| | | 873 | | { |
| | 6 | 874 | | return CanDelete() && IsAuthorizedToDelete(user, allCollectionFolders); |
| | | 875 | | } |
| | | 876 | | |
| | | 877 | | public virtual bool CanDelete(User user) |
| | | 878 | | { |
| | 6 | 879 | | var allCollectionFolders = LibraryManager.GetUserRootFolder().Children.OfType<Folder>().ToList(); |
| | | 880 | | |
| | 6 | 881 | | return CanDelete(user, allCollectionFolders); |
| | | 882 | | } |
| | | 883 | | |
| | | 884 | | public virtual bool CanDownload() |
| | | 885 | | { |
| | 6 | 886 | | return false; |
| | | 887 | | } |
| | | 888 | | |
| | | 889 | | public virtual bool IsAuthorizedToDownload(User user) |
| | | 890 | | { |
| | 0 | 891 | | return user.HasPermission(PermissionKind.EnableContentDownloading); |
| | | 892 | | } |
| | | 893 | | |
| | | 894 | | public bool CanDownload(User user) |
| | | 895 | | { |
| | 6 | 896 | | return CanDownload() && IsAuthorizedToDownload(user); |
| | | 897 | | } |
| | | 898 | | |
| | | 899 | | /// <inheritdoc /> |
| | | 900 | | public override string ToString() |
| | | 901 | | { |
| | 69 | 902 | | return Name; |
| | | 903 | | } |
| | | 904 | | |
| | | 905 | | public virtual string GetInternalMetadataPath() |
| | | 906 | | { |
| | 72 | 907 | | var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath; |
| | | 908 | | |
| | 72 | 909 | | return GetInternalMetadataPath(basePath); |
| | | 910 | | } |
| | | 911 | | |
| | | 912 | | protected virtual string GetInternalMetadataPath(string basePath) |
| | | 913 | | { |
| | 72 | 914 | | if (SourceType == SourceType.Channel) |
| | | 915 | | { |
| | 0 | 916 | | return System.IO.Path.Join(basePath, "channels", ChannelId.ToString("N", CultureInfo.InvariantCulture), |
| | | 917 | | } |
| | | 918 | | |
| | 72 | 919 | | ReadOnlySpan<char> idString = Id.ToString("N", CultureInfo.InvariantCulture); |
| | | 920 | | |
| | 72 | 921 | | return System.IO.Path.Join(basePath, "library", idString[..2], idString); |
| | | 922 | | } |
| | | 923 | | |
| | | 924 | | /// <summary> |
| | | 925 | | /// Creates the name of the sort. |
| | | 926 | | /// </summary> |
| | | 927 | | /// <returns>System.String.</returns> |
| | | 928 | | protected virtual string CreateSortName() |
| | | 929 | | { |
| | 106 | 930 | | return GetSortName(Name, EnableAlphaNumericSorting, ConfigurationManager.Configuration); |
| | | 931 | | } |
| | | 932 | | |
| | | 933 | | /// <summary> |
| | | 934 | | /// Cleans a raw name into its sortable form by applying the configured sort rules. |
| | | 935 | | /// </summary> |
| | | 936 | | /// <param name="name">The raw name to clean.</param> |
| | | 937 | | /// <param name="enableAlphaNumericSorting">Whether alphanumeric sorting rules should be applied.</param> |
| | | 938 | | /// <param name="configuration">The server configuration providing the sort rules.</param> |
| | | 939 | | /// <returns>The cleaned, sortable name, or <c>null</c> if <paramref name="name"/> is <c>null</c>.</returns> |
| | | 940 | | public static string GetSortName(string name, bool enableAlphaNumericSorting, ServerConfiguration configuration) |
| | | 941 | | { |
| | 111 | 942 | | if (name is null) |
| | | 943 | | { |
| | 0 | 944 | | return null; // some items may not have name filled in properly |
| | | 945 | | } |
| | | 946 | | |
| | 111 | 947 | | if (!enableAlphaNumericSorting) |
| | | 948 | | { |
| | 1 | 949 | | return name.TrimStart(); |
| | | 950 | | } |
| | | 951 | | |
| | 110 | 952 | | var sortable = name.Trim().ToLowerInvariant(); |
| | | 953 | | |
| | 880 | 954 | | foreach (var search in configuration.SortRemoveWords) |
| | | 955 | | { |
| | | 956 | | // Remove from beginning if a space follows |
| | 330 | 957 | | if (sortable.StartsWith(search + " ", StringComparison.Ordinal)) |
| | | 958 | | { |
| | 4 | 959 | | sortable = sortable.Remove(0, search.Length + 1); |
| | | 960 | | } |
| | | 961 | | |
| | | 962 | | // Remove from middle if surrounded by spaces |
| | 330 | 963 | | sortable = sortable.Replace(" " + search + " ", " ", StringComparison.Ordinal); |
| | | 964 | | |
| | | 965 | | // Remove from end if preceeded by a space |
| | 330 | 966 | | if (sortable.EndsWith(" " + search, StringComparison.Ordinal)) |
| | | 967 | | { |
| | 0 | 968 | | sortable = sortable.Remove(sortable.Length - (search.Length + 1)); |
| | | 969 | | } |
| | | 970 | | } |
| | | 971 | | |
| | 1540 | 972 | | foreach (var removeChar in configuration.SortRemoveCharacters) |
| | | 973 | | { |
| | 660 | 974 | | sortable = sortable.Replace(removeChar, string.Empty, StringComparison.Ordinal); |
| | | 975 | | } |
| | | 976 | | |
| | 880 | 977 | | foreach (var replaceChar in configuration.SortReplaceCharacters) |
| | | 978 | | { |
| | 330 | 979 | | sortable = sortable.Replace(replaceChar, " ", StringComparison.Ordinal); |
| | | 980 | | } |
| | | 981 | | |
| | 110 | 982 | | return ModifySortChunks(sortable); |
| | | 983 | | } |
| | | 984 | | |
| | | 985 | | internal static string ModifySortChunks(ReadOnlySpan<char> name) |
| | | 986 | | { |
| | | 987 | | static void AppendChunk(StringBuilder builder, bool isDigitChunk, ReadOnlySpan<char> chunk) |
| | | 988 | | { |
| | | 989 | | if (isDigitChunk && chunk.Length < 10) |
| | | 990 | | { |
| | | 991 | | builder.Append('0', 10 - chunk.Length); |
| | | 992 | | } |
| | | 993 | | |
| | | 994 | | builder.Append(chunk); |
| | | 995 | | } |
| | | 996 | | |
| | 116 | 997 | | if (name.IsEmpty) |
| | | 998 | | { |
| | 1 | 999 | | return string.Empty; |
| | | 1000 | | } |
| | | 1001 | | |
| | 115 | 1002 | | var builder = new StringBuilder(name.Length); |
| | | 1003 | | |
| | 115 | 1004 | | int chunkStart = 0; |
| | 115 | 1005 | | bool isDigitChunk = char.IsDigit(name[0]); |
| | 1908 | 1006 | | for (int i = 0; i < name.Length; i++) |
| | | 1007 | | { |
| | 839 | 1008 | | var isDigit = char.IsDigit(name[i]); |
| | 839 | 1009 | | if (isDigit != isDigitChunk) |
| | | 1010 | | { |
| | 6 | 1011 | | AppendChunk(builder, isDigitChunk, name.Slice(chunkStart, i - chunkStart)); |
| | 6 | 1012 | | chunkStart = i; |
| | 6 | 1013 | | isDigitChunk = isDigit; |
| | | 1014 | | } |
| | | 1015 | | } |
| | | 1016 | | |
| | 115 | 1017 | | AppendChunk(builder, isDigitChunk, name.Slice(chunkStart)); |
| | | 1018 | | |
| | | 1019 | | // logger.LogDebug("ModifySortChunks Start: {0} End: {1}", name, builder.ToString()); |
| | 115 | 1020 | | var result = builder.ToString().RemoveDiacritics(); |
| | 115 | 1021 | | if (!result.All(char.IsAscii)) |
| | | 1022 | | { |
| | 0 | 1023 | | result = result.Transliterated(); |
| | | 1024 | | } |
| | | 1025 | | |
| | 115 | 1026 | | return result; |
| | | 1027 | | } |
| | | 1028 | | |
| | | 1029 | | public BaseItem GetParent() |
| | | 1030 | | { |
| | 2143 | 1031 | | var parentId = ParentId; |
| | 2143 | 1032 | | if (parentId.IsEmpty()) |
| | | 1033 | | { |
| | 1849 | 1034 | | return null; |
| | | 1035 | | } |
| | | 1036 | | |
| | 294 | 1037 | | return LibraryManager.GetItemById(parentId); |
| | | 1038 | | } |
| | | 1039 | | |
| | | 1040 | | public IEnumerable<BaseItem> GetParents() |
| | | 1041 | | { |
| | 709 | 1042 | | var parent = GetParent(); |
| | | 1043 | | |
| | 782 | 1044 | | while (parent is not null) |
| | | 1045 | | { |
| | 73 | 1046 | | yield return parent; |
| | | 1047 | | |
| | 73 | 1048 | | parent = parent.GetParent(); |
| | | 1049 | | } |
| | 709 | 1050 | | } |
| | | 1051 | | |
| | | 1052 | | /// <summary> |
| | | 1053 | | /// Finds a parent of a given type. |
| | | 1054 | | /// </summary> |
| | | 1055 | | /// <typeparam name="T">Type of parent.</typeparam> |
| | | 1056 | | /// <returns>``0.</returns> |
| | | 1057 | | public T FindParent<T>() |
| | | 1058 | | where T : Folder |
| | | 1059 | | { |
| | 0 | 1060 | | foreach (var parent in GetParents()) |
| | | 1061 | | { |
| | 0 | 1062 | | if (parent is T item) |
| | | 1063 | | { |
| | 0 | 1064 | | return item; |
| | | 1065 | | } |
| | | 1066 | | } |
| | | 1067 | | |
| | 0 | 1068 | | return null; |
| | 0 | 1069 | | } |
| | | 1070 | | |
| | | 1071 | | /// <summary> |
| | | 1072 | | /// Gets the play access. |
| | | 1073 | | /// </summary> |
| | | 1074 | | /// <param name="user">The user.</param> |
| | | 1075 | | /// <returns>PlayAccess.</returns> |
| | | 1076 | | public PlayAccess GetPlayAccess(User user) |
| | | 1077 | | { |
| | 6 | 1078 | | if (!user.HasPermission(PermissionKind.EnableMediaPlayback)) |
| | | 1079 | | { |
| | 0 | 1080 | | return PlayAccess.None; |
| | | 1081 | | } |
| | | 1082 | | |
| | | 1083 | | // if (!user.IsParentalScheduleAllowed()) |
| | | 1084 | | // { |
| | | 1085 | | // return PlayAccess.None; |
| | | 1086 | | // } |
| | | 1087 | | |
| | 6 | 1088 | | return PlayAccess.Full; |
| | | 1089 | | } |
| | | 1090 | | |
| | | 1091 | | public virtual IReadOnlyList<MediaStream> GetMediaStreams() |
| | | 1092 | | { |
| | 0 | 1093 | | return MediaSourceManager.GetMediaStreams(new MediaStreamQuery |
| | 0 | 1094 | | { |
| | 0 | 1095 | | ItemId = Id |
| | 0 | 1096 | | }); |
| | | 1097 | | } |
| | | 1098 | | |
| | | 1099 | | protected virtual bool IsActiveRecording() |
| | | 1100 | | { |
| | 0 | 1101 | | return false; |
| | | 1102 | | } |
| | | 1103 | | |
| | | 1104 | | public virtual IReadOnlyList<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution) |
| | | 1105 | | { |
| | 6 | 1106 | | if (SourceType == SourceType.Channel) |
| | | 1107 | | { |
| | 0 | 1108 | | var sources = ChannelManager.GetStaticMediaSources(this, CancellationToken.None) |
| | 0 | 1109 | | .ToList(); |
| | | 1110 | | |
| | 0 | 1111 | | if (sources.Count > 0) |
| | | 1112 | | { |
| | 0 | 1113 | | return sources; |
| | | 1114 | | } |
| | | 1115 | | } |
| | | 1116 | | |
| | 6 | 1117 | | var list = GetAllItemsForMediaSources().ToList(); |
| | 6 | 1118 | | var commonPrefix = GetCommonNamePrefix(list); |
| | 6 | 1119 | | var result = list.Select(i => GetVersionInfo(enablePathSubstitution, i.Item, i.MediaSourceType, commonPrefix |
| | | 1120 | | |
| | 6 | 1121 | | if (IsActiveRecording()) |
| | | 1122 | | { |
| | 0 | 1123 | | foreach (var mediaSource in result) |
| | | 1124 | | { |
| | 0 | 1125 | | mediaSource.Type = MediaSourceType.Placeholder; |
| | | 1126 | | } |
| | | 1127 | | } |
| | | 1128 | | |
| | | 1129 | | // The source belonging to the item being queried sorts first so it is the default the client plays. |
| | 6 | 1130 | | var selfId = Id.ToString("N", CultureInfo.InvariantCulture); |
| | | 1131 | | |
| | 6 | 1132 | | return result |
| | 6 | 1133 | | .OrderByDescending(i => string.Equals(i.Id, selfId, StringComparison.OrdinalIgnoreCase)) |
| | 6 | 1134 | | .ThenBy(i => i.VideoType == VideoType.VideoFile ? 0 : 1) |
| | 6 | 1135 | | .ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0) |
| | 6 | 1136 | | .ThenByDescending(i => i, new MediaSourceWidthComparator()) |
| | 6 | 1137 | | .ToArray(); |
| | | 1138 | | } |
| | | 1139 | | |
| | | 1140 | | protected virtual IEnumerable<(BaseItem Item, MediaSourceType MediaSourceType)> GetAllItemsForMediaSources() |
| | | 1141 | | { |
| | 0 | 1142 | | return Enumerable.Empty<(BaseItem, MediaSourceType)>(); |
| | | 1143 | | } |
| | | 1144 | | |
| | | 1145 | | private MediaSourceInfo GetVersionInfo(bool enablePathSubstitution, BaseItem item, MediaSourceType type, string |
| | | 1146 | | { |
| | 18 | 1147 | | ArgumentNullException.ThrowIfNull(item); |
| | | 1148 | | |
| | 18 | 1149 | | var protocol = item.PathProtocol; |
| | 18 | 1150 | | var itemPath = item.Path; |
| | | 1151 | | |
| | 18 | 1152 | | var info = new MediaSourceInfo |
| | 18 | 1153 | | { |
| | 18 | 1154 | | Id = item.Id.ToString("N", CultureInfo.InvariantCulture), |
| | 18 | 1155 | | Protocol = protocol ?? MediaProtocol.File, |
| | 18 | 1156 | | MediaStreams = MediaSourceManager.GetMediaStreams(item.Id), |
| | 18 | 1157 | | MediaAttachments = MediaSourceManager.GetMediaAttachments(item.Id), |
| | 18 | 1158 | | Name = GetMediaSourceName(item, commonPrefix), |
| | 18 | 1159 | | Path = enablePathSubstitution ? GetMappedPath(item, itemPath, protocol) : itemPath, |
| | 18 | 1160 | | RunTimeTicks = item.RunTimeTicks, |
| | 18 | 1161 | | Container = item.Container, |
| | 18 | 1162 | | Size = item.Size, |
| | 18 | 1163 | | Type = type, |
| | 18 | 1164 | | HasSegments = MediaSegmentManager.IsTypeSupported(item) |
| | 18 | 1165 | | && (protocol is null or MediaProtocol.File) |
| | 18 | 1166 | | && MediaSegmentManager.HasSegments(item.Id) |
| | 18 | 1167 | | }; |
| | | 1168 | | |
| | 18 | 1169 | | if (string.IsNullOrEmpty(info.Path)) |
| | | 1170 | | { |
| | 0 | 1171 | | info.Type = MediaSourceType.Placeholder; |
| | | 1172 | | } |
| | | 1173 | | |
| | 18 | 1174 | | if (info.Protocol == MediaProtocol.File) |
| | | 1175 | | { |
| | 18 | 1176 | | info.ETag = item.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N", Cultur |
| | | 1177 | | } |
| | | 1178 | | |
| | 18 | 1179 | | var video = item as Video; |
| | 18 | 1180 | | if (video is not null) |
| | | 1181 | | { |
| | 18 | 1182 | | info.IsoType = video.IsoType; |
| | 18 | 1183 | | info.VideoType = video.VideoType; |
| | 18 | 1184 | | info.Video3DFormat = video.Video3DFormat; |
| | 18 | 1185 | | info.Timestamp = video.Timestamp; |
| | | 1186 | | |
| | 18 | 1187 | | if (video.IsShortcut && !string.IsNullOrEmpty(video.ShortcutPath)) |
| | | 1188 | | { |
| | 0 | 1189 | | var shortcutProtocol = MediaSourceManager.GetPathProtocol(video.ShortcutPath); |
| | | 1190 | | |
| | | 1191 | | // Only allow remote shortcut paths — local file paths in .strm files |
| | | 1192 | | // could be used to read arbitrary files from the server. |
| | 0 | 1193 | | if (shortcutProtocol != MediaProtocol.File) |
| | | 1194 | | { |
| | 0 | 1195 | | info.IsRemote = true; |
| | 0 | 1196 | | info.Path = video.ShortcutPath; |
| | 0 | 1197 | | info.Protocol = shortcutProtocol; |
| | | 1198 | | } |
| | | 1199 | | } |
| | | 1200 | | |
| | 18 | 1201 | | if (string.IsNullOrEmpty(info.Container)) |
| | | 1202 | | { |
| | 18 | 1203 | | if (video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso) |
| | | 1204 | | { |
| | 18 | 1205 | | if (protocol.HasValue && protocol.Value == MediaProtocol.File) |
| | | 1206 | | { |
| | 18 | 1207 | | info.Container = System.IO.Path.GetExtension(item.Path).TrimStart('.'); |
| | | 1208 | | } |
| | | 1209 | | } |
| | | 1210 | | } |
| | | 1211 | | } |
| | | 1212 | | |
| | 18 | 1213 | | if (string.IsNullOrEmpty(info.Container)) |
| | | 1214 | | { |
| | 0 | 1215 | | if (protocol.HasValue && protocol.Value == MediaProtocol.File) |
| | | 1216 | | { |
| | 0 | 1217 | | info.Container = System.IO.Path.GetExtension(item.Path).TrimStart('.'); |
| | | 1218 | | } |
| | | 1219 | | } |
| | | 1220 | | |
| | 18 | 1221 | | if (info.SupportsDirectStream && !string.IsNullOrEmpty(info.Path)) |
| | | 1222 | | { |
| | 18 | 1223 | | info.SupportsDirectStream = MediaSourceManager.SupportsDirectStream(info.Path, info.Protocol); |
| | | 1224 | | } |
| | | 1225 | | |
| | 18 | 1226 | | if (video is not null && video.VideoType != VideoType.VideoFile) |
| | | 1227 | | { |
| | 0 | 1228 | | info.SupportsDirectStream = false; |
| | | 1229 | | } |
| | | 1230 | | |
| | 18 | 1231 | | info.Bitrate = item.TotalBitrate; |
| | 18 | 1232 | | info.InferTotalBitrate(); |
| | | 1233 | | |
| | 18 | 1234 | | return info; |
| | | 1235 | | } |
| | | 1236 | | |
| | | 1237 | | internal string GetMediaSourceName(BaseItem item, string commonPrefix = null) |
| | | 1238 | | { |
| | 38 | 1239 | | var terms = new List<string>(); |
| | | 1240 | | |
| | 38 | 1241 | | var path = item.Path; |
| | 38 | 1242 | | if (item.IsFileProtocol && !string.IsNullOrEmpty(path)) |
| | | 1243 | | { |
| | 38 | 1244 | | var displayName = System.IO.Path.GetFileNameWithoutExtension(path); |
| | | 1245 | | |
| | | 1246 | | // Prefer the suffix that differs from the other versions: strip the prefix shared by |
| | | 1247 | | // all sibling files. This works regardless of folder layout, so it also labels episode |
| | | 1248 | | // versions that share a season folder (e.g. "Greyscale" instead of the full |
| | | 1249 | | // "Show - S01E02 - Title - Greyscale"). The prefix is already retreated to a delimiter |
| | | 1250 | | // boundary (see GetCommonVersionPrefix). |
| | 38 | 1251 | | if (!string.IsNullOrEmpty(commonPrefix) |
| | 38 | 1252 | | && displayName.Length > commonPrefix.Length |
| | 38 | 1253 | | && displayName.StartsWith(commonPrefix, StringComparison.OrdinalIgnoreCase)) |
| | | 1254 | | { |
| | 27 | 1255 | | var name = displayName.AsSpan(commonPrefix.Length).TrimStart([' ', .. VersionDelimiters]); |
| | 27 | 1256 | | if (!name.IsWhiteSpace()) |
| | | 1257 | | { |
| | 27 | 1258 | | terms.Add(name.ToString()); |
| | | 1259 | | } |
| | | 1260 | | } |
| | | 1261 | | |
| | | 1262 | | // Fall back to the containing folder name (the common layout for movie versions, and |
| | | 1263 | | // the path taken when no common prefix could be derived). |
| | 38 | 1264 | | if (terms.Count == 0 && HasLocalAlternateVersions) |
| | | 1265 | | { |
| | 8 | 1266 | | var containingFolderName = System.IO.Path.GetFileName(ContainingFolderPath); |
| | 8 | 1267 | | if (displayName.Length > containingFolderName.Length && displayName.StartsWith(containingFolderName, |
| | | 1268 | | { |
| | 2 | 1269 | | var name = displayName.AsSpan(containingFolderName.Length).TrimStart([' ', .. VersionDelimiters] |
| | 2 | 1270 | | if (!name.IsWhiteSpace()) |
| | | 1271 | | { |
| | 2 | 1272 | | terms.Add(name.ToString()); |
| | | 1273 | | } |
| | | 1274 | | } |
| | | 1275 | | } |
| | | 1276 | | |
| | 38 | 1277 | | if (terms.Count == 0) |
| | | 1278 | | { |
| | 9 | 1279 | | terms.Add(displayName); |
| | | 1280 | | } |
| | | 1281 | | } |
| | | 1282 | | |
| | 38 | 1283 | | if (terms.Count == 0) |
| | | 1284 | | { |
| | 0 | 1285 | | terms.Add(item.Name); |
| | | 1286 | | } |
| | | 1287 | | |
| | 38 | 1288 | | if (item is Video video) |
| | | 1289 | | { |
| | 38 | 1290 | | if (video.Video3DFormat.HasValue) |
| | | 1291 | | { |
| | 0 | 1292 | | terms.Add("3D"); |
| | | 1293 | | } |
| | | 1294 | | |
| | 38 | 1295 | | if (video.VideoType == VideoType.BluRay) |
| | | 1296 | | { |
| | 0 | 1297 | | terms.Add("Bluray"); |
| | | 1298 | | } |
| | 38 | 1299 | | else if (video.VideoType == VideoType.Dvd) |
| | | 1300 | | { |
| | 0 | 1301 | | terms.Add("DVD"); |
| | | 1302 | | } |
| | 38 | 1303 | | else if (video.VideoType == VideoType.Iso) |
| | | 1304 | | { |
| | 0 | 1305 | | if (video.IsoType.HasValue) |
| | | 1306 | | { |
| | 0 | 1307 | | if (video.IsoType.Value == IsoType.BluRay) |
| | | 1308 | | { |
| | 0 | 1309 | | terms.Add("Bluray"); |
| | | 1310 | | } |
| | 0 | 1311 | | else if (video.IsoType.Value == IsoType.Dvd) |
| | | 1312 | | { |
| | 0 | 1313 | | terms.Add("DVD"); |
| | | 1314 | | } |
| | | 1315 | | } |
| | | 1316 | | else |
| | | 1317 | | { |
| | 0 | 1318 | | terms.Add("ISO"); |
| | | 1319 | | } |
| | | 1320 | | } |
| | | 1321 | | } |
| | | 1322 | | |
| | 38 | 1323 | | return string.Join('/', terms); |
| | | 1324 | | } |
| | | 1325 | | |
| | | 1326 | | /// <summary> |
| | | 1327 | | /// Derives the prefix shared by the supplied media source items' file names, used to strip the |
| | | 1328 | | /// common part and surface a short version label per source. Returns null when there are fewer |
| | | 1329 | | /// than two file-based sources, since there is nothing to differentiate. |
| | | 1330 | | /// </summary> |
| | | 1331 | | /// <param name="items">The media source items.</param> |
| | | 1332 | | /// <returns>The shared prefix, or null when no useful prefix exists.</returns> |
| | | 1333 | | private static string GetCommonNamePrefix(IReadOnlyList<(BaseItem Item, MediaSourceType MediaSourceType)> items) |
| | | 1334 | | { |
| | 6 | 1335 | | var fileNames = new List<string>(); |
| | 48 | 1336 | | foreach (var (item, _) in items) |
| | | 1337 | | { |
| | 18 | 1338 | | if (item.IsFileProtocol && !string.IsNullOrEmpty(item.Path)) |
| | | 1339 | | { |
| | 18 | 1340 | | fileNames.Add(System.IO.Path.GetFileNameWithoutExtension(item.Path)); |
| | | 1341 | | } |
| | | 1342 | | } |
| | | 1343 | | |
| | 6 | 1344 | | if (fileNames.Count < 2) |
| | | 1345 | | { |
| | 0 | 1346 | | return null; |
| | | 1347 | | } |
| | | 1348 | | |
| | 6 | 1349 | | var prefix = GetCommonVersionPrefix(fileNames); |
| | 6 | 1350 | | return string.IsNullOrEmpty(prefix) ? null : prefix; |
| | | 1351 | | } |
| | | 1352 | | |
| | | 1353 | | /// <summary> |
| | | 1354 | | /// Computes the case-insensitive longest common prefix of the supplied version file names, |
| | | 1355 | | /// retreated to the last delimiter boundary. Retreating keeps the differing suffix intact: |
| | | 1356 | | /// it avoids slicing through a word every version shares (e.g. "Grey" in "Greyscale" and |
| | | 1357 | | /// "Greyish") while still trimming the common part when every version is suffixed (e.g. |
| | | 1358 | | /// "- Greyscale" / "- Colorized"). It prefers a structural delimiter ('-', '_', '.') so a |
| | | 1359 | | /// token shared by the descriptors but separated only by spaces (e.g. a common "2160p ") is |
| | | 1360 | | /// kept in the label, falling back to a space only when no structural delimiter is shared. The |
| | | 1361 | | /// separators mirror the version delimiters recognised by the naming layer (Emby.Naming |
| | | 1362 | | /// VideoFlagDelimiters). |
| | | 1363 | | /// </summary> |
| | | 1364 | | /// <param name="fileNames">The version file names without extension; must contain at least one entry.</param> |
| | | 1365 | | /// <returns>The shared prefix retreated to a separator boundary, or an empty string when none is shared.</retur |
| | | 1366 | | internal static string GetCommonVersionPrefix(IReadOnlyList<string> fileNames) |
| | | 1367 | | { |
| | 14 | 1368 | | var prefix = fileNames[0]; |
| | 68 | 1369 | | for (var i = 1; i < fileNames.Count && prefix.Length > 0; i++) |
| | | 1370 | | { |
| | 20 | 1371 | | var name = fileNames[i]; |
| | 20 | 1372 | | var length = Math.Min(prefix.Length, name.Length); |
| | 20 | 1373 | | var common = 0; |
| | 253 | 1374 | | while (common < length && char.ToUpperInvariant(prefix[common]) == char.ToUpperInvariant(name[common])) |
| | | 1375 | | { |
| | 233 | 1376 | | common++; |
| | | 1377 | | } |
| | | 1378 | | |
| | 20 | 1379 | | prefix = prefix[..common]; |
| | | 1380 | | } |
| | | 1381 | | |
| | | 1382 | | // If the common prefix is itself a whole file name then one version is unlabelled (the |
| | | 1383 | | // base name); the boundary already sits at the end of that name, so don't retreat into it. |
| | 14 | 1384 | | var prefixIsWholeName = false; |
| | 60 | 1385 | | for (var i = 0; i < fileNames.Count; i++) |
| | | 1386 | | { |
| | 23 | 1387 | | if (fileNames[i].Length == prefix.Length) |
| | | 1388 | | { |
| | 7 | 1389 | | prefixIsWholeName = true; |
| | 7 | 1390 | | break; |
| | | 1391 | | } |
| | | 1392 | | } |
| | | 1393 | | |
| | 14 | 1394 | | if (!prefixIsWholeName) |
| | | 1395 | | { |
| | | 1396 | | // Retreat to the last structural delimiter ('-', '_', '.'). |
| | 7 | 1397 | | var cut = prefix.Length; |
| | 46 | 1398 | | while (cut > 0 && Array.IndexOf(VersionDelimiters, prefix[cut - 1]) < 0) |
| | | 1399 | | { |
| | 39 | 1400 | | cut--; |
| | | 1401 | | } |
| | | 1402 | | |
| | 7 | 1403 | | if (cut == 0) |
| | | 1404 | | { |
| | 1 | 1405 | | cut = prefix.Length; |
| | 2 | 1406 | | while (cut > 0 && prefix[cut - 1] != ' ') |
| | | 1407 | | { |
| | 1 | 1408 | | cut--; |
| | | 1409 | | } |
| | | 1410 | | } |
| | | 1411 | | |
| | 7 | 1412 | | prefix = prefix[..cut]; |
| | | 1413 | | } |
| | | 1414 | | |
| | 14 | 1415 | | return prefix; |
| | | 1416 | | } |
| | | 1417 | | |
| | | 1418 | | public Task RefreshMetadata(CancellationToken cancellationToken) |
| | | 1419 | | { |
| | 54 | 1420 | | return RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(FileSystem)), cancellationToken); |
| | | 1421 | | } |
| | | 1422 | | |
| | | 1423 | | /// <summary> |
| | | 1424 | | /// The base implementation to refresh metadata. |
| | | 1425 | | /// </summary> |
| | | 1426 | | /// <param name="options">The options.</param> |
| | | 1427 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 1428 | | /// <returns>true if a provider reports we changed.</returns> |
| | | 1429 | | public async Task<ItemUpdateType> RefreshMetadata(MetadataRefreshOptions options, CancellationToken cancellation |
| | | 1430 | | { |
| | 61 | 1431 | | var requiresSave = false; |
| | | 1432 | | |
| | 61 | 1433 | | if (SupportsOwnedItems) |
| | | 1434 | | { |
| | | 1435 | | try |
| | | 1436 | | { |
| | 38 | 1437 | | if (IsFileProtocol) |
| | | 1438 | | { |
| | 38 | 1439 | | requiresSave = await RefreshedOwnedItems(options, GetFileSystemChildren(options.DirectoryService |
| | | 1440 | | } |
| | | 1441 | | |
| | 38 | 1442 | | await LibraryManager.UpdateImagesAsync(this).ConfigureAwait(false); // ensure all image properties i |
| | 38 | 1443 | | } |
| | 0 | 1444 | | catch (Exception ex) |
| | | 1445 | | { |
| | 0 | 1446 | | Logger.LogError(ex, "Error refreshing owned items for {Path}", Path ?? Name); |
| | 0 | 1447 | | } |
| | | 1448 | | } |
| | | 1449 | | |
| | 61 | 1450 | | var refreshOptions = requiresSave |
| | 61 | 1451 | | ? new MetadataRefreshOptions(options) |
| | 61 | 1452 | | { |
| | 61 | 1453 | | ForceSave = true |
| | 61 | 1454 | | } |
| | 61 | 1455 | | : options; |
| | | 1456 | | |
| | 61 | 1457 | | return await ProviderManager.RefreshSingleItem(this, refreshOptions, cancellationToken).ConfigureAwait(false |
| | 60 | 1458 | | } |
| | | 1459 | | |
| | | 1460 | | protected bool IsVisibleStandaloneInternal(User user, bool checkFolders) |
| | | 1461 | | { |
| | 0 | 1462 | | if (!IsVisible(user)) |
| | | 1463 | | { |
| | 0 | 1464 | | return false; |
| | | 1465 | | } |
| | | 1466 | | |
| | 0 | 1467 | | var parents = GetParents().ToList(); |
| | 0 | 1468 | | if (parents.Any(i => !i.IsVisible(user, true))) |
| | | 1469 | | { |
| | 0 | 1470 | | return false; |
| | | 1471 | | } |
| | | 1472 | | |
| | 0 | 1473 | | if (checkFolders) |
| | | 1474 | | { |
| | 0 | 1475 | | var topParent = parents.Count > 0 ? parents[^1] : this; |
| | | 1476 | | |
| | 0 | 1477 | | if (string.IsNullOrEmpty(topParent.Path)) |
| | | 1478 | | { |
| | 0 | 1479 | | return true; |
| | | 1480 | | } |
| | | 1481 | | |
| | 0 | 1482 | | var itemCollectionFolders = LibraryManager.GetCollectionFolders(this).Select(i => i.Id).ToList(); |
| | | 1483 | | |
| | 0 | 1484 | | if (itemCollectionFolders.Count > 0) |
| | | 1485 | | { |
| | 0 | 1486 | | var blockedMediaFolders = user.GetPreferenceValues<Guid>(PreferenceKind.BlockedMediaFolders); |
| | | 1487 | | IEnumerable<Guid> userCollectionFolderIds; |
| | 0 | 1488 | | if (blockedMediaFolders.Length > 0) |
| | | 1489 | | { |
| | | 1490 | | // User has blocked folders - get all library folders and exclude blocked ones |
| | 0 | 1491 | | userCollectionFolderIds = LibraryManager.GetUserRootFolder().Children |
| | 0 | 1492 | | .Select(i => i.Id) |
| | 0 | 1493 | | .Where(id => !blockedMediaFolders.Contains(id)); |
| | | 1494 | | } |
| | 0 | 1495 | | else if (user.HasPermission(PermissionKind.EnableAllFolders)) |
| | | 1496 | | { |
| | | 1497 | | // User can access all folders - no need to filter |
| | 0 | 1498 | | return true; |
| | | 1499 | | } |
| | | 1500 | | else |
| | | 1501 | | { |
| | | 1502 | | // User has specific enabled folders |
| | 0 | 1503 | | userCollectionFolderIds = user.GetPreferenceValues<Guid>(PreferenceKind.EnabledFolders); |
| | | 1504 | | } |
| | | 1505 | | |
| | 0 | 1506 | | if (!itemCollectionFolders.Any(userCollectionFolderIds.Contains)) |
| | | 1507 | | { |
| | 0 | 1508 | | return false; |
| | | 1509 | | } |
| | | 1510 | | } |
| | | 1511 | | } |
| | | 1512 | | |
| | 0 | 1513 | | return true; |
| | | 1514 | | } |
| | | 1515 | | |
| | | 1516 | | public void SetParent(Folder parent) |
| | | 1517 | | { |
| | 9 | 1518 | | ParentId = parent is null ? Guid.Empty : parent.Id; |
| | 9 | 1519 | | } |
| | | 1520 | | |
| | | 1521 | | /// <summary> |
| | | 1522 | | /// Refreshes owned items such as trailers, theme videos, special features, etc. |
| | | 1523 | | /// Returns true or false indicating if changes were found. |
| | | 1524 | | /// </summary> |
| | | 1525 | | /// <param name="options">The metadata refresh options.</param> |
| | | 1526 | | /// <param name="fileSystemChildren">The list of filesystem children.</param> |
| | | 1527 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 1528 | | /// <returns><c>true</c> if any items have changed, else <c>false</c>.</returns> |
| | | 1529 | | protected virtual async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystemM |
| | | 1530 | | { |
| | 38 | 1531 | | if (!IsFileProtocol || !SupportsOwnedItems || IsInMixedFolder || this is ICollectionFolder or UserRootFolder |
| | | 1532 | | { |
| | 38 | 1533 | | return false; |
| | | 1534 | | } |
| | | 1535 | | |
| | 0 | 1536 | | return await RefreshExtras(this, options, fileSystemChildren, cancellationToken).ConfigureAwait(false); |
| | 38 | 1537 | | } |
| | | 1538 | | |
| | | 1539 | | protected virtual FileSystemMetadata[] GetFileSystemChildren(IDirectoryService directoryService) |
| | | 1540 | | { |
| | 42 | 1541 | | return directoryService.GetFileSystemEntries(ContainingFolderPath); |
| | | 1542 | | } |
| | | 1543 | | |
| | | 1544 | | private async Task<bool> RefreshExtras(BaseItem item, MetadataRefreshOptions options, IReadOnlyList<FileSystemMe |
| | | 1545 | | { |
| | 0 | 1546 | | var extras = LibraryManager.FindExtras(item, fileSystemChildren, options.DirectoryService).ToArray(); |
| | 0 | 1547 | | var newExtraIds = Array.ConvertAll(extras, x => x.Id); |
| | | 1548 | | |
| | 0 | 1549 | | var currentExtras = LibraryManager.GetItemList(new InternalItemsQuery() |
| | 0 | 1550 | | { |
| | 0 | 1551 | | OwnerIds = [item.Id] |
| | 0 | 1552 | | }); |
| | | 1553 | | |
| | 0 | 1554 | | var currentExtraIds = currentExtras.Select(e => e.Id).ToArray(); |
| | | 1555 | | |
| | 0 | 1556 | | var extrasChanged = !currentExtraIds.OrderBy(x => x).SequenceEqual(newExtraIds.OrderBy(x => x)); |
| | | 1557 | | |
| | 0 | 1558 | | if (!extrasChanged && !options.ReplaceAllMetadata && options.MetadataRefreshMode != MetadataRefreshMode.Full |
| | | 1559 | | { |
| | | 1560 | | // The owner's dates may only have become known after its extras were created, so keep |
| | | 1561 | | // them in sync even when there is nothing to refresh. |
| | 0 | 1562 | | foreach (var extra in currentExtras) |
| | | 1563 | | { |
| | 0 | 1564 | | if (extra.ExtraType is not null && InheritDatesFromOwner(item, extra)) |
| | | 1565 | | { |
| | 0 | 1566 | | await extra.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwa |
| | | 1567 | | } |
| | | 1568 | | } |
| | | 1569 | | |
| | 0 | 1570 | | return false; |
| | | 1571 | | } |
| | | 1572 | | |
| | 0 | 1573 | | var ownerId = item.Id; |
| | | 1574 | | |
| | 0 | 1575 | | var tasks = extras.Select(i => |
| | 0 | 1576 | | { |
| | 0 | 1577 | | var subOptions = new MetadataRefreshOptions(options); |
| | 0 | 1578 | | if (!i.OwnerId.Equals(ownerId) || !i.ParentId.IsEmpty()) |
| | 0 | 1579 | | { |
| | 0 | 1580 | | subOptions.ForceSave = true; |
| | 0 | 1581 | | } |
| | 0 | 1582 | | |
| | 0 | 1583 | | i.OwnerId = ownerId; |
| | 0 | 1584 | | i.ParentId = Guid.Empty; |
| | 0 | 1585 | | |
| | 0 | 1586 | | return RefreshMetadataForOwnedItem(i, true, subOptions, cancellationToken); |
| | 0 | 1587 | | }); |
| | | 1588 | | |
| | 0 | 1589 | | var removedExtraIds = currentExtraIds.Where(e => !newExtraIds.Contains(e)).ToArray(); |
| | 0 | 1590 | | if (removedExtraIds.Length > 0) |
| | | 1591 | | { |
| | 0 | 1592 | | var removedExtras = LibraryManager.GetItemList(new InternalItemsQuery() |
| | 0 | 1593 | | { |
| | 0 | 1594 | | ItemIds = removedExtraIds |
| | 0 | 1595 | | }); |
| | 0 | 1596 | | foreach (var removedExtra in removedExtras) |
| | | 1597 | | { |
| | | 1598 | | // Only delete items that are actual extras (have ExtraType set) |
| | | 1599 | | // Items with OwnerId but no ExtraType might be alternate versions, not extras |
| | 0 | 1600 | | if (removedExtra.ExtraType.HasValue) |
| | | 1601 | | { |
| | 0 | 1602 | | LibraryManager.DeleteItem(removedExtra, new DeleteOptions() |
| | 0 | 1603 | | { |
| | 0 | 1604 | | DeleteFileLocation = false |
| | 0 | 1605 | | }); |
| | | 1606 | | } |
| | | 1607 | | } |
| | | 1608 | | } |
| | | 1609 | | |
| | 0 | 1610 | | await Task.WhenAll(tasks).ConfigureAwait(false); |
| | | 1611 | | |
| | 0 | 1612 | | return true; |
| | 0 | 1613 | | } |
| | | 1614 | | |
| | | 1615 | | public string GetPresentationUniqueKey() |
| | | 1616 | | { |
| | 0 | 1617 | | return PresentationUniqueKey ?? CreatePresentationUniqueKey(); |
| | | 1618 | | } |
| | | 1619 | | |
| | | 1620 | | public virtual bool RequiresRefresh() |
| | | 1621 | | { |
| | 61 | 1622 | | if (string.IsNullOrEmpty(Path) || DateModified == DateTime.MinValue) |
| | | 1623 | | { |
| | 61 | 1624 | | return false; |
| | | 1625 | | } |
| | | 1626 | | |
| | 0 | 1627 | | var info = FileSystem.GetFileSystemInfo(Path); |
| | | 1628 | | |
| | 0 | 1629 | | return info.Exists && this.HasChanged(info.LastWriteTimeUtc); |
| | | 1630 | | } |
| | | 1631 | | |
| | | 1632 | | public virtual List<string> GetUserDataKeys() |
| | | 1633 | | { |
| | 174 | 1634 | | var list = new List<string>(); |
| | | 1635 | | |
| | 174 | 1636 | | if (SourceType == SourceType.Channel) |
| | | 1637 | | { |
| | 0 | 1638 | | if (!string.IsNullOrEmpty(ExternalId)) |
| | | 1639 | | { |
| | 0 | 1640 | | list.Add(ExternalId); |
| | | 1641 | | } |
| | | 1642 | | } |
| | | 1643 | | |
| | 174 | 1644 | | list.Add(Id.ToString()); |
| | 174 | 1645 | | return list; |
| | | 1646 | | } |
| | | 1647 | | |
| | | 1648 | | internal virtual ItemUpdateType UpdateFromResolvedItem(BaseItem newItem) |
| | | 1649 | | { |
| | 5 | 1650 | | var updateType = ItemUpdateType.None; |
| | | 1651 | | |
| | 5 | 1652 | | if (IsInMixedFolder != newItem.IsInMixedFolder) |
| | | 1653 | | { |
| | 0 | 1654 | | IsInMixedFolder = newItem.IsInMixedFolder; |
| | 0 | 1655 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 1656 | | } |
| | | 1657 | | |
| | 5 | 1658 | | return updateType; |
| | | 1659 | | } |
| | | 1660 | | |
| | | 1661 | | public void AfterMetadataRefresh() |
| | | 1662 | | { |
| | 60 | 1663 | | _sortName = null; |
| | 60 | 1664 | | } |
| | | 1665 | | |
| | | 1666 | | /// <summary> |
| | | 1667 | | /// Gets the preferred metadata language. |
| | | 1668 | | /// </summary> |
| | | 1669 | | /// <returns>System.String.</returns> |
| | | 1670 | | public string GetPreferredMetadataLanguage() |
| | | 1671 | | { |
| | 36 | 1672 | | string lang = PreferredMetadataLanguage; |
| | | 1673 | | |
| | 36 | 1674 | | if (string.IsNullOrEmpty(lang)) |
| | | 1675 | | { |
| | 36 | 1676 | | lang = GetParents() |
| | 36 | 1677 | | .Select(i => i.PreferredMetadataLanguage) |
| | 36 | 1678 | | .FirstOrDefault(i => !string.IsNullOrEmpty(i)); |
| | | 1679 | | } |
| | | 1680 | | |
| | 36 | 1681 | | if (string.IsNullOrEmpty(lang)) |
| | | 1682 | | { |
| | 36 | 1683 | | lang = LibraryManager.GetCollectionFolders(this) |
| | 36 | 1684 | | .Select(i => i.PreferredMetadataLanguage) |
| | 36 | 1685 | | .FirstOrDefault(i => !string.IsNullOrEmpty(i)); |
| | | 1686 | | } |
| | | 1687 | | |
| | 36 | 1688 | | if (string.IsNullOrEmpty(lang)) |
| | | 1689 | | { |
| | 36 | 1690 | | lang = LibraryManager.GetLibraryOptions(this).PreferredMetadataLanguage; |
| | | 1691 | | } |
| | | 1692 | | |
| | 36 | 1693 | | if (string.IsNullOrEmpty(lang)) |
| | | 1694 | | { |
| | 36 | 1695 | | lang = ConfigurationManager.Configuration.PreferredMetadataLanguage; |
| | | 1696 | | } |
| | | 1697 | | |
| | 36 | 1698 | | return lang; |
| | | 1699 | | } |
| | | 1700 | | |
| | | 1701 | | /// <summary> |
| | | 1702 | | /// Gets the preferred metadata country code. |
| | | 1703 | | /// </summary> |
| | | 1704 | | /// <returns>System.String.</returns> |
| | | 1705 | | public string GetPreferredMetadataCountryCode() |
| | | 1706 | | { |
| | 36 | 1707 | | string lang = PreferredMetadataCountryCode; |
| | | 1708 | | |
| | 36 | 1709 | | if (string.IsNullOrEmpty(lang)) |
| | | 1710 | | { |
| | 36 | 1711 | | lang = GetParents() |
| | 36 | 1712 | | .Select(i => i.PreferredMetadataCountryCode) |
| | 36 | 1713 | | .FirstOrDefault(i => !string.IsNullOrEmpty(i)); |
| | | 1714 | | } |
| | | 1715 | | |
| | 36 | 1716 | | if (string.IsNullOrEmpty(lang)) |
| | | 1717 | | { |
| | 36 | 1718 | | lang = LibraryManager.GetCollectionFolders(this) |
| | 36 | 1719 | | .Select(i => i.PreferredMetadataCountryCode) |
| | 36 | 1720 | | .FirstOrDefault(i => !string.IsNullOrEmpty(i)); |
| | | 1721 | | } |
| | | 1722 | | |
| | 36 | 1723 | | if (string.IsNullOrEmpty(lang)) |
| | | 1724 | | { |
| | 36 | 1725 | | lang = LibraryManager.GetLibraryOptions(this).MetadataCountryCode; |
| | | 1726 | | } |
| | | 1727 | | |
| | 36 | 1728 | | if (string.IsNullOrEmpty(lang)) |
| | | 1729 | | { |
| | 36 | 1730 | | lang = ConfigurationManager.Configuration.MetadataCountryCode; |
| | | 1731 | | } |
| | | 1732 | | |
| | 36 | 1733 | | return lang; |
| | | 1734 | | } |
| | | 1735 | | |
| | | 1736 | | /// <summary> |
| | | 1737 | | /// Gets the original language of the item, inheriting from parent items if necessary. |
| | | 1738 | | /// </summary> |
| | | 1739 | | /// <returns>System.String.</returns> |
| | | 1740 | | public virtual string GetInheritedOriginalLanguage() |
| | | 1741 | | { |
| | 0 | 1742 | | return OriginalLanguage; |
| | | 1743 | | } |
| | | 1744 | | |
| | | 1745 | | public virtual bool IsSaveLocalMetadataEnabled() |
| | | 1746 | | { |
| | 88 | 1747 | | if (SourceType == SourceType.Channel) |
| | | 1748 | | { |
| | 0 | 1749 | | return false; |
| | | 1750 | | } |
| | | 1751 | | |
| | 88 | 1752 | | var libraryOptions = LibraryManager.GetLibraryOptions(this); |
| | | 1753 | | |
| | 88 | 1754 | | return libraryOptions.SaveLocalMetadata; |
| | | 1755 | | } |
| | | 1756 | | |
| | | 1757 | | /// <summary> |
| | | 1758 | | /// Determines if a given user has access to this item. |
| | | 1759 | | /// </summary> |
| | | 1760 | | /// <param name="user">The user.</param> |
| | | 1761 | | /// <param name="skipAllowedTagsCheck">Don't check for allowed tags.</param> |
| | | 1762 | | /// <returns><c>true</c> if [is parental allowed] [the specified user]; otherwise, <c>false</c>.</returns> |
| | | 1763 | | /// <exception cref="ArgumentNullException">If user is null.</exception> |
| | | 1764 | | public bool IsParentalAllowed(User user, bool skipAllowedTagsCheck) |
| | | 1765 | | { |
| | 10 | 1766 | | ArgumentNullException.ThrowIfNull(user); |
| | | 1767 | | |
| | 10 | 1768 | | if (!IsVisibleViaTags(user, skipAllowedTagsCheck)) |
| | | 1769 | | { |
| | 0 | 1770 | | return false; |
| | | 1771 | | } |
| | | 1772 | | |
| | 10 | 1773 | | var maxAllowedRating = user.MaxParentalRatingScore; |
| | 10 | 1774 | | var maxAllowedSubRating = user.MaxParentalRatingSubScore; |
| | 10 | 1775 | | var rating = CustomRatingForComparison; |
| | | 1776 | | |
| | 10 | 1777 | | if (string.IsNullOrEmpty(rating)) |
| | | 1778 | | { |
| | 10 | 1779 | | rating = OfficialRatingForComparison; |
| | | 1780 | | } |
| | | 1781 | | |
| | 10 | 1782 | | if (string.IsNullOrEmpty(rating)) |
| | | 1783 | | { |
| | 10 | 1784 | | return !GetBlockUnratedValue(user); |
| | | 1785 | | } |
| | | 1786 | | |
| | 0 | 1787 | | var ratingScore = LocalizationManager.GetRatingScore(rating, GetPreferredMetadataCountryCode()); |
| | | 1788 | | |
| | | 1789 | | // Could not determine rating level |
| | 0 | 1790 | | if (ratingScore is null) |
| | | 1791 | | { |
| | 0 | 1792 | | var isAllowed = !GetBlockUnratedValue(user); |
| | | 1793 | | |
| | 0 | 1794 | | if (!isAllowed) |
| | | 1795 | | { |
| | 0 | 1796 | | Logger.LogDebug("{0} has an unrecognized parental rating of {1}.", Name, rating); |
| | | 1797 | | } |
| | | 1798 | | |
| | 0 | 1799 | | return isAllowed; |
| | | 1800 | | } |
| | | 1801 | | |
| | 0 | 1802 | | if (!maxAllowedRating.HasValue) |
| | | 1803 | | { |
| | 0 | 1804 | | return true; |
| | | 1805 | | } |
| | | 1806 | | |
| | 0 | 1807 | | if (ratingScore.Score != maxAllowedRating.Value) |
| | | 1808 | | { |
| | 0 | 1809 | | return ratingScore.Score < maxAllowedRating.Value; |
| | | 1810 | | } |
| | | 1811 | | |
| | 0 | 1812 | | return !maxAllowedSubRating.HasValue || (ratingScore.SubScore ?? 0) <= maxAllowedSubRating.Value; |
| | | 1813 | | } |
| | | 1814 | | |
| | | 1815 | | public ParentalRatingScore GetParentalRatingScore() |
| | | 1816 | | { |
| | 122 | 1817 | | var rating = CustomRatingForComparison; |
| | | 1818 | | |
| | 122 | 1819 | | if (string.IsNullOrEmpty(rating)) |
| | | 1820 | | { |
| | 122 | 1821 | | rating = OfficialRatingForComparison; |
| | | 1822 | | } |
| | | 1823 | | |
| | 122 | 1824 | | if (string.IsNullOrEmpty(rating)) |
| | | 1825 | | { |
| | 122 | 1826 | | return null; |
| | | 1827 | | } |
| | | 1828 | | |
| | 0 | 1829 | | return LocalizationManager.GetRatingScore(rating, GetPreferredMetadataCountryCode()); |
| | | 1830 | | } |
| | | 1831 | | |
| | | 1832 | | public List<string> GetInheritedTags() |
| | | 1833 | | { |
| | 117 | 1834 | | var list = new List<string>(); |
| | 117 | 1835 | | list.AddRange(Tags); |
| | | 1836 | | |
| | 290 | 1837 | | foreach (var parent in GetParents()) |
| | | 1838 | | { |
| | 28 | 1839 | | list.AddRange(parent.Tags); |
| | | 1840 | | } |
| | | 1841 | | |
| | 234 | 1842 | | foreach (var folder in LibraryManager.GetCollectionFolders(this)) |
| | | 1843 | | { |
| | 0 | 1844 | | list.AddRange(folder.Tags); |
| | | 1845 | | } |
| | | 1846 | | |
| | 117 | 1847 | | return list.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); |
| | | 1848 | | } |
| | | 1849 | | |
| | | 1850 | | protected bool IsVisibleViaTags(User user, bool skipAllowedTagsCheck) |
| | | 1851 | | { |
| | 10 | 1852 | | var blockedTags = user.GetPreference(PreferenceKind.BlockedTags); |
| | 10 | 1853 | | var allowedTags = user.GetPreference(PreferenceKind.AllowedTags); |
| | | 1854 | | |
| | 10 | 1855 | | if (blockedTags.Length == 0 && allowedTags.Length == 0) |
| | | 1856 | | { |
| | 10 | 1857 | | return true; |
| | | 1858 | | } |
| | | 1859 | | |
| | | 1860 | | // Normalize tags using the same logic as database queries |
| | 0 | 1861 | | var normalizedBlockedTags = blockedTags |
| | 0 | 1862 | | .Where(t => !string.IsNullOrWhiteSpace(t)) |
| | 0 | 1863 | | .Select(t => t.GetCleanValue()) |
| | 0 | 1864 | | .ToHashSet(StringComparer.Ordinal); |
| | | 1865 | | |
| | 0 | 1866 | | var normalizedItemTags = GetInheritedTags() |
| | 0 | 1867 | | .Select(t => t.GetCleanValue()) |
| | 0 | 1868 | | .ToHashSet(StringComparer.Ordinal); |
| | | 1869 | | |
| | | 1870 | | // Check blocked tags - item is hidden if it has any blocked tag |
| | 0 | 1871 | | if (normalizedBlockedTags.Overlaps(normalizedItemTags)) |
| | | 1872 | | { |
| | 0 | 1873 | | return false; |
| | | 1874 | | } |
| | | 1875 | | |
| | 0 | 1876 | | var parent = GetParents().FirstOrDefault() ?? this; |
| | 0 | 1877 | | if (parent is UserRootFolder or AggregateFolder or UserView) |
| | | 1878 | | { |
| | 0 | 1879 | | return true; |
| | | 1880 | | } |
| | | 1881 | | |
| | | 1882 | | // Check allowed tags - item must have at least one allowed tag |
| | 0 | 1883 | | if (!skipAllowedTagsCheck && allowedTags.Length > 0) |
| | | 1884 | | { |
| | 0 | 1885 | | var normalizedAllowedTags = allowedTags |
| | 0 | 1886 | | .Where(t => !string.IsNullOrWhiteSpace(t)) |
| | 0 | 1887 | | .Select(t => t.GetCleanValue()) |
| | 0 | 1888 | | .ToHashSet(StringComparer.Ordinal); |
| | | 1889 | | |
| | 0 | 1890 | | if (!normalizedAllowedTags.Overlaps(normalizedItemTags)) |
| | | 1891 | | { |
| | 0 | 1892 | | return false; |
| | | 1893 | | } |
| | | 1894 | | } |
| | | 1895 | | |
| | 0 | 1896 | | return true; |
| | | 1897 | | } |
| | | 1898 | | |
| | | 1899 | | public virtual UnratedItem GetBlockUnratedType() |
| | | 1900 | | { |
| | 117 | 1901 | | if (SourceType == SourceType.Channel) |
| | | 1902 | | { |
| | 0 | 1903 | | return UnratedItem.ChannelContent; |
| | | 1904 | | } |
| | | 1905 | | |
| | 117 | 1906 | | return UnratedItem.Other; |
| | | 1907 | | } |
| | | 1908 | | |
| | | 1909 | | /// <summary> |
| | | 1910 | | /// Gets a bool indicating if access to the unrated item is blocked or not. |
| | | 1911 | | /// </summary> |
| | | 1912 | | /// <param name="user">The configuration.</param> |
| | | 1913 | | /// <returns><c>true</c> if blocked, <c>false</c> otherwise.</returns> |
| | | 1914 | | protected virtual bool GetBlockUnratedValue(User user) |
| | | 1915 | | { |
| | | 1916 | | // Don't block plain folders that are unrated. Let the media underneath get blocked |
| | | 1917 | | // Special folders like series and albums will override this method. |
| | 10 | 1918 | | if (IsFolder || this is IItemByName) |
| | | 1919 | | { |
| | 10 | 1920 | | return false; |
| | | 1921 | | } |
| | | 1922 | | |
| | 0 | 1923 | | return user.GetPreferenceValues<UnratedItem>(PreferenceKind.BlockUnratedItems).Contains(GetBlockUnratedType( |
| | | 1924 | | } |
| | | 1925 | | |
| | | 1926 | | /// <summary> |
| | | 1927 | | /// Determines if this folder should be visible to a given user. |
| | | 1928 | | /// Default is just parental allowed. Can be overridden for more functionality. |
| | | 1929 | | /// </summary> |
| | | 1930 | | /// <param name="user">The user.</param> |
| | | 1931 | | /// <param name="skipAllowedTagsCheck">Don't check for allowed tags.</param> |
| | | 1932 | | /// <returns><c>true</c> if the specified user is visible; otherwise, <c>false</c>.</returns> |
| | | 1933 | | /// <exception cref="ArgumentNullException"><paramref name="user" /> is <c>null</c>.</exception> |
| | | 1934 | | public virtual bool IsVisible(User user, bool skipAllowedTagsCheck = false) |
| | | 1935 | | { |
| | 10 | 1936 | | ArgumentNullException.ThrowIfNull(user); |
| | | 1937 | | |
| | 10 | 1938 | | return IsParentalAllowed(user, skipAllowedTagsCheck); |
| | | 1939 | | } |
| | | 1940 | | |
| | | 1941 | | public virtual bool IsVisibleStandalone(User user) |
| | | 1942 | | { |
| | 0 | 1943 | | if (SourceType == SourceType.Channel) |
| | | 1944 | | { |
| | 0 | 1945 | | return IsVisibleStandaloneInternal(user, false) && Channel.IsChannelVisible(this, user); |
| | | 1946 | | } |
| | | 1947 | | |
| | 0 | 1948 | | return IsVisibleStandaloneInternal(user, true); |
| | | 1949 | | } |
| | | 1950 | | |
| | | 1951 | | public virtual string GetClientTypeName() |
| | | 1952 | | { |
| | 121 | 1953 | | if (IsFolder && SourceType == SourceType.Channel && this is not Channel && this is not Season && this is not |
| | | 1954 | | { |
| | 0 | 1955 | | return "ChannelFolderItem"; |
| | | 1956 | | } |
| | | 1957 | | |
| | 121 | 1958 | | return GetType().Name; |
| | | 1959 | | } |
| | | 1960 | | |
| | | 1961 | | public BaseItemKind GetBaseItemKind() |
| | | 1962 | | { |
| | 258 | 1963 | | return _baseItemKind ??= Enum.Parse<BaseItemKind>(GetClientTypeName()); |
| | | 1964 | | } |
| | | 1965 | | |
| | | 1966 | | /// <summary> |
| | | 1967 | | /// Gets the linked child. |
| | | 1968 | | /// </summary> |
| | | 1969 | | /// <param name="info">The info.</param> |
| | | 1970 | | /// <returns>BaseItem.</returns> |
| | | 1971 | | protected BaseItem GetLinkedChild(LinkedChild info) |
| | | 1972 | | { |
| | | 1973 | | // First get using the cached Id |
| | 0 | 1974 | | if (info.ItemId.HasValue) |
| | | 1975 | | { |
| | 0 | 1976 | | if (info.ItemId.Value.IsEmpty()) |
| | | 1977 | | { |
| | 0 | 1978 | | return null; |
| | | 1979 | | } |
| | | 1980 | | |
| | 0 | 1981 | | var itemById = LibraryManager.GetItemById(info.ItemId.Value); |
| | | 1982 | | |
| | 0 | 1983 | | if (itemById is not null) |
| | | 1984 | | { |
| | 0 | 1985 | | return itemById; |
| | | 1986 | | } |
| | | 1987 | | } |
| | | 1988 | | |
| | 0 | 1989 | | var item = FindLinkedChild(info); |
| | | 1990 | | |
| | | 1991 | | // If still null, log |
| | 0 | 1992 | | if (item is null) |
| | | 1993 | | { |
| | | 1994 | | // Don't keep searching over and over |
| | 0 | 1995 | | info.ItemId = Guid.Empty; |
| | | 1996 | | } |
| | | 1997 | | else |
| | | 1998 | | { |
| | | 1999 | | // Cache the id for next time |
| | 0 | 2000 | | info.ItemId = item.Id; |
| | | 2001 | | } |
| | | 2002 | | |
| | 0 | 2003 | | return item; |
| | | 2004 | | } |
| | | 2005 | | |
| | | 2006 | | #pragma warning disable CS0618 // Type or member is obsolete - fallback for legacy LinkedChild data |
| | | 2007 | | private BaseItem FindLinkedChild(LinkedChild info) |
| | | 2008 | | { |
| | | 2009 | | // First try to find by ItemId (new preferred method) |
| | 0 | 2010 | | if (info.ItemId.HasValue && !info.ItemId.Value.Equals(Guid.Empty)) |
| | | 2011 | | { |
| | 0 | 2012 | | var item = LibraryManager.GetItemById(info.ItemId.Value); |
| | 0 | 2013 | | if (item is not null) |
| | | 2014 | | { |
| | 0 | 2015 | | return item; |
| | | 2016 | | } |
| | | 2017 | | |
| | 0 | 2018 | | Logger.LogWarning("Unable to find linked item by ItemId {0}", info.ItemId); |
| | | 2019 | | } |
| | | 2020 | | |
| | | 2021 | | // Fall back to Path (legacy method) |
| | 0 | 2022 | | var path = info.Path; |
| | 0 | 2023 | | if (!string.IsNullOrEmpty(path)) |
| | | 2024 | | { |
| | 0 | 2025 | | path = FileSystem.MakeAbsolutePath(ContainingFolderPath, path); |
| | | 2026 | | |
| | 0 | 2027 | | var itemByPath = LibraryManager.FindByPath(path, null); |
| | | 2028 | | |
| | 0 | 2029 | | if (itemByPath is null) |
| | | 2030 | | { |
| | 0 | 2031 | | Logger.LogWarning("Unable to find linked item at path {0}", info.Path); |
| | | 2032 | | } |
| | | 2033 | | |
| | 0 | 2034 | | return itemByPath; |
| | | 2035 | | } |
| | | 2036 | | |
| | | 2037 | | // Fall back to LibraryItemId (legacy method) |
| | 0 | 2038 | | if (!string.IsNullOrEmpty(info.LibraryItemId)) |
| | | 2039 | | { |
| | 0 | 2040 | | var item = LibraryManager.GetItemById(info.LibraryItemId); |
| | | 2041 | | |
| | 0 | 2042 | | if (item is null) |
| | | 2043 | | { |
| | 0 | 2044 | | Logger.LogWarning("Unable to find linked item by LibraryItemId {0}", info.LibraryItemId); |
| | | 2045 | | } |
| | | 2046 | | |
| | 0 | 2047 | | return item; |
| | | 2048 | | } |
| | | 2049 | | |
| | 0 | 2050 | | return null; |
| | | 2051 | | } |
| | | 2052 | | #pragma warning restore CS0618 |
| | | 2053 | | |
| | | 2054 | | /// <summary> |
| | | 2055 | | /// Adds a studio to the item. |
| | | 2056 | | /// </summary> |
| | | 2057 | | /// <param name="name">The name.</param> |
| | | 2058 | | /// <exception cref="ArgumentNullException">Throws if name is null.</exception> |
| | | 2059 | | public void AddStudio(string name) |
| | | 2060 | | { |
| | 4 | 2061 | | ArgumentException.ThrowIfNullOrEmpty(name); |
| | 4 | 2062 | | var current = Studios; |
| | | 2063 | | |
| | 4 | 2064 | | if (!current.Contains(name, StringComparison.OrdinalIgnoreCase)) |
| | | 2065 | | { |
| | 4 | 2066 | | int curLen = current.Length; |
| | 4 | 2067 | | if (curLen == 0) |
| | | 2068 | | { |
| | 4 | 2069 | | Studios = [name]; |
| | | 2070 | | } |
| | | 2071 | | else |
| | | 2072 | | { |
| | 0 | 2073 | | Studios = [.. current, name]; |
| | | 2074 | | } |
| | | 2075 | | } |
| | 0 | 2076 | | } |
| | | 2077 | | |
| | | 2078 | | public void SetStudios(IEnumerable<string> names) |
| | | 2079 | | { |
| | 5 | 2080 | | Studios = names.Trimmed().Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); |
| | 5 | 2081 | | } |
| | | 2082 | | |
| | | 2083 | | /// <summary> |
| | | 2084 | | /// Adds a genre to the item. |
| | | 2085 | | /// </summary> |
| | | 2086 | | /// <param name="name">The name.</param> |
| | | 2087 | | /// <exception cref="ArgumentNullException">Throws if name is null.</exception> |
| | | 2088 | | public void AddGenre(string name) |
| | | 2089 | | { |
| | 13 | 2090 | | ArgumentException.ThrowIfNullOrEmpty(name); |
| | | 2091 | | |
| | 13 | 2092 | | var genres = Genres; |
| | 13 | 2093 | | if (!genres.Contains(name, StringComparison.OrdinalIgnoreCase)) |
| | | 2094 | | { |
| | 13 | 2095 | | Genres = [.. genres, name]; |
| | | 2096 | | } |
| | 13 | 2097 | | } |
| | | 2098 | | |
| | | 2099 | | /// <summary> |
| | | 2100 | | /// Marks the played. |
| | | 2101 | | /// </summary> |
| | | 2102 | | /// <param name="user">The user.</param> |
| | | 2103 | | /// <param name="datePlayed">The date played.</param> |
| | | 2104 | | /// <param name="resetPosition">if set to <c>true</c> [reset position].</param> |
| | | 2105 | | /// <exception cref="ArgumentNullException">Throws if user is null.</exception> |
| | | 2106 | | public virtual void MarkPlayed( |
| | | 2107 | | User user, |
| | | 2108 | | DateTime? datePlayed, |
| | | 2109 | | bool resetPosition) |
| | | 2110 | | { |
| | 0 | 2111 | | ArgumentNullException.ThrowIfNull(user); |
| | | 2112 | | |
| | 0 | 2113 | | var data = UserDataManager.GetUserData(user, this) ?? new UserItemData() |
| | 0 | 2114 | | { |
| | 0 | 2115 | | Key = GetUserDataKeys().First(), |
| | 0 | 2116 | | }; |
| | | 2117 | | |
| | 0 | 2118 | | if (datePlayed.HasValue) |
| | | 2119 | | { |
| | | 2120 | | // Increment |
| | 0 | 2121 | | data.PlayCount++; |
| | | 2122 | | } |
| | | 2123 | | |
| | | 2124 | | // Ensure it's at least one |
| | 0 | 2125 | | data.PlayCount = Math.Max(data.PlayCount, 1); |
| | | 2126 | | |
| | 0 | 2127 | | if (resetPosition) |
| | | 2128 | | { |
| | 0 | 2129 | | data.PlaybackPositionTicks = 0; |
| | | 2130 | | } |
| | | 2131 | | |
| | 0 | 2132 | | data.LastPlayedDate = datePlayed ?? data.LastPlayedDate ?? DateTime.UtcNow; |
| | 0 | 2133 | | data.Played = true; |
| | | 2134 | | |
| | 0 | 2135 | | UserDataManager.SaveUserData(user, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None); |
| | 0 | 2136 | | } |
| | | 2137 | | |
| | | 2138 | | /// <summary> |
| | | 2139 | | /// Marks the unplayed. |
| | | 2140 | | /// </summary> |
| | | 2141 | | /// <param name="user">The user.</param> |
| | | 2142 | | /// <exception cref="ArgumentNullException">Throws if user is null.</exception> |
| | | 2143 | | public virtual void MarkUnplayed(User user) |
| | | 2144 | | { |
| | 0 | 2145 | | ArgumentNullException.ThrowIfNull(user); |
| | | 2146 | | |
| | 0 | 2147 | | var data = UserDataManager.GetUserData(user, this); |
| | | 2148 | | |
| | | 2149 | | // I think it is okay to do this here. |
| | | 2150 | | // if this is only called when a user is manually forcing something to un-played |
| | | 2151 | | // then it probably is what we want to do... |
| | 0 | 2152 | | ResetPlayedState(data); |
| | | 2153 | | |
| | 0 | 2154 | | UserDataManager.SaveUserData(user, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None); |
| | 0 | 2155 | | } |
| | | 2156 | | |
| | | 2157 | | /// <summary> |
| | | 2158 | | /// Clears the played state on the supplied user data. |
| | | 2159 | | /// </summary> |
| | | 2160 | | /// <param name="data">The user data to reset.</param> |
| | | 2161 | | protected static void ResetPlayedState(UserItemData data) |
| | | 2162 | | { |
| | 2 | 2163 | | ArgumentNullException.ThrowIfNull(data); |
| | | 2164 | | |
| | 2 | 2165 | | data.PlayCount = 0; |
| | 2 | 2166 | | data.PlaybackPositionTicks = 0; |
| | 2 | 2167 | | data.LastPlayedDate = null; |
| | 2 | 2168 | | data.Played = false; |
| | 2 | 2169 | | } |
| | | 2170 | | |
| | | 2171 | | /// <summary> |
| | | 2172 | | /// Do whatever refreshing is necessary when the filesystem pertaining to this item has changed. |
| | | 2173 | | /// </summary> |
| | | 2174 | | public virtual void ChangedExternally() |
| | | 2175 | | { |
| | 0 | 2176 | | ProviderManager.QueueRefresh(Id, new MetadataRefreshOptions(new DirectoryService(FileSystem)), RefreshPriori |
| | 0 | 2177 | | } |
| | | 2178 | | |
| | | 2179 | | /// <summary> |
| | | 2180 | | /// Gets an image. |
| | | 2181 | | /// </summary> |
| | | 2182 | | /// <param name="type">The type.</param> |
| | | 2183 | | /// <param name="imageIndex">Index of the image.</param> |
| | | 2184 | | /// <returns><c>true</c> if the specified type has image; otherwise, <c>false</c>.</returns> |
| | | 2185 | | /// <exception cref="ArgumentException">Backdrops should be accessed using Item.Backdrops.</exception> |
| | | 2186 | | public bool HasImage(ImageType type, int imageIndex) |
| | | 2187 | | { |
| | 148 | 2188 | | return GetImageInfo(type, imageIndex) is not null; |
| | | 2189 | | } |
| | | 2190 | | |
| | | 2191 | | public void SetImage(ItemImageInfo image, int index) |
| | | 2192 | | { |
| | 22 | 2193 | | if (image.Type == ImageType.Chapter) |
| | | 2194 | | { |
| | 0 | 2195 | | throw new ArgumentException("Cannot set chapter images using SetImagePath"); |
| | | 2196 | | } |
| | | 2197 | | |
| | 22 | 2198 | | var existingImage = GetImageInfo(image.Type, index); |
| | | 2199 | | |
| | 22 | 2200 | | if (existingImage is null) |
| | | 2201 | | { |
| | 19 | 2202 | | AddImage(image); |
| | | 2203 | | } |
| | | 2204 | | else |
| | | 2205 | | { |
| | 3 | 2206 | | existingImage.Path = image.Path; |
| | 3 | 2207 | | existingImage.DateModified = image.DateModified; |
| | 3 | 2208 | | existingImage.Width = image.Width; |
| | 3 | 2209 | | existingImage.Height = image.Height; |
| | 3 | 2210 | | existingImage.BlurHash = image.BlurHash; |
| | | 2211 | | } |
| | 3 | 2212 | | } |
| | | 2213 | | |
| | | 2214 | | public void SetImagePath(ImageType type, int index, FileSystemMetadata file) |
| | | 2215 | | { |
| | 46 | 2216 | | if (type == ImageType.Chapter) |
| | | 2217 | | { |
| | 0 | 2218 | | throw new ArgumentException("Cannot set chapter images using SetImagePath"); |
| | | 2219 | | } |
| | | 2220 | | |
| | 46 | 2221 | | var image = GetImageInfo(type, index); |
| | | 2222 | | |
| | 46 | 2223 | | if (image is null) |
| | | 2224 | | { |
| | 45 | 2225 | | AddImage(GetImageInfo(file, type)); |
| | | 2226 | | } |
| | | 2227 | | else |
| | | 2228 | | { |
| | 1 | 2229 | | var imageInfo = GetImageInfo(file, type); |
| | | 2230 | | |
| | 1 | 2231 | | image.Path = file.FullName; |
| | 1 | 2232 | | image.DateModified = imageInfo.DateModified; |
| | | 2233 | | |
| | | 2234 | | // reset these values |
| | 1 | 2235 | | image.Width = 0; |
| | 1 | 2236 | | image.Height = 0; |
| | | 2237 | | } |
| | 1 | 2238 | | } |
| | | 2239 | | |
| | | 2240 | | /// <summary> |
| | | 2241 | | /// Deletes the image. |
| | | 2242 | | /// </summary> |
| | | 2243 | | /// <param name="type">The type.</param> |
| | | 2244 | | /// <param name="index">The index.</param> |
| | | 2245 | | /// <returns>A task.</returns> |
| | | 2246 | | public async Task DeleteImageAsync(ImageType type, int index) |
| | | 2247 | | { |
| | 0 | 2248 | | var info = GetImageInfo(type, index); |
| | | 2249 | | |
| | 0 | 2250 | | if (info is null) |
| | | 2251 | | { |
| | | 2252 | | // Nothing to do |
| | 0 | 2253 | | return; |
| | | 2254 | | } |
| | | 2255 | | |
| | | 2256 | | // Remove from file system |
| | 0 | 2257 | | var path = info.Path; |
| | 0 | 2258 | | if (info.IsLocalFile && !string.IsNullOrWhiteSpace(path)) |
| | | 2259 | | { |
| | 0 | 2260 | | FileSystem.DeleteFile(path); |
| | | 2261 | | } |
| | | 2262 | | |
| | | 2263 | | // Remove from item |
| | 0 | 2264 | | RemoveImage(info); |
| | | 2265 | | |
| | 0 | 2266 | | await UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false); |
| | 0 | 2267 | | } |
| | | 2268 | | |
| | | 2269 | | public void RemoveImage(ItemImageInfo image) |
| | | 2270 | | { |
| | 0 | 2271 | | RemoveImages([image]); |
| | 0 | 2272 | | } |
| | | 2273 | | |
| | | 2274 | | public void RemoveImages(IEnumerable<ItemImageInfo> deletedImages) |
| | | 2275 | | { |
| | 9 | 2276 | | ImageInfos = ImageInfos.Except(deletedImages).ToArray(); |
| | 9 | 2277 | | } |
| | | 2278 | | |
| | | 2279 | | public void AddImage(ItemImageInfo image) |
| | | 2280 | | { |
| | 64 | 2281 | | ImageInfos = [.. ImageInfos, image]; |
| | 64 | 2282 | | } |
| | | 2283 | | |
| | | 2284 | | public virtual async Task UpdateToRepositoryAsync(ItemUpdateType updateReason, CancellationToken cancellationTok |
| | 115 | 2285 | | => await LibraryManager.UpdateItemAsync(this, GetParent(), updateReason, cancellationToken).ConfigureAwait(fals |
| | | 2286 | | |
| | | 2287 | | public async Task ReattachUserDataAsync(CancellationToken cancellationToken) => |
| | 35 | 2288 | | await LibraryManager.ReattachUserDataAsync(this, cancellationToken).ConfigureAwait(false); |
| | | 2289 | | |
| | | 2290 | | /// <summary> |
| | | 2291 | | /// Validates that images within the item are still on the filesystem. |
| | | 2292 | | /// </summary> |
| | | 2293 | | /// <returns><c>true</c> if the images validate, <c>false</c> if not.</returns> |
| | | 2294 | | public bool ValidateImages() |
| | | 2295 | | { |
| | 75 | 2296 | | List<ItemImageInfo> deletedImages = null; |
| | 180 | 2297 | | foreach (var imageInfo in ImageInfos) |
| | | 2298 | | { |
| | 15 | 2299 | | if (!imageInfo.IsLocalFile) |
| | | 2300 | | { |
| | | 2301 | | continue; |
| | | 2302 | | } |
| | | 2303 | | |
| | 15 | 2304 | | if (File.Exists(imageInfo.Path)) |
| | | 2305 | | { |
| | | 2306 | | continue; |
| | | 2307 | | } |
| | | 2308 | | |
| | 3 | 2309 | | (deletedImages ??= []).Add(imageInfo); |
| | | 2310 | | } |
| | | 2311 | | |
| | 75 | 2312 | | var anyImagesRemoved = deletedImages?.Count > 0; |
| | 75 | 2313 | | if (anyImagesRemoved) |
| | | 2314 | | { |
| | 2 | 2315 | | RemoveImages(deletedImages); |
| | | 2316 | | } |
| | | 2317 | | |
| | 75 | 2318 | | return anyImagesRemoved; |
| | | 2319 | | } |
| | | 2320 | | |
| | | 2321 | | /// <summary> |
| | | 2322 | | /// Gets the image path. |
| | | 2323 | | /// </summary> |
| | | 2324 | | /// <param name="imageType">Type of the image.</param> |
| | | 2325 | | /// <param name="imageIndex">Index of the image.</param> |
| | | 2326 | | /// <returns>System.String.</returns> |
| | | 2327 | | /// <exception cref="ArgumentNullException">Item is null.</exception> |
| | | 2328 | | public string GetImagePath(ImageType imageType, int imageIndex) |
| | 4 | 2329 | | => GetImageInfo(imageType, imageIndex)?.Path; |
| | | 2330 | | |
| | | 2331 | | /// <summary> |
| | | 2332 | | /// Gets the image information. |
| | | 2333 | | /// </summary> |
| | | 2334 | | /// <param name="imageType">Type of the image.</param> |
| | | 2335 | | /// <param name="imageIndex">Index of the image.</param> |
| | | 2336 | | /// <returns>ItemImageInfo.</returns> |
| | | 2337 | | public ItemImageInfo GetImageInfo(ImageType imageType, int imageIndex) |
| | | 2338 | | { |
| | 329 | 2339 | | if (imageType == ImageType.Chapter) |
| | | 2340 | | { |
| | 0 | 2341 | | var chapter = ChapterManager.GetChapter(Id, imageIndex); |
| | | 2342 | | |
| | 0 | 2343 | | if (chapter is null) |
| | | 2344 | | { |
| | 0 | 2345 | | return null; |
| | | 2346 | | } |
| | | 2347 | | |
| | 0 | 2348 | | var path = chapter.ImagePath; |
| | | 2349 | | |
| | 0 | 2350 | | if (string.IsNullOrEmpty(path)) |
| | | 2351 | | { |
| | 0 | 2352 | | return null; |
| | | 2353 | | } |
| | | 2354 | | |
| | 0 | 2355 | | return new ItemImageInfo |
| | 0 | 2356 | | { |
| | 0 | 2357 | | Path = path, |
| | 0 | 2358 | | DateModified = chapter.ImageDateModified, |
| | 0 | 2359 | | Type = imageType |
| | 0 | 2360 | | }; |
| | | 2361 | | } |
| | | 2362 | | |
| | 329 | 2363 | | return GetImages(imageType) |
| | 329 | 2364 | | .ElementAtOrDefault(imageIndex); |
| | | 2365 | | } |
| | | 2366 | | |
| | | 2367 | | /// <summary> |
| | | 2368 | | /// Computes image index for given image or raises if no matching image found. |
| | | 2369 | | /// </summary> |
| | | 2370 | | /// <param name="image">Image to compute index for.</param> |
| | | 2371 | | /// <exception cref="ArgumentException">Image index cannot be computed as no matching image found. |
| | | 2372 | | /// </exception> |
| | | 2373 | | /// <returns>Image index.</returns> |
| | | 2374 | | public int GetImageIndex(ItemImageInfo image) |
| | | 2375 | | { |
| | 0 | 2376 | | ArgumentNullException.ThrowIfNull(image); |
| | | 2377 | | |
| | 0 | 2378 | | if (image.Type == ImageType.Chapter) |
| | | 2379 | | { |
| | 0 | 2380 | | var chapters = ChapterManager.GetChapters(Id); |
| | 0 | 2381 | | for (var i = 0; i < chapters.Count; i++) |
| | | 2382 | | { |
| | 0 | 2383 | | if (chapters[i].ImagePath == image.Path) |
| | | 2384 | | { |
| | 0 | 2385 | | return i; |
| | | 2386 | | } |
| | | 2387 | | } |
| | | 2388 | | |
| | 0 | 2389 | | throw new ArgumentException("No chapter index found for image path", image.Path); |
| | | 2390 | | } |
| | | 2391 | | |
| | 0 | 2392 | | var images = GetImages(image.Type).ToArray(); |
| | 0 | 2393 | | for (var i = 0; i < images.Length; i++) |
| | | 2394 | | { |
| | 0 | 2395 | | if (images[i].Path == image.Path) |
| | | 2396 | | { |
| | 0 | 2397 | | return i; |
| | | 2398 | | } |
| | | 2399 | | } |
| | | 2400 | | |
| | 0 | 2401 | | throw new ArgumentException("No image index found for image path", image.Path); |
| | | 2402 | | } |
| | | 2403 | | |
| | | 2404 | | public IEnumerable<ItemImageInfo> GetImages(ImageType imageType) |
| | | 2405 | | { |
| | 439 | 2406 | | if (imageType == ImageType.Chapter) |
| | | 2407 | | { |
| | 0 | 2408 | | throw new ArgumentException("No image info for chapter images"); |
| | | 2409 | | } |
| | | 2410 | | |
| | | 2411 | | // Yield return is more performant than LINQ Where on an Array |
| | 1434 | 2412 | | for (var i = 0; i < ImageInfos.Length; i++) |
| | | 2413 | | { |
| | 307 | 2414 | | var imageInfo = ImageInfos[i]; |
| | 307 | 2415 | | if (imageInfo.Type == imageType) |
| | | 2416 | | { |
| | 161 | 2417 | | yield return imageInfo; |
| | | 2418 | | } |
| | | 2419 | | } |
| | 410 | 2420 | | } |
| | | 2421 | | |
| | | 2422 | | /// <summary> |
| | | 2423 | | /// Adds the images, updating metadata if they already are part of this item. |
| | | 2424 | | /// </summary> |
| | | 2425 | | /// <param name="imageType">Type of the image.</param> |
| | | 2426 | | /// <param name="images">The images.</param> |
| | | 2427 | | /// <returns><c>true</c> if images were added or updated, <c>false</c> otherwise.</returns> |
| | | 2428 | | /// <exception cref="ArgumentException">Cannot call AddImages with chapter images.</exception> |
| | | 2429 | | public bool AddImages(ImageType imageType, List<FileSystemMetadata> images) |
| | | 2430 | | { |
| | 4 | 2431 | | if (imageType == ImageType.Chapter) |
| | | 2432 | | { |
| | 0 | 2433 | | throw new ArgumentException("Cannot call AddImages with chapter images"); |
| | | 2434 | | } |
| | | 2435 | | |
| | 4 | 2436 | | var existingImages = GetImages(imageType) |
| | 4 | 2437 | | .ToList(); |
| | | 2438 | | |
| | 4 | 2439 | | var newImageList = new List<FileSystemMetadata>(); |
| | 4 | 2440 | | var imageUpdated = false; |
| | | 2441 | | |
| | 24 | 2442 | | foreach (var newImage in images) |
| | | 2443 | | { |
| | 8 | 2444 | | if (newImage is null) |
| | | 2445 | | { |
| | 0 | 2446 | | throw new ArgumentException("null image found in list"); |
| | | 2447 | | } |
| | | 2448 | | |
| | 8 | 2449 | | var existing = existingImages |
| | 8 | 2450 | | .Find(i => string.Equals(i.Path, newImage.FullName, StringComparison.OrdinalIgnoreCase)); |
| | | 2451 | | |
| | 8 | 2452 | | if (existing is null) |
| | | 2453 | | { |
| | 4 | 2454 | | newImageList.Add(newImage); |
| | | 2455 | | } |
| | | 2456 | | else |
| | | 2457 | | { |
| | 4 | 2458 | | if (existing.IsLocalFile) |
| | | 2459 | | { |
| | 4 | 2460 | | var newDateModified = FileSystem.GetLastWriteTimeUtc(newImage); |
| | | 2461 | | |
| | | 2462 | | // If date changed then we need to reset saved image dimensions |
| | 4 | 2463 | | if (existing.DateModified != newDateModified && (existing.Width > 0 || existing.Height > 0)) |
| | | 2464 | | { |
| | 2 | 2465 | | existing.Width = 0; |
| | 2 | 2466 | | existing.Height = 0; |
| | 2 | 2467 | | imageUpdated = true; |
| | | 2468 | | } |
| | | 2469 | | |
| | 4 | 2470 | | existing.DateModified = newDateModified; |
| | | 2471 | | } |
| | | 2472 | | } |
| | | 2473 | | } |
| | | 2474 | | |
| | 4 | 2475 | | if (newImageList.Count > 0) |
| | | 2476 | | { |
| | 2 | 2477 | | ImageInfos = ImageInfos.Concat(newImageList.Select(i => GetImageInfo(i, imageType))).ToArray(); |
| | | 2478 | | } |
| | | 2479 | | |
| | 4 | 2480 | | return imageUpdated || newImageList.Count > 0; |
| | | 2481 | | } |
| | | 2482 | | |
| | | 2483 | | private ItemImageInfo GetImageInfo(FileSystemMetadata file, ImageType type) |
| | | 2484 | | { |
| | 50 | 2485 | | return new ItemImageInfo |
| | 50 | 2486 | | { |
| | 50 | 2487 | | Path = file.FullName, |
| | 50 | 2488 | | Type = type, |
| | 50 | 2489 | | DateModified = FileSystem.GetLastWriteTimeUtc(file) |
| | 50 | 2490 | | }; |
| | | 2491 | | } |
| | | 2492 | | |
| | | 2493 | | /// <summary> |
| | | 2494 | | /// Gets the file system path to delete when the item is to be deleted. |
| | | 2495 | | /// </summary> |
| | | 2496 | | /// <returns>The metadata for the deleted paths.</returns> |
| | | 2497 | | public virtual IEnumerable<FileSystemMetadata> GetDeletePaths() |
| | | 2498 | | { |
| | 0 | 2499 | | return new[] |
| | 0 | 2500 | | { |
| | 0 | 2501 | | FileSystem.GetFileSystemInfo(Path) |
| | 0 | 2502 | | }.Concat(GetLocalMetadataFilesToDelete()); |
| | | 2503 | | } |
| | | 2504 | | |
| | | 2505 | | protected List<FileSystemMetadata> GetLocalMetadataFilesToDelete() |
| | | 2506 | | { |
| | 0 | 2507 | | if (IsFolder || !IsInMixedFolder) |
| | | 2508 | | { |
| | 0 | 2509 | | return []; |
| | | 2510 | | } |
| | | 2511 | | |
| | 0 | 2512 | | var filename = System.IO.Path.GetFileNameWithoutExtension(Path); |
| | | 2513 | | |
| | 0 | 2514 | | return FileSystem.GetFiles(System.IO.Path.GetDirectoryName(Path), _supportedExtensions, false, false) |
| | 0 | 2515 | | .Where(i => System.IO.Path.GetFileNameWithoutExtension(i.FullName).StartsWith(filename, StringComparison |
| | 0 | 2516 | | .ToList(); |
| | | 2517 | | } |
| | | 2518 | | |
| | | 2519 | | public bool AllowsMultipleImages(ImageType type) |
| | | 2520 | | { |
| | 23 | 2521 | | return type == ImageType.Backdrop || type == ImageType.Chapter; |
| | | 2522 | | } |
| | | 2523 | | |
| | | 2524 | | public Task SwapImagesAsync(ImageType type, int index1, int index2) |
| | | 2525 | | { |
| | 0 | 2526 | | if (!AllowsMultipleImages(type)) |
| | | 2527 | | { |
| | 0 | 2528 | | throw new ArgumentException("The change index operation is only applicable to backdrops and screen shots |
| | | 2529 | | } |
| | | 2530 | | |
| | 0 | 2531 | | var info1 = GetImageInfo(type, index1); |
| | 0 | 2532 | | var info2 = GetImageInfo(type, index2); |
| | | 2533 | | |
| | 0 | 2534 | | if (info1 is null || info2 is null) |
| | | 2535 | | { |
| | | 2536 | | // Nothing to do |
| | 0 | 2537 | | return Task.CompletedTask; |
| | | 2538 | | } |
| | | 2539 | | |
| | 0 | 2540 | | if (!info1.IsLocalFile || !info2.IsLocalFile) |
| | | 2541 | | { |
| | | 2542 | | // TODO: Not supported yet |
| | 0 | 2543 | | return Task.CompletedTask; |
| | | 2544 | | } |
| | | 2545 | | |
| | 0 | 2546 | | var path1 = info1.Path; |
| | 0 | 2547 | | var path2 = info2.Path; |
| | | 2548 | | |
| | 0 | 2549 | | FileSystem.SwapFiles(path1, path2); |
| | | 2550 | | |
| | | 2551 | | // Refresh these values |
| | 0 | 2552 | | info1.DateModified = FileSystem.GetLastWriteTimeUtc(info1.Path); |
| | 0 | 2553 | | info2.DateModified = FileSystem.GetLastWriteTimeUtc(info2.Path); |
| | | 2554 | | |
| | 0 | 2555 | | info1.Width = 0; |
| | 0 | 2556 | | info1.Height = 0; |
| | 0 | 2557 | | info2.Width = 0; |
| | 0 | 2558 | | info2.Height = 0; |
| | | 2559 | | |
| | 0 | 2560 | | return UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None); |
| | | 2561 | | } |
| | | 2562 | | |
| | | 2563 | | public virtual bool IsPlayed(User user, UserItemData userItemData) |
| | | 2564 | | { |
| | 0 | 2565 | | userItemData ??= UserDataManager.GetUserData(user, this); |
| | | 2566 | | |
| | 0 | 2567 | | return userItemData is not null && userItemData.Played; |
| | | 2568 | | } |
| | | 2569 | | |
| | | 2570 | | public bool IsFavoriteOrLiked(User user, UserItemData userItemData) |
| | | 2571 | | { |
| | 0 | 2572 | | userItemData ??= UserDataManager.GetUserData(user, this); |
| | | 2573 | | |
| | 0 | 2574 | | return userItemData is not null && (userItemData.IsFavorite || (userItemData.Likes ?? false)); |
| | | 2575 | | } |
| | | 2576 | | |
| | | 2577 | | public virtual bool IsUnplayed(User user, UserItemData userItemData) |
| | | 2578 | | { |
| | 0 | 2579 | | ArgumentNullException.ThrowIfNull(user); |
| | | 2580 | | |
| | 0 | 2581 | | userItemData ??= UserDataManager.GetUserData(user, this); |
| | | 2582 | | |
| | 0 | 2583 | | return userItemData is null || !userItemData.Played; |
| | | 2584 | | } |
| | | 2585 | | |
| | | 2586 | | ItemLookupInfo IHasLookupInfo<ItemLookupInfo>.GetLookupInfo() |
| | | 2587 | | { |
| | 36 | 2588 | | return GetItemLookupInfo<ItemLookupInfo>(); |
| | | 2589 | | } |
| | | 2590 | | |
| | | 2591 | | protected T GetItemLookupInfo<T>() |
| | | 2592 | | where T : ItemLookupInfo, new() |
| | | 2593 | | { |
| | 36 | 2594 | | return new T |
| | 36 | 2595 | | { |
| | 36 | 2596 | | Path = Path, |
| | 36 | 2597 | | MetadataCountryCode = GetPreferredMetadataCountryCode(), |
| | 36 | 2598 | | MetadataLanguage = GetPreferredMetadataLanguage(), |
| | 36 | 2599 | | Name = GetNameForMetadataLookup(), |
| | 36 | 2600 | | OriginalTitle = OriginalTitle, |
| | 36 | 2601 | | ProviderIds = ProviderIds, |
| | 36 | 2602 | | IndexNumber = IndexNumber, |
| | 36 | 2603 | | ParentIndexNumber = ParentIndexNumber, |
| | 36 | 2604 | | Year = ProductionYear, |
| | 36 | 2605 | | PremiereDate = PremiereDate |
| | 36 | 2606 | | }; |
| | | 2607 | | } |
| | | 2608 | | |
| | | 2609 | | protected virtual string GetNameForMetadataLookup() |
| | | 2610 | | { |
| | 36 | 2611 | | return Name; |
| | | 2612 | | } |
| | | 2613 | | |
| | | 2614 | | /// <summary> |
| | | 2615 | | /// This is called before any metadata refresh and returns true if changes were made. |
| | | 2616 | | /// </summary> |
| | | 2617 | | /// <param name="replaceAllMetadata">Whether to replace all metadata.</param> |
| | | 2618 | | /// <returns>true if the item has change, else false.</returns> |
| | | 2619 | | public virtual bool BeforeMetadataRefresh(bool replaceAllMetadata) |
| | | 2620 | | { |
| | 36 | 2621 | | _sortName = null; |
| | | 2622 | | |
| | 36 | 2623 | | var hasChanges = false; |
| | | 2624 | | |
| | 36 | 2625 | | if (string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Path)) |
| | | 2626 | | { |
| | 0 | 2627 | | Name = System.IO.Path.GetFileNameWithoutExtension(Path); |
| | 0 | 2628 | | hasChanges = true; |
| | | 2629 | | } |
| | | 2630 | | |
| | 36 | 2631 | | return hasChanges; |
| | | 2632 | | } |
| | | 2633 | | |
| | | 2634 | | protected static string GetMappedPath(BaseItem item, string path, MediaProtocol? protocol) |
| | | 2635 | | { |
| | 0 | 2636 | | if (protocol == MediaProtocol.File) |
| | | 2637 | | { |
| | 0 | 2638 | | return LibraryManager.GetPathAfterNetworkSubstitution(path, item); |
| | | 2639 | | } |
| | | 2640 | | |
| | 0 | 2641 | | return path; |
| | | 2642 | | } |
| | | 2643 | | |
| | | 2644 | | public virtual void FillUserDataDtoValues( |
| | | 2645 | | UserItemDataDto dto, |
| | | 2646 | | UserItemData userData, |
| | | 2647 | | BaseItemDto itemDto, |
| | | 2648 | | User user, |
| | | 2649 | | DtoOptions fields, |
| | | 2650 | | (int Played, int Total)? precomputedCounts = null) |
| | | 2651 | | { |
| | 0 | 2652 | | if (RunTimeTicks.HasValue) |
| | | 2653 | | { |
| | 0 | 2654 | | double pct = RunTimeTicks.Value; |
| | | 2655 | | |
| | 0 | 2656 | | if (pct > 0) |
| | | 2657 | | { |
| | 0 | 2658 | | pct = userData.PlaybackPositionTicks / pct; |
| | | 2659 | | |
| | 0 | 2660 | | if (pct > 0) |
| | | 2661 | | { |
| | 0 | 2662 | | dto.PlayedPercentage = 100 * pct; |
| | | 2663 | | } |
| | | 2664 | | } |
| | | 2665 | | } |
| | 0 | 2666 | | } |
| | | 2667 | | |
| | | 2668 | | /// <summary> |
| | | 2669 | | /// Applies the owner's premiere date and production year to an owned item, returning whether anything changed. |
| | | 2670 | | /// </summary> |
| | | 2671 | | /// <param name="owner">The owner.</param> |
| | | 2672 | | /// <param name="ownedItem">The owned item.</param> |
| | | 2673 | | /// <returns><c>true</c> if the owned item was changed, else <c>false</c>.</returns> |
| | | 2674 | | internal static bool InheritDatesFromOwner(BaseItem owner, BaseItem ownedItem) |
| | | 2675 | | { |
| | | 2676 | | // Extras have no release date of their own, so the owner's is authoritative. |
| | 4 | 2677 | | var changed = false; |
| | | 2678 | | |
| | 4 | 2679 | | if (owner.ProductionYear is not null && ownedItem.ProductionYear != owner.ProductionYear) |
| | | 2680 | | { |
| | 2 | 2681 | | ownedItem.ProductionYear = owner.ProductionYear; |
| | 2 | 2682 | | changed = true; |
| | | 2683 | | } |
| | | 2684 | | |
| | 4 | 2685 | | if (owner.PremiereDate is not null && ownedItem.PremiereDate != owner.PremiereDate) |
| | | 2686 | | { |
| | 2 | 2687 | | ownedItem.PremiereDate = owner.PremiereDate; |
| | 2 | 2688 | | changed = true; |
| | | 2689 | | } |
| | | 2690 | | |
| | 4 | 2691 | | return changed; |
| | | 2692 | | } |
| | | 2693 | | |
| | | 2694 | | protected async Task RefreshMetadataForOwnedItem(BaseItem ownedItem, bool copyTitleMetadata, MetadataRefreshOpti |
| | | 2695 | | { |
| | 0 | 2696 | | var newOptions = new MetadataRefreshOptions(options) |
| | 0 | 2697 | | { |
| | 0 | 2698 | | SearchResult = null |
| | 0 | 2699 | | }; |
| | | 2700 | | |
| | 0 | 2701 | | var item = this; |
| | | 2702 | | |
| | 0 | 2703 | | if (copyTitleMetadata) |
| | | 2704 | | { |
| | | 2705 | | // Take some data from the main item, for querying purposes |
| | 0 | 2706 | | if (!item.Genres.SequenceEqual(ownedItem.Genres, StringComparer.Ordinal)) |
| | | 2707 | | { |
| | 0 | 2708 | | newOptions.ForceSave = true; |
| | 0 | 2709 | | ownedItem.Genres = item.Genres; |
| | | 2710 | | } |
| | | 2711 | | |
| | 0 | 2712 | | if (!item.Studios.SequenceEqual(ownedItem.Studios, StringComparer.Ordinal)) |
| | | 2713 | | { |
| | 0 | 2714 | | newOptions.ForceSave = true; |
| | 0 | 2715 | | ownedItem.Studios = item.Studios; |
| | | 2716 | | } |
| | | 2717 | | |
| | 0 | 2718 | | if (!item.ProductionLocations.SequenceEqual(ownedItem.ProductionLocations, StringComparer.Ordinal)) |
| | | 2719 | | { |
| | 0 | 2720 | | newOptions.ForceSave = true; |
| | 0 | 2721 | | ownedItem.ProductionLocations = item.ProductionLocations; |
| | | 2722 | | } |
| | | 2723 | | |
| | 0 | 2724 | | if (item.CommunityRating != ownedItem.CommunityRating) |
| | | 2725 | | { |
| | 0 | 2726 | | ownedItem.CommunityRating = item.CommunityRating; |
| | 0 | 2727 | | newOptions.ForceSave = true; |
| | | 2728 | | } |
| | | 2729 | | |
| | 0 | 2730 | | if (item.CriticRating != ownedItem.CriticRating) |
| | | 2731 | | { |
| | 0 | 2732 | | ownedItem.CriticRating = item.CriticRating; |
| | 0 | 2733 | | newOptions.ForceSave = true; |
| | | 2734 | | } |
| | | 2735 | | |
| | 0 | 2736 | | if (!string.Equals(item.Overview, ownedItem.Overview, StringComparison.Ordinal)) |
| | | 2737 | | { |
| | 0 | 2738 | | ownedItem.Overview = item.Overview; |
| | 0 | 2739 | | newOptions.ForceSave = true; |
| | | 2740 | | } |
| | | 2741 | | |
| | 0 | 2742 | | if (!string.Equals(item.OfficialRating, ownedItem.OfficialRating, StringComparison.Ordinal)) |
| | | 2743 | | { |
| | 0 | 2744 | | ownedItem.OfficialRating = item.OfficialRating; |
| | 0 | 2745 | | newOptions.ForceSave = true; |
| | | 2746 | | } |
| | | 2747 | | |
| | 0 | 2748 | | if (!string.Equals(item.CustomRating, ownedItem.CustomRating, StringComparison.Ordinal)) |
| | | 2749 | | { |
| | 0 | 2750 | | ownedItem.CustomRating = item.CustomRating; |
| | 0 | 2751 | | newOptions.ForceSave = true; |
| | | 2752 | | } |
| | | 2753 | | |
| | 0 | 2754 | | if (InheritDatesFromOwner(item, ownedItem)) |
| | | 2755 | | { |
| | 0 | 2756 | | newOptions.ForceSave = true; |
| | | 2757 | | } |
| | | 2758 | | } |
| | | 2759 | | |
| | 0 | 2760 | | await ownedItem.RefreshMetadata(newOptions, cancellationToken).ConfigureAwait(false); |
| | 0 | 2761 | | } |
| | | 2762 | | |
| | | 2763 | | protected async Task RefreshMetadataForOwnedVideo(MetadataRefreshOptions options, bool copyTitleMetadata, string |
| | | 2764 | | { |
| | 0 | 2765 | | var newOptions = new MetadataRefreshOptions(options) |
| | 0 | 2766 | | { |
| | 0 | 2767 | | SearchResult = null |
| | 0 | 2768 | | }; |
| | | 2769 | | |
| | 0 | 2770 | | var id = LibraryManager.GetNewItemId(path, typeof(Video)); |
| | | 2771 | | |
| | | 2772 | | // Try to retrieve it from the db. If we don't find it, use the resolved version |
| | 0 | 2773 | | if (LibraryManager.GetItemById(id) is not Video video) |
| | | 2774 | | { |
| | 0 | 2775 | | video = LibraryManager.ResolvePath(FileSystem.GetFileSystemInfo(path)) as Video; |
| | | 2776 | | |
| | 0 | 2777 | | newOptions.ForceSave = true; |
| | | 2778 | | } |
| | | 2779 | | |
| | 0 | 2780 | | if (video is null) |
| | | 2781 | | { |
| | 0 | 2782 | | return; |
| | | 2783 | | } |
| | | 2784 | | |
| | 0 | 2785 | | if (video.OwnerId.IsEmpty()) |
| | | 2786 | | { |
| | 0 | 2787 | | video.OwnerId = Id; |
| | | 2788 | | } |
| | | 2789 | | |
| | 0 | 2790 | | await RefreshMetadataForOwnedItem(video, copyTitleMetadata, newOptions, cancellationToken).ConfigureAwait(fa |
| | 0 | 2791 | | } |
| | | 2792 | | |
| | | 2793 | | public string GetEtag(User user) |
| | | 2794 | | { |
| | 6 | 2795 | | var list = GetEtagValues(user); |
| | | 2796 | | |
| | 6 | 2797 | | return string.Join('|', list).GetMD5().ToString("N", CultureInfo.InvariantCulture); |
| | | 2798 | | } |
| | | 2799 | | |
| | | 2800 | | protected virtual List<string> GetEtagValues(User user) |
| | | 2801 | | { |
| | 6 | 2802 | | return |
| | 6 | 2803 | | [ |
| | 6 | 2804 | | DateLastSaved.Ticks.ToString(CultureInfo.InvariantCulture) |
| | 6 | 2805 | | ]; |
| | | 2806 | | } |
| | | 2807 | | |
| | | 2808 | | public virtual IEnumerable<Guid> GetAncestorIds() |
| | | 2809 | | { |
| | 117 | 2810 | | return GetParents().Select(i => i.Id).Concat(LibraryManager.GetCollectionFolders(this).Select(i => i.Id)); |
| | | 2811 | | } |
| | | 2812 | | |
| | | 2813 | | public BaseItem GetTopParent() |
| | | 2814 | | { |
| | 201 | 2815 | | if (IsTopParent) |
| | | 2816 | | { |
| | 22 | 2817 | | return this; |
| | | 2818 | | } |
| | | 2819 | | |
| | 179 | 2820 | | return GetParents().FirstOrDefault(parent => parent.IsTopParent); |
| | | 2821 | | } |
| | | 2822 | | |
| | | 2823 | | public virtual IEnumerable<Guid> GetIdsForAncestorQuery() |
| | | 2824 | | { |
| | 45 | 2825 | | return [Id]; |
| | | 2826 | | } |
| | | 2827 | | |
| | | 2828 | | public virtual double? GetRefreshProgress() |
| | | 2829 | | { |
| | 0 | 2830 | | return null; |
| | | 2831 | | } |
| | | 2832 | | |
| | | 2833 | | public virtual ItemUpdateType OnMetadataChanged() |
| | | 2834 | | { |
| | 122 | 2835 | | var updateType = ItemUpdateType.None; |
| | | 2836 | | |
| | 122 | 2837 | | var item = this; |
| | | 2838 | | |
| | 122 | 2839 | | var rating = item.GetParentalRatingScore(); |
| | 122 | 2840 | | if (rating is not null) |
| | | 2841 | | { |
| | 0 | 2842 | | if (rating.Score != item.InheritedParentalRatingValue) |
| | | 2843 | | { |
| | 0 | 2844 | | item.InheritedParentalRatingValue = rating.Score; |
| | 0 | 2845 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 2846 | | } |
| | | 2847 | | |
| | 0 | 2848 | | if (rating.SubScore != item.InheritedParentalRatingSubValue) |
| | | 2849 | | { |
| | 0 | 2850 | | item.InheritedParentalRatingSubValue = rating.SubScore; |
| | 0 | 2851 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 2852 | | } |
| | | 2853 | | } |
| | | 2854 | | else |
| | | 2855 | | { |
| | 122 | 2856 | | if (item.InheritedParentalRatingValue is not null) |
| | | 2857 | | { |
| | 0 | 2858 | | item.InheritedParentalRatingValue = null; |
| | 0 | 2859 | | item.InheritedParentalRatingSubValue = null; |
| | 0 | 2860 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 2861 | | } |
| | | 2862 | | } |
| | | 2863 | | |
| | 122 | 2864 | | return updateType; |
| | | 2865 | | } |
| | | 2866 | | |
| | | 2867 | | /// <summary> |
| | | 2868 | | /// Updates the official rating based on content and returns true or false indicating if it changed. |
| | | 2869 | | /// </summary> |
| | | 2870 | | /// <param name="children">Media children.</param> |
| | | 2871 | | /// <returns><c>true</c> if the rating was updated; otherwise <c>false</c>.</returns> |
| | | 2872 | | public bool UpdateRatingToItems(IReadOnlyList<BaseItem> children) |
| | | 2873 | | { |
| | 0 | 2874 | | var currentOfficialRating = OfficialRating; |
| | | 2875 | | |
| | | 2876 | | // Gather all possible ratings |
| | 0 | 2877 | | var ratings = children |
| | 0 | 2878 | | .Select(i => i.OfficialRating) |
| | 0 | 2879 | | .Where(i => !string.IsNullOrEmpty(i)) |
| | 0 | 2880 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 2881 | | .Select(rating => (rating, LocalizationManager.GetRatingScore(rating, GetPreferredMetadataCountryCode()) |
| | 0 | 2882 | | .OrderBy(i => i.Item2 is null ? 1001 : i.Item2.Score) |
| | 0 | 2883 | | .ThenBy(i => i.Item2 is null ? 1001 : i.Item2.SubScore) |
| | 0 | 2884 | | .Select(i => i.rating); |
| | | 2885 | | |
| | 0 | 2886 | | OfficialRating = ratings.FirstOrDefault() ?? currentOfficialRating; |
| | | 2887 | | |
| | 0 | 2888 | | return !string.Equals( |
| | 0 | 2889 | | currentOfficialRating ?? string.Empty, |
| | 0 | 2890 | | OfficialRating ?? string.Empty, |
| | 0 | 2891 | | StringComparison.OrdinalIgnoreCase); |
| | | 2892 | | } |
| | | 2893 | | |
| | | 2894 | | public IReadOnlyList<BaseItem> GetThemeSongs(User user = null) |
| | | 2895 | | { |
| | 0 | 2896 | | return GetThemeSongs(user, Array.Empty<(ItemSortBy, SortOrder)>()); |
| | | 2897 | | } |
| | | 2898 | | |
| | | 2899 | | public IReadOnlyList<BaseItem> GetThemeSongs(User user, IEnumerable<(ItemSortBy SortBy, SortOrder SortOrder)> or |
| | | 2900 | | { |
| | 0 | 2901 | | return LibraryManager.Sort(GetExtras(user).Where(e => e.ExtraType == Model.Entities.ExtraType.ThemeSong), us |
| | | 2902 | | } |
| | | 2903 | | |
| | | 2904 | | public IReadOnlyList<BaseItem> GetThemeVideos(User user = null) |
| | | 2905 | | { |
| | 0 | 2906 | | return GetThemeVideos(user, Array.Empty<(ItemSortBy, SortOrder)>()); |
| | | 2907 | | } |
| | | 2908 | | |
| | | 2909 | | public IReadOnlyList<BaseItem> GetThemeVideos(User user, IEnumerable<(ItemSortBy SortBy, SortOrder SortOrder)> o |
| | | 2910 | | { |
| | 0 | 2911 | | return LibraryManager.Sort(GetExtras(user).Where(e => e.ExtraType == Model.Entities.ExtraType.ThemeVideo), u |
| | | 2912 | | } |
| | | 2913 | | |
| | | 2914 | | /// <summary> |
| | | 2915 | | /// Gets the ids of the items whose owned extras belong to this item. |
| | | 2916 | | /// </summary> |
| | | 2917 | | /// <returns>An array containing the owner ids.</returns> |
| | | 2918 | | protected virtual Guid[] GetExtraOwnerIds() |
| | | 2919 | | { |
| | 6 | 2920 | | return [Id]; |
| | | 2921 | | } |
| | | 2922 | | |
| | | 2923 | | /// <summary> |
| | | 2924 | | /// Get all extras associated with this item, sorted by <see cref="SortName"/>. |
| | | 2925 | | /// </summary> |
| | | 2926 | | /// <param name="user">The user to apply parental restrictions for, or <c>null</c> to skip restriction checks.</ |
| | | 2927 | | /// <returns>An enumerable containing the items.</returns> |
| | | 2928 | | public IEnumerable<BaseItem> GetExtras(User user = null) |
| | | 2929 | | { |
| | 6 | 2930 | | return LibraryManager.GetItemList(new InternalItemsQuery(user) |
| | 6 | 2931 | | { |
| | 6 | 2932 | | OwnerIds = GetExtraOwnerIds(), |
| | 6 | 2933 | | OrderBy = [(ItemSortBy.SortName, SortOrder.Ascending)] |
| | 6 | 2934 | | }); |
| | | 2935 | | } |
| | | 2936 | | |
| | | 2937 | | /// <summary> |
| | | 2938 | | /// Get all extras with specific types that are associated with this item. |
| | | 2939 | | /// </summary> |
| | | 2940 | | /// <param name="extraTypes">The types of extras to retrieve.</param> |
| | | 2941 | | /// <param name="user">The user to apply parental restrictions for, or <c>null</c> to skip restriction checks.</ |
| | | 2942 | | /// <returns>An enumerable containing the extras.</returns> |
| | | 2943 | | public IEnumerable<BaseItem> GetExtras(IReadOnlyCollection<ExtraType> extraTypes, User user = null) |
| | | 2944 | | { |
| | 0 | 2945 | | return LibraryManager.GetItemList(new InternalItemsQuery(user) |
| | 0 | 2946 | | { |
| | 0 | 2947 | | OwnerIds = GetExtraOwnerIds(), |
| | 0 | 2948 | | ExtraTypes = extraTypes.ToArray(), |
| | 0 | 2949 | | OrderBy = [(ItemSortBy.SortName, SortOrder.Ascending)] |
| | 0 | 2950 | | }); |
| | | 2951 | | } |
| | | 2952 | | |
| | | 2953 | | public virtual long GetRunTimeTicksForPlayState() |
| | | 2954 | | { |
| | 0 | 2955 | | return RunTimeTicks ?? 0; |
| | | 2956 | | } |
| | | 2957 | | |
| | | 2958 | | /// <inheritdoc /> |
| | | 2959 | | public override bool Equals(object obj) |
| | | 2960 | | { |
| | 98 | 2961 | | return obj is BaseItem baseItem && this.Equals(baseItem); |
| | | 2962 | | } |
| | | 2963 | | |
| | | 2964 | | /// <inheritdoc /> |
| | 257 | 2965 | | public bool Equals(BaseItem other) => other is not null && other.Id.Equals(Id); |
| | | 2966 | | |
| | | 2967 | | /// <inheritdoc /> |
| | 102 | 2968 | | public override int GetHashCode() => HashCode.Combine(Id); |
| | | 2969 | | } |
| | | 2970 | | } |