LibraryHelpers.cs 7.9 KB

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