LibraryHelpers.cs 11 KB

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