LibraryHelpers.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. namespace MediaBrowser.Api.Library
  8. {
  9. /// <summary>
  10. /// Class LibraryHelpers
  11. /// </summary>
  12. public static class LibraryHelpers
  13. {
  14. /// <summary>
  15. /// The shortcut file extension
  16. /// </summary>
  17. private const string ShortcutFileExtension = ".mblink";
  18. /// <summary>
  19. /// The shortcut file search
  20. /// </summary>
  21. private const string ShortcutFileSearch = "*" + ShortcutFileExtension;
  22. /// <summary>
  23. /// Deletes a shortcut from within a virtual folder, within either the default view or a user view
  24. /// </summary>
  25. /// <param name="fileSystem">The file system.</param>
  26. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  27. /// <param name="mediaPath">The media path.</param>
  28. /// <param name="user">The user.</param>
  29. /// <param name="appPaths">The app paths.</param>
  30. /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
  31. public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
  32. {
  33. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  34. var path = Path.Combine(rootFolderPath, virtualFolderName);
  35. if (!Directory.Exists(path))
  36. {
  37. throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
  38. }
  39. var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
  40. if (!string.IsNullOrEmpty(shortcut))
  41. {
  42. File.Delete(shortcut);
  43. }
  44. }
  45. /// <summary>
  46. /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  47. /// </summary>
  48. /// <param name="fileSystem">The file system.</param>
  49. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  50. /// <param name="path">The path.</param>
  51. /// <param name="user">The user.</param>
  52. /// <param name="appPaths">The app paths.</param>
  53. /// <exception cref="System.ArgumentException">The path is not valid.</exception>
  54. /// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception>
  55. public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
  56. {
  57. if (!Directory.Exists(path))
  58. {
  59. throw new DirectoryNotFoundException("The path does not exist.");
  60. }
  61. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  62. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  63. ValidateNewMediaPath(fileSystem, rootFolderPath, path);
  64. var shortcutFilename = Path.GetFileNameWithoutExtension(path);
  65. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  66. while (File.Exists(lnk))
  67. {
  68. shortcutFilename += "1";
  69. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  70. }
  71. fileSystem.CreateShortcut(lnk, path);
  72. }
  73. /// <summary>
  74. /// Validates that a new media path can be added
  75. /// </summary>
  76. /// <param name="fileSystem">The file system.</param>
  77. /// <param name="currentViewRootFolderPath">The current view root folder path.</param>
  78. /// <param name="mediaPath">The media path.</param>
  79. /// <exception cref="System.ArgumentException">
  80. /// </exception>
  81. private static void ValidateNewMediaPath(IFileSystem fileSystem, string currentViewRootFolderPath, string mediaPath)
  82. {
  83. var pathsInCurrentVIew = Directory.EnumerateFiles(currentViewRootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
  84. .Select(fileSystem.ResolveShortcut)
  85. .ToList();
  86. // Don't allow duplicate sub-paths within the same user library, or it will result in duplicate items
  87. // See comments in IsNewPathValid
  88. var duplicate = pathsInCurrentVIew
  89. .FirstOrDefault(p => !IsNewPathValid(fileSystem, mediaPath, p));
  90. if (!string.IsNullOrEmpty(duplicate))
  91. {
  92. throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate));
  93. }
  94. // Make sure the current root folder doesn't already have a shortcut to the same path
  95. duplicate = pathsInCurrentVIew
  96. .FirstOrDefault(p => string.Equals(mediaPath, p, StringComparison.OrdinalIgnoreCase));
  97. if (!string.IsNullOrEmpty(duplicate))
  98. {
  99. throw new ArgumentException(string.Format("The path {0} already exists in the library", mediaPath));
  100. }
  101. }
  102. /// <summary>
  103. /// Validates that a new path can be added based on an existing path
  104. /// </summary>
  105. /// <param name="fileSystem">The file system.</param>
  106. /// <param name="newPath">The new path.</param>
  107. /// <param name="existingPath">The existing path.</param>
  108. /// <returns><c>true</c> if [is new path valid] [the specified new path]; otherwise, <c>false</c>.</returns>
  109. private static bool IsNewPathValid(IFileSystem fileSystem, string newPath, string existingPath)
  110. {
  111. // Example: D:\Movies is the existing path
  112. // D:\ cannot be added
  113. // Neither can D:\Movies\Kids
  114. // A D:\Movies duplicate is ok here since that will be caught later
  115. if (string.Equals(newPath, existingPath, StringComparison.OrdinalIgnoreCase))
  116. {
  117. return true;
  118. }
  119. // If enforceSubPathRestriction is true, validate the D:\Movies\Kids scenario
  120. if (fileSystem.ContainsSubPath(existingPath, newPath))
  121. {
  122. return false;
  123. }
  124. // Validate the D:\ scenario
  125. if (fileSystem.ContainsSubPath(newPath, existingPath))
  126. {
  127. return false;
  128. }
  129. return true;
  130. }
  131. }
  132. }