2014-09-10 00:28:59 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-07-20 14:57:48 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-03-03 06:58:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-09-10 00:28:59 +00:00
|
|
|
|
using System.Globalization;
|
2013-03-03 06:58:04 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-07-20 14:57:48 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2013-03-03 06:58:04 +00:00
|
|
|
|
|
2013-03-03 16:53:58 +00:00
|
|
|
|
namespace MediaBrowser.Controller.Resolvers
|
2013-03-03 06:58:04 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class EntityResolutionHelper
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class EntityResolutionHelper
|
|
|
|
|
{
|
2014-07-13 04:55:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Any folder named in this list will be ignored - can be added to at runtime for extensibility
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static readonly List<string> IgnoreFolders = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"metadata",
|
|
|
|
|
"ps3_update",
|
|
|
|
|
"ps3_vprm",
|
|
|
|
|
"extrafanart",
|
|
|
|
|
"extrathumbs",
|
|
|
|
|
".actors",
|
|
|
|
|
".wd_tv"
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-12 21:46:50 +00:00
|
|
|
|
private static readonly Regex MultiFileRegex = new Regex(
|
|
|
|
|
@"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)(.*?)(\.[^.]+)$",
|
2013-06-16 19:02:57 +00:00
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
|
|
|
|
|
private static readonly Regex MultiFolderRegex = new Regex(
|
|
|
|
|
@"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)$",
|
|
|
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
2013-06-12 21:46:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is multi part file] [the specified path].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is multi part file] [the specified path]; otherwise, <c>false</c>.</returns>
|
|
|
|
|
public static bool IsMultiPartFile(string path)
|
|
|
|
|
{
|
2014-01-02 21:21:47 +00:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-15 22:52:43 +00:00
|
|
|
|
path = Path.GetFileName(path);
|
|
|
|
|
|
|
|
|
|
return MultiFileRegex.Match(path).Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsMultiPartFolder(string path)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path = Path.GetFileName(path);
|
|
|
|
|
|
|
|
|
|
return MultiFolderRegex.Match(path).Success;
|
2013-06-12 21:46:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 14:18:17 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The audio file extensions
|
|
|
|
|
/// </summary>
|
2014-02-13 05:11:54 +00:00
|
|
|
|
public static readonly string[] AudioFileExtensions =
|
|
|
|
|
{
|
|
|
|
|
".mp3",
|
|
|
|
|
".flac",
|
|
|
|
|
".wma",
|
|
|
|
|
".aac",
|
|
|
|
|
".acc",
|
|
|
|
|
".m4a",
|
|
|
|
|
".m4b",
|
|
|
|
|
".wav",
|
|
|
|
|
".ape",
|
|
|
|
|
".ogg",
|
2014-04-19 17:43:12 +00:00
|
|
|
|
".oga"
|
|
|
|
|
|
|
|
|
|
//".asf",
|
|
|
|
|
//".mp4"
|
2014-02-13 05:11:54 +00:00
|
|
|
|
};
|
2013-07-20 14:57:48 +00:00
|
|
|
|
|
2013-08-07 15:59:13 +00:00
|
|
|
|
private static readonly Dictionary<string, string> AudioFileExtensionsDictionary = AudioFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
2013-04-28 14:18:17 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is audio file] [the specified args].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
|
|
|
|
|
public static bool IsAudioFile(string path)
|
|
|
|
|
{
|
2014-01-02 21:21:47 +00:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-20 14:57:48 +00:00
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(extension))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 15:59:13 +00:00
|
|
|
|
return AudioFileExtensionsDictionary.ContainsKey(extension);
|
2013-04-28 14:18:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 06:58:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is video file] [the specified path].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
|
|
|
|
|
public static bool IsVideoFile(string path)
|
|
|
|
|
{
|
2014-09-10 00:28:59 +00:00
|
|
|
|
return MimeTypes.IsVideoFile(path);
|
2013-03-03 06:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-03 05:11:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is place holder] [the specified path].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">path</exception>
|
|
|
|
|
public static bool IsVideoPlaceHolder(string path)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
|
|
|
|
|
return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 03:25:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is multi disc album folder] [the specified path].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is multi disc album folder] [the specified path]; otherwise, <c>false</c>.</returns>
|
|
|
|
|
public static bool IsMultiDiscAlbumFolder(string path)
|
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileName(path);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(filename))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
|
// Remove whitespace
|
|
|
|
|
filename = filename.Replace("-", string.Empty);
|
2014-09-22 21:56:54 +00:00
|
|
|
|
filename = filename.Replace(".", string.Empty);
|
2014-08-27 03:25:39 +00:00
|
|
|
|
filename = Regex.Replace(filename, @"\s+", "");
|
|
|
|
|
|
2014-09-22 21:56:54 +00:00
|
|
|
|
var prefixes = new[] { "disc", "cd", "disk", "vol", "volume" };
|
2014-08-27 03:25:39 +00:00
|
|
|
|
|
|
|
|
|
foreach (var prefix in prefixes)
|
|
|
|
|
{
|
|
|
|
|
if (filename.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) == 0)
|
|
|
|
|
{
|
|
|
|
|
var tmp = filename.Substring(prefix.Length);
|
|
|
|
|
|
|
|
|
|
int val;
|
|
|
|
|
if (int.TryParse(tmp, NumberStyles.Any, CultureInfo.InvariantCulture, out val))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 06:58:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures DateCreated and DateModified have values
|
|
|
|
|
/// </summary>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2013-03-03 06:58:04 +00:00
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="args">The args.</param>
|
2013-08-14 12:17:45 +00:00
|
|
|
|
/// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
public static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
|
2013-03-03 06:58:04 +00:00
|
|
|
|
{
|
2013-12-20 20:17:54 +00:00
|
|
|
|
if (fileSystem == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("fileSystem");
|
|
|
|
|
}
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("args");
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 06:58:04 +00:00
|
|
|
|
// See if a different path came out of the resolver than what went in
|
2013-06-11 20:35:54 +00:00
|
|
|
|
if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
|
2013-03-03 06:58:04 +00:00
|
|
|
|
{
|
|
|
|
|
var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (childData != null)
|
2013-03-03 06:58:04 +00:00
|
|
|
|
{
|
2013-08-14 12:17:45 +00:00
|
|
|
|
if (includeCreationTime)
|
|
|
|
|
{
|
2014-10-09 22:22:04 +00:00
|
|
|
|
SetDateCreated(item, fileSystem, childData);
|
2013-08-14 12:17:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 14:03:23 +00:00
|
|
|
|
item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
|
2013-03-03 06:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-10-30 14:40:14 +00:00
|
|
|
|
var fileData = fileSystem.GetFileSystemInfo(item.Path);
|
2013-03-03 06:58:04 +00:00
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (fileData.Exists)
|
2013-03-03 06:58:04 +00:00
|
|
|
|
{
|
2013-08-14 12:17:45 +00:00
|
|
|
|
if (includeCreationTime)
|
|
|
|
|
{
|
2014-10-09 22:22:04 +00:00
|
|
|
|
SetDateCreated(item, fileSystem, fileData);
|
2013-08-14 12:17:45 +00:00
|
|
|
|
}
|
2013-10-31 14:03:23 +00:00
|
|
|
|
item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
|
2013-03-03 06:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-08-14 12:17:45 +00:00
|
|
|
|
if (includeCreationTime)
|
|
|
|
|
{
|
2014-10-09 22:22:04 +00:00
|
|
|
|
SetDateCreated(item, fileSystem, args.FileInfo);
|
2013-08-14 12:17:45 +00:00
|
|
|
|
}
|
2013-10-31 14:03:23 +00:00
|
|
|
|
item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
|
2013-03-03 06:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-09 22:22:04 +00:00
|
|
|
|
|
|
|
|
|
private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemInfo info)
|
|
|
|
|
{
|
|
|
|
|
var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
|
|
|
|
|
|
|
|
|
|
if (config.UseFileCreationTimeForDateAdded)
|
|
|
|
|
{
|
|
|
|
|
item.DateModified = fileSystem.GetCreationTimeUtc(info);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item.DateCreated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-03 06:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|