using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
using System;
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());
// If the resolver didn't specify this
if (string.IsNullOrEmpty(item.DisplayMediaType))
{
item.DisplayMediaType = item.GetType().Name;
}
// Make sure the item has a name
EnsureName(item);
item.DontFetchMeta = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1;
// Make sure DateCreated and DateModified have values
EntityResolutionHelper.EnsureDates(item, args, true);
}
///
/// 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.Name, (item.ResolveArgs.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
}
}
///
/// 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;
}
}
}