2021-05-06 22:39:20 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-08-22 19:56:24 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-01-13 19:25:32 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2018-12-27 23:27:57 +00:00
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Library
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// These are arguments relating to the file system that are collected once and then referred to
|
|
|
|
/// whenever needed. Primarily for entity resolution.
|
|
|
|
/// </summary>
|
2021-05-05 13:30:32 +00:00
|
|
|
public class ItemResolveArgs
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The _app paths.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
private readonly IServerApplicationPaths _appPaths;
|
|
|
|
|
2021-05-05 13:30:32 +00:00
|
|
|
private LibraryOptions _libraryOptions;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ItemResolveArgs" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
|
|
|
public ItemResolveArgs(IServerApplicationPaths appPaths, IDirectoryService directoryService)
|
|
|
|
{
|
|
|
|
_appPaths = appPaths;
|
|
|
|
DirectoryService = directoryService;
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:30:32 +00:00
|
|
|
public IDirectoryService DirectoryService { get; }
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the file system children.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The file system children.</value>
|
|
|
|
public FileSystemMetadata[] FileSystemChildren { get; set; }
|
|
|
|
|
2021-05-05 13:30:32 +00:00
|
|
|
public LibraryOptions LibraryOptions
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2021-05-05 13:30:32 +00:00
|
|
|
get => _libraryOptions ??= Parent == null ? new LibraryOptions() : BaseItem.LibraryManager.GetLibraryOptions(Parent);
|
|
|
|
set => _libraryOptions = value;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the parent.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The parent.</value>
|
|
|
|
public Folder Parent { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the file info.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The file info.</value>
|
|
|
|
public FileSystemMetadata FileInfo { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
2021-04-21 23:23:24 +00:00
|
|
|
/// Gets the path.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The path.</value>
|
2021-04-21 23:23:24 +00:00
|
|
|
public string Path => FileInfo.FullName;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether this instance is directory.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
|
2019-01-13 20:31:14 +00:00
|
|
|
public bool IsDirectory => FileInfo.IsDirectory;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether this instance is vf.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
|
|
|
|
public bool IsVf
|
|
|
|
{
|
|
|
|
// we should be considered a virtual folder if we are a child of one of the children of the system root folder.
|
|
|
|
// this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
|
|
|
|
// the root but not be equal to it
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (!IsDirectory)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-01 13:56:16 +00:00
|
|
|
var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
return parentDir.Length > _appPaths.RootFolderPath.Length
|
|
|
|
&& parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether this instance is physical root.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
|
2019-01-13 20:31:14 +00:00
|
|
|
public bool IsPhysicalRoot => IsDirectory && BaseItem.FileSystem.AreEqual(Path, _appPaths.RootFolderPath);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the additional locations.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The additional locations.</value>
|
|
|
|
private List<string> AdditionalLocations { get; set; }
|
|
|
|
|
|
|
|
public bool HasParent<T>()
|
|
|
|
where T : Folder
|
|
|
|
{
|
|
|
|
var parent = Parent;
|
|
|
|
|
|
|
|
if (parent != null)
|
|
|
|
{
|
|
|
|
var item = parent as T;
|
|
|
|
|
2019-01-07 23:27:46 +00:00
|
|
|
// Just in case the user decided to nest episodes.
|
2018-12-27 23:27:57 +00:00
|
|
|
// Not officially supported but in some cases we can handle it.
|
|
|
|
if (item == null)
|
|
|
|
{
|
|
|
|
var parents = parent.GetParents();
|
|
|
|
foreach (var currentParent in parents)
|
|
|
|
{
|
|
|
|
if (currentParent is T)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return item != null;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds the additional location.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
2021-05-05 13:30:32 +00:00
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception>
|
2018-12-27 23:27:57 +00:00
|
|
|
public void AddAdditionalLocation(string path)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
throw new ArgumentException("The path was empty or null.", nameof(path));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 11:51:14 +00:00
|
|
|
AdditionalLocations ??= new List<string>();
|
2018-12-27 23:27:57 +00:00
|
|
|
AdditionalLocations.Add(path);
|
|
|
|
}
|
|
|
|
|
2018-12-27 21:43:48 +00:00
|
|
|
// REVIEW: @bond
|
2020-09-28 20:04:31 +00:00
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the physical locations.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The physical locations.</value>
|
|
|
|
public string[] PhysicalLocations
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2018-12-27 21:43:48 +00:00
|
|
|
var paths = string.IsNullOrEmpty(Path) ? Array.Empty<string>() : new[] { Path };
|
2018-12-27 23:27:57 +00:00
|
|
|
return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations).ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the name of the file system entry by.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <returns>FileSystemInfo.</returns>
|
2021-05-05 13:30:32 +00:00
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
|
2018-12-27 23:27:57 +00:00
|
|
|
public FileSystemMetadata GetFileSystemEntryByName(string name)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentException("The name was empty or null.", nameof(name));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the file system entry by path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <returns>FileSystemInfo.</returns>
|
2019-01-13 20:37:13 +00:00
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
2018-12-27 23:27:57 +00:00
|
|
|
public FileSystemMetadata GetFileSystemEntryByPath(string path)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentException("The path was empty or null.", nameof(path));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var file in FileSystemChildren)
|
|
|
|
{
|
|
|
|
if (string.Equals(file.FullName, path, StringComparison.Ordinal))
|
|
|
|
{
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Determines whether [contains file system entry by name] [the specified name].
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
|
|
|
|
public bool ContainsFileSystemEntryByName(string name)
|
|
|
|
{
|
|
|
|
return GetFileSystemEntryByName(name) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetCollectionType()
|
|
|
|
{
|
|
|
|
return CollectionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string CollectionType { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-13 20:37:13 +00:00
|
|
|
/// Determines whether the specified <see cref="object" /> is equal to this instance.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The object to compare with the current object.</param>
|
2019-01-13 20:37:13 +00:00
|
|
|
/// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
|
2018-12-27 23:27:57 +00:00
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
return Equals(obj as ItemResolveArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a hash code for this instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return Path.GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2018-12-27 21:43:48 +00:00
|
|
|
/// Equals the specified args.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
protected bool Equals(ItemResolveArgs args)
|
|
|
|
{
|
|
|
|
if (args != null)
|
|
|
|
{
|
2020-06-20 08:35:29 +00:00
|
|
|
if (args.Path == null && Path == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
return args.Path != null && BaseItem.FileSystem.AreEqual(args.Path, Path);
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|