| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Concurrent; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Text.Json; |
| | | 11 | | using System.Text.Json.Serialization; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | using Jellyfin.Data.Enums; |
| | | 15 | | using Jellyfin.Database.Implementations.Entities; |
| | | 16 | | using Jellyfin.Extensions.Json; |
| | | 17 | | using MediaBrowser.Controller.IO; |
| | | 18 | | using MediaBrowser.Controller.Library; |
| | | 19 | | using MediaBrowser.Controller.Providers; |
| | | 20 | | using MediaBrowser.Model.Configuration; |
| | | 21 | | using MediaBrowser.Model.IO; |
| | | 22 | | using MediaBrowser.Model.Serialization; |
| | | 23 | | |
| | | 24 | | using Microsoft.Extensions.Logging; |
| | | 25 | | |
| | | 26 | | namespace MediaBrowser.Controller.Entities |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// Specialized Folder class that points to a subset of the physical folders in the system. |
| | | 30 | | /// It is created from the user-specific folders within the system root. |
| | | 31 | | /// </summary> |
| | | 32 | | public class CollectionFolder : Folder, ICollectionFolder |
| | | 33 | | { |
| | 2 | 34 | | private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; |
| | 2 | 35 | | private static readonly ConcurrentDictionary<string, LibraryOptions> _libraryOptions = new ConcurrentDictionary< |
| | | 36 | | private bool _requiresRefresh; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="CollectionFolder"/> class. |
| | | 40 | | /// </summary> |
| | 23 | 41 | | public CollectionFolder() |
| | | 42 | | { |
| | 23 | 43 | | PhysicalLocationsList = Array.Empty<string>(); |
| | 23 | 44 | | PhysicalFolderIds = Array.Empty<Guid>(); |
| | 23 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Event raised when library options are updated for any collection folder. |
| | | 49 | | /// </summary> |
| | | 50 | | public static event EventHandler<LibraryOptionsUpdatedEventArgs> LibraryOptionsUpdated; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the display preferences id. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <remarks> |
| | | 56 | | /// Allow different display preferences for each collection folder. |
| | | 57 | | /// </remarks> |
| | | 58 | | /// <value>The display prefs id.</value> |
| | | 59 | | [JsonIgnore] |
| | 0 | 60 | | public override Guid DisplayPreferencesId => Id; |
| | | 61 | | |
| | | 62 | | [JsonIgnore] |
| | 192 | 63 | | public override string[] PhysicalLocations => PhysicalLocationsList; |
| | | 64 | | |
| | | 65 | | public string[] PhysicalLocationsList { get; set; } |
| | | 66 | | |
| | | 67 | | public Guid[] PhysicalFolderIds { get; set; } |
| | | 68 | | |
| | | 69 | | public static IXmlSerializer XmlSerializer { get; set; } |
| | | 70 | | |
| | | 71 | | public static IServerApplicationHost ApplicationHost { get; set; } |
| | | 72 | | |
| | | 73 | | [JsonIgnore] |
| | 0 | 74 | | public override bool SupportsPlayedStatus => false; |
| | | 75 | | |
| | | 76 | | [JsonIgnore] |
| | 0 | 77 | | public override bool SupportsInheritedParentImages => false; |
| | | 78 | | |
| | | 79 | | public CollectionType? CollectionType { get; set; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets or sets the item's children. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <remarks> |
| | | 85 | | /// Our children are actually just references to the ones in the physical root... |
| | | 86 | | /// Setting to null propagates invalidation to physical folders since the getter |
| | | 87 | | /// always delegates to <see cref="GetActualChildren"/> and never reads the backing field. |
| | | 88 | | /// </remarks> |
| | | 89 | | /// <value>The actual children.</value> |
| | | 90 | | [JsonIgnore] |
| | | 91 | | public override IEnumerable<BaseItem> Children |
| | | 92 | | { |
| | 0 | 93 | | get => GetActualChildren(); |
| | | 94 | | set |
| | | 95 | | { |
| | | 96 | | // The getter delegates to physical folders, so invalidate their caches. |
| | 0 | 97 | | foreach (var folder in GetPhysicalFolders(true)) |
| | | 98 | | { |
| | 0 | 99 | | folder.Children = null; |
| | | 100 | | } |
| | 0 | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | [JsonIgnore] |
| | 4 | 105 | | public override bool SupportsPeople => false; |
| | | 106 | | |
| | | 107 | | public override bool CanDelete() |
| | | 108 | | { |
| | 1 | 109 | | return false; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public LibraryOptions GetLibraryOptions() |
| | | 113 | | { |
| | 38 | 114 | | return GetLibraryOptions(Path); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | public override bool IsVisible(User user, bool skipAllowedTagsCheck = false) |
| | | 118 | | { |
| | 0 | 119 | | if (GetLibraryOptions().Enabled) |
| | | 120 | | { |
| | 0 | 121 | | return base.IsVisible(user, skipAllowedTagsCheck); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | return false; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private static LibraryOptions LoadLibraryOptions(string path) |
| | | 128 | | { |
| | | 129 | | try |
| | | 130 | | { |
| | 2 | 131 | | if (XmlSerializer.DeserializeFromFile(typeof(LibraryOptions), GetLibraryOptionsPath(path)) is not Librar |
| | | 132 | | { |
| | 0 | 133 | | return new LibraryOptions(); |
| | | 134 | | } |
| | | 135 | | |
| | 4 | 136 | | foreach (var mediaPath in result.PathInfos) |
| | | 137 | | { |
| | 0 | 138 | | if (!string.IsNullOrEmpty(mediaPath.Path)) |
| | | 139 | | { |
| | 0 | 140 | | mediaPath.Path = ApplicationHost.ExpandVirtualPath(mediaPath.Path); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | 2 | 144 | | return result; |
| | | 145 | | } |
| | 0 | 146 | | catch (FileNotFoundException) |
| | | 147 | | { |
| | 0 | 148 | | return new LibraryOptions(); |
| | | 149 | | } |
| | 0 | 150 | | catch (IOException) |
| | | 151 | | { |
| | 0 | 152 | | return new LibraryOptions(); |
| | | 153 | | } |
| | 0 | 154 | | catch (Exception ex) |
| | | 155 | | { |
| | 0 | 156 | | Logger.LogError(ex, "Error loading library options"); |
| | | 157 | | |
| | 0 | 158 | | return new LibraryOptions(); |
| | | 159 | | } |
| | 2 | 160 | | } |
| | | 161 | | |
| | | 162 | | private static string GetLibraryOptionsPath(string path) |
| | | 163 | | { |
| | 5 | 164 | | return System.IO.Path.Combine(path, "options.xml"); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public void UpdateLibraryOptions(LibraryOptions options) |
| | | 168 | | { |
| | 1 | 169 | | SaveLibraryOptions(Path, options); |
| | 1 | 170 | | } |
| | | 171 | | |
| | | 172 | | public static LibraryOptions GetLibraryOptions(string path) |
| | 38 | 173 | | => _libraryOptions.GetOrAdd(path, LoadLibraryOptions); |
| | | 174 | | |
| | | 175 | | public static void SaveLibraryOptions(string path, LibraryOptions options) |
| | | 176 | | { |
| | 3 | 177 | | _libraryOptions[path] = options; |
| | | 178 | | |
| | 3 | 179 | | var clone = JsonSerializer.Deserialize<LibraryOptions>(JsonSerializer.SerializeToUtf8Bytes(options, _jsonOpt |
| | 6 | 180 | | foreach (var mediaPath in clone.PathInfos) |
| | | 181 | | { |
| | 0 | 182 | | if (!string.IsNullOrEmpty(mediaPath.Path)) |
| | | 183 | | { |
| | 0 | 184 | | mediaPath.Path = ApplicationHost.ReverseVirtualPath(mediaPath.Path); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | 3 | 188 | | XmlSerializer.SerializeToFile(clone, GetLibraryOptionsPath(path)); |
| | | 189 | | |
| | 3 | 190 | | LibraryOptionsUpdated?.Invoke(null, new LibraryOptionsUpdatedEventArgs(path, options)); |
| | 3 | 191 | | } |
| | | 192 | | |
| | | 193 | | public static void OnCollectionFolderChange() |
| | 1 | 194 | | => _libraryOptions.Clear(); |
| | | 195 | | |
| | | 196 | | public override bool IsSaveLocalMetadataEnabled() |
| | | 197 | | { |
| | 28 | 198 | | return true; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | protected override FileSystemMetadata[] GetFileSystemChildren(IDirectoryService directoryService) |
| | | 202 | | { |
| | 14 | 203 | | return CreateResolveArgs(directoryService, true).FileSystemChildren; |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | public override bool RequiresRefresh() |
| | | 207 | | { |
| | 14 | 208 | | var changed = base.RequiresRefresh() || _requiresRefresh; |
| | | 209 | | |
| | 14 | 210 | | if (!changed) |
| | | 211 | | { |
| | 14 | 212 | | var locations = PhysicalLocations; |
| | | 213 | | |
| | 14 | 214 | | var newLocations = CreateResolveArgs(new DirectoryService(FileSystem), false).PhysicalLocations; |
| | | 215 | | |
| | 14 | 216 | | if (!locations.SequenceEqual(newLocations)) |
| | | 217 | | { |
| | 0 | 218 | | changed = true; |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | |
| | 14 | 222 | | if (!changed) |
| | | 223 | | { |
| | 14 | 224 | | var folderIds = PhysicalFolderIds; |
| | | 225 | | |
| | 14 | 226 | | var newFolderIds = GetPhysicalFolders(false).Select(i => i.Id).ToList(); |
| | | 227 | | |
| | 14 | 228 | | if (!folderIds.SequenceEqual(newFolderIds)) |
| | | 229 | | { |
| | 0 | 230 | | changed = true; |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | |
| | 14 | 234 | | return changed; |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | public override bool BeforeMetadataRefresh(bool replaceAllMetadata) |
| | | 238 | | { |
| | 2 | 239 | | var changed = base.BeforeMetadataRefresh(replaceAllMetadata) || _requiresRefresh; |
| | 2 | 240 | | _requiresRefresh = false; |
| | 2 | 241 | | return changed; |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | public override double? GetRefreshProgress() |
| | | 245 | | { |
| | 1 | 246 | | var folders = GetPhysicalFolders(true).ToList(); |
| | 1 | 247 | | double totalProgresses = 0; |
| | 1 | 248 | | var foldersWithProgress = 0; |
| | | 249 | | |
| | 2 | 250 | | foreach (var folder in folders) |
| | | 251 | | { |
| | 0 | 252 | | var progress = ProviderManager.GetRefreshProgress(folder.Id); |
| | 0 | 253 | | if (progress.HasValue) |
| | | 254 | | { |
| | 0 | 255 | | totalProgresses += progress.Value; |
| | 0 | 256 | | foldersWithProgress++; |
| | | 257 | | } |
| | | 258 | | } |
| | | 259 | | |
| | 1 | 260 | | if (foldersWithProgress == 0) |
| | | 261 | | { |
| | 1 | 262 | | return null; |
| | | 263 | | } |
| | | 264 | | |
| | 0 | 265 | | return totalProgresses / foldersWithProgress; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | protected override bool RefreshLinkedChildren(IEnumerable<FileSystemMetadata> fileSystemChildren) |
| | | 269 | | { |
| | 14 | 270 | | return RefreshLinkedChildrenInternal(true); |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | private bool RefreshLinkedChildrenInternal(bool setFolders) |
| | | 274 | | { |
| | 14 | 275 | | var physicalFolders = GetPhysicalFolders(false) |
| | 14 | 276 | | .ToList(); |
| | | 277 | | |
| | 14 | 278 | | var linkedChildren = physicalFolders |
| | 14 | 279 | | .SelectMany(c => c.LinkedChildren) |
| | 14 | 280 | | .ToList(); |
| | | 281 | | |
| | 14 | 282 | | var changed = !linkedChildren.SequenceEqual(LinkedChildren, new LinkedChildComparer(FileSystem)); |
| | | 283 | | |
| | 14 | 284 | | LinkedChildren = linkedChildren.ToArray(); |
| | | 285 | | |
| | 14 | 286 | | var folderIds = PhysicalFolderIds; |
| | 14 | 287 | | var newFolderIds = physicalFolders.Select(i => i.Id).ToArray(); |
| | | 288 | | |
| | 14 | 289 | | if (!folderIds.SequenceEqual(newFolderIds)) |
| | | 290 | | { |
| | 0 | 291 | | changed = true; |
| | 0 | 292 | | if (setFolders) |
| | | 293 | | { |
| | 0 | 294 | | PhysicalFolderIds = newFolderIds; |
| | | 295 | | } |
| | | 296 | | } |
| | | 297 | | |
| | 14 | 298 | | return changed; |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | private ItemResolveArgs CreateResolveArgs(IDirectoryService directoryService, bool setPhysicalLocations) |
| | | 302 | | { |
| | 28 | 303 | | var path = ContainingFolderPath; |
| | | 304 | | |
| | 28 | 305 | | var args = new ItemResolveArgs(ConfigurationManager.ApplicationPaths, LibraryManager) |
| | 28 | 306 | | { |
| | 28 | 307 | | FileInfo = FileSystem.GetDirectoryInfo(path), |
| | 28 | 308 | | Parent = GetParent() as Folder, |
| | 28 | 309 | | CollectionType = CollectionType |
| | 28 | 310 | | }; |
| | | 311 | | |
| | | 312 | | // Gather child folder and files |
| | 28 | 313 | | if (args.IsDirectory) |
| | | 314 | | { |
| | 28 | 315 | | var flattenFolderDepth = 0; |
| | | 316 | | |
| | 28 | 317 | | var files = FileData.GetFilteredFileSystemEntries(directoryService, args.Path, FileSystem, ApplicationHo |
| | | 318 | | |
| | 28 | 319 | | args.FileSystemChildren = files; |
| | | 320 | | } |
| | | 321 | | |
| | 28 | 322 | | _requiresRefresh = _requiresRefresh || !args.PhysicalLocations.SequenceEqual(PhysicalLocations); |
| | | 323 | | |
| | 28 | 324 | | if (setPhysicalLocations) |
| | | 325 | | { |
| | 14 | 326 | | PhysicalLocationsList = args.PhysicalLocations; |
| | | 327 | | } |
| | | 328 | | |
| | 28 | 329 | | return args; |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Compare our current children (presumably just read from the repo) with the current state of the file system |
| | | 334 | | /// ***Currently does not contain logic to maintain items that are unavailable in the file system***. |
| | | 335 | | /// </summary> |
| | | 336 | | /// <param name="progress">The progress.</param> |
| | | 337 | | /// <param name="recursive">if set to <c>true</c> [recursive].</param> |
| | | 338 | | /// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param> |
| | | 339 | | /// <param name="allowRemoveRoot">remove item even this folder is root.</param> |
| | | 340 | | /// <param name="refreshOptions">The refresh options.</param> |
| | | 341 | | /// <param name="directoryService">The directory service.</param> |
| | | 342 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 343 | | /// <returns>Task.</returns> |
| | | 344 | | protected override Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMe |
| | | 345 | | { |
| | 0 | 346 | | return Task.CompletedTask; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | public IEnumerable<BaseItem> GetActualChildren() |
| | | 350 | | { |
| | 0 | 351 | | return GetPhysicalFolders(true).SelectMany(c => c.Children); |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | public IEnumerable<Folder> GetPhysicalFolders() |
| | | 355 | | { |
| | 0 | 356 | | return GetPhysicalFolders(true); |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | private IEnumerable<Folder> GetPhysicalFolders(bool enableCache) |
| | | 360 | | { |
| | 29 | 361 | | if (enableCache) |
| | | 362 | | { |
| | 1 | 363 | | return PhysicalFolderIds.Select(i => LibraryManager.GetItemById(i)).OfType<Folder>(); |
| | | 364 | | } |
| | | 365 | | |
| | 28 | 366 | | var rootChildren = LibraryManager.RootFolder.Children |
| | 28 | 367 | | .OfType<Folder>() |
| | 28 | 368 | | .ToList(); |
| | | 369 | | |
| | 28 | 370 | | return PhysicalLocations |
| | 28 | 371 | | .Where(i => !FileSystem.AreEqual(i, Path)) |
| | 28 | 372 | | .SelectMany(i => GetPhysicalParents(i, rootChildren)) |
| | 28 | 373 | | .DistinctBy(x => x.Id); |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | private IEnumerable<Folder> GetPhysicalParents(string path, List<Folder> rootChildren) |
| | | 377 | | { |
| | 0 | 378 | | var result = rootChildren |
| | 0 | 379 | | .Where(i => FileSystem.AreEqual(i.Path, path)) |
| | 0 | 380 | | .ToList(); |
| | | 381 | | |
| | 0 | 382 | | if (result.Count == 0) |
| | | 383 | | { |
| | 0 | 384 | | if (LibraryManager.FindByPath(path, true) is Folder folder) |
| | | 385 | | { |
| | 0 | 386 | | result.Add(folder); |
| | | 387 | | } |
| | | 388 | | } |
| | | 389 | | |
| | 0 | 390 | | return result; |
| | | 391 | | } |
| | | 392 | | } |
| | | 393 | | } |