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