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

View File

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