2019-11-01 18:38:54 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2015-08-26 07:10:04 +02:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 20:20:16 +01:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2015-08-26 07:10:04 +02:00
|
|
|
|
2016-11-18 09:39:20 +01:00
|
|
|
namespace Emby.Server.Implementations.Data
|
2015-08-26 07:10:04 +02:00
|
|
|
{
|
2017-07-02 20:58:56 +02:00
|
|
|
public class CleanDatabaseScheduledTask : ILibraryPostScanTask
|
2015-08-26 07:10:04 +02:00
|
|
|
{
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2020-06-06 02:15:56 +02:00
|
|
|
private readonly ILogger<CleanDatabaseScheduledTask> _logger;
|
2015-08-26 07:10:04 +02:00
|
|
|
|
2020-03-03 23:07:10 +01:00
|
|
|
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, ILogger<CleanDatabaseScheduledTask> logger)
|
2015-08-26 07:10:04 +02:00
|
|
|
{
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2017-08-12 21:09:13 +02:00
|
|
|
public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
2015-08-26 07:10:04 +02:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
CleanDeadItems(cancellationToken, progress);
|
|
|
|
return Task.CompletedTask;
|
2015-08-26 07:10:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
private void CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress)
|
2015-08-28 06:19:08 +02:00
|
|
|
{
|
|
|
|
var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
|
|
|
{
|
|
|
|
HasDeadParentId = true
|
|
|
|
});
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
var numItems = itemIds.Count;
|
|
|
|
|
2018-12-13 14:18:25 +01:00
|
|
|
_logger.LogDebug("Cleaning {0} items with dead parent links", numItems);
|
2015-08-28 06:19:08 +02:00
|
|
|
|
|
|
|
foreach (var itemId in itemIds)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
{
|
2018-12-13 14:18:25 +01:00
|
|
|
_logger.LogInformation("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
|
2015-08-28 06:19:08 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
_libraryManager.DeleteItem(item, new DeleteOptions
|
2015-08-28 06:19:08 +02:00
|
|
|
{
|
|
|
|
DeleteFileLocation = false
|
2016-03-20 20:53:22 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
});
|
2015-08-28 06:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
double percent = numComplete;
|
|
|
|
percent /= numItems;
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
}
|
2015-08-26 07:10:04 +02:00
|
|
|
}
|
2018-12-13 14:18:25 +01:00
|
|
|
}
|