From ac3a94f5a1dbb94b374e0160c344fcf99af9b696 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sun, 3 Mar 2013 01:58:04 -0500 Subject: moved resolvers to implementations, trimmed nuget package a bit --- .../Library/ResolverHelper.cs | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 MediaBrowser.Server.Implementations/Library/ResolverHelper.cs (limited to 'MediaBrowser.Server.Implementations/Library/ResolverHelper.cs') diff --git a/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs b/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs new file mode 100644 index 000000000..aea34b0be --- /dev/null +++ b/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs @@ -0,0 +1,82 @@ +using MediaBrowser.Common.Extensions; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using System.IO; +using System.Text.RegularExpressions; + +namespace MediaBrowser.Server.Implementations.Library +{ + /// + /// Class ResolverHelper + /// + public static class ResolverHelper + { + /// + /// Sets the initial item values. + /// + /// The item. + /// The args. + public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args) + { + item.ResolveArgs = args; + + // If the resolver didn't specify this + if (string.IsNullOrEmpty(item.Path)) + { + item.Path = args.Path; + } + + // If the resolver didn't specify this + if (args.Parent != null) + { + item.Parent = args.Parent; + } + + item.Id = item.Path.GetMBId(item.GetType()); + item.DisplayMediaType = item.GetType().Name; + + // Make sure the item has a name + EnsureName(item); + + // Make sure DateCreated and DateModified have values + EntityResolutionHelper.EnsureDates(item, args); + } + + /// + /// Ensures the name. + /// + /// The item. + private static void EnsureName(BaseItem 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; + } + + } +} -- cgit v1.2.3