LibraryHelpers.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.IO;
  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. /// Adds the virtual folder.
  16. /// </summary>
  17. /// <param name="name">The name.</param>
  18. /// <param name="user">The user.</param>
  19. /// <param name="appPaths">The app paths.</param>
  20. /// <exception cref="System.ArgumentException">There is already a media collection with the name + name + .</exception>
  21. public static void AddVirtualFolder(string name, User user, IServerApplicationPaths appPaths)
  22. {
  23. name = FileSystem.GetValidFilename(name);
  24. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  25. var virtualFolderPath = Path.Combine(rootFolderPath, name);
  26. if (Directory.Exists(virtualFolderPath))
  27. {
  28. throw new ArgumentException("There is already a media collection with the name " + name + ".");
  29. }
  30. Directory.CreateDirectory(virtualFolderPath);
  31. }
  32. /// <summary>
  33. /// Removes the virtual folder.
  34. /// </summary>
  35. /// <param name="name">The name.</param>
  36. /// <param name="user">The user.</param>
  37. /// <param name="appPaths">The app paths.</param>
  38. /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
  39. public static void RemoveVirtualFolder(string name, User user, IServerApplicationPaths appPaths)
  40. {
  41. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  42. var path = Path.Combine(rootFolderPath, name);
  43. if (!Directory.Exists(path))
  44. {
  45. throw new DirectoryNotFoundException("The media folder does not exist");
  46. }
  47. Directory.Delete(path, true);
  48. }
  49. /// <summary>
  50. /// Renames the virtual folder.
  51. /// </summary>
  52. /// <param name="name">The name.</param>
  53. /// <param name="newName">The new name.</param>
  54. /// <param name="user">The user.</param>
  55. /// <param name="appPaths">The app paths.</param>
  56. /// <exception cref="System.IO.DirectoryNotFoundException">The media collection does not exist</exception>
  57. /// <exception cref="System.ArgumentException">There is already a media collection with the name + newPath + .</exception>
  58. public static void RenameVirtualFolder(string name, string newName, User user, IServerApplicationPaths appPaths)
  59. {
  60. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  61. var currentPath = Path.Combine(rootFolderPath, name);
  62. var newPath = Path.Combine(rootFolderPath, newName);
  63. if (!Directory.Exists(currentPath))
  64. {
  65. throw new DirectoryNotFoundException("The media collection does not exist");
  66. }
  67. if (!string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(newPath))
  68. {
  69. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  70. }
  71. //Only make a two-phase move when changing capitalization
  72. if (string.Equals(currentPath, newPath, StringComparison.OrdinalIgnoreCase))
  73. {
  74. //Create an unique name
  75. var temporaryName = Guid.NewGuid().ToString();
  76. var temporaryPath = Path.Combine(rootFolderPath, temporaryName);
  77. Directory.Move(currentPath,temporaryPath);
  78. currentPath = temporaryPath;
  79. }
  80. Directory.Move(currentPath, newPath);
  81. }
  82. /// <summary>
  83. /// Deletes a shortcut from within a virtual folder, within either the default view or a user view
  84. /// </summary>
  85. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  86. /// <param name="mediaPath">The media path.</param>
  87. /// <param name="user">The user.</param>
  88. /// <param name="appPaths">The app paths.</param>
  89. /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
  90. public static void RemoveMediaPath(string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
  91. {
  92. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  93. var path = Path.Combine(rootFolderPath, virtualFolderName);
  94. if (!Directory.Exists(path))
  95. {
  96. throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
  97. }
  98. var shortcut = Directory.EnumerateFiles(path, "*.lnk", SearchOption.AllDirectories).FirstOrDefault(f => FileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
  99. if (!string.IsNullOrEmpty(shortcut))
  100. {
  101. File.Delete(shortcut);
  102. }
  103. }
  104. /// <summary>
  105. /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  106. /// </summary>
  107. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  108. /// <param name="path">The path.</param>
  109. /// <param name="user">The user.</param>
  110. /// <param name="appPaths">The app paths.</param>
  111. /// <exception cref="System.ArgumentException">The path is not valid.</exception>
  112. /// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception>
  113. public static void AddMediaPath(string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
  114. {
  115. if (!Path.IsPathRooted(path))
  116. {
  117. throw new ArgumentException("The path is not valid.");
  118. }
  119. if (!Directory.Exists(path))
  120. {
  121. throw new DirectoryNotFoundException("The path does not exist.");
  122. }
  123. // Strip off trailing slash, but not on drives
  124. path = path.TrimEnd(Path.DirectorySeparatorChar);
  125. if (path.EndsWith(":", StringComparison.OrdinalIgnoreCase))
  126. {
  127. path += Path.DirectorySeparatorChar;
  128. }
  129. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  130. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  131. ValidateNewMediaPath(rootFolderPath, path, appPaths);
  132. var shortcutFilename = Path.GetFileNameWithoutExtension(path);
  133. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ".lnk");
  134. while (File.Exists(lnk))
  135. {
  136. shortcutFilename += "1";
  137. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ".lnk");
  138. }
  139. FileSystem.CreateShortcut(lnk, path);
  140. }
  141. /// <summary>
  142. /// Validates that a new media path can be added
  143. /// </summary>
  144. /// <param name="currentViewRootFolderPath">The current view root folder path.</param>
  145. /// <param name="mediaPath">The media path.</param>
  146. /// <param name="appPaths">The app paths.</param>
  147. /// <exception cref="System.ArgumentException"></exception>
  148. private static void ValidateNewMediaPath(string currentViewRootFolderPath, string mediaPath, IServerApplicationPaths appPaths)
  149. {
  150. var duplicate = Directory.EnumerateFiles(appPaths.RootFolderPath, "*.lnk", SearchOption.AllDirectories)
  151. .Select(FileSystem.ResolveShortcut)
  152. .FirstOrDefault(p => !IsNewPathValid(mediaPath, p, false));
  153. if (!string.IsNullOrEmpty(duplicate))
  154. {
  155. throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate));
  156. }
  157. // Don't allow duplicate sub-paths within the same user library, or it will result in duplicate items
  158. // See comments in IsNewPathValid
  159. duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, "*.lnk", SearchOption.AllDirectories)
  160. .Select(FileSystem.ResolveShortcut)
  161. .FirstOrDefault(p => !IsNewPathValid(mediaPath, p, true));
  162. if (!string.IsNullOrEmpty(duplicate))
  163. {
  164. throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate));
  165. }
  166. // Make sure the current root folder doesn't already have a shortcut to the same path
  167. duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, "*.lnk", SearchOption.AllDirectories)
  168. .Select(FileSystem.ResolveShortcut)
  169. .FirstOrDefault(p => mediaPath.Equals(p, StringComparison.OrdinalIgnoreCase));
  170. if (!string.IsNullOrEmpty(duplicate))
  171. {
  172. throw new ArgumentException(string.Format("The path {0} already exists in the library", mediaPath));
  173. }
  174. }
  175. /// <summary>
  176. /// Validates that a new path can be added based on an existing path
  177. /// </summary>
  178. /// <param name="newPath">The new path.</param>
  179. /// <param name="existingPath">The existing path.</param>
  180. /// <param name="enforceSubPathRestriction">if set to <c>true</c> [enforce sub path restriction].</param>
  181. /// <returns><c>true</c> if [is new path valid] [the specified new path]; otherwise, <c>false</c>.</returns>
  182. private static bool IsNewPathValid(string newPath, string existingPath, bool enforceSubPathRestriction)
  183. {
  184. // Example: D:\Movies is the existing path
  185. // D:\ cannot be added
  186. // Neither can D:\Movies\Kids
  187. // A D:\Movies duplicate is ok here since that will be caught later
  188. if (newPath.Equals(existingPath, StringComparison.OrdinalIgnoreCase))
  189. {
  190. return true;
  191. }
  192. // If enforceSubPathRestriction is true, validate the D:\Movies\Kids scenario
  193. if (enforceSubPathRestriction && newPath.StartsWith(existingPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
  194. {
  195. return false;
  196. }
  197. // Validate the D:\ scenario
  198. if (existingPath.StartsWith(newPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
  199. {
  200. return false;
  201. }
  202. return true;
  203. }
  204. }
  205. }