LibraryHelpers.cs 9.5 KB

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