LibraryHelpers.cs 11 KB

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