jellyfin/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs

96 lines
3.3 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
2013-06-09 15:31:23 +02:00
using System;
using System.IO;
2013-12-01 07:25:19 +01:00
using System.Linq;
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>
/// <param name="fileSystem">The file system.</param>
public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem)
{
// 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());
2013-06-25 20:10:39 +02:00
// 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, args);
2014-04-27 05:42:05 +02:00
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
2014-03-22 17:16:43 +01:00
item.Parents.Any(i => i.IsLocked);
2013-06-09 15:31:23 +02:00
// Make sure DateCreated and DateModified have values
EntityResolutionHelper.EnsureDates(fileSystem, item, args, true);
}
/// <summary>
/// Ensures the name.
/// </summary>
/// <param name="item">The item.</param>
2014-11-29 20:51:30 +01:00
/// <param name="args">The arguments.</param>
private static void EnsureName(BaseItem item, ItemResolveArgs args)
{
// 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-29 20:51:30 +01:00
item.Name = GetDisplayName(args.FileInfo.Name, (args.FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
}
}
/// <summary>
2014-11-29 20:51:30 +01:00
/// Gets the display name.
/// </summary>
2014-11-29 20:51:30 +01:00
/// <param name="path">The path.</param>
/// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
2014-11-29 20:51:30 +01:00
/// <returns>System.String.</returns>
private static string GetDisplayName(string path, bool isDirectory)
{
//first just get the file or directory name
var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
return fn;
}
2014-11-29 20:51:30 +01:00
/// <summary>
/// The MB name regex
/// </summary>
private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])", RegexOptions.Compiled);
internal static string StripBrackets(string inputString)
2014-11-16 21:44:08 +01:00
{
var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
return Regex.Replace(output, @"\s+", " ");
}
}
}