@@ -223,14 +223,15 @@ public async Task<bool> SplitAndMergeVideos<TVideo>(
223
223
// Split up any existing merged videos.
224
224
double currentCount = 0d ;
225
225
double totalCount = videos . Count ;
226
+ var visitedVideos = new HashSet < Guid > ( ) ;
226
227
foreach ( var video in videos ) {
227
228
// Handle cancellation and update progress.
228
229
cancellationToken ? . ThrowIfCancellationRequested ( ) ;
229
230
var percent = currentCount ++ / totalCount * 50d ;
230
231
progress ? . Report ( percent ) ;
231
232
232
233
// Remove all alternate sources linked to the video.
233
- await RemoveAlternateSources ( video ) ;
234
+ await RemoveAlternateSources ( video , visitedVideos ) ;
234
235
}
235
236
236
237
// This will likely tax the CPU a bit… maybe, but we need to make sure the videos we're about to merge are up to date.
@@ -276,14 +277,15 @@ public async Task SplitVideos<TVideo>(IReadOnlyList<TVideo> videos, IProgress<do
276
277
// Split up any existing merged videos.
277
278
double currentCount = 0d ;
278
279
double totalMovies = videos . Count ;
280
+ var visitedVideos = new HashSet < Guid > ( ) ;
279
281
foreach ( var video in videos ) {
280
282
// Handle cancellation and update progress.
281
283
cancellationToken ? . ThrowIfCancellationRequested ( ) ;
282
284
var percent = currentCount ++ / totalMovies * 100d ;
283
285
progress ? . Report ( percent ) ;
284
286
285
287
// Remove all alternate sources linked to the video.
286
- await RemoveAlternateSources ( video ) ;
288
+ await RemoveAlternateSources ( video , visitedVideos ) ;
287
289
}
288
290
289
291
progress ? . Report ( 100 ) ;
@@ -356,17 +358,25 @@ await primaryVersion.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, Cancel
356
358
/// videos.
357
359
/// </summary>
358
360
/// <param name="baseItem">The primary video to clean up.</param>
359
- private async Task RemoveAlternateSources < TVideo > ( TVideo ? video , int depth = 0 ) where TVideo : Video
361
+ private async Task RemoveAlternateSources < TVideo > ( TVideo ? video , HashSet < Guid > visited ) where TVideo : Video
360
362
{
361
363
if ( video is null )
362
364
return ;
363
365
366
+ var depth = visited . Count ;
367
+ if ( visited . Contains ( video . Id ) ) {
368
+ _logger . LogTrace ( "Skipping already visited video. (Video={VideoId},Depth={Depth})" , video . Id , depth ) ;
369
+ return ;
370
+ }
371
+
372
+ visited . Add ( video . Id ) ;
373
+
364
374
// Remove all links for the primary video if this is not the primary video.
365
- if ( video . PrimaryVersionId is not null && depth is 0 ) {
375
+ if ( video . PrimaryVersionId is not null ) {
366
376
var primaryVideo = _libraryManager . GetItemById ( video . PrimaryVersionId ) as TVideo ;
367
- if ( primaryVideo is not null && primaryVideo . Id != video . Id ) {
377
+ if ( primaryVideo is not null ) {
368
378
_logger . LogTrace ( "Found primary video to clean up first. (Video={VideoId},Depth={Depth})" , primaryVideo . Id , depth ) ;
369
- await RemoveAlternateSources ( primaryVideo , depth + 1 ) ;
379
+ await RemoveAlternateSources ( primaryVideo , visited ) ;
370
380
}
371
381
}
372
382
@@ -382,7 +392,7 @@ private async Task RemoveAlternateSources<TVideo>(TVideo? video, int depth = 0)
382
392
var linkedAlternateVersions = video . GetLinkedAlternateVersions ( ) . ToList ( ) ;
383
393
_logger . LogTrace ( "Removing {Count} linked alternate sources for video. (Video={VideoId},Depth={Depth})" , linkedAlternateVersions . Count , video . Id , depth ) ;
384
394
foreach ( var linkedVideo in linkedAlternateVersions ) {
385
- await RemoveAlternateSources ( linkedVideo , depth + 1 ) ;
395
+ await RemoveAlternateSources ( linkedVideo , visited ) ;
386
396
}
387
397
388
398
// Remove the link for every local linked video.
@@ -392,7 +402,7 @@ private async Task RemoveAlternateSources<TVideo>(TVideo? video, int depth = 0)
392
402
. ToList ( ) ;
393
403
_logger . LogTrace ( "Removing {Count} local alternate sources for video. (Video={VideoId},Depth={Depth})" , localAlternateVersions . Count , video . Id , depth ) ;
394
404
foreach ( var linkedVideo in localAlternateVersions ) {
395
- await RemoveAlternateSources ( linkedVideo , depth + 1 ) ;
405
+ await RemoveAlternateSources ( linkedVideo , visited ) ;
396
406
}
397
407
398
408
// Remove the link for the primary video.
0 commit comments