123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- #nullable disable
- #pragma warning disable CA1721, CA1819, CS1591
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Providers;
- 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>
- public class ItemResolveArgs
- {
- /// <summary>
- /// The _app paths.
- /// </summary>
- private readonly IServerApplicationPaths _appPaths;
- private LibraryOptions _libraryOptions;
- /// <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;
- }
- // TODO remove dependencies as properties, they should be injected where it makes sense
- public IDirectoryService DirectoryService { get; }
- /// <summary>
- /// Gets or sets the file system children.
- /// </summary>
- /// <value>The file system children.</value>
- public FileSystemMetadata[] FileSystemChildren { get; set; }
- public LibraryOptions LibraryOptions
- {
- get => _libraryOptions ??= Parent is null ? new LibraryOptions() : BaseItem.LibraryManager.GetLibraryOptions(Parent);
- set => _libraryOptions = value;
- }
- /// <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>
- /// Gets the path.
- /// </summary>
- /// <value>The path.</value>
- public string Path => FileInfo.FullName;
- /// <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>
- public bool IsDirectory => FileInfo.IsDirectory;
- /// <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;
- }
- var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty;
- 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>
- public bool IsPhysicalRoot => IsDirectory && BaseItem.FileSystem.AreEqual(Path, _appPaths.RootFolderPath);
- /// <summary>
- /// Gets or sets the additional locations.
- /// </summary>
- /// <value>The additional locations.</value>
- private List<string> AdditionalLocations { get; set; }
- /// <summary>
- /// Gets the physical locations.
- /// </summary>
- /// <value>The physical locations.</value>
- public string[] PhysicalLocations
- {
- get
- {
- var paths = string.IsNullOrEmpty(Path) ? Array.Empty<string>() : new[] { Path };
- return AdditionalLocations is null ? paths : paths.Concat(AdditionalLocations).ToArray();
- }
- }
- public string CollectionType { get; set; }
- public bool HasParent<T>()
- where T : Folder
- {
- var parent = Parent;
- if (parent is not null)
- {
- var item = parent as T;
- // Just in case the user decided to nest episodes.
- // Not officially supported but in some cases we can handle it.
- if (item is null)
- {
- var parents = parent.GetParents();
- foreach (var currentParent in parents)
- {
- if (currentParent is T)
- {
- return true;
- }
- }
- }
- return item is not null;
- }
- return false;
- }
- /// <summary>
- /// Determines whether the specified <see cref="object" /> is equal to this instance.
- /// </summary>
- /// <param name="obj">The object to compare with the current object.</param>
- /// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
- public override bool Equals(object obj)
- {
- return Equals(obj as ItemResolveArgs);
- }
- /// <summary>
- /// Adds the additional location.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception>
- public void AddAdditionalLocation(string path)
- {
- ArgumentException.ThrowIfNullOrEmpty(path);
- AdditionalLocations ??= new List<string>();
- AdditionalLocations.Add(path);
- }
- // REVIEW: @bond
- /// <summary>
- /// Gets the name of the file system entry by.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <returns>FileSystemInfo.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
- public FileSystemMetadata GetFileSystemEntryByName(string name)
- {
- ArgumentException.ThrowIfNullOrEmpty(name);
- 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>
- /// <exception cref="ArgumentNullException">Throws if path is invalid.</exception>
- public FileSystemMetadata GetFileSystemEntryByPath(string path)
- {
- ArgumentException.ThrowIfNullOrEmpty(path);
- 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;
- }
- /// <summary>
- /// Gets the configured content type for the path.
- /// </summary>
- /// <remarks>
- /// This is subject to future refactoring as it relies on a static property in BaseItem.
- /// </remarks>
- /// <returns>The configured content type.</returns>
- public string GetConfiguredContentType()
- {
- return BaseItem.LibraryManager.GetConfiguredContentType(Path);
- }
- /// <summary>
- /// Gets the file system children that do not hit the ignore file check.
- /// </summary>
- /// <remarks>
- /// This is subject to future refactoring as it relies on a static property in BaseItem.
- /// </remarks>
- /// <returns>The file system children that are not ignored.</returns>
- public IEnumerable<FileSystemMetadata> GetActualFileSystemChildren()
- {
- var numberOfChildren = FileSystemChildren.Length;
- for (var i = 0; i < numberOfChildren; i++)
- {
- var child = FileSystemChildren[i];
- if (BaseItem.LibraryManager.IgnoreFile(child, Parent))
- {
- continue;
- }
- yield return child;
- }
- }
- /// <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(StringComparison.Ordinal);
- }
- /// <summary>
- /// Equals the specified args.
- /// </summary>
- /// <param name="args">The args.</param>
- /// <returns><c>true</c> if the arguments are the same, <c>false</c> otherwise.</returns>
- protected bool Equals(ItemResolveArgs args)
- {
- if (args is not null)
- {
- if (args.Path is null && Path is null)
- {
- return true;
- }
- return args.Path is not null && BaseItem.FileSystem.AreEqual(args.Path, Path);
- }
- return false;
- }
- }
- }
|