1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Collections;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
/// <summary>
/// Deletes Path references from collections that no longer exists.
/// </summary>
public class CleanupCollectionPathsTask : IScheduledTask
{
private readonly ILocalizationManager _localization;
private readonly ICollectionManager _collectionManager;
private readonly ILogger<CleanupCollectionPathsTask> _logger;
private readonly IProviderManager _providerManager;
private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="CleanupCollectionPathsTask"/> class.
/// </summary>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
/// <param name="collectionManager">Instance of the <see cref="ICollectionManager"/> interface.</param>
/// <param name="logger">The logger.</param>
/// <param name="providerManager">The provider manager.</param>
/// <param name="fileSystem">The filesystem.</param>
public CleanupCollectionPathsTask(
ILocalizationManager localization,
ICollectionManager collectionManager,
ILogger<CleanupCollectionPathsTask> logger,
IProviderManager providerManager,
IFileSystem fileSystem)
{
_localization = localization;
_collectionManager = collectionManager;
_logger = logger;
_providerManager = providerManager;
_fileSystem = fileSystem;
}
/// <inheritdoc />
public string Name => _localization.GetLocalizedString("TaskCleanCollections");
/// <inheritdoc />
public string Key => "CleanCollections";
/// <inheritdoc />
public string Description => _localization.GetLocalizedString("TaskCleanCollectionsDescription");
/// <inheritdoc />
public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
/// <inheritdoc />
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
var collectionsFolder = await _collectionManager.GetCollectionsFolder(false).ConfigureAwait(false);
if (collectionsFolder is null)
{
_logger.LogDebug("There is no collection folder to be found");
return;
}
var collections = collectionsFolder.Children.OfType<BoxSet>().ToArray();
_logger.LogDebug("Found {CollectionLength} Boxsets", collections.Length);
var itemsToRemove = new List<LinkedChild>();
for (var index = 0; index < collections.Length; index++)
{
var collection = collections[index];
_logger.LogDebug("Check Boxset {CollectionName}", collection.Name);
foreach (var collectionLinkedChild in collection.LinkedChildren)
{
if (!File.Exists(collectionLinkedChild.Path))
{
_logger.LogInformation("Item in boxset {CollectionName} cannot be found at {ItemPath}", collection.Name, collectionLinkedChild.Path);
itemsToRemove.Add(collectionLinkedChild);
}
}
if (itemsToRemove.Count != 0)
{
_logger.LogDebug("Update Boxset {CollectionName}", collection.Name);
collection.LinkedChildren = collection.LinkedChildren.Except(itemsToRemove).ToArray();
await collection.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken)
.ConfigureAwait(false);
_providerManager.QueueRefresh(
collection.Id,
new MetadataRefreshOptions(new DirectoryService(_fileSystem))
{
ForceSave = true
},
RefreshPriority.High);
itemsToRemove.Clear();
}
progress.Report(100D / collections.Length * (index + 1));
}
}
/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
return new[] { new TaskTriggerInfo() { Type = TaskTriggerInfo.TriggerStartup } };
}
}
|