LibraryHelpers.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Strip off trailing slash, but not on drives
  62. path = path.TrimEnd(Path.DirectorySeparatorChar);
  63. if (path.EndsWith(":", StringComparison.OrdinalIgnoreCase))
  64. {
  65. path += Path.DirectorySeparatorChar;
  66. }
  67. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  68. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  69. ValidateNewMediaPath(fileSystem, rootFolderPath, path, appPaths);
  70. var shortcutFilename = Path.GetFileNameWithoutExtension(path);
  71. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  72. while (File.Exists(lnk))
  73. {
  74. shortcutFilename += "1";
  75. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  76. }
  77. fileSystem.CreateShortcut(lnk, path);
  78. }
  79. /// <summary>
  80. /// Validates that a new media path can be added
  81. /// </summary>
  82. /// <param name="fileSystem">The file system.</param>
  83. /// <param name="currentViewRootFolderPath">The current view root folder path.</param>
  84. /// <param name="mediaPath">The media path.</param>
  85. /// <param name="appPaths">The app paths.</param>
  86. /// <exception cref="System.ArgumentException">
  87. /// </exception>
  88. private static void ValidateNewMediaPath(IFileSystem fileSystem, string currentViewRootFolderPath, string mediaPath, IServerApplicationPaths appPaths)
  89. {
  90. var duplicate = Directory.EnumerateFiles(appPaths.RootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
  91. .Select(fileSystem.ResolveShortcut)
  92. .FirstOrDefault(p => !IsNewPathValid(mediaPath, p, false));
  93. if (!string.IsNullOrEmpty(duplicate))
  94. {
  95. throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate));
  96. }
  97. // Don't allow duplicate sub-paths within the same user library, or it will result in duplicate items
  98. // See comments in IsNewPathValid
  99. duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
  100. .Select(fileSystem.ResolveShortcut)
  101. .FirstOrDefault(p => !IsNewPathValid(mediaPath, p, true));
  102. if (!string.IsNullOrEmpty(duplicate))
  103. {
  104. throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate));
  105. }
  106. // Make sure the current root folder doesn't already have a shortcut to the same path
  107. duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, ShortcutFileSearch, SearchOption.AllDirectories)
  108. .Select(fileSystem.ResolveShortcut)
  109. .FirstOrDefault(p => mediaPath.Equals(p, StringComparison.OrdinalIgnoreCase));
  110. if (!string.IsNullOrEmpty(duplicate))
  111. {
  112. throw new ArgumentException(string.Format("The path {0} already exists in the library", mediaPath));
  113. }
  114. }
  115. /// <summary>
  116. /// Validates that a new path can be added based on an existing path
  117. /// </summary>
  118. /// <param name="newPath">The new path.</param>
  119. /// <param name="existingPath">The existing path.</param>
  120. /// <param name="enforceSubPathRestriction">if set to <c>true</c> [enforce sub path restriction].</param>
  121. /// <returns><c>true</c> if [is new path valid] [the specified new path]; otherwise, <c>false</c>.</returns>
  122. private static bool IsNewPathValid(string newPath, string existingPath, bool enforceSubPathRestriction)
  123. {
  124. // Example: D:\Movies is the existing path
  125. // D:\ cannot be added
  126. // Neither can D:\Movies\Kids
  127. // A D:\Movies duplicate is ok here since that will be caught later
  128. if (newPath.Equals(existingPath, StringComparison.OrdinalIgnoreCase))
  129. {
  130. return true;
  131. }
  132. // If enforceSubPathRestriction is true, validate the D:\Movies\Kids scenario
  133. if (enforceSubPathRestriction && newPath.StartsWith(existingPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
  134. {
  135. return false;
  136. }
  137. // Validate the D:\ scenario
  138. if (existingPath.StartsWith(newPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
  139. {
  140. return false;
  141. }
  142. return true;
  143. }
  144. }
  145. }