| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Text.Json.Serialization; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using Jellyfin.Data.Enums; |
| | | 13 | | using Jellyfin.Database.Implementations.Entities; |
| | | 14 | | using Jellyfin.Extensions; |
| | | 15 | | using MediaBrowser.Controller.Library; |
| | | 16 | | using MediaBrowser.Controller.LiveTv; |
| | | 17 | | using MediaBrowser.Controller.Persistence; |
| | | 18 | | using MediaBrowser.Controller.Providers; |
| | | 19 | | using MediaBrowser.Model.Dto; |
| | | 20 | | using MediaBrowser.Model.Entities; |
| | | 21 | | using MediaBrowser.Model.IO; |
| | | 22 | | using MediaBrowser.Model.MediaInfo; |
| | | 23 | | using Microsoft.Extensions.Logging; |
| | | 24 | | |
| | | 25 | | namespace MediaBrowser.Controller.Entities |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Class Video. |
| | | 29 | | /// </summary> |
| | | 30 | | public class Video : BaseItem, |
| | | 31 | | IHasAspectRatio, |
| | | 32 | | ISupportsPlaceHolders, |
| | | 33 | | IHasMediaSources |
| | | 34 | | { |
| | 551 | 35 | | public Video() |
| | | 36 | | { |
| | 551 | 37 | | AdditionalParts = []; |
| | 551 | 38 | | LocalAlternateVersions = []; |
| | 551 | 39 | | SubtitleFiles = []; |
| | 551 | 40 | | AudioFiles = []; |
| | 551 | 41 | | LinkedAlternateVersions = []; |
| | 551 | 42 | | } |
| | | 43 | | |
| | | 44 | | [JsonIgnore] |
| | | 45 | | public Guid? PrimaryVersionId { get; set; } |
| | | 46 | | |
| | | 47 | | public string[] AdditionalParts { get; set; } |
| | | 48 | | |
| | | 49 | | public string[] LocalAlternateVersions { get; set; } |
| | | 50 | | |
| | | 51 | | public LinkedChild[] LinkedAlternateVersions { get; set; } |
| | | 52 | | |
| | | 53 | | [JsonIgnore] |
| | 0 | 54 | | public override bool SupportsPlayedStatus => true; |
| | | 55 | | |
| | | 56 | | [JsonIgnore] |
| | 0 | 57 | | public override bool SupportsPeople => true; |
| | | 58 | | |
| | | 59 | | [JsonIgnore] |
| | 0 | 60 | | public override bool SupportsInheritedParentImages => true; |
| | | 61 | | |
| | | 62 | | [JsonIgnore] |
| | | 63 | | public override bool SupportsPositionTicksResume |
| | | 64 | | { |
| | | 65 | | get |
| | | 66 | | { |
| | 0 | 67 | | var extraType = ExtraType; |
| | 0 | 68 | | if (extraType.HasValue) |
| | | 69 | | { |
| | 0 | 70 | | if (extraType.Value == Model.Entities.ExtraType.Sample) |
| | | 71 | | { |
| | 0 | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | if (extraType.Value == Model.Entities.ExtraType.ThemeVideo) |
| | | 76 | | { |
| | 0 | 77 | | return false; |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | if (extraType.Value == Model.Entities.ExtraType.Trailer) |
| | | 81 | | { |
| | 0 | 82 | | return false; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | return true; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | [JsonIgnore] |
| | 0 | 91 | | public override bool SupportsThemeMedia => true; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets or sets the timestamp. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <value>The timestamp.</value> |
| | | 97 | | public TransportStreamTimestamp? Timestamp { get; set; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets or sets the subtitle paths. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <value>The subtitle paths.</value> |
| | | 103 | | public string[] SubtitleFiles { get; set; } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets or sets the audio paths. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <value>The audio paths.</value> |
| | | 109 | | public string[] AudioFiles { get; set; } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets or sets a value indicating whether this instance has subtitles. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value> |
| | | 115 | | public bool HasSubtitles { get; set; } |
| | | 116 | | |
| | | 117 | | public bool IsPlaceHolder { get; set; } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Gets or sets the default index of the video stream. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <value>The default index of the video stream.</value> |
| | | 123 | | public int? DefaultVideoStreamIndex { get; set; } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Gets or sets the type of the video. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <value>The type of the video.</value> |
| | | 129 | | public VideoType VideoType { get; set; } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Gets or sets the type of the iso. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <value>The type of the iso.</value> |
| | | 135 | | public IsoType? IsoType { get; set; } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Gets or sets the video3 D format. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <value>The video3 D format.</value> |
| | | 141 | | public Video3DFormat? Video3DFormat { get; set; } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets or sets the aspect ratio. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <value>The aspect ratio.</value> |
| | | 147 | | public string AspectRatio { get; set; } |
| | | 148 | | |
| | | 149 | | [JsonIgnore] |
| | 0 | 150 | | public override bool SupportsAddingToPlaylist => true; |
| | | 151 | | |
| | | 152 | | [JsonIgnore] |
| | | 153 | | public int MediaSourceCount |
| | | 154 | | { |
| | | 155 | | get |
| | | 156 | | { |
| | 0 | 157 | | return GetMediaSourceCount(); |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | [JsonIgnore] |
| | 59 | 162 | | public bool IsStacked => AdditionalParts.Length > 0; |
| | | 163 | | |
| | | 164 | | [JsonIgnore] |
| | 15 | 165 | | public override bool HasLocalAlternateVersions => LibraryManager.GetLocalAlternateVersionIds(this).Any(); |
| | | 166 | | |
| | | 167 | | public static IRecordingsManager RecordingsManager { get; set; } |
| | | 168 | | |
| | | 169 | | [JsonIgnore] |
| | | 170 | | public override SourceType SourceType |
| | | 171 | | { |
| | | 172 | | get |
| | | 173 | | { |
| | 45 | 174 | | if (IsActiveRecording()) |
| | | 175 | | { |
| | 0 | 176 | | return SourceType.LiveTV; |
| | | 177 | | } |
| | | 178 | | |
| | 45 | 179 | | return base.SourceType; |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | [JsonIgnore] |
| | | 184 | | public bool IsCompleteMedia |
| | | 185 | | { |
| | | 186 | | get |
| | | 187 | | { |
| | 0 | 188 | | if (SourceType == SourceType.Channel) |
| | | 189 | | { |
| | 0 | 190 | | return !Tags.Contains("livestream", StringComparison.OrdinalIgnoreCase); |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | return !IsActiveRecording(); |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | [JsonIgnore] |
| | 0 | 198 | | protected virtual bool EnableDefaultVideoUserDataKeys => true; |
| | | 199 | | |
| | | 200 | | [JsonIgnore] |
| | | 201 | | public override string ContainingFolderPath |
| | | 202 | | { |
| | | 203 | | get |
| | | 204 | | { |
| | 59 | 205 | | if (IsStacked) |
| | | 206 | | { |
| | 0 | 207 | | return System.IO.Path.GetDirectoryName(Path); |
| | | 208 | | } |
| | | 209 | | |
| | 59 | 210 | | if (!IsPlaceHolder) |
| | | 211 | | { |
| | 59 | 212 | | if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd) |
| | | 213 | | { |
| | 1 | 214 | | return Path; |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | |
| | 58 | 218 | | return base.ContainingFolderPath; |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | [JsonIgnore] |
| | | 223 | | public override string FileNameWithoutExtension |
| | | 224 | | { |
| | | 225 | | get |
| | | 226 | | { |
| | 31 | 227 | | if (IsFileProtocol) |
| | | 228 | | { |
| | 31 | 229 | | if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd) |
| | | 230 | | { |
| | 0 | 231 | | return System.IO.Path.GetFileName(Path); |
| | | 232 | | } |
| | | 233 | | |
| | 31 | 234 | | return System.IO.Path.GetFileNameWithoutExtension(Path); |
| | | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | return null; |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Gets a value indicating whether [is3 D]. |
| | | 243 | | /// </summary> |
| | | 244 | | /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value> |
| | | 245 | | [JsonIgnore] |
| | 0 | 246 | | public bool Is3D => Video3DFormat.HasValue; |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Gets the type of the media. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <value>The type of the media.</value> |
| | | 252 | | [JsonIgnore] |
| | 52 | 253 | | public override MediaType MediaType => MediaType.Video; |
| | | 254 | | |
| | | 255 | | private int GetMediaSourceCount(HashSet<Guid> callstack = null) |
| | | 256 | | { |
| | 0 | 257 | | callstack ??= []; |
| | 0 | 258 | | if (PrimaryVersionId.HasValue) |
| | | 259 | | { |
| | 0 | 260 | | var item = LibraryManager.GetItemById(PrimaryVersionId.Value); |
| | 0 | 261 | | if (item is Video video) |
| | | 262 | | { |
| | 0 | 263 | | if (callstack.Contains(video.Id)) |
| | | 264 | | { |
| | | 265 | | // Count alternate versions using LibraryManager |
| | 0 | 266 | | var linkedCount = LibraryManager.GetLinkedAlternateVersions(video).Count(); |
| | 0 | 267 | | var localCount = LibraryManager.GetLocalAlternateVersionIds(video).Count(); |
| | 0 | 268 | | return linkedCount + localCount + 1; |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | callstack.Add(video.Id); |
| | 0 | 272 | | return video.GetMediaSourceCount(callstack); |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | // Count alternate versions using LibraryManager |
| | 0 | 277 | | var linkedVersionCount = LibraryManager.GetLinkedAlternateVersions(this).Count(); |
| | 0 | 278 | | var localVersionCount = LibraryManager.GetLocalAlternateVersionIds(this).Count(); |
| | 0 | 279 | | return linkedVersionCount + localVersionCount + 1; |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | /// <inheritdoc /> |
| | | 283 | | public override string GetInheritedOriginalLanguage() |
| | | 284 | | { |
| | 31 | 285 | | if (ExtraType.GetValueOrDefault() == Model.Entities.ExtraType.Trailer) |
| | | 286 | | { |
| | 0 | 287 | | return GetOwner()?.GetInheritedOriginalLanguage(); |
| | | 288 | | } |
| | | 289 | | |
| | 31 | 290 | | return OriginalLanguage ?? GetOwner()?.GetInheritedOriginalLanguage(); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | public override List<string> GetUserDataKeys() |
| | | 294 | | { |
| | 0 | 295 | | var list = base.GetUserDataKeys(); |
| | | 296 | | |
| | 0 | 297 | | if (EnableDefaultVideoUserDataKeys) |
| | | 298 | | { |
| | 0 | 299 | | if (ExtraType.HasValue) |
| | | 300 | | { |
| | 0 | 301 | | var key = this.GetProviderId(MetadataProvider.Tmdb); |
| | 0 | 302 | | if (!string.IsNullOrEmpty(key)) |
| | | 303 | | { |
| | 0 | 304 | | list.Insert(0, GetUserDataKey(key)); |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | key = this.GetProviderId(MetadataProvider.Imdb); |
| | 0 | 308 | | if (!string.IsNullOrEmpty(key)) |
| | | 309 | | { |
| | 0 | 310 | | list.Insert(0, GetUserDataKey(key)); |
| | | 311 | | } |
| | | 312 | | } |
| | | 313 | | else |
| | | 314 | | { |
| | 0 | 315 | | var key = this.GetProviderId(MetadataProvider.Imdb); |
| | 0 | 316 | | if (!string.IsNullOrEmpty(key)) |
| | | 317 | | { |
| | 0 | 318 | | list.Insert(0, key); |
| | | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | key = this.GetProviderId(MetadataProvider.Tmdb); |
| | 0 | 322 | | if (!string.IsNullOrEmpty(key)) |
| | | 323 | | { |
| | 0 | 324 | | list.Insert(0, key); |
| | | 325 | | } |
| | | 326 | | } |
| | | 327 | | } |
| | | 328 | | |
| | 0 | 329 | | return list; |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | public void SetPrimaryVersionId(Guid? id) |
| | | 333 | | { |
| | 0 | 334 | | PrimaryVersionId = id; |
| | 0 | 335 | | PresentationUniqueKey = CreatePresentationUniqueKey(); |
| | 0 | 336 | | } |
| | | 337 | | |
| | | 338 | | /// <summary> |
| | | 339 | | /// Marks the played status of this video and propagates it to its alternate versions. |
| | | 340 | | /// </summary> |
| | | 341 | | /// <param name="user">The user.</param> |
| | | 342 | | /// <param name="datePlayed">The date played.</param> |
| | | 343 | | /// <param name="resetPosition">if set to <c>true</c> [reset position].</param> |
| | | 344 | | public override void MarkPlayed(User user, DateTime? datePlayed, bool resetPosition) |
| | | 345 | | { |
| | 0 | 346 | | base.MarkPlayed(user, datePlayed, resetPosition); |
| | 0 | 347 | | PropagatePlayedState(user, true, resetPosition); |
| | 0 | 348 | | } |
| | | 349 | | |
| | | 350 | | /// <summary> |
| | | 351 | | /// Marks this video unplayed and propagates the change to its alternate versions. |
| | | 352 | | /// </summary> |
| | | 353 | | /// <param name="user">The user.</param> |
| | | 354 | | public override void MarkUnplayed(User user) |
| | | 355 | | { |
| | 0 | 356 | | base.MarkUnplayed(user); |
| | | 357 | | |
| | | 358 | | // MarkUnplayed always clears the position on this video, so reset the versions too. |
| | 0 | 359 | | PropagatePlayedState(user, false, true); |
| | 0 | 360 | | } |
| | | 361 | | |
| | | 362 | | /// <summary> |
| | | 363 | | /// Propagates the played status to every alternate version of this video. |
| | | 364 | | /// </summary> |
| | | 365 | | /// <param name="user">The user.</param> |
| | | 366 | | /// <param name="played">The played status to apply to the alternate versions.</param> |
| | | 367 | | /// <param name="resetPosition">When marking played, controls whether each version's resume point |
| | | 368 | | /// is also reset (<c>true</c>) or left untouched (<c>false</c>). Ignored when marking unplayed, |
| | | 369 | | /// which always fully resets every version.</param> |
| | | 370 | | public void PropagatePlayedState(User user, bool played, bool resetPosition = true) |
| | | 371 | | { |
| | 4 | 372 | | ArgumentNullException.ThrowIfNull(user); |
| | | 373 | | |
| | 4 | 374 | | if (!PrimaryVersionId.HasValue && LinkedAlternateVersions.Length == 0 && !HasLocalAlternateVersions) |
| | | 375 | | { |
| | 1 | 376 | | return; |
| | | 377 | | } |
| | | 378 | | |
| | 24 | 379 | | foreach (var (item, _) in GetAllItemsForMediaSources()) |
| | | 380 | | { |
| | 9 | 381 | | if (item.Id.Equals(Id) || item is not Video) |
| | | 382 | | { |
| | | 383 | | continue; |
| | | 384 | | } |
| | | 385 | | |
| | 6 | 386 | | if (played) |
| | | 387 | | { |
| | 4 | 388 | | var dto = new UpdateUserItemDataDto { Played = true }; |
| | 4 | 389 | | if (resetPosition) |
| | | 390 | | { |
| | 2 | 391 | | dto.PlaybackPositionTicks = 0; |
| | | 392 | | } |
| | | 393 | | |
| | | 394 | | // SaveUserData only writes the fields set on the DTO, so play count and other state are preserved. |
| | 4 | 395 | | UserDataManager.SaveUserData(user, item, dto, UserDataSaveReason.TogglePlayed); |
| | | 396 | | } |
| | | 397 | | else |
| | | 398 | | { |
| | 2 | 399 | | var data = UserDataManager.GetUserData(user, item); |
| | 2 | 400 | | if (data is null) |
| | | 401 | | { |
| | | 402 | | continue; |
| | | 403 | | } |
| | | 404 | | |
| | 2 | 405 | | ResetPlayedState(data); |
| | 2 | 406 | | UserDataManager.SaveUserData(user, item, data, UserDataSaveReason.TogglePlayed, CancellationToken.No |
| | | 407 | | } |
| | | 408 | | } |
| | 3 | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Gets this video together with all of its alternate versions (local and linked and, when this |
| | | 413 | | /// is itself an alternate, the primary and the primary's other versions), deduplicated. |
| | | 414 | | /// </summary> |
| | | 415 | | /// <returns>This video and every alternate version of it.</returns> |
| | | 416 | | public IReadOnlyList<Video> GetAllVersions() |
| | | 417 | | { |
| | 10 | 418 | | return GetAllItemsForMediaSources() |
| | 10 | 419 | | .Select(i => i.Item) |
| | 10 | 420 | | .OfType<Video>() |
| | 10 | 421 | | .ToList(); |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Gets the alternate version of this video that matches the supplied item id. |
| | | 426 | | /// </summary> |
| | | 427 | | /// <param name="itemId">The version item id (the playback media source id).</param> |
| | | 428 | | /// <returns>The matching version, or <c>null</c> when the id is not a version of this video.</returns> |
| | | 429 | | public Video GetAlternateVersion(Guid itemId) |
| | | 430 | | { |
| | 4 | 431 | | return GetAllVersions().FirstOrDefault(i => i.Id.Equals(itemId)); |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | public override string CreatePresentationUniqueKey() |
| | | 435 | | { |
| | 0 | 436 | | if (PrimaryVersionId.HasValue) |
| | | 437 | | { |
| | 0 | 438 | | return PrimaryVersionId.Value.ToString("N", CultureInfo.InvariantCulture); |
| | | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | return base.CreatePresentationUniqueKey(); |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | public override bool CanDownload() |
| | | 445 | | { |
| | 0 | 446 | | if (VideoType == VideoType.Dvd || VideoType == VideoType.BluRay) |
| | | 447 | | { |
| | 0 | 448 | | return false; |
| | | 449 | | } |
| | | 450 | | |
| | 0 | 451 | | return IsFileProtocol; |
| | | 452 | | } |
| | | 453 | | |
| | | 454 | | protected override bool IsActiveRecording() |
| | | 455 | | { |
| | 51 | 456 | | return RecordingsManager.GetActiveRecordingInfo(Path) is not null; |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | public override bool CanDelete() |
| | | 460 | | { |
| | 0 | 461 | | if (IsActiveRecording()) |
| | | 462 | | { |
| | 0 | 463 | | return false; |
| | | 464 | | } |
| | | 465 | | |
| | 0 | 466 | | return base.CanDelete(); |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | public IEnumerable<Guid> GetAdditionalPartIds() |
| | | 470 | | { |
| | 0 | 471 | | return AdditionalParts.Select(i => LibraryManager.GetNewItemId(i, typeof(Video))); |
| | | 472 | | } |
| | | 473 | | |
| | | 474 | | private string GetUserDataKey(string providerId) |
| | | 475 | | { |
| | 0 | 476 | | var key = providerId + "-" + ExtraType.ToString().ToLowerInvariant(); |
| | | 477 | | |
| | | 478 | | // Make sure different trailers have their own data. |
| | 0 | 479 | | if (RunTimeTicks.HasValue) |
| | | 480 | | { |
| | 0 | 481 | | key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture); |
| | | 482 | | } |
| | | 483 | | |
| | 0 | 484 | | return key; |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | /// <summary> |
| | | 488 | | /// Gets the additional parts. |
| | | 489 | | /// </summary> |
| | | 490 | | /// <param name="user">The user to apply parental restrictions for, or <c>null</c> to skip restriction checks.</ |
| | | 491 | | /// <returns>IEnumerable{Video}.</returns> |
| | | 492 | | public IOrderedEnumerable<Video> GetAdditionalParts(User user = null) |
| | | 493 | | { |
| | 0 | 494 | | return GetAdditionalPartIds() |
| | 0 | 495 | | .Select(i => LibraryManager.GetItemById<Video>(i)) |
| | 0 | 496 | | .Where(i => i is not null && (user is null || i.IsVisible(user))) |
| | 0 | 497 | | .OrderBy(i => i.SortName); |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | internal override ItemUpdateType UpdateFromResolvedItem(BaseItem newItem) |
| | | 501 | | { |
| | 0 | 502 | | var updateType = base.UpdateFromResolvedItem(newItem); |
| | | 503 | | |
| | 0 | 504 | | if (newItem is Video newVideo) |
| | | 505 | | { |
| | 0 | 506 | | if (!AdditionalParts.SequenceEqual(newVideo.AdditionalParts, StringComparer.Ordinal)) |
| | | 507 | | { |
| | 0 | 508 | | AdditionalParts = newVideo.AdditionalParts; |
| | 0 | 509 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 510 | | } |
| | | 511 | | |
| | 0 | 512 | | if (!LocalAlternateVersions.SequenceEqual(newVideo.LocalAlternateVersions, StringComparer.Ordinal)) |
| | | 513 | | { |
| | 0 | 514 | | LocalAlternateVersions = newVideo.LocalAlternateVersions; |
| | 0 | 515 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 516 | | } |
| | | 517 | | |
| | 0 | 518 | | if (VideoType != newVideo.VideoType) |
| | | 519 | | { |
| | 0 | 520 | | VideoType = newVideo.VideoType; |
| | 0 | 521 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 522 | | } |
| | | 523 | | } |
| | | 524 | | |
| | 0 | 525 | | return updateType; |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystem |
| | | 529 | | { |
| | 0 | 530 | | var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwa |
| | | 531 | | |
| | | 532 | | // Clean up LocalAlternateVersions - remove paths that no longer exist |
| | 0 | 533 | | if (LocalAlternateVersions.Length > 0) |
| | | 534 | | { |
| | 0 | 535 | | var validPaths = LocalAlternateVersions.Where(FileSystem.FileExists).ToArray(); |
| | 0 | 536 | | if (validPaths.Length != LocalAlternateVersions.Length) |
| | | 537 | | { |
| | 0 | 538 | | LocalAlternateVersions = validPaths; |
| | 0 | 539 | | hasChanges = true; |
| | | 540 | | } |
| | | 541 | | } |
| | | 542 | | |
| | 0 | 543 | | if (IsStacked) |
| | | 544 | | { |
| | 0 | 545 | | var tasks = AdditionalParts |
| | 0 | 546 | | .Select(i => RefreshMetadataForOwnedVideo(options, true, i, typeof(Video), cancellationToken)); |
| | | 547 | | |
| | 0 | 548 | | await Task.WhenAll(tasks).ConfigureAwait(false); |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | // Must have a parent to have additional parts or alternate versions |
| | | 552 | | // In other words, it must be part of the Parent/Child tree |
| | | 553 | | // The additional parts won't have additional parts themselves |
| | 0 | 554 | | if (IsFileProtocol && SupportsOwnedItems) |
| | | 555 | | { |
| | | 556 | | // Check if LinkedChildren are in sync before processing |
| | 0 | 557 | | var existingVersionCount = LibraryManager.GetLocalAlternateVersionIds(this).Count(); |
| | 0 | 558 | | var tasks = LocalAlternateVersions |
| | 0 | 559 | | .Select(i => RefreshMetadataForVersions(options, false, i, cancellationToken)); |
| | | 560 | | |
| | 0 | 561 | | await Task.WhenAll(tasks).ConfigureAwait(false); |
| | | 562 | | |
| | 0 | 563 | | if (existingVersionCount != LocalAlternateVersions.Length) |
| | | 564 | | { |
| | 0 | 565 | | hasChanges = true; |
| | | 566 | | } |
| | | 567 | | } |
| | | 568 | | |
| | 0 | 569 | | return hasChanges; |
| | 0 | 570 | | } |
| | | 571 | | |
| | | 572 | | private async Task RefreshMetadataForVersions( |
| | | 573 | | MetadataRefreshOptions options, |
| | | 574 | | bool copyTitleMetadata, |
| | | 575 | | string path, |
| | | 576 | | CancellationToken cancellationToken) |
| | | 577 | | { |
| | | 578 | | // Ensure the alternate version exists with the correct type (e.g. Movie, not Video) |
| | | 579 | | // before refreshing. This must happen here rather than in RefreshMetadataForOwnedVideo |
| | | 580 | | // because that method is also used for stacked parts which should keep their resolved type. |
| | 0 | 581 | | var id = LibraryManager.GetNewItemId(path, GetType()); |
| | 0 | 582 | | if (LibraryManager.GetItemById(id) is not Video && FileSystem.FileExists(path)) |
| | | 583 | | { |
| | 0 | 584 | | var parentFolder = GetParent() as Folder; |
| | 0 | 585 | | var collectionType = LibraryManager.GetContentType(this); |
| | 0 | 586 | | var altVideo = LibraryManager.ResolveAlternateVersion(path, GetType(), parentFolder, collectionType); |
| | 0 | 587 | | if (altVideo is not null) |
| | | 588 | | { |
| | 0 | 589 | | altVideo.OwnerId = Id; |
| | 0 | 590 | | altVideo.SetPrimaryVersionId(Id); |
| | 0 | 591 | | LibraryManager.CreateItem(altVideo, GetParent()); |
| | | 592 | | } |
| | | 593 | | } |
| | | 594 | | |
| | 0 | 595 | | await RefreshMetadataForOwnedVideo(options, copyTitleMetadata, path, cancellationToken).ConfigureAwait(false |
| | | 596 | | |
| | | 597 | | // Create LinkedChild entry for this local alternate version |
| | | 598 | | // This ensures the relationship exists in the database even if the alternate version |
| | | 599 | | // was created after the primary video was first saved |
| | 0 | 600 | | if (LibraryManager.GetItemById(id) is Video video) |
| | | 601 | | { |
| | 0 | 602 | | LibraryManager.UpsertLinkedChild(Id, video.Id, LinkedChildType.LocalAlternateVersion); |
| | | 603 | | |
| | | 604 | | // Ensure PrimaryVersionId is set for existing alternate versions that may not have it |
| | 0 | 605 | | if (!video.PrimaryVersionId.HasValue) |
| | | 606 | | { |
| | 0 | 607 | | video.SetPrimaryVersionId(Id); |
| | 0 | 608 | | await video.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(f |
| | | 609 | | } |
| | | 610 | | } |
| | 0 | 611 | | } |
| | | 612 | | |
| | | 613 | | private new Task RefreshMetadataForOwnedVideo( |
| | | 614 | | MetadataRefreshOptions options, |
| | | 615 | | bool copyTitleMetadata, |
| | | 616 | | string path, |
| | | 617 | | CancellationToken cancellationToken) |
| | 0 | 618 | | => RefreshMetadataForOwnedVideo(options, copyTitleMetadata, path, GetType(), cancellationToken); |
| | | 619 | | |
| | | 620 | | private async Task RefreshMetadataForOwnedVideo( |
| | | 621 | | MetadataRefreshOptions options, |
| | | 622 | | bool copyTitleMetadata, |
| | | 623 | | string path, |
| | | 624 | | Type itemType, |
| | | 625 | | CancellationToken cancellationToken) |
| | | 626 | | { |
| | 0 | 627 | | var newOptions = new MetadataRefreshOptions(options) |
| | 0 | 628 | | { |
| | 0 | 629 | | SearchResult = null |
| | 0 | 630 | | }; |
| | | 631 | | |
| | 0 | 632 | | var id = LibraryManager.GetNewItemId(path, itemType); |
| | | 633 | | |
| | | 634 | | // Check if the file still exists |
| | 0 | 635 | | if (!FileSystem.FileExists(path)) |
| | | 636 | | { |
| | | 637 | | // File was removed - clean up any orphaned database entry |
| | 0 | 638 | | if (LibraryManager.GetItemById(id) is Video orphanedVideo && orphanedVideo.OwnerId.Equals(Id)) |
| | | 639 | | { |
| | 0 | 640 | | Logger.LogInformation("Owned video file no longer exists, removing orphaned item: {Path}", path); |
| | 0 | 641 | | LibraryManager.DeleteItem(orphanedVideo, new DeleteOptions { DeleteFileLocation = false }); |
| | | 642 | | } |
| | | 643 | | |
| | 0 | 644 | | return; |
| | | 645 | | } |
| | | 646 | | |
| | 0 | 647 | | if (LibraryManager.GetItemById(id) is not Video video) |
| | | 648 | | { |
| | 0 | 649 | | var parentFolder = GetParent() as Folder; |
| | 0 | 650 | | var collectionType = LibraryManager.GetContentType(this); |
| | 0 | 651 | | video = LibraryManager.ResolvePath( |
| | 0 | 652 | | FileSystem.GetFileSystemInfo(path), |
| | 0 | 653 | | parentFolder, |
| | 0 | 654 | | collectionType: collectionType) as Video; |
| | | 655 | | |
| | 0 | 656 | | if (video is null) |
| | | 657 | | { |
| | 0 | 658 | | return; |
| | | 659 | | } |
| | | 660 | | |
| | | 661 | | // Ensure parts use the expected base type (e.g. Video, not Movie) |
| | 0 | 662 | | if (video.GetType() != itemType && Activator.CreateInstance(itemType) is Video correctVideo) |
| | | 663 | | { |
| | 0 | 664 | | correctVideo.Path = video.Path; |
| | 0 | 665 | | correctVideo.Name = video.Name; |
| | 0 | 666 | | correctVideo.VideoType = video.VideoType; |
| | 0 | 667 | | correctVideo.ProductionYear = video.ProductionYear; |
| | 0 | 668 | | correctVideo.ExtraType = video.ExtraType; |
| | 0 | 669 | | video = correctVideo; |
| | | 670 | | } |
| | | 671 | | |
| | 0 | 672 | | video.Id = id; |
| | 0 | 673 | | video.OwnerId = Id; |
| | 0 | 674 | | LibraryManager.CreateItem(video, parentFolder); |
| | 0 | 675 | | newOptions.ForceSave = true; |
| | | 676 | | } |
| | | 677 | | |
| | 0 | 678 | | if (video.OwnerId.IsEmpty()) |
| | | 679 | | { |
| | 0 | 680 | | video.OwnerId = Id; |
| | | 681 | | } |
| | | 682 | | |
| | 0 | 683 | | await RefreshMetadataForOwnedItem(video, copyTitleMetadata, newOptions, cancellationToken).ConfigureAwait(fa |
| | 0 | 684 | | } |
| | | 685 | | |
| | | 686 | | /// <inheritdoc /> |
| | | 687 | | public override async Task UpdateToRepositoryAsync(ItemUpdateType updateReason, CancellationToken cancellationTo |
| | | 688 | | { |
| | 0 | 689 | | await base.UpdateToRepositoryAsync(updateReason, cancellationToken).ConfigureAwait(false); |
| | | 690 | | |
| | 0 | 691 | | var localAlternates = LibraryManager.GetLocalAlternateVersionIds(this) |
| | 0 | 692 | | .Select(i => LibraryManager.GetItemById(i)) |
| | 0 | 693 | | .Where(i => i is not null); |
| | | 694 | | |
| | 0 | 695 | | foreach (var item in localAlternates) |
| | | 696 | | { |
| | 0 | 697 | | item.ImageInfos = ImageInfos; |
| | 0 | 698 | | item.Overview = Overview; |
| | 0 | 699 | | item.ProductionYear = ProductionYear; |
| | 0 | 700 | | item.PremiereDate = PremiereDate; |
| | 0 | 701 | | item.CommunityRating = CommunityRating; |
| | 0 | 702 | | item.OfficialRating = OfficialRating; |
| | 0 | 703 | | item.Genres = Genres; |
| | 0 | 704 | | item.ProviderIds = ProviderIds; |
| | | 705 | | |
| | 0 | 706 | | await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataDownload, cancellationToken).ConfigureAwait(fa |
| | | 707 | | } |
| | 0 | 708 | | } |
| | | 709 | | |
| | | 710 | | public override IEnumerable<FileSystemMetadata> GetDeletePaths() |
| | | 711 | | { |
| | 0 | 712 | | if (!IsInMixedFolder) |
| | | 713 | | { |
| | 0 | 714 | | return new[] |
| | 0 | 715 | | { |
| | 0 | 716 | | new FileSystemMetadata |
| | 0 | 717 | | { |
| | 0 | 718 | | FullName = ContainingFolderPath, |
| | 0 | 719 | | IsDirectory = true |
| | 0 | 720 | | } |
| | 0 | 721 | | }; |
| | | 722 | | } |
| | | 723 | | |
| | 0 | 724 | | return base.GetDeletePaths(); |
| | | 725 | | } |
| | | 726 | | |
| | | 727 | | public virtual MediaStream GetDefaultVideoStream() |
| | | 728 | | { |
| | 0 | 729 | | if (!DefaultVideoStreamIndex.HasValue) |
| | | 730 | | { |
| | 0 | 731 | | return null; |
| | | 732 | | } |
| | | 733 | | |
| | 0 | 734 | | return MediaSourceManager.GetMediaStreams(new MediaStreamQuery |
| | 0 | 735 | | { |
| | 0 | 736 | | ItemId = Id, |
| | 0 | 737 | | Index = DefaultVideoStreamIndex.Value |
| | 0 | 738 | | }).FirstOrDefault(); |
| | | 739 | | } |
| | | 740 | | |
| | | 741 | | /// <summary> |
| | | 742 | | /// Gets the ids of the items whose owned extras belong to this item. |
| | | 743 | | /// Extras are linked to a single version but need tp be surfaced for all versions. |
| | | 744 | | /// </summary> |
| | | 745 | | /// <returns>An array containing the owner ids.</returns> |
| | | 746 | | protected override Guid[] GetExtraOwnerIds() |
| | | 747 | | { |
| | 0 | 748 | | return GetAllItemsForMediaSources() |
| | 0 | 749 | | .Select(i => i.Item.Id) |
| | 0 | 750 | | .Distinct() |
| | 0 | 751 | | .ToArray(); |
| | | 752 | | } |
| | | 753 | | |
| | | 754 | | protected override IEnumerable<(BaseItem Item, MediaSourceType MediaSourceType)> GetAllItemsForMediaSources() |
| | | 755 | | { |
| | 22 | 756 | | var primary = PrimaryVersionId.HasValue |
| | 22 | 757 | | ? LibraryManager.GetItemById(PrimaryVersionId.Value) as Video |
| | 22 | 758 | | : null; |
| | | 759 | | |
| | 22 | 760 | | var primaryLinked = primary is null |
| | 22 | 761 | | ? [] |
| | 22 | 762 | | : LibraryManager.GetLinkedAlternateVersions(primary).ToList(); |
| | | 763 | | |
| | | 764 | | // Grouping marks user-merged (splittable) sources. The primary is only such a source when |
| | | 765 | | // this video is linked onto it; for local (file-based) alternates the primary is just |
| | | 766 | | // another default source. |
| | 22 | 767 | | var primaryType = primaryLinked.Any(i => i.Id.Equals(Id)) |
| | 22 | 768 | | ? MediaSourceType.Grouping |
| | 22 | 769 | | : MediaSourceType.Default; |
| | | 770 | | |
| | | 771 | | // This video and its linked alternates, when this is itself an alternate, the primary and the primary's lin |
| | 22 | 772 | | var grouped = new[] { ((BaseItem)this, MediaSourceType.Default) } |
| | 22 | 773 | | .Concat(LibraryManager.GetLinkedAlternateVersions(this).Select(i => ((BaseItem)i, MediaSourceType.Groupi |
| | 22 | 774 | | .Concat(primary is null |
| | 22 | 775 | | ? [] |
| | 22 | 776 | | : primaryLinked.Select(i => ((BaseItem)i, MediaSourceType.Grouping)).Prepend(((BaseItem)primary, pri |
| | 22 | 777 | | .ToList(); |
| | | 778 | | |
| | | 779 | | // The local (file-based) alternate versions of every grouped item. |
| | 22 | 780 | | var localAlternates = grouped |
| | 22 | 781 | | .Select(i => i.Item1) |
| | 22 | 782 | | .OfType<Video>() |
| | 22 | 783 | | .SelectMany(LibraryManager.GetLocalAlternateVersionIds) |
| | 22 | 784 | | .Select(LibraryManager.GetItemById) |
| | 22 | 785 | | .Where(i => i is not null) |
| | 22 | 786 | | .Select(i => (i, MediaSourceType.Default)); |
| | | 787 | | |
| | | 788 | | // Deduplicate |
| | 22 | 789 | | return grouped |
| | 22 | 790 | | .Concat(localAlternates) |
| | 22 | 791 | | .DistinctBy(i => i.Item1.Id) |
| | 22 | 792 | | .ToList(); |
| | | 793 | | } |
| | | 794 | | } |
| | | 795 | | } |