LibraryHelpers.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 (Directory.Exists(newPath))
  68. {
  69. throw new ArgumentException("There is already a media collection with the name " + newPath + ".");
  70. }
  71. Directory.Move(currentPath, newPath);
  72. }
  73. /// <summary>
  74. /// Deletes a shortcut from within a virtual folder, within either the default view or a user view
  75. /// </summary>
  76. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  77. /// <param name="mediaPath">The media path.</param>
  78. /// <param name="user">The user.</param>
  79. /// <param name="appPaths">The app paths.</param>
  80. /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
  81. public static void RemoveMediaPath(string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
  82. {
  83. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  84. var path = Path.Combine(rootFolderPath, virtualFolderName);
  85. if (!Directory.Exists(path))
  86. {
  87. throw new DirectoryNotFoundException("The media folder does not exist");
  88. }
  89. var shortcut = Directory.EnumerateFiles(path, "*.lnk", SearchOption.AllDirectories).FirstOrDefault(f => FileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
  90. if (string.IsNullOrEmpty(shortcut))
  91. {
  92. throw new DirectoryNotFoundException("The media folder does not exist");
  93. }
  94. File.Delete(shortcut);
  95. }
  96. /// <summary>
  97. /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  98. /// </summary>
  99. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  100. /// <param name="path">The path.</param>
  101. /// <param name="user">The user.</param>
  102. /// <param name="appPaths">The app paths.</param>
  103. /// <exception cref="System.ArgumentException">The path is not valid.</exception>
  104. /// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception>
  105. public static void AddMediaPath(string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
  106. {
  107. if (!Path.IsPathRooted(path))
  108. {
  109. throw new ArgumentException("The path is not valid.");
  110. }
  111. if (!Directory.Exists(path))
  112. {
  113. throw new DirectoryNotFoundException("The path does not exist.");
  114. }
  115. // Strip off trailing slash, but not on drives
  116. path = path.TrimEnd(Path.DirectorySeparatorChar);
  117. if (path.EndsWith(":", StringComparison.OrdinalIgnoreCase))
  118. {
  119. path += Path.DirectorySeparatorChar;
  120. }
  121. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  122. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  123. ValidateNewMediaPath(rootFolderPath, path, appPaths);
  124. var shortcutFilename = Path.GetFileNameWithoutExtension(path);
  125. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ".lnk");
  126. while (File.Exists(lnk))
  127. {
  128. shortcutFilename += "1";
  129. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ".lnk");
  130. }
  131. FileSystem.CreateShortcut(lnk, path);
  132. }
  133. /// <summary>
  134. /// Validates that a new media path can be added
  135. /// </summary>
  136. /// <param name="currentViewRootFolderPath">The current view root folder path.</param>
  137. /// <param name="mediaPath">The media path.</param>
  138. /// <param name="appPaths">The app paths.</param>
  139. /// <exception cref="System.ArgumentException"></exception>
  140. private static void ValidateNewMediaPath(string currentViewRootFolderPath, string mediaPath, IServerApplicationPaths appPaths)
  141. {
  142. var duplicate = Directory.EnumerateFiles(appPaths.RootFolderPath, "*.lnk", SearchOption.AllDirectories)
  143. .Select(FileSystem.ResolveShortcut)
  144. .FirstOrDefault(p => !IsNewPathValid(mediaPath, p));
  145. if (!string.IsNullOrEmpty(duplicate))
  146. {
  147. throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate));
  148. }
  149. // Make sure the current root folder doesn't already have a shortcut to the same path
  150. duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, "*.lnk", SearchOption.AllDirectories)
  151. .Select(FileSystem.ResolveShortcut)
  152. .FirstOrDefault(p => mediaPath.Equals(p, StringComparison.OrdinalIgnoreCase));
  153. if (!string.IsNullOrEmpty(duplicate))
  154. {
  155. throw new ArgumentException(string.Format("The path {0} already exists in the library", mediaPath));
  156. }
  157. }
  158. /// <summary>
  159. /// Validates that a new path can be added based on an existing path
  160. /// </summary>
  161. /// <param name="newPath">The new path.</param>
  162. /// <param name="existingPath">The existing path.</param>
  163. /// <returns><c>true</c> if [is new path valid] [the specified new path]; otherwise, <c>false</c>.</returns>
  164. private static bool IsNewPathValid(string newPath, string existingPath)
  165. {
  166. // Example: D:\Movies is the existing path
  167. // D:\ cannot be added
  168. // Neither can D:\Movies\Kids
  169. // A D:\Movies duplicate is ok here since that will be caught later
  170. if (newPath.Equals(existingPath, StringComparison.OrdinalIgnoreCase))
  171. {
  172. return true;
  173. }
  174. // Validate the D:\Movies\Kids scenario
  175. if (newPath.StartsWith(existingPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
  176. {
  177. return false;
  178. }
  179. // Validate the D:\ scenario
  180. if (existingPath.StartsWith(newPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
  181. {
  182. return false;
  183. }
  184. return true;
  185. }
  186. }
  187. }