FileData.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace MediaBrowser.Controller.IO
  7. {
  8. /// <summary>
  9. /// Provides low level File access that is much faster than the File/Directory api's
  10. /// </summary>
  11. public static class FileData
  12. {
  13. /// <summary>
  14. /// Gets the filtered file system entries.
  15. /// </summary>
  16. /// <param name="path">The path.</param>
  17. /// <param name="logger">The logger.</param>
  18. /// <param name="searchPattern">The search pattern.</param>
  19. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  20. /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
  21. /// <param name="args">The args.</param>
  22. /// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
  23. /// <exception cref="System.ArgumentNullException">path</exception>
  24. public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true, ItemResolveArgs args = null)
  25. {
  26. if (string.IsNullOrEmpty(path))
  27. {
  28. throw new ArgumentNullException("path");
  29. }
  30. var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  31. var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
  32. foreach (var entry in entries)
  33. {
  34. var isDirectory = (entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  35. var fullName = entry.FullName;
  36. if (resolveShortcuts && FileSystem.IsShortcut(fullName))
  37. {
  38. var newPath = FileSystem.ResolveShortcut(fullName);
  39. if (string.IsNullOrWhiteSpace(newPath))
  40. {
  41. //invalid shortcut - could be old or target could just be unavailable
  42. logger.Warn("Encountered invalid shortcut: " + fullName);
  43. continue;
  44. }
  45. // Don't check if it exists here because that could return false for network shares.
  46. var data = new DirectoryInfo(newPath);
  47. // add to our physical locations
  48. if (args != null)
  49. {
  50. args.AddAdditionalLocation(newPath);
  51. }
  52. dict[newPath] = data;
  53. }
  54. else if (flattenFolderDepth > 0 && isDirectory)
  55. {
  56. foreach (var child in GetFilteredFileSystemEntries(fullName, logger, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  57. {
  58. dict[child.Key] = child.Value;
  59. }
  60. }
  61. else
  62. {
  63. dict[fullName] = entry;
  64. }
  65. }
  66. return dict;
  67. }
  68. }
  69. }