Skip to content

Commit b420e89

Browse files
committed
fix: skip visited videos regardless of depth
1 parent dc96e7d commit b420e89

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Shokofin/MergeVersions/MergeVersionManager.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,15 @@ public async Task<bool> SplitAndMergeVideos<TVideo>(
223223
// Split up any existing merged videos.
224224
double currentCount = 0d;
225225
double totalCount = videos.Count;
226+
var visitedVideos = new HashSet<Guid>();
226227
foreach (var video in videos) {
227228
// Handle cancellation and update progress.
228229
cancellationToken?.ThrowIfCancellationRequested();
229230
var percent = currentCount++ / totalCount * 50d;
230231
progress?.Report(percent);
231232

232233
// Remove all alternate sources linked to the video.
233-
await RemoveAlternateSources(video);
234+
await RemoveAlternateSources(video, visitedVideos);
234235
}
235236

236237
// 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
276277
// Split up any existing merged videos.
277278
double currentCount = 0d;
278279
double totalMovies = videos.Count;
280+
var visitedVideos = new HashSet<Guid>();
279281
foreach (var video in videos) {
280282
// Handle cancellation and update progress.
281283
cancellationToken?.ThrowIfCancellationRequested();
282284
var percent = currentCount++ / totalMovies * 100d;
283285
progress?.Report(percent);
284286

285287
// Remove all alternate sources linked to the video.
286-
await RemoveAlternateSources(video);
288+
await RemoveAlternateSources(video, visitedVideos);
287289
}
288290

289291
progress?.Report(100);
@@ -356,17 +358,25 @@ await primaryVersion.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, Cancel
356358
/// videos.
357359
/// </summary>
358360
/// <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
360362
{
361363
if (video is null)
362364
return;
363365

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+
364374
// 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) {
366376
var primaryVideo = _libraryManager.GetItemById(video.PrimaryVersionId) as TVideo;
367-
if (primaryVideo is not null && primaryVideo.Id != video.Id) {
377+
if (primaryVideo is not null) {
368378
_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);
370380
}
371381
}
372382

@@ -382,7 +392,7 @@ private async Task RemoveAlternateSources<TVideo>(TVideo? video, int depth = 0)
382392
var linkedAlternateVersions = video.GetLinkedAlternateVersions().ToList();
383393
_logger.LogTrace("Removing {Count} linked alternate sources for video. (Video={VideoId},Depth={Depth})", linkedAlternateVersions.Count, video.Id, depth);
384394
foreach (var linkedVideo in linkedAlternateVersions) {
385-
await RemoveAlternateSources(linkedVideo, depth + 1);
395+
await RemoveAlternateSources(linkedVideo, visited);
386396
}
387397

388398
// Remove the link for every local linked video.
@@ -392,7 +402,7 @@ private async Task RemoveAlternateSources<TVideo>(TVideo? video, int depth = 0)
392402
.ToList();
393403
_logger.LogTrace("Removing {Count} local alternate sources for video. (Video={VideoId},Depth={Depth})", localAlternateVersions.Count, video.Id, depth);
394404
foreach (var linkedVideo in localAlternateVersions) {
395-
await RemoveAlternateSources(linkedVideo, depth + 1);
405+
await RemoveAlternateSources(linkedVideo, visited);
396406
}
397407

398408
// Remove the link for the primary video.

0 commit comments

Comments
 (0)