2013-03-03 01:58:04 -05:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2013-10-31 10:03:23 -04:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-03-03 01:58:04 -05:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 09:31:23 -04:00
|
|
|
|
using System;
|
2013-03-03 01:58:04 -05:00
|
|
|
|
using System.IO;
|
2013-12-01 01:25:19 -05:00
|
|
|
|
using System.Linq;
|
2013-03-03 01:58:04 -05:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class ResolverHelper
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ResolverHelper
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the initial item values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="args">The args.</param>
|
2013-10-30 10:40:14 -04:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
|
|
|
|
public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem)
|
2013-03-03 01:58:04 -05:00
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-18 11:23:28 -04:00
|
|
|
|
item.Id = item.Path.GetMBId(item.GetType());
|
2013-06-25 14:10:39 -04:00
|
|
|
|
|
|
|
|
|
// If the resolver didn't specify this
|
|
|
|
|
if (string.IsNullOrEmpty(item.DisplayMediaType))
|
|
|
|
|
{
|
|
|
|
|
item.DisplayMediaType = item.GetType().Name;
|
|
|
|
|
}
|
2013-03-03 01:58:04 -05:00
|
|
|
|
|
|
|
|
|
// Make sure the item has a name
|
2014-01-28 13:37:01 -05:00
|
|
|
|
EnsureName(item, args);
|
2013-03-03 01:58:04 -05:00
|
|
|
|
|
2014-04-26 23:42:05 -04:00
|
|
|
|
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
|
2014-03-22 12:16:43 -04:00
|
|
|
|
item.Parents.Any(i => i.IsLocked);
|
2013-06-09 09:31:23 -04:00
|
|
|
|
|
2013-03-03 01:58:04 -05:00
|
|
|
|
// Make sure DateCreated and DateModified have values
|
2013-10-30 10:40:14 -04:00
|
|
|
|
EntityResolutionHelper.EnsureDates(fileSystem, item, args, true);
|
2013-03-03 01:58:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2014-01-28 13:37:01 -05:00
|
|
|
|
private static void EnsureName(BaseItem item, ItemResolveArgs args)
|
2013-03-03 01:58:04 -05:00
|
|
|
|
{
|
|
|
|
|
// 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
|
2014-11-16 15:44:08 -05:00
|
|
|
|
item.Name = GetMbName(args.FileInfo.Name, (args.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
|
2013-03-03 01:58:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The MB name regex
|
|
|
|
|
/// </summary>
|
2014-11-16 15:44:08 -05:00
|
|
|
|
private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])", RegexOptions.Compiled);
|
2013-03-03 01:58:04 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Strip out attribute items and return just the name we will use for items
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Assumed to be a file or directory path</param>
|
|
|
|
|
/// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
|
|
|
|
|
/// <returns>The cleaned name</returns>
|
2014-11-16 15:44:08 -05:00
|
|
|
|
private static string GetMbName(string path, bool isDirectory)
|
2013-03-03 01:58:04 -05:00
|
|
|
|
{
|
|
|
|
|
//first just get the file or directory name
|
|
|
|
|
var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
|
|
|
|
|
|
|
|
|
|
//now - strip out anything inside brackets
|
2013-11-02 21:19:33 -04:00
|
|
|
|
fn = StripBrackets(fn);
|
2013-03-03 01:58:04 -05:00
|
|
|
|
|
|
|
|
|
return fn;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-16 15:44:08 -05:00
|
|
|
|
private static string StripBrackets(string inputString)
|
|
|
|
|
{
|
|
|
|
|
var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
|
2013-11-02 21:19:33 -04:00
|
|
|
|
return Regex.Replace(output, @"\s+", " ");
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 01:58:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|