using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Providers.Savers;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Movies
{
///
/// Class MovieProviderFromXml
///
public class MovieProviderFromXml : BaseMetadataProvider
{
internal static MovieProviderFromXml Current { get; private set; }
private readonly IItemRepository _itemRepo;
private readonly IFileSystem _fileSystem;
public MovieProviderFromXml(ILogManager logManager, IServerConfigurationManager configurationManager, IItemRepository itemRepo, IFileSystem fileSystem)
: base(logManager, configurationManager)
{
_itemRepo = itemRepo;
_fileSystem = fileSystem;
Current = this;
}
///
/// Supportses the specified item.
///
/// The item.
/// true if XXXX, false otherwise
public override bool Supports(BaseItem item)
{
if (item.LocationType != LocationType.FileSystem)
{
return false;
}
var trailer = item as Trailer;
if (trailer != null)
{
return !trailer.IsLocalTrailer;
}
return item is Movie || item is MusicVideo || item is AdultVideo;
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.First; }
}
protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
{
var savePath = MovieXmlSaver.GetMovieSavePath(item);
var xml = item.ResolveArgs.GetMetaFileByPath(savePath) ?? new FileInfo(savePath);
if (!xml.Exists)
{
return false;
}
return _fileSystem.GetLastWriteTimeUtc(xml) > providerInfo.LastRefreshed;
}
///
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
///
/// The item.
/// if set to true [force].
/// The cancellation token.
/// Task{System.Boolean}.
public override Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
return Fetch(item, cancellationToken);
}
///
/// Fetches the specified item.
///
/// The item.
/// The cancellation token.
/// true if XXXX, false otherwise
private async Task Fetch(BaseItem item, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var path = MovieXmlSaver.GetMovieSavePath(item);
if (File.Exists(path))
{
await XmlParsingResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
var video = (Video)item;
await new MovieXmlParser(Logger, _itemRepo).FetchAsync(video, path, cancellationToken).ConfigureAwait(false);
}
finally
{
XmlParsingResourcePool.Release();
}
}
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
}
}