faster file extension comparisons

This commit is contained in:
Luke Pulverenti 2013-07-20 10:57:48 -04:00
parent 8ac574c74f
commit 1a1cfba795
2 changed files with 27 additions and 18 deletions

View file

@ -1,11 +1,11 @@
using System.Text.RegularExpressions; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO; using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using MediaBrowser.Controller.Library; using System.Text.RegularExpressions;
namespace MediaBrowser.Controller.Resolvers namespace MediaBrowser.Controller.Resolvers
{ {
@ -46,6 +46,8 @@ namespace MediaBrowser.Controller.Resolvers
".mts" ".mts"
}; };
private static readonly Dictionary<string, string> VideoFileExtensionsDictionary = VideoFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
private static readonly Regex MultiFileRegex = new Regex( private static readonly Regex MultiFileRegex = new Regex(
@"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)(.*?)(\.[^.]+)$", @"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)(.*?)(\.[^.]+)$",
RegexOptions.Compiled | RegexOptions.IgnoreCase); RegexOptions.Compiled | RegexOptions.IgnoreCase);
@ -67,7 +69,7 @@ namespace MediaBrowser.Controller.Resolvers
/// <summary> /// <summary>
/// The audio file extensions /// The audio file extensions
/// </summary> /// </summary>
private static readonly string[] AudioFileExtensions = new[] { private static readonly Dictionary<string,string> AudioFileExtensions = new[] {
".mp3", ".mp3",
".flac", ".flac",
".wma", ".wma",
@ -79,7 +81,8 @@ namespace MediaBrowser.Controller.Resolvers
".ape", ".ape",
".ogg", ".ogg",
".oga" ".oga"
};
}.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
/// <summary> /// <summary>
/// Determines whether [is audio file] [the specified args]. /// Determines whether [is audio file] [the specified args].
@ -88,7 +91,14 @@ namespace MediaBrowser.Controller.Resolvers
/// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
public static bool IsAudioFile(string path) public static bool IsAudioFile(string path)
{ {
return AudioFileExtensions.Contains(Path.GetExtension(path), StringComparer.OrdinalIgnoreCase); var extension = Path.GetExtension(path);
if (string.IsNullOrEmpty(extension))
{
return false;
}
return AudioFileExtensions.ContainsKey(extension);
} }
/// <summary> /// <summary>
@ -98,8 +108,14 @@ namespace MediaBrowser.Controller.Resolvers
/// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
public static bool IsVideoFile(string path) public static bool IsVideoFile(string path)
{ {
var extension = Path.GetExtension(path) ?? String.Empty; var extension = Path.GetExtension(path);
return VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
if (string.IsNullOrEmpty(extension))
{
return false;
}
return VideoFileExtensionsDictionary.ContainsKey(extension);
} }
/// <summary> /// <summary>

View file

@ -1,7 +1,6 @@
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers; using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -14,12 +13,10 @@ namespace MediaBrowser.Server.Implementations.Library
/// </summary> /// </summary>
public class CoreResolutionIgnoreRule : IResolverIgnoreRule public class CoreResolutionIgnoreRule : IResolverIgnoreRule
{ {
private readonly ILogger _logger;
/// <summary> /// <summary>
/// Any folder named in this list will be ignored - can be added to at runtime for extensibility /// Any folder named in this list will be ignored - can be added to at runtime for extensibility
/// </summary> /// </summary>
private static readonly List<string> IgnoreFolders = new List<string> private static readonly Dictionary<string,string> IgnoreFolders = new List<string>
{ {
"metadata", "metadata",
"certificate", "certificate",
@ -28,12 +25,8 @@ namespace MediaBrowser.Server.Implementations.Library
"ps3_vprm", "ps3_vprm",
"adv_obj", "adv_obj",
"extrafanart" "extrafanart"
};
public CoreResolutionIgnoreRule(ILogger logger) }.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
{
_logger = logger;
}
/// <summary> /// <summary>
/// Shoulds the ignore. /// Shoulds the ignore.
@ -81,7 +74,7 @@ namespace MediaBrowser.Server.Implementations.Library
var filename = args.FileInfo.Name; var filename = args.FileInfo.Name;
// Ignore any folders in our list // Ignore any folders in our list
if (IgnoreFolders.Contains(filename, StringComparer.OrdinalIgnoreCase)) if (IgnoreFolders.ContainsKey(filename))
{ {
return true; return true;
} }