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

318 lines
11 KiB
C#
Raw Normal View History

2013-02-21 01:33:05 +00:00
using MediaBrowser.Common.Extensions;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class MovieResolver
/// </summary>
public class MovieResolver : BaseVideoResolver<Video>
2013-02-21 01:33:05 +00:00
{
2013-03-04 05:43:06 +00:00
private IServerApplicationPaths ApplicationPaths { get; set; }
2013-07-12 19:56:40 +00:00
private readonly ILibraryManager _libraryManager;
2013-07-12 19:56:40 +00:00
public MovieResolver(IServerApplicationPaths appPaths, ILibraryManager libraryManager)
2013-03-04 05:43:06 +00:00
{
ApplicationPaths = appPaths;
2013-07-12 19:56:40 +00:00
_libraryManager = libraryManager;
2013-03-04 05:43:06 +00:00
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override ResolverPriority Priority
{
get
{
// Give plugins a chance to catch iso's first
// Also since we have to loop through child files looking for videos,
// see if we can avoid some of that by letting other resolvers claim folders first
return ResolverPriority.Second;
}
}
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>Video.</returns>
protected override Video Resolve(ItemResolveArgs args)
2013-02-21 01:33:05 +00:00
{
// Must be a directory
2013-02-21 01:33:05 +00:00
if (args.IsDirectory)
{
// Avoid expensive tests against VF's and all their children by not allowing this
2013-06-16 19:02:57 +00:00
if (args.Parent != null)
2013-02-21 01:33:05 +00:00
{
2013-06-16 19:02:57 +00:00
if (args.Parent.IsRoot)
2013-02-21 01:33:05 +00:00
{
return null;
}
2013-06-16 19:02:57 +00:00
// If the parent is not a boxset, the only other allowed parent type is Folder
if (!(args.Parent is BoxSet))
{
if (args.Parent.GetType() != typeof(Folder))
{
return null;
}
}
2013-02-21 01:33:05 +00:00
}
// Since the looping is expensive, this is an optimization to help us avoid it
if (args.ContainsMetaFileByName("series.xml") || args.Path.IndexOf("[tvdbid", StringComparison.OrdinalIgnoreCase) != -1)
{
return null;
}
2013-07-12 19:56:40 +00:00
var collectionType = args.Parent == null ? null : _libraryManager.FindCollectionType(args.Parent);
2013-06-16 19:02:57 +00:00
2013-07-12 19:56:40 +00:00
if (args.Path.IndexOf("[trailers]", StringComparison.OrdinalIgnoreCase) != -1 ||
string.Equals(collectionType, CollectionType.Trailers, StringComparison.OrdinalIgnoreCase))
{
2013-07-12 19:56:40 +00:00
return FindMovie<Trailer>(args.Path, args.FileSystemChildren);
}
2013-07-12 19:56:40 +00:00
if (args.Path.IndexOf("[musicvideos]", StringComparison.OrdinalIgnoreCase) != -1 ||
string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
{
2013-07-12 19:56:40 +00:00
return FindMovie<MusicVideo>(args.Path, args.FileSystemChildren);
}
if (!string.IsNullOrEmpty(collectionType) &&
!string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
{
return null;
}
2013-07-12 19:56:40 +00:00
return FindMovie<Movie>(args.Path, args.FileSystemChildren);
2013-02-21 01:33:05 +00:00
}
return null;
}
/// <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);
SetProviderIdFromPath(item);
}
/// <summary>
/// Sets the provider id from path.
/// </summary>
/// <param name="item">The item.</param>
private void SetProviderIdFromPath(Video item)
2013-02-21 01:33:05 +00:00
{
//we need to only look at the name of this actual item (not parents)
var justName = Path.GetFileName(item.Path);
2013-02-21 01:33:05 +00:00
var id = justName.GetAttributeValue("tmdbid");
if (!string.IsNullOrEmpty(id))
{
item.SetProviderId(MetadataProviders.Tmdb, id);
}
}
/// <summary>
/// Finds a movie based on a child file system entries
/// </summary>
2013-06-16 19:02:57 +00:00
/// <typeparam name="T"></typeparam>
/// <param name="path">The path.</param>
/// <param name="fileSystemEntries">The file system entries.</param>
2013-02-21 01:33:05 +00:00
/// <returns>Movie.</returns>
2013-07-12 19:56:40 +00:00
private T FindMovie<T>(string path, IEnumerable<FileSystemInfo> fileSystemEntries)
2013-06-16 19:02:57 +00:00
where T : Video, new()
2013-02-21 01:33:05 +00:00
{
var movies = new List<T>();
2013-02-21 01:33:05 +00:00
2013-06-16 19:02:57 +00:00
var multiDiscFolders = new List<FileSystemInfo>();
2013-02-21 01:33:05 +00:00
// Loop through each child file/folder and see if we find a video
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;
if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
2013-02-21 01:33:05 +00:00
{
2013-06-20 02:23:07 +00:00
if (IsDvdDirectory(filename))
2013-02-21 01:33:05 +00:00
{
return 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
};
}
2013-06-20 02:23:07 +00:00
if (IsBluRayDirectory(filename))
2013-02-21 01:33:05 +00:00
{
return 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
};
}
2013-06-16 19:02:57 +00:00
2013-06-20 02:23:07 +00:00
if (EntityResolutionHelper.IsMultiPartFile(filename))
2013-02-21 01:33:05 +00:00
{
2013-06-16 19:02:57 +00:00
multiDiscFolders.Add(child);
2013-02-21 01:33:05 +00:00
}
continue;
}
// Don't misidentify xbmc trailers as a movie
2013-06-20 02:23:07 +00:00
if (filename.IndexOf(BaseItem.XbmcTrailerFileSuffix, StringComparison.OrdinalIgnoreCase) != -1)
{
continue;
}
2013-03-04 05:43:06 +00:00
var childArgs = new ItemResolveArgs(ApplicationPaths)
2013-02-21 01:33:05 +00:00
{
FileInfo = child,
Path = child.FullName
2013-02-21 01:33:05 +00:00
};
var item = ResolveVideo<T>(childArgs);
2013-02-21 01:33:05 +00:00
if (item != null)
{
movies.Add(item);
}
}
if (movies.Count > 1)
{
return GetMultiFileMovie(movies);
}
2013-06-16 19:02:57 +00:00
if (movies.Count == 1)
{
return movies[0];
}
if (multiDiscFolders.Count > 0)
{
return GetMultiDiscMovie<T>(multiDiscFolders);
}
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>
/// <typeparam name="T"></typeparam>
/// <param name="folders">The folders.</param>
/// <returns>``0.</returns>
private T GetMultiDiscMovie<T>(List<FileSystemInfo> folders)
where T : Video, new()
{
var videoType = VideoType.BluRay;
2013-06-20 02:23:07 +00:00
var folderPaths = folders.Select(i => i.FullName).Where(i =>
2013-06-16 19:02:57 +00:00
{
2013-06-20 02:23:07 +00:00
var subfolders = Directory.GetDirectories(i).Select(Path.GetFileName).ToList();
2013-06-16 19:02:57 +00:00
if (subfolders.Any(IsDvdDirectory))
{
videoType = VideoType.Dvd;
return true;
}
if (subfolders.Any(IsBluRayDirectory))
{
videoType = VideoType.BluRay;
return true;
}
return false;
2013-06-20 02:23:07 +00:00
}).OrderBy(i => i).ToList();
2013-06-16 19:02:57 +00:00
2013-06-20 02:23:07 +00:00
if (folderPaths.Count == 0)
2013-06-16 19:02:57 +00:00
{
return null;
}
return new T
{
2013-06-20 02:23:07 +00:00
Path = folderPaths[0],
2013-06-16 19:02:57 +00:00
IsMultiPart = true,
VideoType = videoType
};
}
/// <summary>
/// Gets the multi file movie.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="movies">The movies.</param>
/// <returns>``0.</returns>
private T GetMultiFileMovie<T>(List<T> movies)
where T : Video, new()
{
var multiPartMovies = movies.OrderBy(i => i.Path)
2013-06-16 19:32:53 +00:00
.Where(i => EntityResolutionHelper.IsMultiPartFile(i.Path))
.ToList();
// They must all be part of the sequence
if (multiPartMovies.Count != movies.Count)
{
return null;
}
var firstPart = multiPartMovies[0];
firstPart.IsMultiPart = true;
return firstPart;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Determines whether [is DVD directory] [the specified directory name].
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
/// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
private bool IsDvdDirectory(string directoryName)
{
return directoryName.Equals("video_ts", StringComparison.OrdinalIgnoreCase);
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Determines whether [is hd DVD directory] [the specified directory name].
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
/// <returns><c>true</c> if [is hd DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
private bool IsHdDvdDirectory(string directoryName)
{
return directoryName.Equals("hvdvd_ts", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Determines whether [is blu ray directory] [the specified directory name].
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
/// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
private bool IsBluRayDirectory(string directoryName)
{
return directoryName.Equals("bdmv", StringComparison.OrdinalIgnoreCase);
}
}
}