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