| | | 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.Extensions; |
| | | 14 | | using MediaBrowser.Controller.Library; |
| | | 15 | | using MediaBrowser.Controller.LiveTv; |
| | | 16 | | using MediaBrowser.Controller.Persistence; |
| | | 17 | | using MediaBrowser.Controller.Providers; |
| | | 18 | | using MediaBrowser.Model.Dto; |
| | | 19 | | using MediaBrowser.Model.Entities; |
| | | 20 | | using MediaBrowser.Model.IO; |
| | | 21 | | using MediaBrowser.Model.MediaInfo; |
| | | 22 | | using Microsoft.Extensions.Logging; |
| | | 23 | | |
| | | 24 | | namespace MediaBrowser.Controller.Entities |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Class Video. |
| | | 28 | | /// </summary> |
| | | 29 | | public class Video : BaseItem, |
| | | 30 | | IHasAspectRatio, |
| | | 31 | | ISupportsPlaceHolders, |
| | | 32 | | IHasMediaSources |
| | | 33 | | { |
| | 440 | 34 | | public Video() |
| | | 35 | | { |
| | 440 | 36 | | AdditionalParts = Array.Empty<string>(); |
| | 440 | 37 | | LocalAlternateVersions = Array.Empty<string>(); |
| | 440 | 38 | | SubtitleFiles = Array.Empty<string>(); |
| | 440 | 39 | | AudioFiles = Array.Empty<string>(); |
| | 440 | 40 | | LinkedAlternateVersions = Array.Empty<LinkedChild>(); |
| | 440 | 41 | | } |
| | | 42 | | |
| | | 43 | | [JsonIgnore] |
| | | 44 | | public Guid? PrimaryVersionId { get; set; } |
| | | 45 | | |
| | | 46 | | public string[] AdditionalParts { get; set; } |
| | | 47 | | |
| | | 48 | | public string[] LocalAlternateVersions { get; set; } |
| | | 49 | | |
| | | 50 | | public LinkedChild[] LinkedAlternateVersions { get; set; } |
| | | 51 | | |
| | | 52 | | [JsonIgnore] |
| | 0 | 53 | | public override bool SupportsPlayedStatus => true; |
| | | 54 | | |
| | | 55 | | [JsonIgnore] |
| | 0 | 56 | | public override bool SupportsPeople => true; |
| | | 57 | | |
| | | 58 | | [JsonIgnore] |
| | 0 | 59 | | public override bool SupportsInheritedParentImages => true; |
| | | 60 | | |
| | | 61 | | [JsonIgnore] |
| | | 62 | | public override bool SupportsPositionTicksResume |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 0 | 66 | | var extraType = ExtraType; |
| | 0 | 67 | | if (extraType.HasValue) |
| | | 68 | | { |
| | 0 | 69 | | if (extraType.Value == Model.Entities.ExtraType.Sample) |
| | | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | if (extraType.Value == Model.Entities.ExtraType.ThemeVideo) |
| | | 75 | | { |
| | 0 | 76 | | return false; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | if (extraType.Value == Model.Entities.ExtraType.Trailer) |
| | | 80 | | { |
| | 0 | 81 | | return false; |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | return true; |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | [JsonIgnore] |
| | 0 | 90 | | public override bool SupportsThemeMedia => true; |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets or sets the timestamp. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <value>The timestamp.</value> |
| | | 96 | | public TransportStreamTimestamp? Timestamp { get; set; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets or sets the subtitle paths. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <value>The subtitle paths.</value> |
| | | 102 | | public string[] SubtitleFiles { get; set; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets or sets the audio paths. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <value>The audio paths.</value> |
| | | 108 | | public string[] AudioFiles { get; set; } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets or sets a value indicating whether this instance has subtitles. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value> |
| | | 114 | | public bool HasSubtitles { get; set; } |
| | | 115 | | |
| | | 116 | | public bool IsPlaceHolder { get; set; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets or sets the default index of the video stream. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <value>The default index of the video stream.</value> |
| | | 122 | | public int? DefaultVideoStreamIndex { get; set; } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Gets or sets the type of the video. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <value>The type of the video.</value> |
| | | 128 | | public VideoType VideoType { get; set; } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Gets or sets the type of the iso. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <value>The type of the iso.</value> |
| | | 134 | | public IsoType? IsoType { get; set; } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Gets or sets the video3 D format. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <value>The video3 D format.</value> |
| | | 140 | | public Video3DFormat? Video3DFormat { get; set; } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets or sets the aspect ratio. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <value>The aspect ratio.</value> |
| | | 146 | | public string AspectRatio { get; set; } |
| | | 147 | | |
| | | 148 | | [JsonIgnore] |
| | 0 | 149 | | public override bool SupportsAddingToPlaylist => true; |
| | | 150 | | |
| | | 151 | | [JsonIgnore] |
| | | 152 | | public int MediaSourceCount |
| | | 153 | | { |
| | | 154 | | get |
| | | 155 | | { |
| | 0 | 156 | | return GetMediaSourceCount(); |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | [JsonIgnore] |
| | 54 | 161 | | public bool IsStacked => AdditionalParts.Length > 0; |
| | | 162 | | |
| | | 163 | | [JsonIgnore] |
| | 4 | 164 | | public override bool HasLocalAlternateVersions => LibraryManager.GetLocalAlternateVersionIds(this).Any(); |
| | | 165 | | |
| | | 166 | | public static IRecordingsManager RecordingsManager { get; set; } |
| | | 167 | | |
| | | 168 | | [JsonIgnore] |
| | | 169 | | public override SourceType SourceType |
| | | 170 | | { |
| | | 171 | | get |
| | | 172 | | { |
| | 28 | 173 | | if (IsActiveRecording()) |
| | | 174 | | { |
| | 0 | 175 | | return SourceType.LiveTV; |
| | | 176 | | } |
| | | 177 | | |
| | 28 | 178 | | return base.SourceType; |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | [JsonIgnore] |
| | | 183 | | public bool IsCompleteMedia |
| | | 184 | | { |
| | | 185 | | get |
| | | 186 | | { |
| | 0 | 187 | | if (SourceType == SourceType.Channel) |
| | | 188 | | { |
| | 0 | 189 | | return !Tags.Contains("livestream", StringComparison.OrdinalIgnoreCase); |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | return !IsActiveRecording(); |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | [JsonIgnore] |
| | 0 | 197 | | protected virtual bool EnableDefaultVideoUserDataKeys => true; |
| | | 198 | | |
| | | 199 | | [JsonIgnore] |
| | | 200 | | public override string ContainingFolderPath |
| | | 201 | | { |
| | | 202 | | get |
| | | 203 | | { |
| | 54 | 204 | | if (IsStacked) |
| | | 205 | | { |
| | 0 | 206 | | return System.IO.Path.GetDirectoryName(Path); |
| | | 207 | | } |
| | | 208 | | |
| | 54 | 209 | | if (!IsPlaceHolder) |
| | | 210 | | { |
| | 54 | 211 | | if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd) |
| | | 212 | | { |
| | 1 | 213 | | return Path; |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | |
| | 53 | 217 | | return base.ContainingFolderPath; |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | [JsonIgnore] |
| | | 222 | | public override string FileNameWithoutExtension |
| | | 223 | | { |
| | | 224 | | get |
| | | 225 | | { |
| | 31 | 226 | | if (IsFileProtocol) |
| | | 227 | | { |
| | 31 | 228 | | if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd) |
| | | 229 | | { |
| | 0 | 230 | | return System.IO.Path.GetFileName(Path); |
| | | 231 | | } |
| | | 232 | | |
| | 31 | 233 | | return System.IO.Path.GetFileNameWithoutExtension(Path); |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | return null; |
| | | 237 | | } |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Gets a value indicating whether [is3 D]. |
| | | 242 | | /// </summary> |
| | | 243 | | /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value> |
| | | 244 | | [JsonIgnore] |
| | 0 | 245 | | public bool Is3D => Video3DFormat.HasValue; |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Gets the type of the media. |
| | | 249 | | /// </summary> |
| | | 250 | | /// <value>The type of the media.</value> |
| | | 251 | | [JsonIgnore] |
| | 0 | 252 | | public override MediaType MediaType => MediaType.Video; |
| | | 253 | | |
| | | 254 | | private int GetMediaSourceCount(HashSet<Guid> callstack = null) |
| | | 255 | | { |
| | 0 | 256 | | callstack ??= new(); |
| | 0 | 257 | | if (PrimaryVersionId.HasValue) |
| | | 258 | | { |
| | 0 | 259 | | var item = LibraryManager.GetItemById(PrimaryVersionId.Value); |
| | 0 | 260 | | if (item is Video video) |
| | | 261 | | { |
| | 0 | 262 | | if (callstack.Contains(video.Id)) |
| | | 263 | | { |
| | | 264 | | // Count alternate versions using LibraryManager |
| | 0 | 265 | | var linkedCount = LibraryManager.GetLinkedAlternateVersions(video).Count(); |
| | 0 | 266 | | var localCount = LibraryManager.GetLocalAlternateVersionIds(video).Count(); |
| | 0 | 267 | | return linkedCount + localCount + 1; |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | callstack.Add(video.Id); |
| | 0 | 271 | | return video.GetMediaSourceCount(callstack); |
| | | 272 | | } |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | // Count alternate versions using LibraryManager |
| | 0 | 276 | | var linkedVersionCount = LibraryManager.GetLinkedAlternateVersions(this).Count(); |
| | 0 | 277 | | var localVersionCount = LibraryManager.GetLocalAlternateVersionIds(this).Count(); |
| | 0 | 278 | | return linkedVersionCount + localVersionCount + 1; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | public override List<string> GetUserDataKeys() |
| | | 282 | | { |
| | 0 | 283 | | var list = base.GetUserDataKeys(); |
| | | 284 | | |
| | 0 | 285 | | if (EnableDefaultVideoUserDataKeys) |
| | | 286 | | { |
| | 0 | 287 | | if (ExtraType.HasValue) |
| | | 288 | | { |
| | 0 | 289 | | var key = this.GetProviderId(MetadataProvider.Tmdb); |
| | 0 | 290 | | if (!string.IsNullOrEmpty(key)) |
| | | 291 | | { |
| | 0 | 292 | | list.Insert(0, GetUserDataKey(key)); |
| | | 293 | | } |
| | | 294 | | |
| | 0 | 295 | | key = this.GetProviderId(MetadataProvider.Imdb); |
| | 0 | 296 | | if (!string.IsNullOrEmpty(key)) |
| | | 297 | | { |
| | 0 | 298 | | list.Insert(0, GetUserDataKey(key)); |
| | | 299 | | } |
| | | 300 | | } |
| | | 301 | | else |
| | | 302 | | { |
| | 0 | 303 | | var key = this.GetProviderId(MetadataProvider.Imdb); |
| | 0 | 304 | | if (!string.IsNullOrEmpty(key)) |
| | | 305 | | { |
| | 0 | 306 | | list.Insert(0, key); |
| | | 307 | | } |
| | | 308 | | |
| | 0 | 309 | | key = this.GetProviderId(MetadataProvider.Tmdb); |
| | 0 | 310 | | if (!string.IsNullOrEmpty(key)) |
| | | 311 | | { |
| | 0 | 312 | | list.Insert(0, key); |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | } |
| | | 316 | | |
| | 0 | 317 | | return list; |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | public void SetPrimaryVersionId(Guid? id) |
| | | 321 | | { |
| | 0 | 322 | | PrimaryVersionId = id; |
| | 0 | 323 | | PresentationUniqueKey = CreatePresentationUniqueKey(); |
| | 0 | 324 | | } |
| | | 325 | | |
| | | 326 | | public override string CreatePresentationUniqueKey() |
| | | 327 | | { |
| | 0 | 328 | | if (PrimaryVersionId.HasValue) |
| | | 329 | | { |
| | 0 | 330 | | return PrimaryVersionId.Value.ToString("N", CultureInfo.InvariantCulture); |
| | | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | return base.CreatePresentationUniqueKey(); |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | public override bool CanDownload() |
| | | 337 | | { |
| | 0 | 338 | | if (VideoType == VideoType.Dvd || VideoType == VideoType.BluRay) |
| | | 339 | | { |
| | 0 | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | return IsFileProtocol; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | protected override bool IsActiveRecording() |
| | | 347 | | { |
| | 28 | 348 | | return RecordingsManager.GetActiveRecordingInfo(Path) is not null; |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | public override bool CanDelete() |
| | | 352 | | { |
| | 0 | 353 | | if (IsActiveRecording()) |
| | | 354 | | { |
| | 0 | 355 | | return false; |
| | | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | return base.CanDelete(); |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | public IEnumerable<Guid> GetAdditionalPartIds() |
| | | 362 | | { |
| | 0 | 363 | | return AdditionalParts.Select(i => LibraryManager.GetNewItemId(i, typeof(Video))); |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | private string GetUserDataKey(string providerId) |
| | | 367 | | { |
| | 0 | 368 | | var key = providerId + "-" + ExtraType.ToString().ToLowerInvariant(); |
| | | 369 | | |
| | | 370 | | // Make sure different trailers have their own data. |
| | 0 | 371 | | if (RunTimeTicks.HasValue) |
| | | 372 | | { |
| | 0 | 373 | | key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture); |
| | | 374 | | } |
| | | 375 | | |
| | 0 | 376 | | return key; |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | /// <summary> |
| | | 380 | | /// Gets the additional parts. |
| | | 381 | | /// </summary> |
| | | 382 | | /// <returns>IEnumerable{Video}.</returns> |
| | | 383 | | public IOrderedEnumerable<Video> GetAdditionalParts() |
| | | 384 | | { |
| | 0 | 385 | | return GetAdditionalPartIds() |
| | 0 | 386 | | .Select(i => LibraryManager.GetItemById(i)) |
| | 0 | 387 | | .Where(i => i is not null) |
| | 0 | 388 | | .OfType<Video>() |
| | 0 | 389 | | .OrderBy(i => i.SortName); |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | internal override ItemUpdateType UpdateFromResolvedItem(BaseItem newItem) |
| | | 393 | | { |
| | 0 | 394 | | var updateType = base.UpdateFromResolvedItem(newItem); |
| | | 395 | | |
| | 0 | 396 | | if (newItem is Video newVideo) |
| | | 397 | | { |
| | 0 | 398 | | if (!AdditionalParts.SequenceEqual(newVideo.AdditionalParts, StringComparer.Ordinal)) |
| | | 399 | | { |
| | 0 | 400 | | AdditionalParts = newVideo.AdditionalParts; |
| | 0 | 401 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | if (!LocalAlternateVersions.SequenceEqual(newVideo.LocalAlternateVersions, StringComparer.Ordinal)) |
| | | 405 | | { |
| | 0 | 406 | | LocalAlternateVersions = newVideo.LocalAlternateVersions; |
| | 0 | 407 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 408 | | } |
| | | 409 | | |
| | 0 | 410 | | if (VideoType != newVideo.VideoType) |
| | | 411 | | { |
| | 0 | 412 | | VideoType = newVideo.VideoType; |
| | 0 | 413 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 414 | | } |
| | | 415 | | } |
| | | 416 | | |
| | 0 | 417 | | return updateType; |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystem |
| | | 421 | | { |
| | 0 | 422 | | var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwa |
| | | 423 | | |
| | | 424 | | // Clean up LocalAlternateVersions - remove paths that no longer exist |
| | 0 | 425 | | if (LocalAlternateVersions.Length > 0) |
| | | 426 | | { |
| | 0 | 427 | | var validPaths = LocalAlternateVersions.Where(FileSystem.FileExists).ToArray(); |
| | 0 | 428 | | if (validPaths.Length != LocalAlternateVersions.Length) |
| | | 429 | | { |
| | 0 | 430 | | LocalAlternateVersions = validPaths; |
| | 0 | 431 | | hasChanges = true; |
| | | 432 | | } |
| | | 433 | | } |
| | | 434 | | |
| | 0 | 435 | | if (IsStacked) |
| | | 436 | | { |
| | 0 | 437 | | var tasks = AdditionalParts |
| | 0 | 438 | | .Select(i => RefreshMetadataForOwnedVideo(options, true, i, typeof(Video), cancellationToken)); |
| | | 439 | | |
| | 0 | 440 | | await Task.WhenAll(tasks).ConfigureAwait(false); |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | // Must have a parent to have additional parts or alternate versions |
| | | 444 | | // In other words, it must be part of the Parent/Child tree |
| | | 445 | | // The additional parts won't have additional parts themselves |
| | 0 | 446 | | if (IsFileProtocol && SupportsOwnedItems) |
| | | 447 | | { |
| | | 448 | | // Check if LinkedChildren are in sync before processing |
| | 0 | 449 | | var existingVersionCount = LibraryManager.GetLocalAlternateVersionIds(this).Count(); |
| | 0 | 450 | | var tasks = LocalAlternateVersions |
| | 0 | 451 | | .Select(i => RefreshMetadataForVersions(options, false, i, cancellationToken)); |
| | | 452 | | |
| | 0 | 453 | | await Task.WhenAll(tasks).ConfigureAwait(false); |
| | | 454 | | |
| | 0 | 455 | | if (existingVersionCount != LocalAlternateVersions.Length) |
| | | 456 | | { |
| | 0 | 457 | | hasChanges = true; |
| | | 458 | | } |
| | | 459 | | } |
| | | 460 | | |
| | 0 | 461 | | return hasChanges; |
| | 0 | 462 | | } |
| | | 463 | | |
| | | 464 | | private async Task RefreshMetadataForVersions( |
| | | 465 | | MetadataRefreshOptions options, |
| | | 466 | | bool copyTitleMetadata, |
| | | 467 | | string path, |
| | | 468 | | CancellationToken cancellationToken) |
| | | 469 | | { |
| | | 470 | | // Ensure the alternate version exists with the correct type (e.g. Movie, not Video) |
| | | 471 | | // before refreshing. This must happen here rather than in RefreshMetadataForOwnedVideo |
| | | 472 | | // because that method is also used for stacked parts which should keep their resolved type. |
| | 0 | 473 | | var id = LibraryManager.GetNewItemId(path, GetType()); |
| | 0 | 474 | | if (LibraryManager.GetItemById(id) is not Video && FileSystem.FileExists(path)) |
| | | 475 | | { |
| | 0 | 476 | | var parentFolder = GetParent() as Folder; |
| | 0 | 477 | | var collectionType = LibraryManager.GetContentType(this); |
| | 0 | 478 | | var altVideo = LibraryManager.ResolveAlternateVersion(path, GetType(), parentFolder, collectionType); |
| | 0 | 479 | | if (altVideo is not null) |
| | | 480 | | { |
| | 0 | 481 | | altVideo.OwnerId = Id; |
| | 0 | 482 | | altVideo.SetPrimaryVersionId(Id); |
| | 0 | 483 | | LibraryManager.CreateItem(altVideo, GetParent()); |
| | | 484 | | } |
| | | 485 | | } |
| | | 486 | | |
| | 0 | 487 | | await RefreshMetadataForOwnedVideo(options, copyTitleMetadata, path, cancellationToken).ConfigureAwait(false |
| | | 488 | | |
| | | 489 | | // Create LinkedChild entry for this local alternate version |
| | | 490 | | // This ensures the relationship exists in the database even if the alternate version |
| | | 491 | | // was created after the primary video was first saved |
| | 0 | 492 | | if (LibraryManager.GetItemById(id) is Video video) |
| | | 493 | | { |
| | 0 | 494 | | LibraryManager.UpsertLinkedChild(Id, video.Id, LinkedChildType.LocalAlternateVersion); |
| | | 495 | | |
| | | 496 | | // Ensure PrimaryVersionId is set for existing alternate versions that may not have it |
| | 0 | 497 | | if (!video.PrimaryVersionId.HasValue) |
| | | 498 | | { |
| | 0 | 499 | | video.SetPrimaryVersionId(Id); |
| | 0 | 500 | | await video.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(f |
| | | 501 | | } |
| | | 502 | | } |
| | 0 | 503 | | } |
| | | 504 | | |
| | | 505 | | private new Task RefreshMetadataForOwnedVideo( |
| | | 506 | | MetadataRefreshOptions options, |
| | | 507 | | bool copyTitleMetadata, |
| | | 508 | | string path, |
| | | 509 | | CancellationToken cancellationToken) |
| | 0 | 510 | | => RefreshMetadataForOwnedVideo(options, copyTitleMetadata, path, GetType(), cancellationToken); |
| | | 511 | | |
| | | 512 | | private async Task RefreshMetadataForOwnedVideo( |
| | | 513 | | MetadataRefreshOptions options, |
| | | 514 | | bool copyTitleMetadata, |
| | | 515 | | string path, |
| | | 516 | | Type itemType, |
| | | 517 | | CancellationToken cancellationToken) |
| | | 518 | | { |
| | 0 | 519 | | var newOptions = new MetadataRefreshOptions(options) |
| | 0 | 520 | | { |
| | 0 | 521 | | SearchResult = null |
| | 0 | 522 | | }; |
| | | 523 | | |
| | 0 | 524 | | var id = LibraryManager.GetNewItemId(path, itemType); |
| | | 525 | | |
| | | 526 | | // Check if the file still exists |
| | 0 | 527 | | if (!FileSystem.FileExists(path)) |
| | | 528 | | { |
| | | 529 | | // File was removed - clean up any orphaned database entry |
| | 0 | 530 | | if (LibraryManager.GetItemById(id) is Video orphanedVideo && orphanedVideo.OwnerId.Equals(Id)) |
| | | 531 | | { |
| | 0 | 532 | | Logger.LogInformation("Owned video file no longer exists, removing orphaned item: {Path}", path); |
| | 0 | 533 | | LibraryManager.DeleteItem(orphanedVideo, new DeleteOptions { DeleteFileLocation = false }); |
| | | 534 | | } |
| | | 535 | | |
| | 0 | 536 | | return; |
| | | 537 | | } |
| | | 538 | | |
| | 0 | 539 | | if (LibraryManager.GetItemById(id) is not Video video) |
| | | 540 | | { |
| | 0 | 541 | | var parentFolder = GetParent() as Folder; |
| | 0 | 542 | | var collectionType = LibraryManager.GetContentType(this); |
| | 0 | 543 | | video = LibraryManager.ResolvePath( |
| | 0 | 544 | | FileSystem.GetFileSystemInfo(path), |
| | 0 | 545 | | parentFolder, |
| | 0 | 546 | | collectionType: collectionType) as Video; |
| | | 547 | | |
| | 0 | 548 | | if (video is null) |
| | | 549 | | { |
| | 0 | 550 | | return; |
| | | 551 | | } |
| | | 552 | | |
| | | 553 | | // Ensure parts use the expected base type (e.g. Video, not Movie) |
| | 0 | 554 | | if (video.GetType() != itemType && Activator.CreateInstance(itemType) is Video correctVideo) |
| | | 555 | | { |
| | 0 | 556 | | correctVideo.Path = video.Path; |
| | 0 | 557 | | correctVideo.Name = video.Name; |
| | 0 | 558 | | correctVideo.VideoType = video.VideoType; |
| | 0 | 559 | | correctVideo.ProductionYear = video.ProductionYear; |
| | 0 | 560 | | correctVideo.ExtraType = video.ExtraType; |
| | 0 | 561 | | video = correctVideo; |
| | | 562 | | } |
| | | 563 | | |
| | 0 | 564 | | video.Id = id; |
| | 0 | 565 | | video.OwnerId = Id; |
| | 0 | 566 | | LibraryManager.CreateItem(video, parentFolder); |
| | 0 | 567 | | newOptions.ForceSave = true; |
| | | 568 | | } |
| | | 569 | | |
| | 0 | 570 | | if (video.OwnerId.IsEmpty()) |
| | | 571 | | { |
| | 0 | 572 | | video.OwnerId = Id; |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | await RefreshMetadataForOwnedItem(video, copyTitleMetadata, newOptions, cancellationToken).ConfigureAwait(fa |
| | 0 | 576 | | } |
| | | 577 | | |
| | | 578 | | /// <inheritdoc /> |
| | | 579 | | public override async Task UpdateToRepositoryAsync(ItemUpdateType updateReason, CancellationToken cancellationTo |
| | | 580 | | { |
| | 0 | 581 | | await base.UpdateToRepositoryAsync(updateReason, cancellationToken).ConfigureAwait(false); |
| | | 582 | | |
| | 0 | 583 | | var localAlternates = LibraryManager.GetLocalAlternateVersionIds(this) |
| | 0 | 584 | | .Select(i => LibraryManager.GetItemById(i)) |
| | 0 | 585 | | .Where(i => i is not null); |
| | | 586 | | |
| | 0 | 587 | | foreach (var item in localAlternates) |
| | | 588 | | { |
| | 0 | 589 | | item.ImageInfos = ImageInfos; |
| | 0 | 590 | | item.Overview = Overview; |
| | 0 | 591 | | item.ProductionYear = ProductionYear; |
| | 0 | 592 | | item.PremiereDate = PremiereDate; |
| | 0 | 593 | | item.CommunityRating = CommunityRating; |
| | 0 | 594 | | item.OfficialRating = OfficialRating; |
| | 0 | 595 | | item.Genres = Genres; |
| | 0 | 596 | | item.ProviderIds = ProviderIds; |
| | | 597 | | |
| | 0 | 598 | | await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataDownload, cancellationToken).ConfigureAwait(fa |
| | | 599 | | } |
| | 0 | 600 | | } |
| | | 601 | | |
| | | 602 | | public override IEnumerable<FileSystemMetadata> GetDeletePaths() |
| | | 603 | | { |
| | 0 | 604 | | if (!IsInMixedFolder) |
| | | 605 | | { |
| | 0 | 606 | | return new[] |
| | 0 | 607 | | { |
| | 0 | 608 | | new FileSystemMetadata |
| | 0 | 609 | | { |
| | 0 | 610 | | FullName = ContainingFolderPath, |
| | 0 | 611 | | IsDirectory = true |
| | 0 | 612 | | } |
| | 0 | 613 | | }; |
| | | 614 | | } |
| | | 615 | | |
| | 0 | 616 | | return base.GetDeletePaths(); |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | public virtual MediaStream GetDefaultVideoStream() |
| | | 620 | | { |
| | 0 | 621 | | if (!DefaultVideoStreamIndex.HasValue) |
| | | 622 | | { |
| | 0 | 623 | | return null; |
| | | 624 | | } |
| | | 625 | | |
| | 0 | 626 | | return MediaSourceManager.GetMediaStreams(new MediaStreamQuery |
| | 0 | 627 | | { |
| | 0 | 628 | | ItemId = Id, |
| | 0 | 629 | | Index = DefaultVideoStreamIndex.Value |
| | 0 | 630 | | }).FirstOrDefault(); |
| | | 631 | | } |
| | | 632 | | |
| | | 633 | | protected override IEnumerable<(BaseItem Item, MediaSourceType MediaSourceType)> GetAllItemsForMediaSources() |
| | | 634 | | { |
| | 0 | 635 | | var list = new List<(BaseItem, MediaSourceType)> |
| | 0 | 636 | | { |
| | 0 | 637 | | (this, MediaSourceType.Default) |
| | 0 | 638 | | }; |
| | | 639 | | |
| | 0 | 640 | | list.AddRange( |
| | 0 | 641 | | LibraryManager.GetLinkedAlternateVersions(this) |
| | 0 | 642 | | .Select(i => ((BaseItem)i, MediaSourceType.Grouping))); |
| | | 643 | | |
| | 0 | 644 | | if (PrimaryVersionId.HasValue) |
| | | 645 | | { |
| | 0 | 646 | | if (LibraryManager.GetItemById(PrimaryVersionId.Value) is Video primary) |
| | | 647 | | { |
| | 0 | 648 | | var existingIds = list.Select(i => i.Item1.Id).ToList(); |
| | 0 | 649 | | list.Add((primary, MediaSourceType.Grouping)); |
| | 0 | 650 | | list.AddRange(LibraryManager.GetLinkedAlternateVersions(primary).Where(i => !existingIds.Contains(i. |
| | | 651 | | } |
| | | 652 | | } |
| | | 653 | | |
| | 0 | 654 | | var localAlternates = list |
| | 0 | 655 | | .SelectMany(i => |
| | 0 | 656 | | { |
| | 0 | 657 | | return i.Item1 is Video video ? LibraryManager.GetLocalAlternateVersionIds(video) : Enumerable.Empty |
| | 0 | 658 | | }) |
| | 0 | 659 | | .Select(LibraryManager.GetItemById) |
| | 0 | 660 | | .Where(i => i is not null) |
| | 0 | 661 | | .ToList(); |
| | | 662 | | |
| | 0 | 663 | | list.AddRange(localAlternates.Select(i => (i, MediaSourceType.Default))); |
| | | 664 | | |
| | 0 | 665 | | return list; |
| | | 666 | | } |
| | | 667 | | } |
| | | 668 | | } |