2014-11-30 12:01:33 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-03-02 23:58:04 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-12-03 22:24:41 -07:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-06-09 06:31:23 -07:00
|
|
|
|
using System;
|
2013-03-02 23:58:04 -07:00
|
|
|
|
using System.IO;
|
2013-11-30 23:25:19 -07:00
|
|
|
|
using System.Linq;
|
2013-03-02 23:58:04 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class ResolverHelper
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ResolverHelper
|
|
|
|
|
{
|
2014-12-03 22:24:41 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the initial item values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="parent">The parent.</param>
|
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
|
|
|
|
/// <exception cref="System.ArgumentException">Item must have a path</exception>
|
|
|
|
|
public static void SetInitialItemValues(BaseItem item, Folder parent, IFileSystem fileSystem, ILibraryManager libraryManager, IDirectoryService directoryService)
|
|
|
|
|
{
|
|
|
|
|
// This version of the below method has no ItemResolveArgs, so we have to require the path already being set
|
|
|
|
|
if (string.IsNullOrWhiteSpace(item.Path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Item must have a Path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the resolver didn't specify this
|
|
|
|
|
if (parent != null)
|
|
|
|
|
{
|
2015-07-08 22:52:25 -07:00
|
|
|
|
item.SetParent(parent);
|
2014-12-03 22:24:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
|
|
|
|
|
|
|
|
|
|
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
|
item.Parents.Any(i => i.IsLocked);
|
|
|
|
|
|
|
|
|
|
// Make sure DateCreated and DateModified have values
|
|
|
|
|
var fileInfo = directoryService.GetFile(item.Path);
|
|
|
|
|
item.DateModified = fileSystem.GetLastWriteTimeUtc(fileInfo);
|
|
|
|
|
SetDateCreated(item, fileSystem, fileInfo);
|
|
|
|
|
|
|
|
|
|
EnsureName(item, fileInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-02 23:58:04 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the initial item values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="args">The args.</param>
|
2013-10-30 07:40:14 -07:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2014-12-03 22:24:41 -07:00
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
2014-11-30 12:01:33 -07:00
|
|
|
|
public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
|
2013-03-02 23:58:04 -07: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)
|
|
|
|
|
{
|
2015-07-08 22:52:25 -07:00
|
|
|
|
item.SetParent(args.Parent);
|
2013-03-02 23:58:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-30 12:01:33 -07:00
|
|
|
|
item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
|
2013-06-25 11:10:39 -07:00
|
|
|
|
|
2013-03-02 23:58:04 -07:00
|
|
|
|
// Make sure the item has a name
|
2014-12-03 22:24:41 -07:00
|
|
|
|
EnsureName(item, args.FileInfo);
|
2013-03-02 23:58:04 -07:00
|
|
|
|
|
2014-04-26 20:42:05 -07:00
|
|
|
|
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
|
2014-03-22 09:16:43 -07:00
|
|
|
|
item.Parents.Any(i => i.IsLocked);
|
2013-06-09 06:31:23 -07:00
|
|
|
|
|
2013-03-02 23:58:04 -07:00
|
|
|
|
// Make sure DateCreated and DateModified have values
|
2014-12-03 22:24:41 -07:00
|
|
|
|
EnsureDates(fileSystem, item, args, true);
|
2013-03-02 23:58:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2014-12-03 22:24:41 -07:00
|
|
|
|
/// <param name="fileInfo">The file information.</param>
|
|
|
|
|
private static void EnsureName(BaseItem item, FileSystemInfo fileInfo)
|
2013-03-02 23:58:04 -07:00
|
|
|
|
{
|
|
|
|
|
// If the subclass didn't supply a name, add it here
|
|
|
|
|
if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
|
|
|
|
|
{
|
2014-12-03 22:24:41 -07:00
|
|
|
|
item.Name = GetDisplayName(fileInfo.Name, (fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory);
|
2013-03-02 23:58:04 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-11-29 12:51:30 -07:00
|
|
|
|
/// Gets the display name.
|
2013-03-02 23:58:04 -07:00
|
|
|
|
/// </summary>
|
2014-11-29 12:51:30 -07:00
|
|
|
|
/// <param name="path">The path.</param>
|
2013-03-02 23:58:04 -07:00
|
|
|
|
/// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
|
2014-11-29 12:51:30 -07:00
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private static string GetDisplayName(string path, bool isDirectory)
|
2013-03-02 23:58:04 -07:00
|
|
|
|
{
|
2014-12-03 22:24:41 -07:00
|
|
|
|
return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
|
2013-03-02 23:58:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-29 12:51:30 -07: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 13:44:08 -07:00
|
|
|
|
{
|
|
|
|
|
var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
|
2013-11-02 18:19:33 -07:00
|
|
|
|
return Regex.Replace(output, @"\s+", " ");
|
|
|
|
|
}
|
2014-12-03 22:24:41 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures DateCreated and DateModified have values
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
|
/// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
|
|
|
|
|
private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
|
|
|
|
|
{
|
|
|
|
|
if (fileSystem == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("fileSystem");
|
|
|
|
|
}
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("args");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// See if a different path came out of the resolver than what went in
|
|
|
|
|
if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
|
|
|
|
|
|
|
|
|
|
if (childData != null)
|
|
|
|
|
{
|
|
|
|
|
if (includeCreationTime)
|
|
|
|
|
{
|
|
|
|
|
SetDateCreated(item, fileSystem, childData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var fileData = fileSystem.GetFileSystemInfo(item.Path);
|
|
|
|
|
|
|
|
|
|
if (fileData.Exists)
|
|
|
|
|
{
|
|
|
|
|
if (includeCreationTime)
|
|
|
|
|
{
|
|
|
|
|
SetDateCreated(item, fileSystem, fileData);
|
|
|
|
|
}
|
|
|
|
|
item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (includeCreationTime)
|
|
|
|
|
{
|
|
|
|
|
SetDateCreated(item, fileSystem, args.FileInfo);
|
|
|
|
|
}
|
|
|
|
|
item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemInfo info)
|
|
|
|
|
{
|
|
|
|
|
var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
|
|
|
|
|
|
|
|
|
|
if (config.UseFileCreationTimeForDateAdded)
|
|
|
|
|
{
|
|
|
|
|
item.DateCreated = fileSystem.GetCreationTimeUtc(info);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item.DateCreated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-02 23:58:04 -07:00
|
|
|
|
}
|
|
|
|
|
}
|