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