diff options
Diffstat (limited to 'MediaBrowser.Controller/Resolvers')
11 files changed, 804 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/Resolvers/AudioResolver.cs b/MediaBrowser.Controller/Resolvers/AudioResolver.cs new file mode 100644 index 000000000..8f10e45e5 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/AudioResolver.cs @@ -0,0 +1,54 @@ +using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using System.ComponentModel.Composition;
+using System.IO;
+
+namespace MediaBrowser.Controller.Resolvers
+{
+ [Export(typeof(IBaseItemResolver))]
+ public class AudioResolver : BaseItemResolver<Audio>
+ {
+ public override ResolverPriority Priority
+ {
+ get { return ResolverPriority.Last; }
+ }
+
+ protected override Audio Resolve(ItemResolveEventArgs args)
+ {
+ // Return audio if the path is a file and has a matching extension
+
+ if (!args.IsDirectory)
+ {
+ if (IsAudioFile(args.Path))
+ {
+ return new Audio();
+ }
+ }
+
+ return null;
+ }
+
+ private static bool IsAudioFile(string path)
+ {
+ string extension = Path.GetExtension(path).ToLower();
+
+ switch (extension)
+ {
+ case ".mp3":
+ case ".wma":
+ case ".aac":
+ case ".acc":
+ case ".flac":
+ case ".m4a":
+ case ".m4b":
+ case ".wav":
+ case ".ape":
+ return true;
+
+ default:
+ return false;
+ }
+
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/BaseItemResolver.cs b/MediaBrowser.Controller/Resolvers/BaseItemResolver.cs new file mode 100644 index 000000000..7c9677e4e --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/BaseItemResolver.cs @@ -0,0 +1,126 @@ +using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Common.Extensions;
+using System;
+using System.IO;
+
+namespace MediaBrowser.Controller.Resolvers
+{
+ public abstract class BaseItemResolver<T> : IBaseItemResolver
+ where T : BaseItem, new()
+ {
+ protected virtual T Resolve(ItemResolveEventArgs args)
+ {
+ return null;
+ }
+
+ public virtual ResolverPriority Priority
+ {
+ get
+ {
+ return ResolverPriority.First;
+ }
+ }
+
+ /// <summary>
+ /// Sets initial values on the newly resolved item
+ /// </summary>
+ protected virtual void SetInitialItemValues(T item, ItemResolveEventArgs args)
+ {
+ // If the subclass didn't specify this
+ if (string.IsNullOrEmpty(item.Path))
+ {
+ item.Path = args.Path;
+ }
+
+ // If the subclass didn't specify this
+ if (args.Parent != null)
+ {
+ item.Parent = args.Parent;
+ }
+
+ item.Id = (item.GetType().FullName + item.Path).GetMD5();
+ }
+
+ public BaseItem ResolvePath(ItemResolveEventArgs args)
+ {
+ T item = Resolve(args);
+
+ if (item != null)
+ {
+ // Set initial values on the newly resolved item
+ SetInitialItemValues(item, args);
+
+ // Make sure the item has a name
+ EnsureName(item);
+
+ // Make sure DateCreated and DateModified have values
+ EnsureDates(item, args);
+ }
+
+ return item;
+ }
+
+ private void EnsureName(T item)
+ {
+ // If the subclass didn't supply a name, add it here
+ if (string.IsNullOrEmpty(item.Name))
+ {
+ item.Name = Path.GetFileNameWithoutExtension(item.Path);
+ }
+
+ }
+
+ /// <summary>
+ /// Ensures DateCreated and DateModified have values
+ /// </summary>
+ private void EnsureDates(T item, ItemResolveEventArgs args)
+ {
+ if (!Path.IsPathRooted(item.Path))
+ {
+ return;
+ }
+
+ // See if a different path came out of the resolver than what went in
+ if (!args.Path.Equals(item.Path, StringComparison.OrdinalIgnoreCase))
+ {
+ WIN32_FIND_DATA? childData = args.GetFileSystemEntry(item.Path);
+
+ if (childData != null)
+ {
+ item.DateCreated = childData.Value.CreationTimeUtc;
+ item.DateModified = childData.Value.LastWriteTimeUtc;
+ }
+ else
+ {
+ WIN32_FIND_DATA fileData = FileData.GetFileData(item.Path);
+ item.DateCreated = fileData.CreationTimeUtc;
+ item.DateModified = fileData.LastWriteTimeUtc;
+ }
+ }
+ else
+ {
+ item.DateCreated = args.FileInfo.CreationTimeUtc;
+ item.DateModified = args.FileInfo.LastWriteTimeUtc;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Weed this to keep a list of resolvers, since Resolvers are built with generics
+ /// </summary>
+ public interface IBaseItemResolver
+ {
+ BaseItem ResolvePath(ItemResolveEventArgs args);
+ ResolverPriority Priority { get; }
+ }
+
+ public enum ResolverPriority
+ {
+ First = 1,
+ Second = 2,
+ Third = 3,
+ Last = 4
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/EntityResolutionHelper.cs b/MediaBrowser.Controller/Resolvers/EntityResolutionHelper.cs new file mode 100644 index 000000000..b821f8801 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/EntityResolutionHelper.cs @@ -0,0 +1,70 @@ +using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.IO;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Entities.TV;
+
+namespace MediaBrowser.Controller.Resolvers
+{
+ public static class EntityResolutionHelper
+ {
+ /// <summary>
+ /// Any folder named in this list will be ignored - can be added to at runtime for extensibility
+ /// </summary>
+ public static List<string> IgnoreFolders = new List<string>()
+ {
+ "trailers",
+ "metadata",
+ "bdmv",
+ "certificate",
+ "backup",
+ "video_ts",
+ "audio_ts",
+ "ps3_update",
+ "ps3_vprm",
+ "adv_obj",
+ "hvdvd_ts"
+ };
+ /// <summary>
+ /// Determines whether a path should be resolved or ignored entirely - called before we even look at the contents
+ /// </summary>
+ /// <param name="path"></param>
+ /// <returns>false if the path should be ignored</returns>
+ public static bool ShouldResolvePath(WIN32_FIND_DATA path)
+ {
+ bool resolve = true;
+ // Ignore hidden files and folders
+ if (path.IsHidden || path.IsSystemFile)
+ {
+ resolve = false;
+ }
+
+ // Ignore any folders in our list
+ else if (path.IsDirectory && IgnoreFolders.Contains(Path.GetFileName(path.Path), StringComparer.OrdinalIgnoreCase))
+ {
+ resolve = false;
+ }
+
+ return resolve;
+ }
+
+ /// <summary>
+ /// Determines whether a path should be ignored based on its contents - called after the contents have been read
+ /// </summary>
+ public static bool ShouldResolvePathContents(ItemResolveEventArgs args)
+ {
+ bool resolve = true;
+ if (args.ContainsFile(".ignore"))
+ {
+ // Ignore any folders containing a file called .ignore
+ resolve = false;
+ }
+
+ return resolve;
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/FolderResolver.cs b/MediaBrowser.Controller/Resolvers/FolderResolver.cs new file mode 100644 index 000000000..028c85f86 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/FolderResolver.cs @@ -0,0 +1,36 @@ +using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using System.ComponentModel.Composition;
+
+namespace MediaBrowser.Controller.Resolvers
+{
+ [Export(typeof(IBaseItemResolver))]
+ public class FolderResolver : BaseFolderResolver<Folder>
+ {
+ public override ResolverPriority Priority
+ {
+ get { return ResolverPriority.Last; }
+ }
+
+ protected override Folder Resolve(ItemResolveEventArgs args)
+ {
+ if (args.IsDirectory)
+ {
+ return new Folder();
+ }
+
+ return null;
+ }
+ }
+
+ public abstract class BaseFolderResolver<TItemType> : BaseItemResolver<TItemType>
+ where TItemType : Folder, new()
+ {
+ protected override void SetInitialItemValues(TItemType item, ItemResolveEventArgs args)
+ {
+ base.SetInitialItemValues(item, args);
+
+ item.IsRoot = args.Parent == null;
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/Movies/BoxSetResolver.cs b/MediaBrowser.Controller/Resolvers/Movies/BoxSetResolver.cs new file mode 100644 index 000000000..069068067 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/Movies/BoxSetResolver.cs @@ -0,0 +1,28 @@ +using MediaBrowser.Controller.Entities.Movies;
+using MediaBrowser.Controller.Library;
+using System;
+using System.ComponentModel.Composition;
+using System.IO;
+
+namespace MediaBrowser.Controller.Resolvers.Movies
+{
+ [Export(typeof(IBaseItemResolver))]
+ public class BoxSetResolver : BaseFolderResolver<BoxSet>
+ {
+ protected override BoxSet Resolve(ItemResolveEventArgs args)
+ {
+ // It's a boxset if all of the following conditions are met:
+ // Is a Directory
+ // Contains [boxset] in the path
+ if (args.IsDirectory)
+ {
+ if (Path.GetFileName(args.Path).IndexOf("[boxset]", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return new BoxSet();
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/Movies/MovieResolver.cs b/MediaBrowser.Controller/Resolvers/Movies/MovieResolver.cs new file mode 100644 index 000000000..825850b20 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/Movies/MovieResolver.cs @@ -0,0 +1,116 @@ +using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Movies;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Entities;
+using System.ComponentModel.Composition;
+using System.Collections.Generic;
+
+namespace MediaBrowser.Controller.Resolvers.Movies
+{
+ [Export(typeof(IBaseItemResolver))]
+ public class MovieResolver : BaseVideoResolver<Movie>
+ {
+ protected override Movie Resolve(ItemResolveEventArgs args)
+ {
+ // Must be a directory and under a 'Movies' VF
+ if (args.IsDirectory)
+ {
+ // If the parent is not a boxset, the only other allowed parent type is Folder
+ if (!(args.Parent is BoxSet))
+ {
+ if (args.Parent != null && args.Parent.GetType() != typeof(Folder))
+ {
+ return null;
+ }
+ }
+
+ // Optimization to avoid running all these tests against VF's
+ if (args.Parent != null && args.Parent.IsRoot)
+ {
+ return null;
+ }
+
+ // Return a movie if the video resolver finds something in the folder
+ return GetMovie(args);
+ }
+
+ return null;
+ }
+
+ protected override void SetInitialItemValues(Movie item, ItemResolveEventArgs args)
+ {
+ base.SetInitialItemValues(item, args);
+
+ SetProviderIdFromPath(item);
+ }
+
+ private void SetProviderIdFromPath(Movie item)
+ {
+ const string srch = "[tmdbid=";
+ int index = item.Path.IndexOf(srch, System.StringComparison.OrdinalIgnoreCase);
+
+ if (index != -1)
+ {
+ string id = item.Path.Substring(index + srch.Length);
+
+ id = id.Substring(0, id.IndexOf(']'));
+
+ item.SetProviderId(MetadataProviders.Tmdb, id);
+ }
+ }
+
+ private Movie GetMovie(ItemResolveEventArgs args)
+ {
+ //first see if the discovery process has already determined we are a DVD or BD
+ if (args.IsDVDFolder)
+ {
+ return new Movie()
+ {
+ Path = args.Path,
+ VideoType = VideoType.Dvd
+ };
+ }
+ else if (args.IsBDFolder)
+ {
+ return new Movie()
+ {
+ Path = args.Path,
+ VideoType = VideoType.BluRay
+ };
+ }
+ else if (args.IsHDDVDFolder)
+ {
+ return new Movie()
+ {
+ Path = args.Path,
+ VideoType = VideoType.HdDvd
+ };
+ }
+
+ // Loop through each child file/folder and see if we find a video
+ foreach (var child in args.FileSystemChildren)
+ {
+ var childArgs = new ItemResolveEventArgs
+ {
+ FileInfo = child,
+ FileSystemChildren = new WIN32_FIND_DATA[] { },
+ Path = child.Path
+ };
+
+ var item = base.Resolve(childArgs);
+
+ if (item != null)
+ {
+ return new Movie
+ {
+ Path = item.Path,
+ VideoType = item.VideoType
+ };
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/TV/EpisodeResolver.cs b/MediaBrowser.Controller/Resolvers/TV/EpisodeResolver.cs new file mode 100644 index 000000000..0961edd1a --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/TV/EpisodeResolver.cs @@ -0,0 +1,21 @@ +using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.Library;
+using System.ComponentModel.Composition;
+
+namespace MediaBrowser.Controller.Resolvers.TV
+{
+ [Export(typeof(IBaseItemResolver))]
+ public class EpisodeResolver : BaseVideoResolver<Episode>
+ {
+ protected override Episode Resolve(ItemResolveEventArgs args)
+ {
+ // If the parent is a Season or Series, then this is an Episode if the VideoResolver returns something
+ if (args.Parent is Season || args.Parent is Series)
+ {
+ return base.Resolve(args);
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/TV/SeasonResolver.cs b/MediaBrowser.Controller/Resolvers/TV/SeasonResolver.cs new file mode 100644 index 000000000..0ad0782e0 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/TV/SeasonResolver.cs @@ -0,0 +1,25 @@ +using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.Library;
+using System.ComponentModel.Composition;
+using System.IO;
+
+namespace MediaBrowser.Controller.Resolvers.TV
+{
+ [Export(typeof(IBaseItemResolver))]
+ public class SeasonResolver : BaseFolderResolver<Season>
+ {
+ protected override Season Resolve(ItemResolveEventArgs args)
+ {
+ if (args.Parent is Series && args.IsDirectory)
+ {
+ var season = new Season { };
+
+ season.IndexNumber = TVUtils.GetSeasonNumberFromPath(args.Path);
+
+ return season;
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/TV/SeriesResolver.cs b/MediaBrowser.Controller/Resolvers/TV/SeriesResolver.cs new file mode 100644 index 000000000..b8ff2c37b --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/TV/SeriesResolver.cs @@ -0,0 +1,64 @@ +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
+{
+ [Export(typeof(IBaseItemResolver))]
+ public class SeriesResolver : BaseFolderResolver<Series>
+ {
+ protected override Series Resolve(ItemResolveEventArgs args)
+ {
+ if (args.IsDirectory)
+ {
+ // Optimization to avoid running all these tests against VF's
+ 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
+ if (args.ContainsFile("series.xml") || Path.GetFileName(args.Path).IndexOf("[tvdbid=", StringComparison.OrdinalIgnoreCase) != -1 || TVUtils.IsSeriesFolder(args.Path, args.FileSystemChildren))
+ {
+ return new Series();
+ }
+ }
+
+ return null;
+ }
+
+ protected override void SetInitialItemValues(Series item, ItemResolveEventArgs args)
+ {
+ base.SetInitialItemValues(item, args);
+
+ SetProviderIdFromPath(item);
+ }
+
+ private void SetProviderIdFromPath(Series item)
+ {
+ const string srch = "[tvdbid=";
+ int index = item.Path.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
+
+ if (index != -1)
+ {
+ string id = item.Path.Substring(index + srch.Length);
+
+ id = id.Substring(0, id.IndexOf(']'));
+
+ item.SetProviderId(MetadataProviders.Tvdb, id);
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/TV/TVUtils.cs b/MediaBrowser.Controller/Resolvers/TV/TVUtils.cs new file mode 100644 index 000000000..ec3305e16 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/TV/TVUtils.cs @@ -0,0 +1,164 @@ +using MediaBrowser.Controller.IO;
+using System;
+using System.Text.RegularExpressions;
+
+namespace MediaBrowser.Controller.Resolvers.TV
+{
+ public static class TVUtils
+ {
+ /// <summary>
+ /// A season folder must contain one of these somewhere in the name
+ /// </summary>
+ private static readonly string[] SeasonFolderNames = new string[] {
+ "season",
+ "sæson",
+ "temporada",
+ "saison",
+ "staffel"
+ };
+
+ /// <summary>
+ /// Used to detect paths that represent episodes, need to make sure they don't also
+ /// match movie titles like "2001 A Space..."
+ /// Currently we limit the numbers here to 2 digits to try and avoid this
+ /// </summary>
+ /// <remarks>
+ /// The order here is important, if the order is changed some of the later
+ /// ones might incorrectly match things that higher ones would have caught.
+ /// The most restrictive expressions should appear first
+ /// </remarks>
+ private static readonly Regex[] episodeExpressions = new Regex[] {
+ new Regex(@".*\\[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled), // 01x02 blah.avi S01x01 balh.avi
+ new Regex(@".*\\[s|S](?<seasonnumber>\d{1,2})x?[e|E](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled), // S01E02 blah.avi, S01xE01 blah.avi
+ new Regex(@".*\\(?<seriesname>[^\\]*)[s|S]?(?<seasonnumber>\d{1,2})[x|X](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled), // 01x02 blah.avi S01x01 balh.avi
+ new Regex(@".*\\(?<seriesname>[^\\]*)[s|S](?<seasonnumber>\d{1,2})[x|X|\.]?[e|E](?<epnumber>\d{1,3})[^\\]*$", RegexOptions.Compiled) // S01E02 blah.avi, S01xE01 blah.avi
+ };
+ /// <summary>
+ /// To avoid the following matching movies they are only valid when contained in a folder which has been matched as a being season
+ /// </summary>
+ private static readonly Regex[] episodeExpressionsInASeasonFolder = new Regex[] {
+ new Regex(@".*\\(?<epnumber>\d{1,2})\s?-\s?[^\\]*$", RegexOptions.Compiled), // 01 - blah.avi, 01-blah.avi
+ new Regex(@".*\\(?<epnumber>\d{1,2})[^\d\\]*[^\\]*$", RegexOptions.Compiled), // 01.avi, 01.blah.avi "01 - 22 blah.avi"
+ new Regex(@".*\\(?<seasonnumber>\d)(?<epnumber>\d{1,2})[^\d\\]+[^\\]*$", RegexOptions.Compiled), // 01.avi, 01.blah.avi
+ new Regex(@".*\\\D*\d+(?<epnumber>\d{2})", RegexOptions.Compiled) // hell0 - 101 - hello.avi
+
+ };
+
+ public static int? GetSeasonNumberFromPath(string path)
+ {
+ // Look for one of the season folder names
+ foreach (string name in SeasonFolderNames)
+ {
+ int index = path.IndexOf(name, StringComparison.OrdinalIgnoreCase);
+
+ if (index != -1)
+ {
+ return GetSeasonNumberFromPathSubstring(path.Substring(index + name.Length));
+ }
+ }
+
+ return null;
+ }
+
+ /// <summary>
+ /// Extracts the season number from the second half of the Season folder name (everything after "Season", or "Staffel")
+ /// </summary>
+ private static int? GetSeasonNumberFromPathSubstring(string path)
+ {
+ int numericStart = -1;
+ int length = 0;
+
+ // Find out where the numbers start, and then keep going until they end
+ for (int i = 0; i < path.Length; i++)
+ {
+ if (char.IsNumber(path, i))
+ {
+ if (numericStart == -1)
+ {
+ numericStart = i;
+ }
+ length++;
+ }
+ else if (numericStart != -1)
+ {
+ break;
+ }
+ }
+
+ if (numericStart == -1)
+ {
+ return null;
+ }
+
+ return int.Parse(path.Substring(numericStart, length));
+ }
+
+ public static bool IsSeasonFolder(string path)
+ {
+ return GetSeasonNumberFromPath(path) != null;
+ }
+
+ public static bool IsSeriesFolder(string path, WIN32_FIND_DATA[] fileSystemChildren)
+ {
+ // A folder with more than 3 non-season folders in will not becounted as a series
+ int nonSeriesFolders = 0;
+
+ for (int i = 0; i < fileSystemChildren.Length; i++)
+ {
+ var child = fileSystemChildren[i];
+
+ if (child.IsHidden || child.IsSystemFile)
+ {
+ continue;
+ }
+
+ if (child.IsDirectory)
+ {
+ if (IsSeasonFolder(child.Path))
+ {
+ return true;
+ }
+
+ nonSeriesFolders++;
+
+ if (nonSeriesFolders >= 3)
+ {
+ return false;
+ }
+ }
+ else
+ {
+ if (FileSystemHelper.IsVideoFile(child.Path) && !string.IsNullOrEmpty(EpisodeNumberFromFile(child.Path, false)))
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ public static string EpisodeNumberFromFile(string fullPath, bool isInSeason)
+ {
+ string fl = fullPath.ToLower();
+ foreach (Regex r in episodeExpressions)
+ {
+ Match m = r.Match(fl);
+ if (m.Success)
+ return m.Groups["epnumber"].Value;
+ }
+ if (isInSeason)
+ {
+ foreach (Regex r in episodeExpressionsInASeasonFolder)
+ {
+ Match m = r.Match(fl);
+ if (m.Success)
+ return m.Groups["epnumber"].Value;
+ }
+
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/MediaBrowser.Controller/Resolvers/VideoResolver.cs b/MediaBrowser.Controller/Resolvers/VideoResolver.cs new file mode 100644 index 000000000..bc3be5e43 --- /dev/null +++ b/MediaBrowser.Controller/Resolvers/VideoResolver.cs @@ -0,0 +1,100 @@ +using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Controller.IO;
+using System.ComponentModel.Composition;
+using System.IO;
+
+namespace MediaBrowser.Controller.Resolvers
+{
+ /// <summary>
+ /// Resolves a Path into a Video
+ /// </summary>
+ [Export(typeof(IBaseItemResolver))]
+ public class VideoResolver : BaseVideoResolver<Video>
+ {
+ public override ResolverPriority Priority
+ {
+ get { return ResolverPriority.Last; }
+ }
+ }
+
+ /// <summary>
+ /// Resolves a Path into a Video or Video subclass
+ /// </summary>
+ public abstract class BaseVideoResolver<T> : BaseItemResolver<T>
+ where T : Video, new()
+ {
+ protected override T Resolve(ItemResolveEventArgs args)
+ {
+ // If the path is a file check for a matching extensions
+ if (!args.IsDirectory)
+ {
+ if (FileSystemHelper.IsVideoFile(args.Path))
+ {
+ VideoType type = Path.GetExtension(args.Path).EndsWith("iso", System.StringComparison.OrdinalIgnoreCase) ? VideoType.Iso : VideoType.VideoFile;
+
+ return new T
+ {
+ VideoType = type,
+ Path = args.Path
+ };
+ }
+ }
+
+ else
+ {
+ // If the path is a folder, check if it's bluray or dvd
+ T item = ResolveFromFolderName(args.Path);
+
+ if (item != null)
+ {
+ return item;
+ }
+
+ // Also check the subfolders for bluray or dvd
+ for (int i = 0; i < args.FileSystemChildren.Length; i++)
+ {
+ var folder = args.FileSystemChildren[i];
+
+ if (!folder.IsDirectory)
+ {
+ continue;
+ }
+
+ item = ResolveFromFolderName(folder.Path);
+
+ if (item != null)
+ {
+ return item;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private T ResolveFromFolderName(string folder)
+ {
+ if (folder.IndexOf("video_ts", System.StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return new T
+ {
+ VideoType = VideoType.Dvd,
+ Path = Path.GetDirectoryName(folder)
+ };
+ }
+ if (folder.IndexOf("bdmv", System.StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return new T
+ {
+ VideoType = VideoType.BluRay,
+ Path = Path.GetDirectoryName(folder)
+ };
+ }
+
+ return null;
+ }
+
+ }
+}
|
