UpdateMediaLibraryHandler.cs 10 KB

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