FileData.cs 3.5 KB

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