|  |  | 1 |  | #nullable disable | 
|  |  | 2 |  |  | 
|  |  | 3 |  | #pragma warning disable CS1591 | 
|  |  | 4 |  |  | 
|  |  | 5 |  | using System; | 
|  |  | 6 |  | using System.Collections.Generic; | 
|  |  | 7 |  | using System.Linq; | 
|  |  | 8 |  | using Jellyfin.Data.Enums; | 
|  |  | 9 |  | using MediaBrowser.Controller.Entities; | 
|  |  | 10 |  | using MediaBrowser.Model.Configuration; | 
|  |  | 11 |  | using MediaBrowser.Model.IO; | 
|  |  | 12 |  |  | 
|  |  | 13 |  | namespace MediaBrowser.Controller.Library | 
|  |  | 14 |  | { | 
|  |  | 15 |  |     /// <summary> | 
|  |  | 16 |  |     /// These are arguments relating to the file system that are collected once and then referred to | 
|  |  | 17 |  |     /// whenever needed.  Primarily for entity resolution. | 
|  |  | 18 |  |     /// </summary> | 
|  |  | 19 |  |     public class ItemResolveArgs | 
|  |  | 20 |  |     { | 
|  |  | 21 |  |         /// <summary> | 
|  |  | 22 |  |         /// The _app paths. | 
|  |  | 23 |  |         /// </summary> | 
|  |  | 24 |  |         private readonly IServerApplicationPaths _appPaths; | 
|  |  | 25 |  |  | 
|  |  | 26 |  |         private readonly ILibraryManager _libraryManager; | 
|  |  | 27 |  |         private LibraryOptions _libraryOptions; | 
|  |  | 28 |  |  | 
|  |  | 29 |  |         /// <summary> | 
|  |  | 30 |  |         /// Initializes a new instance of the <see cref="ItemResolveArgs" /> class. | 
|  |  | 31 |  |         /// </summary> | 
|  |  | 32 |  |         /// <param name="appPaths">The app paths.</param> | 
|  |  | 33 |  |         /// <param name="libraryManager">The library manager.</param> | 
|  |  | 34 |  |         public ItemResolveArgs(IServerApplicationPaths appPaths, ILibraryManager libraryManager) | 
|  |  | 35 |  |         { | 
|  | 177 | 36 |  |             _appPaths = appPaths; | 
|  | 177 | 37 |  |             _libraryManager = libraryManager; | 
|  | 177 | 38 |  |         } | 
|  |  | 39 |  |  | 
|  |  | 40 |  |         /// <summary> | 
|  |  | 41 |  |         /// Gets or sets the file system children. | 
|  |  | 42 |  |         /// </summary> | 
|  |  | 43 |  |         /// <value>The file system children.</value> | 
|  |  | 44 |  |         public FileSystemMetadata[] FileSystemChildren { get; set; } | 
|  |  | 45 |  |  | 
|  |  | 46 |  |         public LibraryOptions LibraryOptions | 
|  |  | 47 |  |         { | 
|  | 0 | 48 |  |             get => _libraryOptions ??= Parent is null ? new LibraryOptions() : _libraryManager.GetLibraryOptions(Parent) | 
|  | 76 | 49 |  |             set => _libraryOptions = value; | 
|  |  | 50 |  |         } | 
|  |  | 51 |  |  | 
|  |  | 52 |  |         /// <summary> | 
|  |  | 53 |  |         /// Gets or sets the parent. | 
|  |  | 54 |  |         /// </summary> | 
|  |  | 55 |  |         /// <value>The parent.</value> | 
|  |  | 56 |  |         public Folder Parent { get; set; } | 
|  |  | 57 |  |  | 
|  |  | 58 |  |         /// <summary> | 
|  |  | 59 |  |         /// Gets or sets the file info. | 
|  |  | 60 |  |         /// </summary> | 
|  |  | 61 |  |         /// <value>The file info.</value> | 
|  |  | 62 |  |         public FileSystemMetadata FileInfo { get; set; } | 
|  |  | 63 |  |  | 
|  |  | 64 |  |         /// <summary> | 
|  |  | 65 |  |         /// Gets the path. | 
|  |  | 66 |  |         /// </summary> | 
|  |  | 67 |  |         /// <value>The path.</value> | 
|  | 905 | 68 |  |         public string Path => FileInfo.FullName; | 
|  |  | 69 |  |  | 
|  |  | 70 |  |         /// <summary> | 
|  |  | 71 |  |         /// Gets a value indicating whether this instance is directory. | 
|  |  | 72 |  |         /// </summary> | 
|  |  | 73 |  |         /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value> | 
|  | 688 | 74 |  |         public bool IsDirectory => FileInfo.IsDirectory; | 
|  |  | 75 |  |  | 
|  |  | 76 |  |         /// <summary> | 
|  |  | 77 |  |         /// Gets a value indicating whether this instance is vf. | 
|  |  | 78 |  |         /// </summary> | 
|  |  | 79 |  |         /// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value> | 
|  |  | 80 |  |         public bool IsVf | 
|  |  | 81 |  |         { | 
|  |  | 82 |  |             // we should be considered a virtual folder if we are a child of one of the children of the system root fold | 
|  |  | 83 |  |             //  this is a bit of a trick to determine that...  the directory name of a sub-child of the root will start  | 
|  |  | 84 |  |             //  the root but not be equal to it | 
|  |  | 85 |  |             get | 
|  |  | 86 |  |             { | 
|  | 56 | 87 |  |                 if (!IsDirectory) | 
|  |  | 88 |  |                 { | 
|  | 0 | 89 |  |                     return false; | 
|  |  | 90 |  |                 } | 
|  |  | 91 |  |  | 
|  | 56 | 92 |  |                 var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty; | 
|  |  | 93 |  |  | 
|  | 56 | 94 |  |                 return parentDir.Length > _appPaths.RootFolderPath.Length | 
|  | 56 | 95 |  |                        && parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase); | 
|  |  | 96 |  |             } | 
|  |  | 97 |  |         } | 
|  |  | 98 |  |  | 
|  |  | 99 |  |         /// <summary> | 
|  |  | 100 |  |         /// Gets a value indicating whether this instance is physical root. | 
|  |  | 101 |  |         /// </summary> | 
|  |  | 102 |  |         /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value> | 
|  | 98 | 103 |  |         public bool IsPhysicalRoot => IsDirectory && BaseItem.FileSystem.AreEqual(Path, _appPaths.RootFolderPath); | 
|  |  | 104 |  |  | 
|  |  | 105 |  |         /// <summary> | 
|  |  | 106 |  |         /// Gets or sets the additional locations. | 
|  |  | 107 |  |         /// </summary> | 
|  |  | 108 |  |         /// <value>The additional locations.</value> | 
|  |  | 109 |  |         private List<string> AdditionalLocations { get; set; } | 
|  |  | 110 |  |  | 
|  |  | 111 |  |         /// <summary> | 
|  |  | 112 |  |         /// Gets the physical locations. | 
|  |  | 113 |  |         /// </summary> | 
|  |  | 114 |  |         /// <value>The physical locations.</value> | 
|  |  | 115 |  |         public string[] PhysicalLocations | 
|  |  | 116 |  |         { | 
|  |  | 117 |  |             get | 
|  |  | 118 |  |             { | 
|  | 151 | 119 |  |                 var paths = string.IsNullOrEmpty(Path) ? Array.Empty<string>() : [Path]; | 
|  | 151 | 120 |  |                 return AdditionalLocations is null ? paths : [..paths, ..AdditionalLocations]; | 
|  |  | 121 |  |             } | 
|  |  | 122 |  |         } | 
|  |  | 123 |  |  | 
|  |  | 124 |  |         public CollectionType? CollectionType { get; set; } | 
|  |  | 125 |  |  | 
|  |  | 126 |  |         public bool HasParent<T>() | 
|  |  | 127 |  |             where T : Folder | 
|  |  | 128 |  |         { | 
|  | 10 | 129 |  |             var parent = Parent; | 
|  |  | 130 |  |  | 
|  | 10 | 131 |  |             if (parent is not null) | 
|  |  | 132 |  |             { | 
|  | 10 | 133 |  |                 var item = parent as T; | 
|  |  | 134 |  |  | 
|  |  | 135 |  |                 // Just in case the user decided to nest episodes. | 
|  |  | 136 |  |                 // Not officially supported but in some cases we can handle it. | 
|  | 10 | 137 |  |                 if (item is null) | 
|  |  | 138 |  |                 { | 
|  | 10 | 139 |  |                     var parents = parent.GetParents(); | 
|  | 20 | 140 |  |                     foreach (var currentParent in parents) | 
|  |  | 141 |  |                     { | 
|  | 0 | 142 |  |                         if (currentParent is T) | 
|  |  | 143 |  |                         { | 
|  | 0 | 144 |  |                             return true; | 
|  |  | 145 |  |                         } | 
|  |  | 146 |  |                     } | 
|  |  | 147 |  |                 } | 
|  |  | 148 |  |  | 
|  | 10 | 149 |  |                 return item is not null; | 
|  |  | 150 |  |             } | 
|  |  | 151 |  |  | 
|  | 0 | 152 |  |             return false; | 
|  | 0 | 153 |  |         } | 
|  |  | 154 |  |  | 
|  |  | 155 |  |         /// <summary> | 
|  |  | 156 |  |         /// Determines whether the specified <see cref="object" /> is equal to this instance. | 
|  |  | 157 |  |         /// </summary> | 
|  |  | 158 |  |         /// <param name="obj">The object to compare with the current object.</param> | 
|  |  | 159 |  |         /// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</ | 
|  |  | 160 |  |         public override bool Equals(object obj) | 
|  |  | 161 |  |         { | 
|  | 0 | 162 |  |             return Equals(obj as ItemResolveArgs); | 
|  |  | 163 |  |         } | 
|  |  | 164 |  |  | 
|  |  | 165 |  |         /// <summary> | 
|  |  | 166 |  |         /// Adds the additional location. | 
|  |  | 167 |  |         /// </summary> | 
|  |  | 168 |  |         /// <param name="path">The path.</param> | 
|  |  | 169 |  |         /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception> | 
|  |  | 170 |  |         public void AddAdditionalLocation(string path) | 
|  |  | 171 |  |         { | 
|  | 0 | 172 |  |             ArgumentException.ThrowIfNullOrEmpty(path); | 
|  |  | 173 |  |  | 
|  | 0 | 174 |  |             AdditionalLocations ??= new List<string>(); | 
|  | 0 | 175 |  |             AdditionalLocations.Add(path); | 
|  | 0 | 176 |  |         } | 
|  |  | 177 |  |  | 
|  |  | 178 |  |         // REVIEW: @bond | 
|  |  | 179 |  |  | 
|  |  | 180 |  |         /// <summary> | 
|  |  | 181 |  |         /// Gets the name of the file system entry by. | 
|  |  | 182 |  |         /// </summary> | 
|  |  | 183 |  |         /// <param name="name">The name.</param> | 
|  |  | 184 |  |         /// <returns>FileSystemInfo.</returns> | 
|  |  | 185 |  |         /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception> | 
|  |  | 186 |  |         public FileSystemMetadata GetFileSystemEntryByName(string name) | 
|  |  | 187 |  |         { | 
|  | 0 | 188 |  |             ArgumentException.ThrowIfNullOrEmpty(name); | 
|  |  | 189 |  |  | 
|  | 0 | 190 |  |             return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name)); | 
|  |  | 191 |  |         } | 
|  |  | 192 |  |  | 
|  |  | 193 |  |         /// <summary> | 
|  |  | 194 |  |         /// Gets the file system entry by path. | 
|  |  | 195 |  |         /// </summary> | 
|  |  | 196 |  |         /// <param name="path">The path.</param> | 
|  |  | 197 |  |         /// <returns>FileSystemInfo.</returns> | 
|  |  | 198 |  |         /// <exception cref="ArgumentNullException">Throws if path is invalid.</exception> | 
|  |  | 199 |  |         public FileSystemMetadata GetFileSystemEntryByPath(string path) | 
|  |  | 200 |  |         { | 
|  | 0 | 201 |  |             ArgumentException.ThrowIfNullOrEmpty(path); | 
|  |  | 202 |  |  | 
|  | 0 | 203 |  |             foreach (var file in FileSystemChildren) | 
|  |  | 204 |  |             { | 
|  | 0 | 205 |  |                 if (string.Equals(file.FullName, path, StringComparison.Ordinal)) | 
|  |  | 206 |  |                 { | 
|  | 0 | 207 |  |                     return file; | 
|  |  | 208 |  |                 } | 
|  |  | 209 |  |             } | 
|  |  | 210 |  |  | 
|  | 0 | 211 |  |             return null; | 
|  |  | 212 |  |         } | 
|  |  | 213 |  |  | 
|  |  | 214 |  |         /// <summary> | 
|  |  | 215 |  |         /// Determines whether [contains file system entry by name] [the specified name]. | 
|  |  | 216 |  |         /// </summary> | 
|  |  | 217 |  |         /// <param name="name">The name.</param> | 
|  |  | 218 |  |         /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.< | 
|  |  | 219 |  |         public bool ContainsFileSystemEntryByName(string name) | 
|  |  | 220 |  |         { | 
|  | 0 | 221 |  |             return GetFileSystemEntryByName(name) is not null; | 
|  |  | 222 |  |         } | 
|  |  | 223 |  |  | 
|  |  | 224 |  |         public CollectionType? GetCollectionType() | 
|  |  | 225 |  |         { | 
|  | 73 | 226 |  |             return CollectionType; | 
|  |  | 227 |  |         } | 
|  |  | 228 |  |  | 
|  |  | 229 |  |         /// <summary> | 
|  |  | 230 |  |         /// Gets the configured content type for the path. | 
|  |  | 231 |  |         /// </summary> | 
|  |  | 232 |  |         /// <returns>The configured content type.</returns> | 
|  |  | 233 |  |         public CollectionType? GetConfiguredContentType() | 
|  |  | 234 |  |         { | 
|  | 0 | 235 |  |             return _libraryManager.GetConfiguredContentType(Path); | 
|  |  | 236 |  |         } | 
|  |  | 237 |  |  | 
|  |  | 238 |  |         /// <summary> | 
|  |  | 239 |  |         /// Gets the file system children that do not hit the ignore file check. | 
|  |  | 240 |  |         /// </summary> | 
|  |  | 241 |  |         /// <returns>The file system children that are not ignored.</returns> | 
|  |  | 242 |  |         public IEnumerable<FileSystemMetadata> GetActualFileSystemChildren() | 
|  |  | 243 |  |         { | 
|  |  | 244 |  |             var numberOfChildren = FileSystemChildren.Length; | 
|  |  | 245 |  |             for (var i = 0; i < numberOfChildren; i++) | 
|  |  | 246 |  |             { | 
|  |  | 247 |  |                 var child = FileSystemChildren[i]; | 
|  |  | 248 |  |                 if (_libraryManager.IgnoreFile(child, Parent)) | 
|  |  | 249 |  |                 { | 
|  |  | 250 |  |                     continue; | 
|  |  | 251 |  |                 } | 
|  |  | 252 |  |  | 
|  |  | 253 |  |                 yield return child; | 
|  |  | 254 |  |             } | 
|  |  | 255 |  |         } | 
|  |  | 256 |  |  | 
|  |  | 257 |  |         /// <summary> | 
|  |  | 258 |  |         /// Returns a hash code for this instance. | 
|  |  | 259 |  |         /// </summary> | 
|  |  | 260 |  |         /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha | 
|  |  | 261 |  |         public override int GetHashCode() | 
|  |  | 262 |  |         { | 
|  | 0 | 263 |  |             return Path.GetHashCode(StringComparison.Ordinal); | 
|  |  | 264 |  |         } | 
|  |  | 265 |  |  | 
|  |  | 266 |  |         /// <summary> | 
|  |  | 267 |  |         /// Equals the specified args. | 
|  |  | 268 |  |         /// </summary> | 
|  |  | 269 |  |         /// <param name="args">The args.</param> | 
|  |  | 270 |  |         /// <returns><c>true</c> if the arguments are the same, <c>false</c> otherwise.</returns> | 
|  |  | 271 |  |         protected bool Equals(ItemResolveArgs args) | 
|  |  | 272 |  |         { | 
|  | 0 | 273 |  |             if (args is not null) | 
|  |  | 274 |  |             { | 
|  | 0 | 275 |  |                 if (args.Path is null && Path is null) | 
|  |  | 276 |  |                 { | 
|  | 0 | 277 |  |                     return true; | 
|  |  | 278 |  |                 } | 
|  |  | 279 |  |  | 
|  | 0 | 280 |  |                 return args.Path is not null && BaseItem.FileSystem.AreEqual(args.Path, Path); | 
|  |  | 281 |  |             } | 
|  |  | 282 |  |  | 
|  | 0 | 283 |  |             return false; | 
|  |  | 284 |  |         } | 
|  |  | 285 |  |     } | 
|  |  | 286 |  | } |