2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2013-03-03 01:58:04 -05:00
|
|
|
using System.IO;
|
2013-12-01 01:25:19 -05:00
|
|
|
using System.Linq;
|
2019-01-13 14:21:32 -05:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2016-10-25 15:02:04 -04:00
|
|
|
using MediaBrowser.Model.IO;
|
2013-03-03 01:58:04 -05:00
|
|
|
|
2016-11-03 02:37:52 -04:00
|
|
|
namespace Emby.Server.Implementations.Library
|
2013-03-03 01:58:04 -05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-02-23 06:11:43 -05:00
|
|
|
/// Class ResolverHelper.
|
2013-03-03 01:58:04 -05:00
|
|
|
/// </summary>
|
|
|
|
public static class ResolverHelper
|
|
|
|
{
|
2014-12-04 00:24:41 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the initial item values.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="parent">The parent.</param>
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
2022-10-08 22:28:16 -04:00
|
|
|
/// <returns>True if initializing was successful.</returns>
|
2021-04-21 19:23:24 -04:00
|
|
|
/// <exception cref="ArgumentException">Item must have a path.</exception>
|
2022-10-08 22:28:16 -04:00
|
|
|
public static bool SetInitialItemValues(BaseItem item, Folder? parent, ILibraryManager libraryManager, IDirectoryService directoryService)
|
2014-12-04 00:24:41 -05:00
|
|
|
{
|
|
|
|
// This version of the below method has no ItemResolveArgs, so we have to require the path already being set
|
2018-09-12 13:26:21 -04:00
|
|
|
if (string.IsNullOrEmpty(item.Path))
|
2014-12-04 00:24:41 -05:00
|
|
|
{
|
|
|
|
throw new ArgumentException("Item must have a Path");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the resolver didn't specify this
|
|
|
|
if (parent != null)
|
|
|
|
{
|
2015-07-09 01:52:25 -04:00
|
|
|
item.SetParent(parent);
|
2014-12-04 00:24:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
|
|
|
|
|
|
|
|
item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
|
2015-11-11 09:56:31 -05:00
|
|
|
item.GetParents().Any(i => i.IsLocked);
|
2014-12-04 00:24:41 -05:00
|
|
|
|
|
|
|
// Make sure DateCreated and DateModified have values
|
|
|
|
var fileInfo = directoryService.GetFile(item.Path);
|
2021-05-06 18:52:06 -04:00
|
|
|
if (fileInfo == null)
|
|
|
|
{
|
2022-10-08 22:28:16 -04:00
|
|
|
return false;
|
2021-05-06 18:52:06 -04:00
|
|
|
}
|
|
|
|
|
2021-04-21 19:23:24 -04:00
|
|
|
SetDateCreated(item, fileInfo);
|
2014-12-04 00:24:41 -05:00
|
|
|
|
2021-04-21 19:23:24 -04:00
|
|
|
EnsureName(item, fileInfo);
|
2022-10-08 22:28:16 -04:00
|
|
|
|
|
|
|
return true;
|
2014-12-04 00:24:41 -05:00
|
|
|
}
|
|
|
|
|
2013-03-03 01:58:04 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the initial item values.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
2013-10-30 10:40:14 -04:00
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2014-12-04 00:24:41 -05:00
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
2014-11-30 14:01:33 -05:00
|
|
|
public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryManager libraryManager)
|
2013-03-03 01:58:04 -05: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-09 01:52:25 -04:00
|
|
|
item.SetParent(args.Parent);
|
2013-03-03 01:58:04 -05:00
|
|
|
}
|
|
|
|
|
2014-11-30 14:01:33 -05:00
|
|
|
item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
|
2013-06-25 14:10:39 -04:00
|
|
|
|
2013-03-03 01:58:04 -05:00
|
|
|
// Make sure the item has a name
|
2021-04-21 19:23:24 -04:00
|
|
|
EnsureName(item, args.FileInfo);
|
2013-03-03 01:58:04 -05:00
|
|
|
|
2021-04-21 19:23:24 -04:00
|
|
|
item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) ||
|
2015-11-11 09:56:31 -05:00
|
|
|
item.GetParents().Any(i => i.IsLocked);
|
2013-06-09 09:31:23 -04:00
|
|
|
|
2013-03-03 01:58:04 -05:00
|
|
|
// Make sure DateCreated and DateModified have values
|
2016-07-24 12:46:17 -04:00
|
|
|
EnsureDates(fileSystem, item, args);
|
2013-03-03 01:58:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Ensures the name.
|
|
|
|
/// </summary>
|
2021-04-21 19:23:24 -04:00
|
|
|
private static void EnsureName(BaseItem item, FileSystemMetadata fileInfo)
|
2013-03-03 01:58:04 -05:00
|
|
|
{
|
|
|
|
// If the subclass didn't supply a name, add it here
|
2021-04-21 19:23:24 -04:00
|
|
|
if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
|
2013-03-03 01:58:04 -05:00
|
|
|
{
|
2021-04-21 19:23:24 -04:00
|
|
|
item.Name = fileInfo.IsDirectory ? fileInfo.Name : Path.GetFileNameWithoutExtension(fileInfo.Name);
|
2013-03-03 01:58:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 00:24:41 -05:00
|
|
|
/// <summary>
|
2020-06-15 18:37:52 -04:00
|
|
|
/// Ensures DateCreated and DateModified have values.
|
2014-12-04 00:24:41 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
2016-07-24 12:46:17 -04:00
|
|
|
private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
|
2014-12-04 00:24:41 -05:00
|
|
|
{
|
|
|
|
// See if a different path came out of the resolver than what went in
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!fileSystem.AreEqual(args.Path, item.Path))
|
2014-12-04 00:24:41 -05:00
|
|
|
{
|
|
|
|
var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
|
|
|
|
|
|
|
|
if (childData != null)
|
|
|
|
{
|
2021-04-21 19:23:24 -04:00
|
|
|
SetDateCreated(item, childData);
|
2014-12-04 00:24:41 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var fileData = fileSystem.GetFileSystemInfo(item.Path);
|
|
|
|
|
|
|
|
if (fileData.Exists)
|
|
|
|
{
|
2021-04-21 19:23:24 -04:00
|
|
|
SetDateCreated(item, fileData);
|
2014-12-04 00:24:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-21 19:23:24 -04:00
|
|
|
SetDateCreated(item, args.FileInfo);
|
2014-12-04 00:24:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 19:23:24 -04:00
|
|
|
private static void SetDateCreated(BaseItem item, FileSystemMetadata? info)
|
2014-12-04 00:24:41 -05:00
|
|
|
{
|
|
|
|
var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
|
|
|
|
|
|
|
|
if (config.UseFileCreationTimeForDateAdded)
|
|
|
|
{
|
2017-07-25 14:32:03 -04:00
|
|
|
// directoryService.getFile may return null
|
|
|
|
if (info != null)
|
|
|
|
{
|
2021-04-21 19:23:24 -04:00
|
|
|
var dateCreated = info.CreationTimeUtc;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
if (dateCreated.Equals(DateTime.MinValue))
|
|
|
|
{
|
|
|
|
dateCreated = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.DateCreated = dateCreated;
|
2017-07-25 14:32:03 -04:00
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.DateCreated = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
}
|
2013-03-03 01:58:04 -05:00
|
|
|
}
|
|
|
|
}
|