using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using System;
using System.ComponentModel.Composition;
using System.IO;
namespace MediaBrowser.Controller.Resolvers.TV
{
///
/// Class SeriesResolver
///
[Export(typeof(IBaseItemResolver))]
public class SeriesResolver : BaseFolderResolver
{
///
/// Gets the priority.
///
/// The priority.
public override ResolverPriority Priority
{
get
{
return ResolverPriority.Second;
}
}
///
/// Resolves the specified args.
///
/// The args.
/// Series.
protected override Series Resolve(ItemResolveArgs args)
{
if (args.IsDirectory)
{
// Avoid expensive tests against VF's and all their children by not allowing this
if (args.Parent == null || args.Parent.IsRoot)
{
return null;
}
// Optimization to avoid running these tests against Seasons
if (args.Parent is Series)
{
return null;
}
// It's a Series if any of the following conditions are met:
// series.xml exists
// [tvdbid= is present in the path
// TVUtils.IsSeriesFolder returns true
var filename = Path.GetFileName(args.Path);
if (string.IsNullOrEmpty(filename))
{
return null;
}
if (args.ContainsMetaFileByName("series.xml") || filename.IndexOf("[tvdbid=", StringComparison.OrdinalIgnoreCase) != -1 || TVUtils.IsSeriesFolder(args.Path, args.FileSystemChildren))
{
return new Series();
}
}
return null;
}
///
/// Sets the initial item values.
///
/// The item.
/// The args.
protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
{
base.SetInitialItemValues(item, args);
Season.AddMetadataFiles(args);
SetProviderIdFromPath(item);
}
///
/// Sets the provider id from path.
///
/// The item.
private void SetProviderIdFromPath(Series item)
{
var justName = item.Path.Substring(item.Path.LastIndexOf(Path.DirectorySeparatorChar));
var id = justName.GetAttributeValue("tvdbid");
if (!string.IsNullOrEmpty(id))
{
item.SetProviderId(MetadataProviders.Tvdb, id);
}
}
}
}