| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using MediaBrowser.Controller.Library; |
| | | 4 | | using MediaBrowser.Controller.Providers; |
| | | 5 | | using MediaBrowser.Model.IO; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace MediaBrowser.Controller.IO |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides low level File access that is much faster than the File/Directory api's. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class FileData |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the filtered file system entries. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="directoryService">The directory service.</param> |
| | | 19 | | /// <param name="path">The path.</param> |
| | | 20 | | /// <param name="fileSystem">The file system.</param> |
| | | 21 | | /// <param name="appHost">The application host.</param> |
| | | 22 | | /// <param name="logger">The logger.</param> |
| | | 23 | | /// <param name="args">The args.</param> |
| | | 24 | | /// <param name="flattenFolderDepth">The flatten folder depth.</param> |
| | | 25 | | /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param> |
| | | 26 | | /// <returns>Dictionary{System.StringFileSystemInfo}.</returns> |
| | | 27 | | /// <exception cref="ArgumentNullException"><paramref name="path" /> is <c>null</c> or empty.</exception> |
| | | 28 | | public static FileSystemMetadata[] GetFilteredFileSystemEntries( |
| | | 29 | | IDirectoryService directoryService, |
| | | 30 | | string path, |
| | | 31 | | IFileSystem fileSystem, |
| | | 32 | | IServerApplicationHost appHost, |
| | | 33 | | ILogger logger, |
| | | 34 | | ItemResolveArgs args, |
| | | 35 | | int flattenFolderDepth = 0, |
| | | 36 | | bool resolveShortcuts = true) |
| | | 37 | | { |
| | 234 | 38 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | | 39 | | |
| | 234 | 40 | | ArgumentNullException.ThrowIfNull(args); |
| | | 41 | | |
| | 234 | 42 | | var entries = directoryService.GetFileSystemEntries(path); |
| | | 43 | | |
| | 233 | 44 | | if (!resolveShortcuts && flattenFolderDepth == 0) |
| | | 45 | | { |
| | 21 | 46 | | return entries; |
| | | 47 | | } |
| | | 48 | | |
| | 212 | 49 | | var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase); |
| | | 50 | | |
| | 720 | 51 | | foreach (var entry in entries) |
| | | 52 | | { |
| | 148 | 53 | | var isDirectory = entry.IsDirectory; |
| | | 54 | | |
| | 148 | 55 | | var fullName = entry.FullName; |
| | | 56 | | |
| | 148 | 57 | | if (resolveShortcuts && fileSystem.IsShortcut(fullName)) |
| | | 58 | | { |
| | | 59 | | try |
| | | 60 | | { |
| | 0 | 61 | | var newPath = appHost.ExpandVirtualPath(fileSystem.ResolveShortcut(fullName)); |
| | | 62 | | |
| | 0 | 63 | | if (string.IsNullOrEmpty(newPath)) |
| | | 64 | | { |
| | | 65 | | // invalid shortcut - could be old or target could just be unavailable |
| | 0 | 66 | | logger.LogWarning("Encountered invalid shortcut: {Path}", fullName); |
| | 0 | 67 | | continue; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // Don't check if it exists here because that could return false for network shares. |
| | 0 | 71 | | var data = fileSystem.GetDirectoryInfo(newPath); |
| | | 72 | | |
| | | 73 | | // add to our physical locations |
| | 0 | 74 | | args.AddAdditionalLocation(newPath); |
| | | 75 | | |
| | 0 | 76 | | dict[newPath] = data; |
| | 0 | 77 | | } |
| | 0 | 78 | | catch (Exception ex) |
| | | 79 | | { |
| | 0 | 80 | | logger.LogError(ex, "Error resolving shortcut from {Path}", fullName); |
| | 0 | 81 | | } |
| | | 82 | | } |
| | 148 | 83 | | else if (flattenFolderDepth > 0 && isDirectory) |
| | | 84 | | { |
| | 264 | 85 | | foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, appHost, |
| | | 86 | | { |
| | 34 | 87 | | dict[child.FullName] = child; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | else |
| | | 91 | | { |
| | 50 | 92 | | dict[fullName] = entry; |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 212 | 96 | | var returnResult = new FileSystemMetadata[dict.Count]; |
| | 212 | 97 | | var index = 0; |
| | 212 | 98 | | var values = dict.Values; |
| | 592 | 99 | | foreach (var value in values) |
| | | 100 | | { |
| | 84 | 101 | | returnResult[index] = value; |
| | 84 | 102 | | index++; |
| | | 103 | | } |
| | | 104 | | |
| | 212 | 105 | | return returnResult; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |