LibraryHelpers.cs 12 KB

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