using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using System.IO; using System.Text.RegularExpressions; namespace MediaBrowser.Controller.Resolvers { /// /// Class BaseItemResolver /// /// public abstract class BaseItemResolver : IBaseItemResolver where T : BaseItem, new() { /// /// Resolves the specified args. /// /// The args. /// `0. protected virtual T Resolve(ItemResolveArgs args) { return null; } /// /// Gets the priority. /// /// The priority. public virtual ResolverPriority Priority { get { return ResolverPriority.First; } } /// /// Sets initial values on the newly resolved item /// /// The item. /// The args. protected virtual void SetInitialItemValues(T item, ItemResolveArgs 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.Path.GetMBId(item.GetType()); item.DisplayMediaType = item.GetType().Name; } /// /// Resolves the path. /// /// The args. /// BaseItem. public BaseItem ResolvePath(ItemResolveArgs args) { T item = Resolve(args); if (item != null) { // Set the args on the item item.ResolveArgs = args; // 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 EntityResolutionHelper.EnsureDates(item, args); } return item; } /// /// Ensures the name. /// /// The item. private void EnsureName(T item) { // If the subclass didn't supply a name, add it here if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path)) { //we use our resolve args name here to get the name of the containg folder, not actual video file item.Name = GetMBName(item.ResolveArgs.FileInfo.cFileName, item.ResolveArgs.FileInfo.IsDirectory); } } /// /// The MB name regex /// private static readonly Regex MBNameRegex = new Regex("(\\[.*\\])", RegexOptions.Compiled); /// /// Strip out attribute items and return just the name we will use for items /// /// Assumed to be a file or directory path /// if set to true [is directory]. /// The cleaned name private static string GetMBName(string path, bool isDirectory) { //first just get the file or directory name var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path); //now - strip out anything inside brackets fn = MBNameRegex.Replace(fn, string.Empty); return fn; } } /// /// Weed this to keep a list of resolvers, since Resolvers are built with generics /// public interface IBaseItemResolver { /// /// Resolves the path. /// /// The args. /// BaseItem. BaseItem ResolvePath(ItemResolveArgs args); /// /// Gets the priority. /// /// The priority. ResolverPriority Priority { get; } } /// /// Enum ResolverPriority /// public enum ResolverPriority { /// /// The first /// First = 1, /// /// The second /// Second = 2, /// /// The third /// Third = 3, /// /// The last /// Last = 4 } }