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

183 lines
6.5 KiB
C#
Raw Normal View History

using System;
using System.IO;
2013-12-01 07:25:19 +01:00
using System.Linq;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
namespace Emby.Server.Implementations.Library
{
/// <summary>
/// Class ResolverHelper.
/// </summary>
public static class ResolverHelper
{
2014-12-04 06:24:41 +01: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>
2019-01-13 21:37:13 +01:00
/// <exception cref="ArgumentException">Item must have a path</exception>
2014-12-04 06:24:41 +01:00
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
2018-09-12 19:26:21 +02:00
if (string.IsNullOrEmpty(item.Path))
2014-12-04 06:24:41 +01:00
{
throw new ArgumentException("Item must have a Path");
}
// If the resolver didn't specify this
if (parent != null)
{
2015-07-09 07:52:25 +02:00
item.SetParent(parent);
2014-12-04 06:24:41 +01:00
}
item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
2015-11-11 15:56:31 +01:00
item.GetParents().Any(i => i.IsLocked);
2014-12-04 06:24:41 +01:00
// Make sure DateCreated and DateModified have values
var fileInfo = directoryService.GetFile(item.Path);
SetDateCreated(item, fileSystem, fileInfo);
2017-07-25 20:32:03 +02:00
EnsureName(item, item.Path, fileInfo);
2014-12-04 06:24:41 +01:00
}
/// <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>
2014-12-04 06:24:41 +01:00
/// <param name="libraryManager">The library manager.</param>
2014-11-30 20:01:33 +01:00
public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
{
// 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-09 07:52:25 +02:00
item.SetParent(args.Parent);
}
2014-11-30 20:01:33 +01:00
item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
2013-06-25 20:10:39 +02:00
// Make sure the item has a name
2017-07-25 20:32:03 +02:00
EnsureName(item, item.Path, args.FileInfo);
2014-04-27 05:42:05 +02:00
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
2015-11-11 15:56:31 +01:00
item.GetParents().Any(i => i.IsLocked);
2013-06-09 15:31:23 +02:00
// Make sure DateCreated and DateModified have values
2016-07-24 18:46:17 +02:00
EnsureDates(fileSystem, item, args);
}
/// <summary>
/// Ensures the name.
/// </summary>
2017-07-25 20:32:03 +02:00
private static void EnsureName(BaseItem item, string fullPath, FileSystemMetadata fileInfo)
{
// If the subclass didn't supply a name, add it here
2017-07-25 20:32:03 +02:00
if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(fullPath))
{
2017-07-25 20:32:03 +02:00
var fileName = fileInfo == null ? Path.GetFileName(fullPath) : fileInfo.Name;
item.Name = GetDisplayName(fileName, fileInfo != null && fileInfo.IsDirectory);
}
}
/// <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)
{
return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
}
2014-12-04 06:24:41 +01:00
/// <summary>
/// Ensures DateCreated and DateModified have values.
2014-12-04 06:24:41 +01:00
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
2016-07-24 18:46:17 +02:00
private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
2014-12-04 06:24:41 +01:00
{
if (fileSystem == null)
{
throw new ArgumentNullException(nameof(fileSystem));
2014-12-04 06:24:41 +01:00
}
2020-04-19 15:18:28 +02:00
2014-12-04 06:24:41 +01:00
if (item == null)
{
throw new ArgumentNullException(nameof(item));
2014-12-04 06:24:41 +01:00
}
2020-04-19 15:18:28 +02:00
2014-12-04 06:24:41 +01:00
if (args == null)
{
throw new ArgumentNullException(nameof(args));
2014-12-04 06:24:41 +01:00
}
// See if a different path came out of the resolver than what went in
2018-09-12 19:26:21 +02:00
if (!fileSystem.AreEqual(args.Path, item.Path))
2014-12-04 06:24:41 +01:00
{
var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
if (childData != null)
{
2016-07-24 18:46:17 +02:00
SetDateCreated(item, fileSystem, childData);
2014-12-04 06:24:41 +01:00
}
else
{
var fileData = fileSystem.GetFileSystemInfo(item.Path);
if (fileData.Exists)
{
2016-07-24 18:46:17 +02:00
SetDateCreated(item, fileSystem, fileData);
2014-12-04 06:24:41 +01:00
}
}
}
else
{
2016-07-24 18:46:17 +02:00
SetDateCreated(item, fileSystem, args.FileInfo);
2014-12-04 06:24:41 +01:00
}
}
2015-10-04 05:38:46 +02:00
private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info)
2014-12-04 06:24:41 +01:00
{
var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
if (config.UseFileCreationTimeForDateAdded)
{
2017-07-25 20:32:03 +02:00
// directoryService.getFile may return null
if (info != null)
{
2018-09-12 19:26:21 +02:00
var dateCreated = fileSystem.GetCreationTimeUtc(info);
if (dateCreated.Equals(DateTime.MinValue))
{
dateCreated = DateTime.UtcNow;
}
item.DateCreated = dateCreated;
2017-07-25 20:32:03 +02:00
}
2014-12-04 06:24:41 +01:00
}
else
{
item.DateCreated = DateTime.UtcNow;
}
}
}
}