2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-10-04 17:04:18 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2013-10-04 17:04:18 +00:00
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
|
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
2013-10-04 17:04:18 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class RefreshIntrosTask
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RefreshIntrosTask : ILibraryPostScanTask
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2013-10-30 14:40:14 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-08 20:02:35 +00:00
|
|
|
|
|
2013-10-04 17:04:18 +00:00
|
|
|
|
/// <summary>
|
2014-02-08 20:02:35 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="RefreshIntrosTask" /> class.
|
2013-10-04 17:04:18 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2014-02-08 20:02:35 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
public RefreshIntrosTask(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem)
|
2013-10-04 17:04:18 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_logger = logger;
|
2013-10-30 14:40:14 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-10-04 17:04:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the specified progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var files = _libraryManager.GetAllIntroFiles().ToList();
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await RefreshIntro(file, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error refreshing intro {0}", ex, file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= files.Count;
|
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes the intro.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task RefreshIntro(string path, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2013-10-30 14:40:14 +00:00
|
|
|
|
var item = _libraryManager.ResolvePath(_fileSystem.GetFileSystemInfo(path));
|
2013-10-04 17:04:18 +00:00
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("Intro resolver returned null for {0}", path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-14 20:03:35 +00:00
|
|
|
|
var dbItem = _libraryManager.GetItemById(item.Id);
|
2013-10-04 17:04:18 +00:00
|
|
|
|
|
|
|
|
|
if (dbItem != null)
|
|
|
|
|
{
|
|
|
|
|
item = dbItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Force the save if it's a new item
|
2014-02-06 04:39:16 +00:00
|
|
|
|
await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
|
2013-10-04 17:04:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|