using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Naming.Common;
using MediaBrowser.Naming.TV;
using System;
using System.Collections.Generic;
using System.IO;
namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
{
///
/// Class SeriesResolver
///
public class SeriesResolver : FolderResolver
{
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
public SeriesResolver(IFileSystem fileSystem, ILogger logger, ILibraryManager libraryManager)
{
_fileSystem = fileSystem;
_logger = logger;
_libraryManager = libraryManager;
}
///
/// 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.IsRoot)
{
return null;
}
var collectionType = args.GetCollectionType();
// If there's a collection type and it's not tv, it can't be a series
if (!string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
{
return null;
}
if (args.HasParent() || args.HasParent())
{
return null;
}
if (IsSeriesFolder(args.Path, args.FileSystemChildren, args.DirectoryService, _fileSystem, _logger, _libraryManager))
{
return new Series
{
Path = args.Path,
Name = Path.GetFileName(args.Path)
};
}
}
return null;
}
///
/// Determines whether [is series folder] [the specified path].
///
/// The path.
/// The file system children.
/// The directory service.
/// The file system.
/// The logger.
/// The library manager.
/// true if [is series folder] [the specified path]; otherwise, false.
public static bool IsSeriesFolder(string path, IEnumerable fileSystemChildren, IDirectoryService directoryService, IFileSystem fileSystem, ILogger logger, ILibraryManager libraryManager)
{
foreach (var child in fileSystemChildren)
{
var attributes = child.Attributes;
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
//logger.Debug("Igoring series file or folder marked hidden: {0}", child.FullName);
continue;
}
// Can't enforce this because files saved by Bitcasa are always marked System
//if ((attributes & FileAttributes.System) == FileAttributes.System)
//{
// logger.Debug("Igoring series subfolder marked system: {0}", child.FullName);
// continue;
//}
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
if (IsSeasonFolder(child.FullName))
{
//logger.Debug("{0} is a series because of season folder {1}.", path, child.FullName);
return true;
}
}
else
{
var fullName = child.FullName;
if (libraryManager.IsVideoFile(fullName) || IsVideoPlaceHolder(fullName))
{
return true;
}
}
}
logger.Debug("{0} is not a series folder.", path);
return false;
}
///
/// Determines whether [is place holder] [the specified path].
///
/// The path.
/// true if [is place holder] [the specified path]; otherwise, false.
/// path
private static bool IsVideoPlaceHolder(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
var extension = Path.GetExtension(path);
return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
}
///
/// Determines whether [is season folder] [the specified path].
///
/// The path.
/// true if [is season folder] [the specified path]; otherwise, false.
private static bool IsSeasonFolder(string path)
{
var seasonNumber = new SeasonPathParser(new ExtendedNamingOptions(), new RegexProvider()).Parse(path, true).SeasonNumber;
return seasonNumber.HasValue;
}
///
/// Sets the initial item values.
///
/// The item.
/// The args.
protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
{
base.SetInitialItemValues(item, args);
SetProviderIdFromPath(item, args.Path);
}
///
/// Sets the provider id from path.
///
/// The item.
/// The path.
private void SetProviderIdFromPath(Series item, string path)
{
var justName = Path.GetFileName(path);
var id = justName.GetAttributeValue("tvdbid");
if (!string.IsNullOrEmpty(id))
{
item.SetProviderId(MetadataProviders.Tvdb, id);
}
}
}
}