jellyfin/MediaBrowser.Providers/Games/GameXmlProvider.cs
2014-02-02 08:36:31 -05:00

75 lines
2.1 KiB
C#

using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Logging;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Games
{
public class GameXmlProvider : BaseXmlProvider, ILocalMetadataProvider<Game>
{
private readonly ILogger _logger;
public GameXmlProvider(IFileSystem fileSystem, ILogger logger)
: base(fileSystem)
{
_logger = logger;
}
public async Task<MetadataResult<Game>> GetMetadata(string path, CancellationToken cancellationToken)
{
path = GetXmlFile(path).FullName;
var result = new MetadataResult<Game>();
await XmlParsingResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
var item = new Game();
new GameXmlParser(_logger).Fetch(item, path, cancellationToken);
result.HasMetadata = true;
result.Item = item;
}
catch (FileNotFoundException)
{
result.HasMetadata = false;
}
finally
{
XmlParsingResourcePool.Release();
}
return result;
}
public string Name
{
get { return "Media Browser xml"; }
}
protected override FileInfo GetXmlFile(string path)
{
var fileInfo = FileSystem.GetFileSystemInfo(path);
var directoryInfo = fileInfo as DirectoryInfo;
if (directoryInfo == null)
{
directoryInfo = new DirectoryInfo(Path.GetDirectoryName(path));
}
var directoryPath = directoryInfo.FullName;
var specificFile = Path.Combine(directoryPath, Path.GetFileNameWithoutExtension(path) + ".xml");
var file = new FileInfo(specificFile);
return file.Exists ? file : new FileInfo(Path.Combine(directoryPath, "game.xml"));
}
}
}