jellyfin/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs

225 lines
8.2 KiB
C#
Raw Normal View History

2019-11-01 18:38:54 +01:00
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.IO;
using Emby.Naming.TV;
using MediaBrowser.Controller.Entities.TV;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Controller.Library;
2015-01-10 02:38:01 +01:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.Entities;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
2013-02-21 02:33:05 +01:00
namespace Emby.Server.Implementations.Library.Resolvers.TV
2013-02-21 02:33:05 +01:00
{
/// <summary>
2019-11-01 18:38:54 +01:00
/// Class SeriesResolver.
2013-02-21 02:33:05 +01:00
/// </summary>
public class SeriesResolver : FolderResolver<Series>
2013-02-21 02:33:05 +01:00
{
2015-01-10 02:38:01 +01:00
private readonly IFileSystem _fileSystem;
2020-06-06 02:15:56 +02:00
private readonly ILogger<SeriesResolver> _logger;
2015-01-10 02:38:01 +01:00
private readonly ILibraryManager _libraryManager;
2019-11-01 18:38:54 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="SeriesResolver"/> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="logger">The logger.</param>
/// <param name="libraryManager">The library manager.</param>
public SeriesResolver(IFileSystem fileSystem, ILogger<SeriesResolver> logger, ILibraryManager libraryManager)
2015-01-10 02:38:01 +01:00
{
_fileSystem = fileSystem;
_logger = logger;
_libraryManager = libraryManager;
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override ResolverPriority Priority => ResolverPriority.Second;
2013-02-21 02:33:05 +01:00
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>Series.</returns>
protected override Series Resolve(ItemResolveArgs args)
{
if (args.IsDirectory)
{
2016-09-16 01:19:27 +02:00
if (args.HasParent<Series>() || args.HasParent<Season>())
2016-09-03 19:16:36 +02:00
{
return null;
}
var collectionType = args.GetCollectionType();
2014-12-28 07:21:39 +01:00
if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
2013-02-21 02:33:05 +01:00
{
2020-06-14 11:11:11 +02:00
// if (args.ContainsFileSystemEntryByName("tvshow.nfo"))
2016-10-13 23:13:30 +02:00
//{
// return new Series
// {
// Path = args.Path,
// Name = Path.GetFileName(args.Path)
// };
//}
2016-10-13 20:43:47 +02:00
2015-01-10 02:38:01 +01:00
var configuredContentType = _libraryManager.GetConfiguredContentType(args.Path);
if (!string.Equals(configuredContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
{
return new Series
{
Path = args.Path,
Name = Path.GetFileName(args.Path)
};
}
}
2018-09-12 19:26:21 +02:00
else if (string.IsNullOrEmpty(collectionType))
2015-01-10 02:38:01 +01:00
{
2016-10-13 20:43:47 +02:00
if (args.ContainsFileSystemEntryByName("tvshow.nfo"))
{
2018-09-12 19:26:21 +02:00
if (args.Parent != null && args.Parent.IsRoot)
2016-10-16 00:12:16 +02:00
{
// For now, return null, but if we want to allow this in the future then add some additional checks to guard against a misplaced tvshow.nfo
return null;
}
2016-10-13 20:43:47 +02:00
return new Series
{
Path = args.Path,
Name = Path.GetFileName(args.Path)
};
}
2018-09-12 19:26:21 +02:00
if (args.Parent != null && args.Parent.IsRoot)
2014-11-29 20:51:30 +01:00
{
2016-10-09 09:18:43 +02:00
return null;
}
2020-02-19 21:56:35 +01:00
if (IsSeriesFolder(args.Path, args.FileSystemChildren, args.DirectoryService, _fileSystem, _logger, _libraryManager, false))
2016-10-09 09:18:43 +02:00
{
return new Series
2015-01-10 02:38:01 +01:00
{
2016-10-09 09:18:43 +02:00
Path = args.Path,
Name = Path.GetFileName(args.Path)
};
2015-01-10 02:38:01 +01:00
}
2013-02-21 02:33:05 +01:00
}
}
return null;
}
public static bool IsSeriesFolder(
string path,
2015-10-04 05:38:46 +02:00
IEnumerable<FileSystemMetadata> fileSystemChildren,
2015-01-10 02:38:01 +01:00
IDirectoryService directoryService,
IFileSystem fileSystem,
2020-06-06 02:15:56 +02:00
ILogger<SeriesResolver> logger,
2015-01-10 02:38:01 +01:00
ILibraryManager libraryManager,
bool isTvContentType)
{
foreach (var child in fileSystemChildren)
{
2016-10-25 21:02:04 +02:00
if (child.IsDirectory)
2015-01-10 02:38:01 +01:00
{
2015-07-23 18:32:34 +02:00
if (IsSeasonFolder(child.FullName, isTvContentType, libraryManager))
2015-01-10 02:38:01 +01:00
{
logger.LogDebug("{Path} is a series because of season folder {Dir}.", path, child.FullName);
2015-01-10 02:38:01 +01:00
return true;
}
}
else
{
string fullName = child.FullName;
2020-02-19 21:56:35 +01:00
if (libraryManager.IsVideoFile(fullName))
2015-01-10 02:38:01 +01:00
{
if (isTvContentType)
{
return true;
}
2018-09-12 19:26:21 +02:00
var namingOptions = ((LibraryManager)libraryManager).GetNamingOptions();
2015-01-10 20:42:14 +01:00
2019-01-13 21:37:13 +01:00
var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions);
2018-09-12 19:26:21 +02:00
var episodeInfo = episodeResolver.Resolve(fullName, false, true, false, fillExtendedInfo: false);
2015-01-10 02:38:01 +01:00
if (episodeInfo != null && episodeInfo.EpisodeNumber.HasValue)
{
return true;
}
}
}
}
logger.LogDebug("{Path} is not a series folder.", path);
2015-01-10 02:38:01 +01:00
return false;
}
/// <summary>
/// Determines whether [is place holder] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
/// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
2019-01-13 21:37:13 +01:00
/// <exception cref="ArgumentNullException">path</exception>
2015-01-10 02:38:01 +01:00
private static bool IsVideoPlaceHolder(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
2015-01-10 02:38:01 +01:00
}
var extension = Path.GetExtension(path);
return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Determines whether [is season folder] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
/// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
2015-07-23 18:32:34 +02:00
/// <param name="libraryManager">The library manager.</param>
2015-01-10 02:38:01 +01:00
/// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
2015-07-23 18:32:34 +02:00
private static bool IsSeasonFolder(string path, bool isTvContentType, ILibraryManager libraryManager)
2015-01-10 02:38:01 +01:00
{
2020-01-22 22:18:56 +01:00
var seasonNumber = SeasonPathParser.Parse(path, isTvContentType, isTvContentType).SeasonNumber;
2015-01-10 02:38:01 +01:00
return seasonNumber.HasValue;
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Sets the initial item values.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
{
base.SetInitialItemValues(item, args);
SetProviderIdFromPath(item, args.Path);
2013-02-21 02:33:05 +01:00
}
/// <summary>
/// Sets the provider id from path.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="path">The path.</param>
private static void SetProviderIdFromPath(Series item, string path)
2013-02-21 02:33:05 +01:00
{
var justName = Path.GetFileName(path);
2013-02-21 02:33:05 +01:00
var id = justName.GetAttributeValue("tvdbid");
if (!string.IsNullOrEmpty(id))
{
2020-06-06 21:17:49 +02:00
item.SetProviderId(MetadataProvider.Tvdb, id);
2013-02-21 02:33:05 +01:00
}
}
}
}