jellyfin/Emby.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs

556 lines
19 KiB
C#
Raw Normal View History

#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Emby.Naming.Common;
using Emby.Naming.Video;
using Jellyfin.Extensions;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities.Movies;
2015-01-10 05:53:35 +00:00
using MediaBrowser.Controller.Entities.TV;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Library;
2014-02-13 05:11:54 +00:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2013-02-21 01:33:05 +00:00
namespace Emby.Server.Implementations.Library.Resolvers.Movies
2013-02-21 01:33:05 +00:00
{
/// <summary>
2019-11-01 17:38:54 +00:00
/// Class MovieResolver.
2013-02-21 01:33:05 +00:00
/// </summary>
2014-12-04 05:24:41 +00:00
public class MovieResolver : BaseVideoResolver<Video>, IMultiItemResolver
2013-02-21 01:33:05 +00:00
{
2021-10-02 17:59:58 +00:00
private readonly IImageProcessor _imageProcessor;
private readonly StackResolver _stackResolver;
2021-10-02 17:59:58 +00:00
2020-01-22 21:18:56 +00:00
private string[] _validCollectionTypes = new[]
{
CollectionType.Movies,
CollectionType.HomeVideos,
CollectionType.MusicVideos,
CollectionType.Movies,
CollectionType.Photos
};
/// <summary>
/// Initializes a new instance of the <see cref="MovieResolver"/> class.
/// </summary>
/// <param name="imageProcessor">The image processor.</param>
/// <param name="namingOptions">The naming options.</param>
public MovieResolver(IImageProcessor imageProcessor, NamingOptions namingOptions)
: base(namingOptions)
2020-01-22 21:18:56 +00:00
{
_imageProcessor = imageProcessor;
_stackResolver = new StackResolver(NamingOptions);
2020-01-22 21:18:56 +00:00
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override ResolverPriority Priority => ResolverPriority.Fourth;
2013-02-21 01:33:05 +00:00
2019-11-01 17:38:54 +00:00
/// <inheritdoc />
2019-07-06 14:15:38 +00:00
public MultiItemResolverResult ResolveMultiple(
Folder parent,
2015-10-04 03:38:46 +00:00
List<FileSystemMetadata> files,
2014-12-04 05:24:41 +00:00
string collectionType,
IDirectoryService directoryService)
{
var result = ResolveMultipleInternal(parent, files, collectionType, directoryService);
if (result != null)
{
foreach (var item in result.Items)
{
SetInitialItemValues((Video)item, null);
}
}
return result;
}
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>Video.</returns>
public override Video Resolve(ItemResolveArgs args)
{
var collectionType = args.GetCollectionType();
// Find movies with their own folders
if (args.IsDirectory)
{
if (IsInvalid(args.Parent, collectionType))
{
return null;
}
var files = args.GetActualFileSystemChildren().ToList();
if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
{
return FindMovie<MusicVideo>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
}
if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
{
return FindMovie<Video>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
}
if (string.IsNullOrEmpty(collectionType))
{
// Owned items will be caught by the plain video resolver
if (args.Parent == null)
{
// return FindMovie<Video>(args.Path, args.Parent, files, args.DirectoryService, collectionType);
return null;
}
if (args.HasParent<Series>())
{
return null;
}
{
return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
}
}
if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
{
return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
}
return null;
}
// Handle owned items
if (args.Parent == null)
{
return base.Resolve(args);
}
if (IsInvalid(args.Parent, collectionType))
{
return null;
}
Video item = null;
if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
{
item = ResolveVideo<MusicVideo>(args, false);
}
// To find a movie file, the collection type must be movies or boxsets
else if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
{
item = ResolveVideo<Movie>(args, true);
}
else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
{
item = ResolveVideo<Video>(args, false);
}
else if (string.IsNullOrEmpty(collectionType))
{
if (args.HasParent<Series>())
{
return null;
}
item = ResolveVideo<Video>(args, false);
}
if (item != null)
{
item.IsInMixedFolder = true;
}
return item;
}
2019-07-06 14:15:38 +00:00
private MultiItemResolverResult ResolveMultipleInternal(
Folder parent,
2015-10-04 03:38:46 +00:00
List<FileSystemMetadata> files,
string collectionType,
IDirectoryService directoryService)
2014-12-04 05:24:41 +00:00
{
if (IsInvalid(parent, collectionType))
2014-12-04 05:24:41 +00:00
{
return null;
}
if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
{
2017-10-13 05:44:20 +00:00
return ResolveVideos<MusicVideo>(parent, files, directoryService, true, collectionType, false);
2014-12-04 05:24:41 +00:00
}
2016-01-26 18:36:56 +00:00
if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
2016-02-25 15:12:22 +00:00
string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
2014-12-04 05:24:41 +00:00
{
2017-10-13 05:44:20 +00:00
return ResolveVideos<Video>(parent, files, directoryService, false, collectionType, false);
2014-12-04 05:24:41 +00:00
}
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(collectionType))
2014-12-04 05:24:41 +00:00
{
// Owned items should just use the plain video type
if (parent == null)
{
2017-10-13 05:44:20 +00:00
return ResolveVideos<Video>(parent, files, directoryService, false, collectionType, false);
2014-12-04 05:24:41 +00:00
}
2015-11-11 14:56:31 +00:00
if (parent is Series || parent.GetParents().OfType<Series>().Any())
2015-01-10 05:53:35 +00:00
{
return null;
}
2017-10-13 05:44:20 +00:00
return ResolveVideos<Movie>(parent, files, directoryService, false, collectionType, true);
2014-12-04 05:24:41 +00:00
}
2014-12-19 04:20:07 +00:00
if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
2014-12-04 05:24:41 +00:00
{
2017-10-13 05:44:20 +00:00
return ResolveVideos<Movie>(parent, files, directoryService, true, collectionType, true);
2014-12-04 05:24:41 +00:00
}
return null;
}
2019-07-06 14:15:38 +00:00
private MultiItemResolverResult ResolveVideos<T>(
Folder parent,
IEnumerable<FileSystemMetadata> fileSystemEntries,
IDirectoryService directoryService,
bool suppportMultiEditions,
string collectionType,
bool parseName)
2014-12-04 05:24:41 +00:00
where T : Video, new()
{
2015-10-04 03:38:46 +00:00
var files = new List<FileSystemMetadata>();
2014-12-04 05:24:41 +00:00
var videos = new List<BaseItem>();
2015-10-04 03:38:46 +00:00
var leftOver = new List<FileSystemMetadata>();
2014-12-04 05:24:41 +00:00
// Loop through each child file/folder and see if we find a video
foreach (var child in fileSystemEntries)
{
2016-12-22 23:53:57 +00:00
// This is a hack but currently no better way to resolve a sometimes ambiguous situation
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(collectionType))
2016-12-22 23:53:57 +00:00
{
2019-07-06 14:15:38 +00:00
if (string.Equals(child.Name, "tvshow.nfo", StringComparison.OrdinalIgnoreCase)
|| string.Equals(child.Name, "season.nfo", StringComparison.OrdinalIgnoreCase))
2016-12-22 23:53:57 +00:00
{
return null;
}
}
2016-10-25 19:02:04 +00:00
if (child.IsDirectory)
2014-12-04 05:24:41 +00:00
{
leftOver.Add(child);
2018-09-12 17:26:21 +00:00
}
2019-07-06 14:15:38 +00:00
else if (!IsIgnored(child.Name))
2014-12-04 05:24:41 +00:00
{
files.Add(child);
}
}
var resolverResult = VideoListResolver.Resolve(files, NamingOptions, suppportMultiEditions).ToList();
2014-12-04 05:24:41 +00:00
var result = new MultiItemResolverResult
{
ExtraFiles = leftOver,
Items = videos
};
2017-10-03 18:39:37 +00:00
var isInMixedFolder = resolverResult.Count > 1 || (parent != null && parent.IsTopParent);
2014-12-09 04:57:18 +00:00
2014-12-04 05:24:41 +00:00
foreach (var video in resolverResult)
{
2020-01-22 21:18:56 +00:00
var firstVideo = video.Files[0];
2014-12-04 05:24:41 +00:00
var videoItem = new T
{
Path = video.Files[0].Path,
2014-12-09 04:57:18 +00:00
IsInMixedFolder = isInMixedFolder,
2014-12-04 05:24:41 +00:00
ProductionYear = video.Year,
2017-10-13 05:44:20 +00:00
Name = parseName ?
video.Name :
2017-11-01 19:50:30 +00:00
Path.GetFileNameWithoutExtension(video.Files[0].Path),
2017-08-10 18:01:31 +00:00
AdditionalParts = video.Files.Skip(1).Select(i => i.Path).ToArray(),
LocalAlternateVersions = video.AlternateVersions.Select(i => i.Path).ToArray()
2014-12-04 05:24:41 +00:00
};
SetVideoType(videoItem, firstVideo);
Set3DFormat(videoItem, firstVideo);
result.Items.Add(videoItem);
}
2016-02-25 15:12:22 +00:00
result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
2014-12-04 05:24:41 +00:00
return result;
}
private static bool IsIgnored(string filename)
2018-09-12 17:26:21 +00:00
{
// Ignore samples
2019-07-06 14:15:38 +00:00
Match m = Regex.Match(filename, @"\bsample\b", RegexOptions.IgnoreCase);
2018-09-12 17:26:21 +00:00
return m.Success;
2018-09-12 17:26:21 +00:00
}
2016-02-25 15:12:22 +00:00
private bool ContainsFile(List<VideoInfo> result, FileSystemMetadata file)
{
return result.Any(i => ContainsFile(i, file));
}
private bool ContainsFile(VideoInfo result, FileSystemMetadata file)
{
return result.Files.Any(i => ContainsFile(i, file)) ||
result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
result.Extras.Any(i => ContainsFile(i, file));
}
private static bool ContainsFile(VideoFileInfo result, FileSystemMetadata file)
2016-02-25 15:12:22 +00:00
{
return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Sets the initial item values.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
protected override void SetInitialItemValues(Video item, ItemResolveArgs args)
2013-02-21 01:33:05 +00:00
{
base.SetInitialItemValues(item, args);
2015-01-02 14:29:20 +00:00
SetProviderIdsFromPath(item);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Sets the provider id from path.
/// </summary>
/// <param name="item">The item.</param>
private static void SetProviderIdsFromPath(Video item)
2013-02-21 01:33:05 +00:00
{
2015-01-04 14:34:15 +00:00
if (item is Movie || item is MusicVideo)
{
2020-01-22 21:18:56 +00:00
// We need to only look at the name of this actual item (not parents)
2015-01-04 14:34:15 +00:00
var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path) : Path.GetFileName(item.ContainingFolderPath);
2013-02-21 01:33:05 +00:00
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(justName))
2015-01-04 14:34:15 +00:00
{
2015-09-10 03:22:52 +00:00
// check for tmdb id
var tmdbid = justName.GetAttributeValue("tmdbid");
2015-01-02 14:29:20 +00:00
2015-09-10 03:22:52 +00:00
if (!string.IsNullOrWhiteSpace(tmdbid))
{
2020-06-06 19:17:49 +00:00
item.SetProviderId(MetadataProvider.Tmdb, tmdbid);
2015-09-10 03:22:52 +00:00
}
}
2015-01-02 14:29:20 +00:00
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(item.Path))
2015-01-04 14:34:15 +00:00
{
2015-09-10 03:22:52 +00:00
// check for imdb id - we use full media path, as we can assume, that this will match in any use case (wither id in parent dir or in file name)
var imdbid = item.Path.GetAttributeValue("imdbid");
if (!string.IsNullOrWhiteSpace(imdbid))
{
2020-06-06 19:17:49 +00:00
item.SetProviderId(MetadataProvider.Imdb, imdbid);
2015-09-10 03:22:52 +00:00
}
2015-01-04 14:34:15 +00:00
}
2015-01-02 14:29:20 +00:00
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
2020-01-22 21:18:56 +00:00
/// Finds a movie based on a child file system entries.
2013-02-21 01:33:05 +00:00
/// </summary>
/// <returns>Movie.</returns>
2017-10-21 16:39:52 +00:00
private T FindMovie<T>(ItemResolveArgs args, string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool parseName)
2013-06-16 19:02:57 +00:00
where T : Video, new()
2013-02-21 01:33:05 +00:00
{
2015-10-04 03:38:46 +00:00
var multiDiscFolders = new List<FileSystemMetadata>();
2015-01-04 14:34:15 +00:00
2021-05-05 13:30:32 +00:00
var libraryOptions = args.LibraryOptions;
2017-10-21 16:39:52 +00:00
var supportPhotos = string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && libraryOptions.EnablePhotos;
var photos = new List<FileSystemMetadata>();
2014-12-04 05:24:41 +00:00
// Search for a folder rip
2013-06-16 19:02:57 +00:00
foreach (var child in fileSystemEntries)
2013-02-21 01:33:05 +00:00
{
2013-06-20 02:23:07 +00:00
var filename = child.Name;
2016-10-25 19:02:04 +00:00
if (child.IsDirectory)
2013-02-21 01:33:05 +00:00
{
2016-12-03 21:46:06 +00:00
if (IsDvdDirectory(child.FullName, filename, directoryService))
2013-02-21 01:33:05 +00:00
{
2014-12-04 05:24:41 +00:00
var movie = new T
2013-02-21 01:33:05 +00:00
{
2013-06-16 19:02:57 +00:00
Path = path,
2013-02-21 01:33:05 +00:00
VideoType = VideoType.Dvd
};
2014-12-04 05:24:41 +00:00
Set3DFormat(movie);
return movie;
2013-02-21 01:33:05 +00:00
}
2020-01-22 21:18:56 +00:00
2021-08-28 22:32:50 +00:00
if (IsBluRayDirectory(filename))
2013-02-21 01:33:05 +00:00
{
2014-12-04 05:24:41 +00:00
var movie = new T
2013-02-21 01:33:05 +00:00
{
2013-06-16 19:02:57 +00:00
Path = path,
2013-02-21 01:33:05 +00:00
VideoType = VideoType.BluRay
};
2014-12-04 05:24:41 +00:00
Set3DFormat(movie);
return movie;
2013-02-21 01:33:05 +00:00
}
2013-06-16 19:02:57 +00:00
2014-11-18 02:48:22 +00:00
multiDiscFolders.Add(child);
2013-02-21 01:33:05 +00:00
}
2014-12-09 04:57:18 +00:00
else if (IsDvdFile(filename))
{
var movie = new T
{
Path = path,
VideoType = VideoType.Dvd
};
Set3DFormat(movie);
return movie;
}
2018-09-12 17:26:21 +00:00
else if (supportPhotos && PhotoResolver.IsImageFile(child.FullName, _imageProcessor))
2017-10-21 16:39:52 +00:00
{
photos.Add(child);
}
2013-02-21 01:33:05 +00:00
}
2017-10-21 16:39:52 +00:00
// TODO: Allow GetMultiDiscMovie in here
2020-01-22 21:18:56 +00:00
const bool SupportsMultiVersion = true;
2017-10-21 16:39:52 +00:00
2020-01-22 21:18:56 +00:00
var result = ResolveVideos<T>(parent, fileSystemEntries, directoryService, SupportsMultiVersion, collectionType, parseName) ??
2017-10-21 16:39:52 +00:00
new MultiItemResolverResult();
2017-10-21 16:39:52 +00:00
if (result.Items.Count == 1)
{
var videoPath = result.Items[0].Path;
var hasPhotos = photos.Any(i => !PhotoResolver.IsOwnedByResolvedMedia(videoPath, i.Name));
2017-10-21 16:39:52 +00:00
if (!hasPhotos)
2016-10-17 16:40:43 +00:00
{
var movie = (T)result.Items[0];
movie.IsInMixedFolder = false;
movie.Name = Path.GetFileName(movie.ContainingFolderPath);
return movie;
}
2017-10-21 16:39:52 +00:00
}
2020-02-19 20:56:35 +00:00
else if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
2017-10-21 16:39:52 +00:00
{
return GetMultiDiscMovie<T>(multiDiscFolders, directoryService);
2013-06-16 19:02:57 +00:00
}
return null;
2013-02-21 01:33:05 +00:00
}
2013-06-16 19:02:57 +00:00
/// <summary>
/// Gets the multi disc movie.
/// </summary>
/// <param name="multiDiscFolders">The folders.</param>
2014-11-18 02:48:22 +00:00
/// <param name="directoryService">The directory service.</param>
2013-06-16 19:02:57 +00:00
/// <returns>``0.</returns>
2015-10-04 03:38:46 +00:00
private T GetMultiDiscMovie<T>(List<FileSystemMetadata> multiDiscFolders, IDirectoryService directoryService)
2013-06-16 19:02:57 +00:00
where T : Video, new()
{
var videoTypes = new List<VideoType>();
2013-06-16 19:02:57 +00:00
var folderPaths = multiDiscFolders.Select(i => i.FullName).Where(i =>
2013-06-16 19:02:57 +00:00
{
var subFileEntries = directoryService.GetFileSystemEntries(i);
2014-12-09 04:57:18 +00:00
var subfolders = subFileEntries
2020-01-22 21:18:56 +00:00
.Where(e => e.IsDirectory)
2014-11-18 02:48:22 +00:00
.ToList();
2013-06-16 19:02:57 +00:00
2016-12-03 21:46:06 +00:00
if (subfolders.Any(s => IsDvdDirectory(s.FullName, s.Name, directoryService)))
2013-06-16 19:02:57 +00:00
{
videoTypes.Add(VideoType.Dvd);
2013-06-16 19:02:57 +00:00
return true;
}
2020-01-22 21:18:56 +00:00
2021-08-28 22:32:50 +00:00
if (subfolders.Any(s => IsBluRayDirectory(s.Name)))
2013-06-16 19:02:57 +00:00
{
videoTypes.Add(VideoType.BluRay);
2013-06-16 19:02:57 +00:00
return true;
}
2014-12-09 04:57:18 +00:00
var subFiles = subFileEntries
2016-10-25 19:02:04 +00:00
.Where(e => !e.IsDirectory)
2014-12-09 04:57:18 +00:00
.Select(d => d.Name);
if (subFiles.Any(IsDvdFile))
{
videoTypes.Add(VideoType.Dvd);
return true;
}
2013-06-16 19:02:57 +00:00
return false;
2013-06-20 02:23:07 +00:00
}).OrderBy(i => i).ToList();
2013-06-16 19:02:57 +00:00
// If different video types were found, don't allow this
2014-11-18 02:48:22 +00:00
if (videoTypes.Distinct().Count() > 1)
{
return null;
}
2013-06-20 02:23:07 +00:00
if (folderPaths.Count == 0)
2013-06-16 19:02:57 +00:00
{
return null;
}
var result = _stackResolver.ResolveDirectories(folderPaths).ToList();
2020-01-22 21:18:56 +00:00
if (result.Count != 1)
{
return null;
}
2014-12-04 05:24:41 +00:00
2020-02-19 20:56:35 +00:00
int additionalPartsLen = folderPaths.Count - 1;
var additionalParts = new string[additionalPartsLen];
folderPaths.CopyTo(1, additionalParts, 0, additionalPartsLen);
2016-02-09 04:09:29 +00:00
var returnVideo = new T
2013-06-16 19:02:57 +00:00
{
2013-06-20 02:23:07 +00:00
Path = folderPaths[0],
2020-02-19 20:56:35 +00:00
AdditionalParts = additionalParts,
2014-11-18 02:48:22 +00:00
VideoType = videoTypes[0],
2020-01-22 21:18:56 +00:00
Name = result[0].Name
2013-06-16 19:02:57 +00:00
};
2016-02-09 04:09:29 +00:00
SetIsoType(returnVideo);
return returnVideo;
2013-06-16 19:02:57 +00:00
}
2021-05-23 22:30:41 +00:00
private bool IsInvalid(Folder parent, ReadOnlySpan<char> collectionType)
{
2014-12-04 05:24:41 +00:00
if (parent != null)
{
2014-12-04 05:24:41 +00:00
if (parent.IsRoot)
{
return true;
}
}
2021-05-23 22:30:41 +00:00
if (collectionType.IsEmpty)
{
return false;
}
2021-05-23 22:30:41 +00:00
return !_validCollectionTypes.Contains(collectionType, StringComparison.OrdinalIgnoreCase);
}
2013-02-21 01:33:05 +00:00
}
2015-09-20 17:36:09 +00:00
}