using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Logging; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Providers { /// /// Class RefreshIntrosTask /// public class RefreshIntrosTask : ILibraryPostScanTask { /// /// The _library manager /// private readonly ILibraryManager _libraryManager; /// /// The _logger /// private readonly ILogger _logger; private readonly IFileSystem _fileSystem; /// /// Initializes a new instance of the class. /// /// The library manager. /// The logger. public RefreshIntrosTask(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem) { _libraryManager = libraryManager; _logger = logger; _fileSystem = fileSystem; } /// /// Runs the specified progress. /// /// The progress. /// The cancellation token. /// Task. public async Task Run(IProgress 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); } } /// /// Refreshes the intro. /// /// The path. /// The cancellation token. /// Task. private async Task RefreshIntro(string path, CancellationToken cancellationToken) { var item = _libraryManager.ResolvePath(_fileSystem.GetFileSystemInfo(path)); if (item == null) { _logger.Error("Intro resolver returned null for {0}", path); return; } var dbItem = _libraryManager.RetrieveItem(item.Id); var isNewItem = false; if (dbItem != null) { dbItem.ResetResolveArgs(item.ResolveArgs); item = dbItem; } else { isNewItem = true; } // Force the save if it's a new item await item.RefreshMetadata(cancellationToken, isNewItem).ConfigureAwait(false); } } }