LibraryHelpers.cs 9.7 KB

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