FileData.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.HasFlag(FileAttributes.Directory);
  35. if (resolveShortcuts && FileSystem.IsShortcut(entry.FullName))
  36. {
  37. var newPath = FileSystem.ResolveShortcut(entry.FullName);
  38. if (string.IsNullOrWhiteSpace(newPath))
  39. {
  40. //invalid shortcut - could be old or target could just be unavailable
  41. logger.Warn("Encountered invalid shortcut: " + entry.FullName);
  42. continue;
  43. }
  44. var data = FileSystem.GetFileSystemInfo(newPath);
  45. dict[data.FullName] = data;
  46. }
  47. else if (flattenFolderDepth > 0 && isDirectory)
  48. {
  49. foreach (var child in GetFilteredFileSystemEntries(entry.FullName, logger, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  50. {
  51. dict[child.Key] = child.Value;
  52. }
  53. }
  54. else
  55. {
  56. dict[entry.FullName] = entry;
  57. }
  58. }
  59. return dict;
  60. }
  61. }
  62. }