| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CA1002, CA1721, CA1819, CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Collections.Immutable; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Security; |
| | | 11 | | using System.Text.Json.Serialization; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | using J2N.Collections.Generic.Extensions; |
| | | 15 | | using Jellyfin.Data; |
| | | 16 | | using Jellyfin.Data.Enums; |
| | | 17 | | using Jellyfin.Database.Implementations.Entities; |
| | | 18 | | using Jellyfin.Database.Implementations.Enums; |
| | | 19 | | using Jellyfin.Extensions; |
| | | 20 | | using MediaBrowser.Controller.Channels; |
| | | 21 | | using MediaBrowser.Controller.Collections; |
| | | 22 | | using MediaBrowser.Controller.Configuration; |
| | | 23 | | using MediaBrowser.Controller.Dto; |
| | | 24 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 25 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 26 | | using MediaBrowser.Controller.Library; |
| | | 27 | | using MediaBrowser.Controller.LibraryTaskScheduler; |
| | | 28 | | using MediaBrowser.Controller.Providers; |
| | | 29 | | using MediaBrowser.Model.Dto; |
| | | 30 | | using MediaBrowser.Model.IO; |
| | | 31 | | using MediaBrowser.Model.Querying; |
| | | 32 | | using Microsoft.Extensions.Logging; |
| | | 33 | | using Episode = MediaBrowser.Controller.Entities.TV.Episode; |
| | | 34 | | using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum; |
| | | 35 | | using Season = MediaBrowser.Controller.Entities.TV.Season; |
| | | 36 | | using Series = MediaBrowser.Controller.Entities.TV.Series; |
| | | 37 | | |
| | | 38 | | namespace MediaBrowser.Controller.Entities |
| | | 39 | | { |
| | | 40 | | /// <summary> |
| | | 41 | | /// Class Folder. |
| | | 42 | | /// </summary> |
| | | 43 | | public class Folder : BaseItem |
| | | 44 | | { |
| | | 45 | | private IEnumerable<BaseItem> _children; |
| | | 46 | | |
| | 355 | 47 | | public Folder() |
| | | 48 | | { |
| | 355 | 49 | | LinkedChildren = Array.Empty<LinkedChild>(); |
| | 355 | 50 | | } |
| | | 51 | | |
| | | 52 | | public static IUserViewManager UserViewManager { get; set; } |
| | | 53 | | |
| | | 54 | | public static ILimitedConcurrencyLibraryScheduler LimitedConcurrencyLibraryScheduler { get; set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets a value indicating whether this instance is root. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value> |
| | | 60 | | public bool IsRoot { get; set; } |
| | | 61 | | |
| | | 62 | | public LinkedChild[] LinkedChildren { get; set; } |
| | | 63 | | |
| | | 64 | | [JsonIgnore] |
| | | 65 | | public DateTime? DateLastMediaAdded { get; set; } |
| | | 66 | | |
| | | 67 | | [JsonIgnore] |
| | 0 | 68 | | public override bool SupportsThemeMedia => true; |
| | | 69 | | |
| | | 70 | | [JsonIgnore] |
| | 0 | 71 | | public virtual bool IsPreSorted => false; |
| | | 72 | | |
| | | 73 | | [JsonIgnore] |
| | 10 | 74 | | public virtual bool IsPhysicalRoot => false; |
| | | 75 | | |
| | | 76 | | [JsonIgnore] |
| | 0 | 77 | | public override bool SupportsInheritedParentImages => true; |
| | | 78 | | |
| | | 79 | | [JsonIgnore] |
| | 0 | 80 | | public override bool SupportsPlayedStatus => true; |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets a value indicating whether this instance is folder. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value> |
| | | 86 | | [JsonIgnore] |
| | 899 | 87 | | public override bool IsFolder => true; |
| | | 88 | | |
| | | 89 | | [JsonIgnore] |
| | 0 | 90 | | public override bool IsDisplayedAsFolder => true; |
| | | 91 | | |
| | | 92 | | [JsonIgnore] |
| | 90 | 93 | | public virtual bool SupportsCumulativeRunTimeTicks => false; |
| | | 94 | | |
| | | 95 | | [JsonIgnore] |
| | 34 | 96 | | public virtual bool SupportsDateLastMediaAdded => false; |
| | | 97 | | |
| | | 98 | | [JsonIgnore] |
| | | 99 | | public override string FileNameWithoutExtension |
| | | 100 | | { |
| | | 101 | | get |
| | | 102 | | { |
| | 304 | 103 | | if (IsFileProtocol) |
| | | 104 | | { |
| | 304 | 105 | | return System.IO.Path.GetFileName(Path); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return null; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Gets or Sets the actual children. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <value>The actual children.</value> |
| | | 116 | | [JsonIgnore] |
| | | 117 | | public virtual IEnumerable<BaseItem> Children |
| | | 118 | | { |
| | 1119 | 119 | | get => _children ??= LoadChildren(); |
| | 205 | 120 | | set => _children = value; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets thread-safe access to all recursive children of this folder - without regard to user. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <value>The recursive children.</value> |
| | | 127 | | [JsonIgnore] |
| | 0 | 128 | | public IEnumerable<BaseItem> RecursiveChildren => GetRecursiveChildren(); |
| | | 129 | | |
| | | 130 | | [JsonIgnore] |
| | 22 | 131 | | protected virtual bool SupportsShortcutChildren => false; |
| | | 132 | | |
| | 10 | 133 | | protected virtual bool FilterLinkedChildrenPerUser => false; |
| | | 134 | | |
| | | 135 | | [JsonIgnore] |
| | 90 | 136 | | protected override bool SupportsOwnedItems => base.SupportsOwnedItems || SupportsShortcutChildren; |
| | | 137 | | |
| | | 138 | | [JsonIgnore] |
| | | 139 | | public virtual bool SupportsUserDataFromChildren |
| | | 140 | | { |
| | | 141 | | get |
| | | 142 | | { |
| | | 143 | | // These are just far too slow. |
| | 9 | 144 | | if (this is ICollectionFolder) |
| | | 145 | | { |
| | 3 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 6 | 149 | | if (this is UserView) |
| | | 150 | | { |
| | 0 | 151 | | return false; |
| | | 152 | | } |
| | | 153 | | |
| | 6 | 154 | | if (this is UserRootFolder) |
| | | 155 | | { |
| | 6 | 156 | | return false; |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | if (this is Channel) |
| | | 160 | | { |
| | 0 | 161 | | return false; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (SourceType != SourceType.Library) |
| | | 165 | | { |
| | 0 | 166 | | return false; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | if (this is IItemByName) |
| | | 170 | | { |
| | 0 | 171 | | if (this is not IHasDualAccess hasDualAccess || hasDualAccess.IsAccessedByName) |
| | | 172 | | { |
| | 0 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | return true; |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | public static ICollectionManager CollectionManager { get; set; } |
| | | 182 | | |
| | | 183 | | public override bool CanDelete() |
| | | 184 | | { |
| | 6 | 185 | | if (IsRoot) |
| | | 186 | | { |
| | 6 | 187 | | return false; |
| | | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | return base.CanDelete(); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | public override bool RequiresRefresh() |
| | | 194 | | { |
| | 56 | 195 | | var baseResult = base.RequiresRefresh(); |
| | | 196 | | |
| | 56 | 197 | | if (SupportsCumulativeRunTimeTicks && !RunTimeTicks.HasValue) |
| | | 198 | | { |
| | 0 | 199 | | baseResult = true; |
| | | 200 | | } |
| | | 201 | | |
| | 56 | 202 | | return baseResult; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Adds the child. |
| | | 207 | | /// </summary> |
| | | 208 | | /// <param name="item">The item.</param> |
| | | 209 | | /// <exception cref="InvalidOperationException">Unable to add + item.Name.</exception> |
| | | 210 | | public void AddChild(BaseItem item) |
| | | 211 | | { |
| | 0 | 212 | | item.SetParent(this); |
| | | 213 | | |
| | 0 | 214 | | if (item.Id.IsEmpty()) |
| | | 215 | | { |
| | 0 | 216 | | item.Id = LibraryManager.GetNewItemId(item.Path, item.GetType()); |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | if (item.DateCreated == DateTime.MinValue) |
| | | 220 | | { |
| | 0 | 221 | | item.DateCreated = DateTime.UtcNow; |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | if (item.DateModified == DateTime.MinValue) |
| | | 225 | | { |
| | 0 | 226 | | item.DateModified = DateTime.UtcNow; |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | LibraryManager.CreateItem(item, this); |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | public override bool IsVisible(User user, bool skipAllowedTagsCheck = false) |
| | | 233 | | { |
| | 13 | 234 | | if (this is ICollectionFolder && this is not BasePluginFolder) |
| | | 235 | | { |
| | 0 | 236 | | var blockedMediaFolders = user.GetPreferenceValues<Guid>(PreferenceKind.BlockedMediaFolders); |
| | 0 | 237 | | if (blockedMediaFolders.Length > 0) |
| | | 238 | | { |
| | 0 | 239 | | if (blockedMediaFolders.Contains(Id)) |
| | | 240 | | { |
| | 0 | 241 | | return false; |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | else |
| | | 245 | | { |
| | 0 | 246 | | if (!user.HasPermission(PermissionKind.EnableAllFolders) |
| | 0 | 247 | | && !user.GetPreferenceValues<Guid>(PreferenceKind.EnabledFolders).Contains(Id)) |
| | | 248 | | { |
| | 0 | 249 | | return false; |
| | | 250 | | } |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | 13 | 254 | | return base.IsVisible(user, skipAllowedTagsCheck); |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Loads our children. Validation will occur externally. |
| | | 259 | | /// We want this synchronous. |
| | | 260 | | /// </summary> |
| | | 261 | | /// <returns>Returns children.</returns> |
| | | 262 | | protected virtual IReadOnlyList<BaseItem> LoadChildren() |
| | | 263 | | { |
| | | 264 | | // logger.LogDebug("Loading children from {0} {1} {2}", GetType().Name, Id, Path); |
| | | 265 | | // just load our children from the repo - the library will be validated and maintained in other processes |
| | 124 | 266 | | return GetCachedChildren(); |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | public override double? GetRefreshProgress() |
| | | 270 | | { |
| | 0 | 271 | | return ProviderManager.GetRefreshProgress(Id); |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | public Task ValidateChildren(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 275 | | { |
| | 0 | 276 | | return ValidateChildren(progress, new MetadataRefreshOptions(new DirectoryService(FileSystem)), cancellation |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Validates that the children of the folder still exist. |
| | | 281 | | /// </summary> |
| | | 282 | | /// <param name="progress">The progress.</param> |
| | | 283 | | /// <param name="metadataRefreshOptions">The metadata refresh options.</param> |
| | | 284 | | /// <param name="recursive">if set to <c>true</c> [recursive].</param> |
| | | 285 | | /// <param name="allowRemoveRoot">remove item even this folder is root.</param> |
| | | 286 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 287 | | /// <returns>Task.</returns> |
| | | 288 | | public Task ValidateChildren(IProgress<double> progress, MetadataRefreshOptions metadataRefreshOptions, bool rec |
| | | 289 | | { |
| | 61 | 290 | | Children = null; // invalidate cached children. |
| | 61 | 291 | | return ValidateChildrenInternal(progress, recursive, true, allowRemoveRoot, metadataRefreshOptions, metadata |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | private Dictionary<Guid, BaseItem> GetActualChildrenDictionary() |
| | | 295 | | { |
| | 59 | 296 | | var dictionary = new Dictionary<Guid, BaseItem>(); |
| | | 297 | | |
| | 59 | 298 | | Children = null; // invalidate cached children. |
| | 59 | 299 | | var childrenList = Children.ToList(); |
| | | 300 | | |
| | 208 | 301 | | foreach (var child in childrenList) |
| | | 302 | | { |
| | 45 | 303 | | var id = child.Id; |
| | 45 | 304 | | if (dictionary.ContainsKey(id)) |
| | | 305 | | { |
| | 0 | 306 | | Logger.LogError( |
| | 0 | 307 | | "Found folder containing items with duplicate id. Path: {Path}, Child Name: {ChildName}", |
| | 0 | 308 | | Path ?? Name, |
| | 0 | 309 | | child.Path ?? child.Name); |
| | | 310 | | } |
| | | 311 | | else |
| | | 312 | | { |
| | 45 | 313 | | dictionary[id] = child; |
| | | 314 | | } |
| | | 315 | | } |
| | | 316 | | |
| | 59 | 317 | | return dictionary; |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Validates the children internal. |
| | | 322 | | /// </summary> |
| | | 323 | | /// <param name="progress">The progress.</param> |
| | | 324 | | /// <param name="recursive">if set to <c>true</c> [recursive].</param> |
| | | 325 | | /// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param> |
| | | 326 | | /// <param name="allowRemoveRoot">remove item even this folder is root.</param> |
| | | 327 | | /// <param name="refreshOptions">The refresh options.</param> |
| | | 328 | | /// <param name="directoryService">The directory service.</param> |
| | | 329 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 330 | | /// <returns>Task.</returns> |
| | | 331 | | protected virtual async Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshCh |
| | | 332 | | { |
| | | 333 | | if (recursive) |
| | | 334 | | { |
| | | 335 | | ProviderManager.OnRefreshStart(this); |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | try |
| | | 339 | | { |
| | | 340 | | if (GetParents().Any(f => f.Id.Equals(Id))) |
| | | 341 | | { |
| | | 342 | | throw new InvalidOperationException("Recursive datastructure detected abort processing this item."); |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | await ValidateChildrenInternal2(progress, recursive, refreshChildMetadata, allowRemoveRoot, refreshOptio |
| | | 346 | | } |
| | | 347 | | finally |
| | | 348 | | { |
| | | 349 | | if (recursive) |
| | | 350 | | { |
| | | 351 | | ProviderManager.OnRefreshComplete(this); |
| | | 352 | | } |
| | | 353 | | } |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | private static bool IsLibraryFolderAccessible(IDirectoryService directoryService, BaseItem item, bool checkColle |
| | | 357 | | { |
| | 107 | 358 | | if (!checkCollection && (item is BoxSet || string.Equals(item.FileNameWithoutExtension, "collections", Strin |
| | | 359 | | { |
| | 0 | 360 | | return true; |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | // For top parents i.e. Library folders, skip the validation if it's empty or inaccessible |
| | 107 | 364 | | if (item.IsTopParent && !directoryService.IsAccessible(item.ContainingFolderPath)) |
| | | 365 | | { |
| | 38 | 366 | | Logger.LogWarning("Library folder {LibraryFolderPath} is inaccessible or empty, skipping", item.Containi |
| | 38 | 367 | | return false; |
| | | 368 | | } |
| | | 369 | | |
| | 69 | 370 | | return true; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | private async Task ValidateChildrenInternal2(IProgress<double> progress, bool recursive, bool refreshChildMetada |
| | | 374 | | { |
| | | 375 | | if (!IsLibraryFolderAccessible(directoryService, this, allowRemoveRoot)) |
| | | 376 | | { |
| | | 377 | | return; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 381 | | |
| | | 382 | | var validChildren = new List<BaseItem>(); |
| | | 383 | | var validChildrenNeedGeneration = false; |
| | | 384 | | |
| | | 385 | | if (IsFileProtocol) |
| | | 386 | | { |
| | | 387 | | IEnumerable<BaseItem> nonCachedChildren = []; |
| | | 388 | | |
| | | 389 | | try |
| | | 390 | | { |
| | | 391 | | nonCachedChildren = GetNonCachedChildren(directoryService); |
| | | 392 | | } |
| | | 393 | | catch (IOException ex) |
| | | 394 | | { |
| | | 395 | | Logger.LogError(ex, "Error retrieving children from file system"); |
| | | 396 | | } |
| | | 397 | | catch (SecurityException ex) |
| | | 398 | | { |
| | | 399 | | Logger.LogError(ex, "Error retrieving children from file system"); |
| | | 400 | | } |
| | | 401 | | catch (Exception ex) |
| | | 402 | | { |
| | | 403 | | Logger.LogError(ex, "Error retrieving children"); |
| | | 404 | | return; |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | progress.Report(ProgressHelpers.RetrievedChildren); |
| | | 408 | | |
| | | 409 | | if (recursive) |
| | | 410 | | { |
| | | 411 | | ProviderManager.OnRefreshProgress(this, ProgressHelpers.RetrievedChildren); |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | // Build a dictionary of the current children we have now by Id so we can compare quickly and easily |
| | | 415 | | var currentChildren = GetActualChildrenDictionary(); |
| | | 416 | | |
| | | 417 | | // Create a list for our validated children |
| | | 418 | | var newItems = new List<BaseItem>(); |
| | | 419 | | |
| | | 420 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 421 | | |
| | | 422 | | foreach (var child in nonCachedChildren) |
| | | 423 | | { |
| | | 424 | | if (!IsLibraryFolderAccessible(directoryService, child, allowRemoveRoot)) |
| | | 425 | | { |
| | | 426 | | continue; |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | if (currentChildren.TryGetValue(child.Id, out BaseItem currentChild)) |
| | | 430 | | { |
| | | 431 | | validChildren.Add(currentChild); |
| | | 432 | | |
| | | 433 | | if (currentChild.UpdateFromResolvedItem(child) > ItemUpdateType.None) |
| | | 434 | | { |
| | | 435 | | await currentChild.UpdateToRepositoryAsync(ItemUpdateType.MetadataImport, cancellationToken) |
| | | 436 | | } |
| | | 437 | | else |
| | | 438 | | { |
| | | 439 | | // metadata is up-to-date; make sure DB has correct images dimensions and hash |
| | | 440 | | await LibraryManager.UpdateImagesAsync(currentChild).ConfigureAwait(false); |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | continue; |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | // Brand new item - needs to be added |
| | | 447 | | child.SetParent(this); |
| | | 448 | | newItems.Add(child); |
| | | 449 | | validChildren.Add(child); |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | // That's all the new and changed ones - now see if any have been removed and need cleanup |
| | | 453 | | var itemsRemoved = currentChildren.Values.Except(validChildren).ToList(); |
| | | 454 | | var shouldRemove = !IsRoot || allowRemoveRoot; |
| | | 455 | | // If it's an AggregateFolder, don't remove |
| | | 456 | | if (shouldRemove && itemsRemoved.Count > 0) |
| | | 457 | | { |
| | | 458 | | foreach (var item in itemsRemoved) |
| | | 459 | | { |
| | | 460 | | if (!item.CanDelete()) |
| | | 461 | | { |
| | | 462 | | Logger.LogDebug("Item marked as non-removable, skipping: {Path}", item.Path ?? item.Name); |
| | | 463 | | continue; |
| | | 464 | | } |
| | | 465 | | |
| | | 466 | | if (item.IsFileProtocol) |
| | | 467 | | { |
| | | 468 | | Logger.LogDebug("Removed item: {Path}", item.Path); |
| | | 469 | | |
| | | 470 | | item.SetParent(null); |
| | | 471 | | LibraryManager.DeleteItem(item, new DeleteOptions { DeleteFileLocation = false }, this, fals |
| | | 472 | | } |
| | | 473 | | } |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | if (newItems.Count > 0) |
| | | 477 | | { |
| | | 478 | | LibraryManager.CreateItems(newItems, this, cancellationToken); |
| | | 479 | | } |
| | | 480 | | } |
| | | 481 | | else |
| | | 482 | | { |
| | | 483 | | validChildrenNeedGeneration = true; |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | progress.Report(ProgressHelpers.UpdatedChildItems); |
| | | 487 | | |
| | | 488 | | if (recursive) |
| | | 489 | | { |
| | | 490 | | ProviderManager.OnRefreshProgress(this, ProgressHelpers.UpdatedChildItems); |
| | | 491 | | } |
| | | 492 | | |
| | | 493 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 494 | | |
| | | 495 | | if (recursive) |
| | | 496 | | { |
| | | 497 | | var folder = this; |
| | | 498 | | var innerProgress = new Progress<double>(innerPercent => |
| | | 499 | | { |
| | | 500 | | var percent = ProgressHelpers.GetProgress(ProgressHelpers.UpdatedChildItems, ProgressHelpers.Scanned |
| | | 501 | | |
| | | 502 | | progress.Report(percent); |
| | | 503 | | |
| | | 504 | | ProviderManager.OnRefreshProgress(folder, percent); |
| | | 505 | | }); |
| | | 506 | | |
| | | 507 | | if (validChildrenNeedGeneration) |
| | | 508 | | { |
| | | 509 | | validChildren = Children.ToList(); |
| | | 510 | | validChildrenNeedGeneration = false; |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | await ValidateSubFolders(validChildren.OfType<Folder>().ToList(), directoryService, innerProgress, cance |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | if (refreshChildMetadata) |
| | | 517 | | { |
| | | 518 | | progress.Report(ProgressHelpers.ScannedSubfolders); |
| | | 519 | | |
| | | 520 | | if (recursive) |
| | | 521 | | { |
| | | 522 | | ProviderManager.OnRefreshProgress(this, ProgressHelpers.ScannedSubfolders); |
| | | 523 | | } |
| | | 524 | | |
| | | 525 | | var container = this as IMetadataContainer; |
| | | 526 | | |
| | | 527 | | var folder = this; |
| | | 528 | | var innerProgress = new Progress<double>(innerPercent => |
| | | 529 | | { |
| | | 530 | | var percent = ProgressHelpers.GetProgress(ProgressHelpers.ScannedSubfolders, ProgressHelpers.Refresh |
| | | 531 | | |
| | | 532 | | progress.Report(percent); |
| | | 533 | | |
| | | 534 | | if (recursive) |
| | | 535 | | { |
| | | 536 | | ProviderManager.OnRefreshProgress(folder, percent); |
| | | 537 | | } |
| | | 538 | | }); |
| | | 539 | | |
| | | 540 | | if (container is not null) |
| | | 541 | | { |
| | | 542 | | await RefreshAllMetadataForContainer(container, refreshOptions, innerProgress, cancellationToken).Co |
| | | 543 | | } |
| | | 544 | | else |
| | | 545 | | { |
| | | 546 | | if (validChildrenNeedGeneration) |
| | | 547 | | { |
| | | 548 | | Children = null; // invalidate cached children. |
| | | 549 | | validChildren = Children.ToList(); |
| | | 550 | | } |
| | | 551 | | |
| | | 552 | | await RefreshMetadataRecursive(validChildren, refreshOptions, recursive, innerProgress, cancellation |
| | | 553 | | } |
| | | 554 | | } |
| | | 555 | | } |
| | | 556 | | |
| | | 557 | | private async Task RefreshMetadataRecursive(IList<BaseItem> children, MetadataRefreshOptions refreshOptions, boo |
| | | 558 | | { |
| | | 559 | | await RunTasks( |
| | | 560 | | (baseItem, innerProgress) => RefreshChildMetadata(baseItem, refreshOptions, recursive && baseItem.IsFold |
| | | 561 | | children, |
| | | 562 | | progress, |
| | | 563 | | cancellationToken).ConfigureAwait(false); |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | private async Task RefreshAllMetadataForContainer(IMetadataContainer container, MetadataRefreshOptions refreshOp |
| | | 567 | | { |
| | | 568 | | if (container is Series series) |
| | | 569 | | { |
| | | 570 | | await series.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false); |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | await container.RefreshAllMetadata(refreshOptions, progress, cancellationToken).ConfigureAwait(false); |
| | | 574 | | } |
| | | 575 | | |
| | | 576 | | private async Task RefreshChildMetadata(BaseItem child, MetadataRefreshOptions refreshOptions, bool recursive, I |
| | | 577 | | { |
| | | 578 | | if (child is IMetadataContainer container) |
| | | 579 | | { |
| | | 580 | | await RefreshAllMetadataForContainer(container, refreshOptions, progress, cancellationToken).ConfigureAw |
| | | 581 | | } |
| | | 582 | | else |
| | | 583 | | { |
| | | 584 | | if (refreshOptions.RefreshItem(child)) |
| | | 585 | | { |
| | | 586 | | await child.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false); |
| | | 587 | | } |
| | | 588 | | |
| | | 589 | | if (recursive && child is Folder folder) |
| | | 590 | | { |
| | | 591 | | folder.Children = null; // invalidate cached children. |
| | | 592 | | await folder.RefreshMetadataRecursive(folder.Children.Except([this, child]).ToList(), refreshOptions |
| | | 593 | | } |
| | | 594 | | } |
| | | 595 | | } |
| | | 596 | | |
| | | 597 | | /// <summary> |
| | | 598 | | /// Refreshes the children. |
| | | 599 | | /// </summary> |
| | | 600 | | /// <param name="children">The children.</param> |
| | | 601 | | /// <param name="directoryService">The directory service.</param> |
| | | 602 | | /// <param name="progress">The progress.</param> |
| | | 603 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 604 | | /// <returns>Task.</returns> |
| | | 605 | | private async Task ValidateSubFolders(IList<Folder> children, IDirectoryService directoryService, IProgress<doub |
| | | 606 | | { |
| | | 607 | | await RunTasks( |
| | | 608 | | (folder, innerProgress) => folder.ValidateChildrenInternal(innerProgress, true, false, false, null, dire |
| | | 609 | | children, |
| | | 610 | | progress, |
| | | 611 | | cancellationToken).ConfigureAwait(false); |
| | | 612 | | } |
| | | 613 | | |
| | | 614 | | /// <summary> |
| | | 615 | | /// Runs an action block on a list of children. |
| | | 616 | | /// </summary> |
| | | 617 | | /// <param name="task">The task to run for each child.</param> |
| | | 618 | | /// <param name="children">The list of children.</param> |
| | | 619 | | /// <param name="progress">The progress.</param> |
| | | 620 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 621 | | /// <returns>Task.</returns> |
| | | 622 | | private async Task RunTasks<T>(Func<T, IProgress<double>, Task> task, IList<T> children, IProgress<double> progr |
| | | 623 | | { |
| | | 624 | | await LimitedConcurrencyLibraryScheduler |
| | | 625 | | .Enqueue( |
| | | 626 | | children.ToArray(), |
| | | 627 | | task, |
| | | 628 | | progress, |
| | | 629 | | cancellationToken) |
| | | 630 | | .ConfigureAwait(false); |
| | | 631 | | } |
| | | 632 | | |
| | | 633 | | /// <summary> |
| | | 634 | | /// Get the children of this folder from the actual file system. |
| | | 635 | | /// </summary> |
| | | 636 | | /// <returns>IEnumerable{BaseItem}.</returns> |
| | | 637 | | /// <param name="directoryService">The directory service to use for operation.</param> |
| | | 638 | | /// <returns>Returns set of base items.</returns> |
| | | 639 | | protected virtual IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService) |
| | | 640 | | { |
| | 59 | 641 | | var collectionType = LibraryManager.GetContentType(this); |
| | 59 | 642 | | var libraryOptions = LibraryManager.GetLibraryOptions(this); |
| | | 643 | | |
| | 59 | 644 | | return LibraryManager.ResolvePaths(GetFileSystemChildren(directoryService), directoryService, this, libraryO |
| | | 645 | | } |
| | | 646 | | |
| | | 647 | | /// <summary> |
| | | 648 | | /// Get our children from the repo - stubbed for now. |
| | | 649 | | /// </summary> |
| | | 650 | | /// <returns>IEnumerable{BaseItem}.</returns> |
| | | 651 | | protected IReadOnlyList<BaseItem> GetCachedChildren() |
| | | 652 | | { |
| | 124 | 653 | | return ItemRepository.GetItemList(new InternalItemsQuery |
| | 124 | 654 | | { |
| | 124 | 655 | | Parent = this, |
| | 124 | 656 | | GroupByPresentationUniqueKey = false, |
| | 124 | 657 | | DtoOptions = new DtoOptions(true) |
| | 124 | 658 | | }); |
| | | 659 | | } |
| | | 660 | | |
| | | 661 | | public virtual int GetChildCount(User user) |
| | | 662 | | { |
| | 0 | 663 | | if (LinkedChildren.Length > 0) |
| | | 664 | | { |
| | 0 | 665 | | if (this is not ICollectionFolder) |
| | | 666 | | { |
| | 0 | 667 | | return GetChildren(user, true).Count; |
| | | 668 | | } |
| | | 669 | | } |
| | | 670 | | |
| | 0 | 671 | | var result = GetItems(new InternalItemsQuery(user) |
| | 0 | 672 | | { |
| | 0 | 673 | | Recursive = false, |
| | 0 | 674 | | Limit = 0, |
| | 0 | 675 | | Parent = this, |
| | 0 | 676 | | DtoOptions = new DtoOptions(false) |
| | 0 | 677 | | { |
| | 0 | 678 | | EnableImages = false |
| | 0 | 679 | | } |
| | 0 | 680 | | }); |
| | | 681 | | |
| | 0 | 682 | | return result.TotalRecordCount; |
| | | 683 | | } |
| | | 684 | | |
| | | 685 | | public virtual int GetRecursiveChildCount(User user) |
| | | 686 | | { |
| | 0 | 687 | | return GetItems(new InternalItemsQuery(user) |
| | 0 | 688 | | { |
| | 0 | 689 | | Recursive = true, |
| | 0 | 690 | | IsFolder = false, |
| | 0 | 691 | | IsVirtualItem = false, |
| | 0 | 692 | | EnableTotalRecordCount = true, |
| | 0 | 693 | | Limit = 0, |
| | 0 | 694 | | DtoOptions = new DtoOptions(false) |
| | 0 | 695 | | { |
| | 0 | 696 | | EnableImages = false |
| | 0 | 697 | | } |
| | 0 | 698 | | }).TotalRecordCount; |
| | | 699 | | } |
| | | 700 | | |
| | | 701 | | public QueryResult<BaseItem> QueryRecursive(InternalItemsQuery query) |
| | | 702 | | { |
| | 12 | 703 | | var user = query.User; |
| | | 704 | | |
| | 12 | 705 | | if (!query.ForceDirect && RequiresPostFiltering(query)) |
| | | 706 | | { |
| | | 707 | | IEnumerable<BaseItem> items; |
| | 0 | 708 | | Func<BaseItem, bool> filter = i => UserViewBuilder.Filter(i, user, query, UserDataManager, LibraryManage |
| | | 709 | | |
| | 0 | 710 | | var totalCount = 0; |
| | 0 | 711 | | if (query.User is null) |
| | | 712 | | { |
| | 0 | 713 | | items = GetRecursiveChildren(filter); |
| | 0 | 714 | | totalCount = items.Count(); |
| | | 715 | | } |
| | | 716 | | else |
| | | 717 | | { |
| | | 718 | | // Save pagination params before clearing them to prevent pagination from happening |
| | | 719 | | // before sorting. PostFilterAndSort will apply pagination after sorting. |
| | 0 | 720 | | var limit = query.Limit; |
| | 0 | 721 | | var startIndex = query.StartIndex; |
| | 0 | 722 | | query.Limit = null; |
| | 0 | 723 | | query.StartIndex = null; |
| | | 724 | | |
| | 0 | 725 | | items = GetRecursiveChildren(user, query, out totalCount); |
| | | 726 | | |
| | | 727 | | // Restore pagination params so PostFilterAndSort can apply them after sorting |
| | 0 | 728 | | query.Limit = limit; |
| | 0 | 729 | | query.StartIndex = startIndex; |
| | | 730 | | } |
| | | 731 | | |
| | 0 | 732 | | var result = PostFilterAndSort(items, query); |
| | 0 | 733 | | result.TotalRecordCount = totalCount; |
| | 0 | 734 | | return result; |
| | | 735 | | } |
| | | 736 | | |
| | 12 | 737 | | if (this is not UserRootFolder |
| | 12 | 738 | | && this is not AggregateFolder |
| | 12 | 739 | | && query.ParentId.IsEmpty()) |
| | | 740 | | { |
| | 12 | 741 | | query.Parent = this; |
| | | 742 | | } |
| | | 743 | | |
| | 12 | 744 | | if (RequiresPostFiltering2(query)) |
| | | 745 | | { |
| | 0 | 746 | | return QueryWithPostFiltering2(query); |
| | | 747 | | } |
| | | 748 | | |
| | 12 | 749 | | return LibraryManager.GetItemsResult(query); |
| | | 750 | | } |
| | | 751 | | |
| | | 752 | | protected QueryResult<BaseItem> QueryWithPostFiltering2(InternalItemsQuery query) |
| | | 753 | | { |
| | 1 | 754 | | var startIndex = query.StartIndex; |
| | 1 | 755 | | var limit = query.Limit; |
| | | 756 | | |
| | 1 | 757 | | query.StartIndex = null; |
| | 1 | 758 | | query.Limit = null; |
| | | 759 | | |
| | 1 | 760 | | IEnumerable<BaseItem> itemsList = LibraryManager.GetItemList(query); |
| | 1 | 761 | | var user = query.User; |
| | | 762 | | |
| | 1 | 763 | | if (user is not null) |
| | | 764 | | { |
| | | 765 | | // needed for boxsets |
| | 1 | 766 | | itemsList = itemsList.Where(i => i.IsVisibleStandalone(query.User)); |
| | | 767 | | } |
| | | 768 | | |
| | | 769 | | IEnumerable<BaseItem> returnItems; |
| | 1 | 770 | | int totalCount = 0; |
| | | 771 | | |
| | 1 | 772 | | if (query.EnableTotalRecordCount) |
| | | 773 | | { |
| | 0 | 774 | | var itemArray = itemsList.ToArray(); |
| | 0 | 775 | | totalCount = itemArray.Length; |
| | 0 | 776 | | returnItems = itemArray; |
| | | 777 | | } |
| | | 778 | | else |
| | | 779 | | { |
| | 1 | 780 | | returnItems = itemsList; |
| | | 781 | | } |
| | | 782 | | |
| | 1 | 783 | | if (limit.HasValue) |
| | | 784 | | { |
| | 0 | 785 | | returnItems = returnItems.Skip(startIndex ?? 0).Take(limit.Value); |
| | | 786 | | } |
| | 1 | 787 | | else if (startIndex.HasValue) |
| | | 788 | | { |
| | 0 | 789 | | returnItems = returnItems.Skip(startIndex.Value); |
| | | 790 | | } |
| | | 791 | | |
| | 1 | 792 | | return new QueryResult<BaseItem>( |
| | 1 | 793 | | query.StartIndex, |
| | 1 | 794 | | totalCount, |
| | 1 | 795 | | returnItems.ToArray()); |
| | | 796 | | } |
| | | 797 | | |
| | | 798 | | private bool RequiresPostFiltering2(InternalItemsQuery query) |
| | | 799 | | { |
| | 12 | 800 | | if (query.IncludeItemTypes.Length == 1 && query.IncludeItemTypes[0] == BaseItemKind.BoxSet) |
| | | 801 | | { |
| | 0 | 802 | | Logger.LogDebug("Query requires post-filtering due to BoxSet query"); |
| | 0 | 803 | | return true; |
| | | 804 | | } |
| | | 805 | | |
| | 12 | 806 | | return false; |
| | | 807 | | } |
| | | 808 | | |
| | | 809 | | private bool RequiresPostFiltering(InternalItemsQuery query) |
| | | 810 | | { |
| | 12 | 811 | | if (LinkedChildren.Length > 0) |
| | | 812 | | { |
| | 0 | 813 | | if (this is not ICollectionFolder) |
| | | 814 | | { |
| | 0 | 815 | | Logger.LogDebug("{Type}: Query requires post-filtering due to LinkedChildren.", GetType().Name); |
| | 0 | 816 | | return true; |
| | | 817 | | } |
| | | 818 | | } |
| | | 819 | | |
| | | 820 | | // Filter by Video3DFormat |
| | 12 | 821 | | if (query.Is3D.HasValue) |
| | | 822 | | { |
| | 0 | 823 | | Logger.LogDebug("Query requires post-filtering due to Is3D"); |
| | 0 | 824 | | return true; |
| | | 825 | | } |
| | | 826 | | |
| | 12 | 827 | | if (query.HasOfficialRating.HasValue) |
| | | 828 | | { |
| | 0 | 829 | | Logger.LogDebug("Query requires post-filtering due to HasOfficialRating"); |
| | 0 | 830 | | return true; |
| | | 831 | | } |
| | | 832 | | |
| | 12 | 833 | | if (query.IsPlaceHolder.HasValue) |
| | | 834 | | { |
| | 0 | 835 | | Logger.LogDebug("Query requires post-filtering due to IsPlaceHolder"); |
| | 0 | 836 | | return true; |
| | | 837 | | } |
| | | 838 | | |
| | 12 | 839 | | if (query.HasSpecialFeature.HasValue) |
| | | 840 | | { |
| | 0 | 841 | | Logger.LogDebug("Query requires post-filtering due to HasSpecialFeature"); |
| | 0 | 842 | | return true; |
| | | 843 | | } |
| | | 844 | | |
| | 12 | 845 | | if (query.HasSubtitles.HasValue) |
| | | 846 | | { |
| | 0 | 847 | | Logger.LogDebug("Query requires post-filtering due to HasSubtitles"); |
| | 0 | 848 | | return true; |
| | | 849 | | } |
| | | 850 | | |
| | 12 | 851 | | if (query.HasTrailer.HasValue) |
| | | 852 | | { |
| | 0 | 853 | | Logger.LogDebug("Query requires post-filtering due to HasTrailer"); |
| | 0 | 854 | | return true; |
| | | 855 | | } |
| | | 856 | | |
| | 12 | 857 | | if (query.HasThemeSong.HasValue) |
| | | 858 | | { |
| | 0 | 859 | | Logger.LogDebug("Query requires post-filtering due to HasThemeSong"); |
| | 0 | 860 | | return true; |
| | | 861 | | } |
| | | 862 | | |
| | 12 | 863 | | if (query.HasThemeVideo.HasValue) |
| | | 864 | | { |
| | 0 | 865 | | Logger.LogDebug("Query requires post-filtering due to HasThemeVideo"); |
| | 0 | 866 | | return true; |
| | | 867 | | } |
| | | 868 | | |
| | | 869 | | // Filter by VideoType |
| | 12 | 870 | | if (query.VideoTypes.Length > 0) |
| | | 871 | | { |
| | 0 | 872 | | Logger.LogDebug("Query requires post-filtering due to VideoTypes"); |
| | 0 | 873 | | return true; |
| | | 874 | | } |
| | | 875 | | |
| | 12 | 876 | | if (CollapseBoxSetItems(query, this, query.User, ConfigurationManager)) |
| | | 877 | | { |
| | 0 | 878 | | Logger.LogDebug("Query requires post-filtering due to CollapseBoxSetItems"); |
| | 0 | 879 | | return true; |
| | | 880 | | } |
| | | 881 | | |
| | 12 | 882 | | if (!query.AdjacentTo.IsNullOrEmpty()) |
| | | 883 | | { |
| | 0 | 884 | | Logger.LogDebug("Query requires post-filtering due to AdjacentTo"); |
| | 0 | 885 | | return true; |
| | | 886 | | } |
| | | 887 | | |
| | 12 | 888 | | if (query.SeriesStatuses.Length > 0) |
| | | 889 | | { |
| | 0 | 890 | | Logger.LogDebug("Query requires post-filtering due to SeriesStatuses"); |
| | 0 | 891 | | return true; |
| | | 892 | | } |
| | | 893 | | |
| | 12 | 894 | | if (query.AiredDuringSeason.HasValue) |
| | | 895 | | { |
| | 0 | 896 | | Logger.LogDebug("Query requires post-filtering due to AiredDuringSeason"); |
| | 0 | 897 | | return true; |
| | | 898 | | } |
| | | 899 | | |
| | 12 | 900 | | if (query.IsPlayed.HasValue) |
| | | 901 | | { |
| | 0 | 902 | | if (query.IncludeItemTypes.Length == 1 && query.IncludeItemTypes.Contains(BaseItemKind.Series)) |
| | | 903 | | { |
| | 0 | 904 | | Logger.LogDebug("Query requires post-filtering due to IsPlayed"); |
| | 0 | 905 | | return true; |
| | | 906 | | } |
| | | 907 | | } |
| | | 908 | | |
| | 12 | 909 | | return false; |
| | | 910 | | } |
| | | 911 | | |
| | | 912 | | private static BaseItem[] SortItemsByRequest(InternalItemsQuery query, IReadOnlyList<BaseItem> items) |
| | | 913 | | { |
| | 0 | 914 | | return items.OrderBy(i => Array.IndexOf(query.ItemIds, i.Id)).ToArray(); |
| | | 915 | | } |
| | | 916 | | |
| | | 917 | | public QueryResult<BaseItem> GetItems(InternalItemsQuery query) |
| | | 918 | | { |
| | 0 | 919 | | if (query.ItemIds.Length > 0) |
| | | 920 | | { |
| | 0 | 921 | | var result = LibraryManager.GetItemsResult(query); |
| | | 922 | | |
| | 0 | 923 | | if (query.OrderBy.Count == 0 && query.ItemIds.Length > 1) |
| | | 924 | | { |
| | 0 | 925 | | result.Items = SortItemsByRequest(query, result.Items); |
| | | 926 | | } |
| | | 927 | | |
| | 0 | 928 | | return result; |
| | | 929 | | } |
| | | 930 | | |
| | 0 | 931 | | return GetItemsInternal(query); |
| | | 932 | | } |
| | | 933 | | |
| | | 934 | | public IReadOnlyList<BaseItem> GetItemList(InternalItemsQuery query) |
| | | 935 | | { |
| | 13 | 936 | | query.EnableTotalRecordCount = false; |
| | | 937 | | |
| | 13 | 938 | | if (query.ItemIds.Length > 0) |
| | | 939 | | { |
| | 0 | 940 | | var result = LibraryManager.GetItemList(query); |
| | | 941 | | |
| | 0 | 942 | | if (query.OrderBy.Count == 0 && query.ItemIds.Length > 1) |
| | | 943 | | { |
| | 0 | 944 | | return SortItemsByRequest(query, result); |
| | | 945 | | } |
| | | 946 | | |
| | 0 | 947 | | return result; |
| | | 948 | | } |
| | | 949 | | |
| | 13 | 950 | | return GetItemsInternal(query).Items; |
| | | 951 | | } |
| | | 952 | | |
| | | 953 | | protected virtual QueryResult<BaseItem> GetItemsInternal(InternalItemsQuery query) |
| | | 954 | | { |
| | 12 | 955 | | if (SourceType == SourceType.Channel) |
| | | 956 | | { |
| | | 957 | | try |
| | | 958 | | { |
| | 0 | 959 | | query.Parent = this; |
| | 0 | 960 | | query.ChannelIds = new[] { ChannelId }; |
| | | 961 | | |
| | | 962 | | // Don't blow up here because it could cause parent screens with other content to fail |
| | 0 | 963 | | return ChannelManager.GetChannelItemsInternal(query, new Progress<double>(), CancellationToken.None) |
| | | 964 | | } |
| | 0 | 965 | | catch |
| | | 966 | | { |
| | | 967 | | // Already logged at lower levels |
| | 0 | 968 | | return new QueryResult<BaseItem>(); |
| | | 969 | | } |
| | | 970 | | } |
| | | 971 | | |
| | 12 | 972 | | if (query.Recursive) |
| | | 973 | | { |
| | 12 | 974 | | return QueryRecursive(query); |
| | | 975 | | } |
| | | 976 | | |
| | 0 | 977 | | var user = query.User; |
| | | 978 | | |
| | 0 | 979 | | Func<BaseItem, bool> filter = i => UserViewBuilder.Filter(i, user, query, UserDataManager, LibraryManager); |
| | | 980 | | |
| | | 981 | | IEnumerable<BaseItem> items; |
| | | 982 | | |
| | 0 | 983 | | int totalItemCount = 0; |
| | 0 | 984 | | if (query.User is null) |
| | | 985 | | { |
| | 0 | 986 | | items = Children.Where(filter); |
| | 0 | 987 | | totalItemCount = items.Count(); |
| | | 988 | | } |
| | | 989 | | else |
| | | 990 | | { |
| | | 991 | | // need to pass this param to the children. |
| | | 992 | | // Note: Don't pass Limit/StartIndex here as pagination should happen after sorting in PostFilterAndSort |
| | 0 | 993 | | var childQuery = new InternalItemsQuery |
| | 0 | 994 | | { |
| | 0 | 995 | | DisplayAlbumFolders = query.DisplayAlbumFolders, |
| | 0 | 996 | | NameStartsWith = query.NameStartsWith, |
| | 0 | 997 | | NameStartsWithOrGreater = query.NameStartsWithOrGreater, |
| | 0 | 998 | | NameLessThan = query.NameLessThan |
| | 0 | 999 | | }; |
| | | 1000 | | |
| | 0 | 1001 | | items = GetChildren(user, true, out totalItemCount, childQuery).Where(filter); |
| | | 1002 | | } |
| | | 1003 | | |
| | 0 | 1004 | | var result = PostFilterAndSort(items, query); |
| | 0 | 1005 | | result.TotalRecordCount = totalItemCount; |
| | 0 | 1006 | | return result; |
| | 0 | 1007 | | } |
| | | 1008 | | |
| | | 1009 | | protected QueryResult<BaseItem> PostFilterAndSort(IEnumerable<BaseItem> items, InternalItemsQuery query) |
| | | 1010 | | { |
| | 0 | 1011 | | var user = query.User; |
| | | 1012 | | |
| | | 1013 | | // Check recursive - don't substitute in plain folder views |
| | 0 | 1014 | | if (user is not null) |
| | | 1015 | | { |
| | 0 | 1016 | | items = CollapseBoxSetItemsIfNeeded(items, query, this, user, ConfigurationManager, CollectionManager); |
| | | 1017 | | } |
| | | 1018 | | |
| | | 1019 | | #pragma warning disable CA1309 |
| | 0 | 1020 | | if (!string.IsNullOrEmpty(query.NameStartsWithOrGreater)) |
| | | 1021 | | { |
| | 0 | 1022 | | items = items.Where(i => string.Compare(query.NameStartsWithOrGreater, i.SortName, StringComparison.Inva |
| | | 1023 | | } |
| | | 1024 | | |
| | 0 | 1025 | | if (!string.IsNullOrEmpty(query.NameStartsWith)) |
| | | 1026 | | { |
| | 0 | 1027 | | items = items.Where(i => i.SortName.StartsWith(query.NameStartsWith, StringComparison.InvariantCultureIg |
| | | 1028 | | } |
| | | 1029 | | |
| | 0 | 1030 | | if (!string.IsNullOrEmpty(query.NameLessThan)) |
| | | 1031 | | { |
| | 0 | 1032 | | items = items.Where(i => string.Compare(query.NameLessThan, i.SortName, StringComparison.InvariantCultur |
| | | 1033 | | } |
| | | 1034 | | #pragma warning restore CA1309 |
| | | 1035 | | |
| | | 1036 | | // This must be the last filter |
| | 0 | 1037 | | if (!query.AdjacentTo.IsNullOrEmpty()) |
| | | 1038 | | { |
| | 0 | 1039 | | items = UserViewBuilder.FilterForAdjacency(items.ToList(), query.AdjacentTo.Value); |
| | | 1040 | | } |
| | | 1041 | | |
| | 0 | 1042 | | return UserViewBuilder.SortAndPage(items, null, query, LibraryManager); |
| | | 1043 | | } |
| | | 1044 | | |
| | | 1045 | | private static IEnumerable<BaseItem> CollapseBoxSetItemsIfNeeded( |
| | | 1046 | | IEnumerable<BaseItem> items, |
| | | 1047 | | InternalItemsQuery query, |
| | | 1048 | | BaseItem queryParent, |
| | | 1049 | | User user, |
| | | 1050 | | IServerConfigurationManager configurationManager, |
| | | 1051 | | ICollectionManager collectionManager) |
| | | 1052 | | { |
| | 0 | 1053 | | ArgumentNullException.ThrowIfNull(items); |
| | | 1054 | | |
| | 0 | 1055 | | if (CollapseBoxSetItems(query, queryParent, user, configurationManager)) |
| | | 1056 | | { |
| | 0 | 1057 | | items = collectionManager.CollapseItemsWithinBoxSets(items, user); |
| | | 1058 | | } |
| | | 1059 | | |
| | 0 | 1060 | | return items; |
| | | 1061 | | } |
| | | 1062 | | |
| | | 1063 | | private static bool CollapseBoxSetItems( |
| | | 1064 | | InternalItemsQuery query, |
| | | 1065 | | BaseItem queryParent, |
| | | 1066 | | User user, |
| | | 1067 | | IServerConfigurationManager configurationManager) |
| | | 1068 | | { |
| | | 1069 | | // Could end up stuck in a loop like this |
| | 12 | 1070 | | if (queryParent is BoxSet) |
| | | 1071 | | { |
| | 0 | 1072 | | return false; |
| | | 1073 | | } |
| | | 1074 | | |
| | 12 | 1075 | | if (queryParent is Season) |
| | | 1076 | | { |
| | 0 | 1077 | | return false; |
| | | 1078 | | } |
| | | 1079 | | |
| | 12 | 1080 | | if (queryParent is MusicAlbum) |
| | | 1081 | | { |
| | 0 | 1082 | | return false; |
| | | 1083 | | } |
| | | 1084 | | |
| | 12 | 1085 | | if (queryParent is MusicArtist) |
| | | 1086 | | { |
| | 0 | 1087 | | return false; |
| | | 1088 | | } |
| | | 1089 | | |
| | 12 | 1090 | | var param = query.CollapseBoxSetItems; |
| | | 1091 | | |
| | 12 | 1092 | | if (!param.HasValue) |
| | | 1093 | | { |
| | 0 | 1094 | | if (user is not null && query.IncludeItemTypes.Any(type => |
| | 0 | 1095 | | (type == BaseItemKind.Movie && !configurationManager.Configuration.EnableGroupingMoviesIntoCollectio |
| | 0 | 1096 | | (type == BaseItemKind.Series && !configurationManager.Configuration.EnableGroupingShowsIntoCollectio |
| | | 1097 | | { |
| | 0 | 1098 | | return false; |
| | | 1099 | | } |
| | | 1100 | | |
| | 0 | 1101 | | if (query.IncludeItemTypes.Length == 0 |
| | 0 | 1102 | | || query.IncludeItemTypes.Any(type => type == BaseItemKind.Movie || type == BaseItemKind.Series)) |
| | | 1103 | | { |
| | 0 | 1104 | | param = true; |
| | | 1105 | | } |
| | | 1106 | | } |
| | | 1107 | | |
| | 12 | 1108 | | return param.HasValue && param.Value && AllowBoxSetCollapsing(query); |
| | | 1109 | | } |
| | | 1110 | | |
| | | 1111 | | private static bool AllowBoxSetCollapsing(InternalItemsQuery request) |
| | | 1112 | | { |
| | 0 | 1113 | | if (request.IsFavorite.HasValue) |
| | | 1114 | | { |
| | 0 | 1115 | | return false; |
| | | 1116 | | } |
| | | 1117 | | |
| | 0 | 1118 | | if (request.IsFavoriteOrLiked.HasValue) |
| | | 1119 | | { |
| | 0 | 1120 | | return false; |
| | | 1121 | | } |
| | | 1122 | | |
| | 0 | 1123 | | if (request.IsLiked.HasValue) |
| | | 1124 | | { |
| | 0 | 1125 | | return false; |
| | | 1126 | | } |
| | | 1127 | | |
| | 0 | 1128 | | if (request.IsPlayed.HasValue) |
| | | 1129 | | { |
| | 0 | 1130 | | return false; |
| | | 1131 | | } |
| | | 1132 | | |
| | 0 | 1133 | | if (request.IsResumable.HasValue) |
| | | 1134 | | { |
| | 0 | 1135 | | return false; |
| | | 1136 | | } |
| | | 1137 | | |
| | 0 | 1138 | | if (request.IsFolder.HasValue) |
| | | 1139 | | { |
| | 0 | 1140 | | return false; |
| | | 1141 | | } |
| | | 1142 | | |
| | 0 | 1143 | | if (request.Genres.Count > 0) |
| | | 1144 | | { |
| | 0 | 1145 | | return false; |
| | | 1146 | | } |
| | | 1147 | | |
| | 0 | 1148 | | if (request.GenreIds.Count > 0) |
| | | 1149 | | { |
| | 0 | 1150 | | return false; |
| | | 1151 | | } |
| | | 1152 | | |
| | 0 | 1153 | | if (request.HasImdbId.HasValue) |
| | | 1154 | | { |
| | 0 | 1155 | | return false; |
| | | 1156 | | } |
| | | 1157 | | |
| | 0 | 1158 | | if (request.HasOfficialRating.HasValue) |
| | | 1159 | | { |
| | 0 | 1160 | | return false; |
| | | 1161 | | } |
| | | 1162 | | |
| | 0 | 1163 | | if (request.HasOverview.HasValue) |
| | | 1164 | | { |
| | 0 | 1165 | | return false; |
| | | 1166 | | } |
| | | 1167 | | |
| | 0 | 1168 | | if (request.HasParentalRating.HasValue) |
| | | 1169 | | { |
| | 0 | 1170 | | return false; |
| | | 1171 | | } |
| | | 1172 | | |
| | 0 | 1173 | | if (request.HasSpecialFeature.HasValue) |
| | | 1174 | | { |
| | 0 | 1175 | | return false; |
| | | 1176 | | } |
| | | 1177 | | |
| | 0 | 1178 | | if (request.HasSubtitles.HasValue) |
| | | 1179 | | { |
| | 0 | 1180 | | return false; |
| | | 1181 | | } |
| | | 1182 | | |
| | 0 | 1183 | | if (request.HasThemeSong.HasValue) |
| | | 1184 | | { |
| | 0 | 1185 | | return false; |
| | | 1186 | | } |
| | | 1187 | | |
| | 0 | 1188 | | if (request.HasThemeVideo.HasValue) |
| | | 1189 | | { |
| | 0 | 1190 | | return false; |
| | | 1191 | | } |
| | | 1192 | | |
| | 0 | 1193 | | if (request.HasTmdbId.HasValue) |
| | | 1194 | | { |
| | 0 | 1195 | | return false; |
| | | 1196 | | } |
| | | 1197 | | |
| | 0 | 1198 | | if (request.HasTrailer.HasValue) |
| | | 1199 | | { |
| | 0 | 1200 | | return false; |
| | | 1201 | | } |
| | | 1202 | | |
| | 0 | 1203 | | if (request.ImageTypes.Length > 0) |
| | | 1204 | | { |
| | 0 | 1205 | | return false; |
| | | 1206 | | } |
| | | 1207 | | |
| | 0 | 1208 | | if (request.Is3D.HasValue) |
| | | 1209 | | { |
| | 0 | 1210 | | return false; |
| | | 1211 | | } |
| | | 1212 | | |
| | 0 | 1213 | | if (request.Is4K.HasValue) |
| | | 1214 | | { |
| | 0 | 1215 | | return false; |
| | | 1216 | | } |
| | | 1217 | | |
| | 0 | 1218 | | if (request.IsHD.HasValue) |
| | | 1219 | | { |
| | 0 | 1220 | | return false; |
| | | 1221 | | } |
| | | 1222 | | |
| | 0 | 1223 | | if (request.IsLocked.HasValue) |
| | | 1224 | | { |
| | 0 | 1225 | | return false; |
| | | 1226 | | } |
| | | 1227 | | |
| | 0 | 1228 | | if (request.IsPlaceHolder.HasValue) |
| | | 1229 | | { |
| | 0 | 1230 | | return false; |
| | | 1231 | | } |
| | | 1232 | | |
| | 0 | 1233 | | if (!string.IsNullOrWhiteSpace(request.Person)) |
| | | 1234 | | { |
| | 0 | 1235 | | return false; |
| | | 1236 | | } |
| | | 1237 | | |
| | 0 | 1238 | | if (request.PersonIds.Length > 0) |
| | | 1239 | | { |
| | 0 | 1240 | | return false; |
| | | 1241 | | } |
| | | 1242 | | |
| | 0 | 1243 | | if (request.ItemIds.Length > 0) |
| | | 1244 | | { |
| | 0 | 1245 | | return false; |
| | | 1246 | | } |
| | | 1247 | | |
| | 0 | 1248 | | if (request.StudioIds.Length > 0) |
| | | 1249 | | { |
| | 0 | 1250 | | return false; |
| | | 1251 | | } |
| | | 1252 | | |
| | 0 | 1253 | | if (request.VideoTypes.Length > 0) |
| | | 1254 | | { |
| | 0 | 1255 | | return false; |
| | | 1256 | | } |
| | | 1257 | | |
| | 0 | 1258 | | if (request.Years.Length > 0) |
| | | 1259 | | { |
| | 0 | 1260 | | return false; |
| | | 1261 | | } |
| | | 1262 | | |
| | 0 | 1263 | | if (request.Tags.Length > 0) |
| | | 1264 | | { |
| | 0 | 1265 | | return false; |
| | | 1266 | | } |
| | | 1267 | | |
| | 0 | 1268 | | if (request.OfficialRatings.Length > 0) |
| | | 1269 | | { |
| | 0 | 1270 | | return false; |
| | | 1271 | | } |
| | | 1272 | | |
| | 0 | 1273 | | if (request.MinIndexNumber.HasValue) |
| | | 1274 | | { |
| | 0 | 1275 | | return false; |
| | | 1276 | | } |
| | | 1277 | | |
| | 0 | 1278 | | if (request.OrderBy.Any(o => |
| | 0 | 1279 | | o.OrderBy == ItemSortBy.CommunityRating || |
| | 0 | 1280 | | o.OrderBy == ItemSortBy.CriticRating || |
| | 0 | 1281 | | o.OrderBy == ItemSortBy.Runtime)) |
| | | 1282 | | { |
| | 0 | 1283 | | return false; |
| | | 1284 | | } |
| | | 1285 | | |
| | 0 | 1286 | | return true; |
| | | 1287 | | } |
| | | 1288 | | |
| | | 1289 | | public virtual IReadOnlyList<BaseItem> GetChildren(User user, bool includeLinkedChildren, out int totalItemCount |
| | | 1290 | | { |
| | 10 | 1291 | | ArgumentNullException.ThrowIfNull(user); |
| | 10 | 1292 | | query ??= new InternalItemsQuery(); |
| | 10 | 1293 | | query.User = user; |
| | | 1294 | | |
| | | 1295 | | // the true root should return our users root folder children |
| | 10 | 1296 | | if (IsPhysicalRoot) |
| | | 1297 | | { |
| | 0 | 1298 | | return LibraryManager.GetUserRootFolder().GetChildren(user, includeLinkedChildren, out totalItemCount); |
| | | 1299 | | } |
| | | 1300 | | |
| | 10 | 1301 | | var result = new Dictionary<Guid, BaseItem>(); |
| | | 1302 | | |
| | 10 | 1303 | | totalItemCount = AddChildren(user, includeLinkedChildren, result, false, query); |
| | | 1304 | | |
| | 10 | 1305 | | return result.Values.ToArray(); |
| | | 1306 | | } |
| | | 1307 | | |
| | | 1308 | | public virtual IReadOnlyList<BaseItem> GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery que |
| | | 1309 | | { |
| | 10 | 1310 | | return GetChildren(user, includeLinkedChildren, out _, query); |
| | | 1311 | | } |
| | | 1312 | | |
| | | 1313 | | protected virtual IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user) |
| | | 1314 | | { |
| | 10 | 1315 | | return Children; |
| | | 1316 | | } |
| | | 1317 | | |
| | | 1318 | | /// <summary> |
| | | 1319 | | /// Adds the children to list. |
| | | 1320 | | /// </summary> |
| | | 1321 | | private int AddChildren(User user, bool includeLinkedChildren, Dictionary<Guid, BaseItem> result, bool recursive |
| | | 1322 | | { |
| | | 1323 | | // Prevent infinite recursion of nested folders |
| | 10 | 1324 | | visitedFolders ??= new HashSet<Folder>(); |
| | 10 | 1325 | | if (!visitedFolders.Add(this)) |
| | | 1326 | | { |
| | 0 | 1327 | | return 0; |
| | | 1328 | | } |
| | | 1329 | | |
| | | 1330 | | // If Query.AlbumFolders is set, then enforce the format as per the db in that it permits sub-folders in mus |
| | 10 | 1331 | | IEnumerable<BaseItem> children = null; |
| | 10 | 1332 | | if ((query?.DisplayAlbumFolders ?? false) && (this is MusicAlbum)) |
| | | 1333 | | { |
| | 0 | 1334 | | children = Children; |
| | 0 | 1335 | | query = null; |
| | | 1336 | | } |
| | | 1337 | | |
| | | 1338 | | // If there are not sub-folders, proceed as normal. |
| | 10 | 1339 | | if (children is null) |
| | | 1340 | | { |
| | 10 | 1341 | | children = GetEligibleChildrenForRecursiveChildren(user); |
| | | 1342 | | } |
| | | 1343 | | |
| | 10 | 1344 | | if (includeLinkedChildren) |
| | | 1345 | | { |
| | 10 | 1346 | | children = children.Concat(GetLinkedChildren(user)).ToArray(); |
| | | 1347 | | } |
| | | 1348 | | |
| | 10 | 1349 | | return AddChildrenFromCollection(children, user, includeLinkedChildren, result, recursive, query, visitedFol |
| | | 1350 | | } |
| | | 1351 | | |
| | | 1352 | | private int AddChildrenFromCollection(IEnumerable<BaseItem> children, User user, bool includeLinkedChildren, Dic |
| | | 1353 | | { |
| | 10 | 1354 | | query ??= new InternalItemsQuery(); |
| | 10 | 1355 | | var limit = query.Limit > 0 ? query.Limit : int.MaxValue; |
| | 10 | 1356 | | query.Limit = 0; |
| | | 1357 | | |
| | 10 | 1358 | | var visibleChildren = children |
| | 10 | 1359 | | .Where(e => e.IsVisible(user)) |
| | 10 | 1360 | | .ToArray(); |
| | | 1361 | | |
| | 10 | 1362 | | var realChildren = visibleChildren |
| | 10 | 1363 | | .Where(e => query is null || UserViewBuilder.FilterItem(e, query)) |
| | 10 | 1364 | | .ToArray(); |
| | | 1365 | | |
| | 10 | 1366 | | if (this is BoxSet && (query.OrderBy is null || query.OrderBy.Count == 0)) |
| | | 1367 | | { |
| | 0 | 1368 | | realChildren = realChildren |
| | 0 | 1369 | | .OrderBy(e => e.ProductionYear ?? int.MaxValue) |
| | 0 | 1370 | | .ToArray(); |
| | | 1371 | | } |
| | | 1372 | | |
| | 10 | 1373 | | var childCount = realChildren.Length; |
| | 10 | 1374 | | if (result.Count < limit) |
| | | 1375 | | { |
| | 10 | 1376 | | var remainingCount = (int)(limit - result.Count); |
| | 40 | 1377 | | foreach (var child in realChildren |
| | 10 | 1378 | | .Skip(query.StartIndex ?? 0) |
| | 10 | 1379 | | .Take(remainingCount)) |
| | | 1380 | | { |
| | 10 | 1381 | | result[child.Id] = child; |
| | | 1382 | | } |
| | | 1383 | | } |
| | | 1384 | | |
| | 10 | 1385 | | if (recursive) |
| | | 1386 | | { |
| | 0 | 1387 | | foreach (var child in visibleChildren |
| | 0 | 1388 | | .Where(e => e.IsFolder) |
| | 0 | 1389 | | .OfType<Folder>()) |
| | | 1390 | | { |
| | 0 | 1391 | | childCount += child.AddChildren(user, includeLinkedChildren, result, true, query, visitedFolders); |
| | | 1392 | | } |
| | | 1393 | | } |
| | | 1394 | | |
| | 10 | 1395 | | return childCount; |
| | | 1396 | | } |
| | | 1397 | | |
| | | 1398 | | public virtual IReadOnlyList<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query, out int totalCo |
| | | 1399 | | { |
| | 0 | 1400 | | ArgumentNullException.ThrowIfNull(user); |
| | | 1401 | | |
| | 0 | 1402 | | var result = new Dictionary<Guid, BaseItem>(); |
| | | 1403 | | |
| | 0 | 1404 | | totalCount = AddChildren(user, true, result, true, query); |
| | | 1405 | | |
| | 0 | 1406 | | return result.Values.ToArray(); |
| | | 1407 | | } |
| | | 1408 | | |
| | | 1409 | | /// <summary> |
| | | 1410 | | /// Gets the recursive children. |
| | | 1411 | | /// </summary> |
| | | 1412 | | /// <returns>IList{BaseItem}.</returns> |
| | | 1413 | | public IReadOnlyList<BaseItem> GetRecursiveChildren() |
| | | 1414 | | { |
| | 0 | 1415 | | return GetRecursiveChildren(true); |
| | | 1416 | | } |
| | | 1417 | | |
| | | 1418 | | public IReadOnlyList<BaseItem> GetRecursiveChildren(bool includeLinkedChildren) |
| | | 1419 | | { |
| | 0 | 1420 | | return GetRecursiveChildren(i => true, includeLinkedChildren); |
| | | 1421 | | } |
| | | 1422 | | |
| | | 1423 | | public IReadOnlyList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter) |
| | | 1424 | | { |
| | 0 | 1425 | | return GetRecursiveChildren(filter, true); |
| | | 1426 | | } |
| | | 1427 | | |
| | | 1428 | | public IReadOnlyList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter, bool includeLinkedChildren) |
| | | 1429 | | { |
| | 0 | 1430 | | var result = new Dictionary<Guid, BaseItem>(); |
| | | 1431 | | |
| | 0 | 1432 | | AddChildrenToList(result, includeLinkedChildren, true, filter); |
| | | 1433 | | |
| | 0 | 1434 | | return result.Values.ToArray(); |
| | | 1435 | | } |
| | | 1436 | | |
| | | 1437 | | /// <summary> |
| | | 1438 | | /// Adds the children to list. |
| | | 1439 | | /// </summary> |
| | | 1440 | | private void AddChildrenToList(Dictionary<Guid, BaseItem> result, bool includeLinkedChildren, bool recursive, Fu |
| | | 1441 | | { |
| | 0 | 1442 | | foreach (var child in Children) |
| | | 1443 | | { |
| | 0 | 1444 | | if (filter is null || filter(child)) |
| | | 1445 | | { |
| | 0 | 1446 | | result[child.Id] = child; |
| | | 1447 | | } |
| | | 1448 | | |
| | 0 | 1449 | | if (recursive && child.IsFolder) |
| | | 1450 | | { |
| | 0 | 1451 | | var folder = (Folder)child; |
| | | 1452 | | |
| | | 1453 | | // We can only support includeLinkedChildren for the first folder, or we might end up stuck in a loo |
| | 0 | 1454 | | folder.AddChildrenToList(result, false, true, filter); |
| | | 1455 | | } |
| | | 1456 | | } |
| | | 1457 | | |
| | 0 | 1458 | | if (includeLinkedChildren) |
| | | 1459 | | { |
| | 0 | 1460 | | foreach (var child in GetLinkedChildren()) |
| | | 1461 | | { |
| | 0 | 1462 | | if (filter is null || filter(child)) |
| | | 1463 | | { |
| | 0 | 1464 | | result[child.Id] = child; |
| | | 1465 | | } |
| | | 1466 | | } |
| | | 1467 | | } |
| | 0 | 1468 | | } |
| | | 1469 | | |
| | | 1470 | | /// <summary> |
| | | 1471 | | /// Gets the linked children. |
| | | 1472 | | /// </summary> |
| | | 1473 | | /// <returns>IEnumerable{BaseItem}.</returns> |
| | | 1474 | | public List<BaseItem> GetLinkedChildren() |
| | | 1475 | | { |
| | 10 | 1476 | | var linkedChildren = LinkedChildren; |
| | 10 | 1477 | | var list = new List<BaseItem>(linkedChildren.Length); |
| | | 1478 | | |
| | 20 | 1479 | | foreach (var i in linkedChildren) |
| | | 1480 | | { |
| | 0 | 1481 | | var child = GetLinkedChild(i); |
| | | 1482 | | |
| | 0 | 1483 | | if (child is not null) |
| | | 1484 | | { |
| | 0 | 1485 | | list.Add(child); |
| | | 1486 | | } |
| | | 1487 | | } |
| | | 1488 | | |
| | 10 | 1489 | | return list; |
| | | 1490 | | } |
| | | 1491 | | |
| | | 1492 | | public bool ContainsLinkedChildByItemId(Guid itemId) |
| | | 1493 | | { |
| | 0 | 1494 | | var linkedChildren = LinkedChildren; |
| | 0 | 1495 | | foreach (var i in linkedChildren) |
| | | 1496 | | { |
| | 0 | 1497 | | if (i.ItemId.HasValue) |
| | | 1498 | | { |
| | 0 | 1499 | | if (i.ItemId.Value.Equals(itemId)) |
| | | 1500 | | { |
| | 0 | 1501 | | return true; |
| | | 1502 | | } |
| | | 1503 | | |
| | | 1504 | | continue; |
| | | 1505 | | } |
| | | 1506 | | |
| | 0 | 1507 | | var child = GetLinkedChild(i); |
| | | 1508 | | |
| | 0 | 1509 | | if (child is not null && child.Id.Equals(itemId)) |
| | | 1510 | | { |
| | 0 | 1511 | | return true; |
| | | 1512 | | } |
| | | 1513 | | } |
| | | 1514 | | |
| | 0 | 1515 | | return false; |
| | | 1516 | | } |
| | | 1517 | | |
| | | 1518 | | public List<BaseItem> GetLinkedChildren(User user) |
| | | 1519 | | { |
| | 10 | 1520 | | if (!FilterLinkedChildrenPerUser || user is null) |
| | | 1521 | | { |
| | 10 | 1522 | | return GetLinkedChildren(); |
| | | 1523 | | } |
| | | 1524 | | |
| | 0 | 1525 | | var linkedChildren = LinkedChildren; |
| | 0 | 1526 | | var list = new List<BaseItem>(linkedChildren.Length); |
| | | 1527 | | |
| | 0 | 1528 | | if (linkedChildren.Length == 0) |
| | | 1529 | | { |
| | 0 | 1530 | | return list; |
| | | 1531 | | } |
| | | 1532 | | |
| | 0 | 1533 | | var allUserRootChildren = LibraryManager.GetUserRootFolder() |
| | 0 | 1534 | | .GetChildren(user, true) |
| | 0 | 1535 | | .OfType<Folder>() |
| | 0 | 1536 | | .ToList(); |
| | | 1537 | | |
| | 0 | 1538 | | var collectionFolderIds = allUserRootChildren |
| | 0 | 1539 | | .Select(i => i.Id) |
| | 0 | 1540 | | .ToList(); |
| | | 1541 | | |
| | 0 | 1542 | | foreach (var i in linkedChildren) |
| | | 1543 | | { |
| | 0 | 1544 | | var child = GetLinkedChild(i); |
| | | 1545 | | |
| | 0 | 1546 | | if (child is null) |
| | | 1547 | | { |
| | | 1548 | | continue; |
| | | 1549 | | } |
| | | 1550 | | |
| | 0 | 1551 | | var childOwner = child.GetOwner() ?? child; |
| | | 1552 | | |
| | 0 | 1553 | | if (child is not IItemByName) |
| | | 1554 | | { |
| | 0 | 1555 | | var childProtocol = childOwner.PathProtocol; |
| | 0 | 1556 | | if (!childProtocol.HasValue || childProtocol.Value != Model.MediaInfo.MediaProtocol.File) |
| | | 1557 | | { |
| | 0 | 1558 | | if (!childOwner.IsVisibleStandalone(user)) |
| | | 1559 | | { |
| | 0 | 1560 | | continue; |
| | | 1561 | | } |
| | | 1562 | | } |
| | | 1563 | | else |
| | | 1564 | | { |
| | 0 | 1565 | | var itemCollectionFolderIds = |
| | 0 | 1566 | | LibraryManager.GetCollectionFolders(childOwner, allUserRootChildren).Select(f => f.Id); |
| | | 1567 | | |
| | 0 | 1568 | | if (!itemCollectionFolderIds.Any(collectionFolderIds.Contains)) |
| | | 1569 | | { |
| | | 1570 | | continue; |
| | | 1571 | | } |
| | | 1572 | | } |
| | | 1573 | | } |
| | | 1574 | | |
| | 0 | 1575 | | list.Add(child); |
| | | 1576 | | } |
| | | 1577 | | |
| | 0 | 1578 | | return list; |
| | | 1579 | | } |
| | | 1580 | | |
| | | 1581 | | /// <summary> |
| | | 1582 | | /// Gets the linked children. |
| | | 1583 | | /// </summary> |
| | | 1584 | | /// <returns>IEnumerable{BaseItem}.</returns> |
| | | 1585 | | public IReadOnlyList<Tuple<LinkedChild, BaseItem>> GetLinkedChildrenInfos() |
| | | 1586 | | { |
| | 0 | 1587 | | return LinkedChildren |
| | 0 | 1588 | | .Select(i => new Tuple<LinkedChild, BaseItem>(i, GetLinkedChild(i))) |
| | 0 | 1589 | | .Where(i => i.Item2 is not null) |
| | 0 | 1590 | | .ToArray(); |
| | | 1591 | | } |
| | | 1592 | | |
| | | 1593 | | protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystem |
| | | 1594 | | { |
| | | 1595 | | var changesFound = false; |
| | | 1596 | | |
| | | 1597 | | if (IsFileProtocol) |
| | | 1598 | | { |
| | | 1599 | | if (RefreshLinkedChildren(fileSystemChildren)) |
| | | 1600 | | { |
| | | 1601 | | changesFound = true; |
| | | 1602 | | } |
| | | 1603 | | } |
| | | 1604 | | |
| | | 1605 | | var baseHasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).Configur |
| | | 1606 | | |
| | | 1607 | | return baseHasChanges || changesFound; |
| | | 1608 | | } |
| | | 1609 | | |
| | | 1610 | | /// <summary> |
| | | 1611 | | /// Refreshes the linked children. |
| | | 1612 | | /// </summary> |
| | | 1613 | | /// <param name="fileSystemChildren">The enumerable of file system metadata.</param> |
| | | 1614 | | /// <returns><c>true</c> if the linked children were updated, <c>false</c> otherwise.</returns> |
| | | 1615 | | protected virtual bool RefreshLinkedChildren(IEnumerable<FileSystemMetadata> fileSystemChildren) |
| | | 1616 | | { |
| | 21 | 1617 | | if (SupportsShortcutChildren) |
| | | 1618 | | { |
| | 21 | 1619 | | var newShortcutLinks = fileSystemChildren |
| | 21 | 1620 | | .Where(i => !i.IsDirectory && FileSystem.IsShortcut(i.FullName)) |
| | 21 | 1621 | | .Select(i => |
| | 21 | 1622 | | { |
| | 21 | 1623 | | try |
| | 21 | 1624 | | { |
| | 21 | 1625 | | Logger.LogDebug("Found shortcut at {0}", i.FullName); |
| | 21 | 1626 | | |
| | 21 | 1627 | | var resolvedPath = CollectionFolder.ApplicationHost.ExpandVirtualPath(FileSystem.ResolveShor |
| | 21 | 1628 | | |
| | 21 | 1629 | | if (!string.IsNullOrEmpty(resolvedPath)) |
| | 21 | 1630 | | { |
| | 21 | 1631 | | return new LinkedChild |
| | 21 | 1632 | | { |
| | 21 | 1633 | | Path = resolvedPath, |
| | 21 | 1634 | | Type = LinkedChildType.Shortcut |
| | 21 | 1635 | | }; |
| | 21 | 1636 | | } |
| | 21 | 1637 | | |
| | 21 | 1638 | | Logger.LogError("Error resolving shortcut {0}", i.FullName); |
| | 21 | 1639 | | |
| | 21 | 1640 | | return null; |
| | 21 | 1641 | | } |
| | 21 | 1642 | | catch (IOException ex) |
| | 21 | 1643 | | { |
| | 21 | 1644 | | Logger.LogError(ex, "Error resolving shortcut {0}", i.FullName); |
| | 21 | 1645 | | return null; |
| | 21 | 1646 | | } |
| | 21 | 1647 | | }) |
| | 21 | 1648 | | .Where(i => i is not null) |
| | 21 | 1649 | | .ToList(); |
| | | 1650 | | |
| | 21 | 1651 | | var currentShortcutLinks = LinkedChildren.Where(i => i.Type == LinkedChildType.Shortcut).ToList(); |
| | | 1652 | | |
| | 21 | 1653 | | if (!newShortcutLinks.SequenceEqual(currentShortcutLinks, new LinkedChildComparer(FileSystem))) |
| | | 1654 | | { |
| | 0 | 1655 | | Logger.LogInformation("Shortcut links have changed for {0}", Path); |
| | | 1656 | | |
| | 0 | 1657 | | newShortcutLinks.AddRange(LinkedChildren.Where(i => i.Type == LinkedChildType.Manual)); |
| | 0 | 1658 | | LinkedChildren = newShortcutLinks.ToArray(); |
| | 0 | 1659 | | return true; |
| | | 1660 | | } |
| | | 1661 | | } |
| | | 1662 | | |
| | 42 | 1663 | | foreach (var child in LinkedChildren) |
| | | 1664 | | { |
| | | 1665 | | // Reset the cached value |
| | 0 | 1666 | | child.ItemId = null; |
| | | 1667 | | } |
| | | 1668 | | |
| | 21 | 1669 | | return false; |
| | | 1670 | | } |
| | | 1671 | | |
| | | 1672 | | /// <summary> |
| | | 1673 | | /// Marks the played. |
| | | 1674 | | /// </summary> |
| | | 1675 | | /// <param name="user">The user.</param> |
| | | 1676 | | /// <param name="datePlayed">The date played.</param> |
| | | 1677 | | /// <param name="resetPosition">if set to <c>true</c> [reset position].</param> |
| | | 1678 | | public override void MarkPlayed( |
| | | 1679 | | User user, |
| | | 1680 | | DateTime? datePlayed, |
| | | 1681 | | bool resetPosition) |
| | | 1682 | | { |
| | 0 | 1683 | | var query = new InternalItemsQuery |
| | 0 | 1684 | | { |
| | 0 | 1685 | | User = user, |
| | 0 | 1686 | | Recursive = true, |
| | 0 | 1687 | | IsFolder = false, |
| | 0 | 1688 | | EnableTotalRecordCount = false |
| | 0 | 1689 | | }; |
| | | 1690 | | |
| | 0 | 1691 | | if (!user.DisplayMissingEpisodes) |
| | | 1692 | | { |
| | 0 | 1693 | | query.IsVirtualItem = false; |
| | | 1694 | | } |
| | | 1695 | | |
| | 0 | 1696 | | var itemsResult = GetItemList(query); |
| | | 1697 | | |
| | | 1698 | | // Sweep through recursively and update status |
| | 0 | 1699 | | foreach (var item in itemsResult) |
| | | 1700 | | { |
| | 0 | 1701 | | if (item.IsVirtualItem) |
| | | 1702 | | { |
| | | 1703 | | // The querying doesn't support virtual unaired |
| | 0 | 1704 | | var episode = item as Episode; |
| | 0 | 1705 | | if (episode is not null && episode.IsUnaired) |
| | | 1706 | | { |
| | | 1707 | | continue; |
| | | 1708 | | } |
| | | 1709 | | } |
| | | 1710 | | |
| | 0 | 1711 | | item.MarkPlayed(user, datePlayed, resetPosition); |
| | | 1712 | | } |
| | 0 | 1713 | | } |
| | | 1714 | | |
| | | 1715 | | /// <summary> |
| | | 1716 | | /// Marks the unplayed. |
| | | 1717 | | /// </summary> |
| | | 1718 | | /// <param name="user">The user.</param> |
| | | 1719 | | public override void MarkUnplayed(User user) |
| | | 1720 | | { |
| | 0 | 1721 | | var itemsResult = GetItemList(new InternalItemsQuery |
| | 0 | 1722 | | { |
| | 0 | 1723 | | User = user, |
| | 0 | 1724 | | Recursive = true, |
| | 0 | 1725 | | IsFolder = false, |
| | 0 | 1726 | | EnableTotalRecordCount = false |
| | 0 | 1727 | | }); |
| | | 1728 | | |
| | | 1729 | | // Sweep through recursively and update status |
| | 0 | 1730 | | foreach (var item in itemsResult) |
| | | 1731 | | { |
| | 0 | 1732 | | item.MarkUnplayed(user); |
| | | 1733 | | } |
| | 0 | 1734 | | } |
| | | 1735 | | |
| | | 1736 | | public override bool IsPlayed(User user, UserItemData userItemData) |
| | | 1737 | | { |
| | 0 | 1738 | | return ItemRepository.GetIsPlayed(user, Id, true); |
| | | 1739 | | } |
| | | 1740 | | |
| | | 1741 | | public override bool IsUnplayed(User user, UserItemData userItemData) |
| | | 1742 | | { |
| | 0 | 1743 | | return !IsPlayed(user, userItemData); |
| | | 1744 | | } |
| | | 1745 | | |
| | | 1746 | | public override void FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User |
| | | 1747 | | { |
| | 9 | 1748 | | if (!SupportsUserDataFromChildren) |
| | | 1749 | | { |
| | 9 | 1750 | | return; |
| | | 1751 | | } |
| | | 1752 | | |
| | 0 | 1753 | | if (itemDto is not null && fields.ContainsField(ItemFields.RecursiveItemCount)) |
| | | 1754 | | { |
| | 0 | 1755 | | itemDto.RecursiveItemCount = GetRecursiveChildCount(user); |
| | | 1756 | | } |
| | | 1757 | | |
| | 0 | 1758 | | if (SupportsPlayedStatus) |
| | | 1759 | | { |
| | 0 | 1760 | | var unplayedQueryResult = GetItems(new InternalItemsQuery(user) |
| | 0 | 1761 | | { |
| | 0 | 1762 | | Recursive = true, |
| | 0 | 1763 | | IsFolder = false, |
| | 0 | 1764 | | IsVirtualItem = false, |
| | 0 | 1765 | | EnableTotalRecordCount = true, |
| | 0 | 1766 | | Limit = 0, |
| | 0 | 1767 | | IsPlayed = false, |
| | 0 | 1768 | | DtoOptions = new DtoOptions(false) |
| | 0 | 1769 | | { |
| | 0 | 1770 | | EnableImages = false |
| | 0 | 1771 | | } |
| | 0 | 1772 | | }).TotalRecordCount; |
| | | 1773 | | |
| | 0 | 1774 | | dto.UnplayedItemCount = unplayedQueryResult; |
| | | 1775 | | |
| | 0 | 1776 | | if (itemDto?.RecursiveItemCount > 0) |
| | | 1777 | | { |
| | 0 | 1778 | | var unplayedPercentage = ((double)unplayedQueryResult / itemDto.RecursiveItemCount.Value) * 100; |
| | 0 | 1779 | | dto.PlayedPercentage = 100 - unplayedPercentage; |
| | 0 | 1780 | | dto.Played = dto.PlayedPercentage.Value >= 100; |
| | | 1781 | | } |
| | | 1782 | | else |
| | | 1783 | | { |
| | 0 | 1784 | | dto.Played = (dto.UnplayedItemCount ?? 0) == 0; |
| | | 1785 | | } |
| | | 1786 | | } |
| | 0 | 1787 | | } |
| | | 1788 | | |
| | | 1789 | | /// <summary> |
| | | 1790 | | /// Contains constants used when reporting scan progress. |
| | | 1791 | | /// </summary> |
| | | 1792 | | private static class ProgressHelpers |
| | | 1793 | | { |
| | | 1794 | | /// <summary> |
| | | 1795 | | /// Reported after the folders immediate children are retrieved. |
| | | 1796 | | /// </summary> |
| | | 1797 | | public const int RetrievedChildren = 5; |
| | | 1798 | | |
| | | 1799 | | /// <summary> |
| | | 1800 | | /// Reported after add, updating, or deleting child items from the LibraryManager. |
| | | 1801 | | /// </summary> |
| | | 1802 | | public const int UpdatedChildItems = 10; |
| | | 1803 | | |
| | | 1804 | | /// <summary> |
| | | 1805 | | /// Reported once subfolders are scanned. |
| | | 1806 | | /// When scanning subfolders, the progress will be between [UpdatedItems, ScannedSubfolders]. |
| | | 1807 | | /// </summary> |
| | | 1808 | | public const int ScannedSubfolders = 50; |
| | | 1809 | | |
| | | 1810 | | /// <summary> |
| | | 1811 | | /// Reported once metadata is refreshed. |
| | | 1812 | | /// When refreshing metadata, the progress will be between [ScannedSubfolders, MetadataRefreshed]. |
| | | 1813 | | /// </summary> |
| | | 1814 | | public const int RefreshedMetadata = 100; |
| | | 1815 | | |
| | | 1816 | | /// <summary> |
| | | 1817 | | /// Gets the current progress given the previous step, next step, and progress in between. |
| | | 1818 | | /// </summary> |
| | | 1819 | | /// <param name="previousProgressStep">The previous progress step.</param> |
| | | 1820 | | /// <param name="nextProgressStep">The next progress step.</param> |
| | | 1821 | | /// <param name="currentProgress">The current progress step.</param> |
| | | 1822 | | /// <returns>The progress.</returns> |
| | | 1823 | | public static double GetProgress(int previousProgressStep, int nextProgressStep, double currentProgress) |
| | | 1824 | | { |
| | 78 | 1825 | | return previousProgressStep + ((nextProgressStep - previousProgressStep) * (currentProgress / 100)); |
| | | 1826 | | } |
| | | 1827 | | } |
| | | 1828 | | } |
| | | 1829 | | } |