LibraryHelpers.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using CommonIO;
  7. namespace MediaBrowser.Api.Library
  8. {
  9. /// <summary>
  10. /// Class LibraryHelpers
  11. /// </summary>
  12. public static class LibraryHelpers
  13. {
  14. /// <summary>
  15. /// The shortcut file extension
  16. /// </summary>
  17. private const string ShortcutFileExtension = ".mblink";
  18. /// <summary>
  19. /// The shortcut file search
  20. /// </summary>
  21. private const string ShortcutFileSearch = "*" + ShortcutFileExtension;
  22. /// <summary>
  23. /// Deletes a shortcut from within a virtual folder, within either the default view or a user view
  24. /// </summary>
  25. /// <param name="fileSystem">The file system.</param>
  26. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  27. /// <param name="mediaPath">The media path.</param>
  28. /// <param name="appPaths">The app paths.</param>
  29. /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
  30. public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, IServerApplicationPaths appPaths)
  31. {
  32. var rootFolderPath = appPaths.DefaultUserViewsPath;
  33. var path = Path.Combine(rootFolderPath, virtualFolderName);
  34. if (!fileSystem.DirectoryExists(path))
  35. {
  36. throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
  37. }
  38. var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
  39. if (!string.IsNullOrEmpty(shortcut))
  40. {
  41. fileSystem.DeleteFile(shortcut);
  42. }
  43. }
  44. /// <summary>
  45. /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  46. /// </summary>
  47. /// <param name="fileSystem">The file system.</param>
  48. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  49. /// <param name="path">The path.</param>
  50. /// <param name="appPaths">The app paths.</param>
  51. /// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception>
  52. /// <exception cref="System.ArgumentException">The path is not valid.</exception>
  53. public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, IServerApplicationPaths appPaths)
  54. {
  55. if (!fileSystem.DirectoryExists(path))
  56. {
  57. throw new DirectoryNotFoundException("The path does not exist.");
  58. }
  59. var rootFolderPath = appPaths.DefaultUserViewsPath;
  60. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  61. var shortcutFilename = fileSystem.GetFileNameWithoutExtension(path);
  62. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  63. while (fileSystem.FileExists(lnk))
  64. {
  65. shortcutFilename += "1";
  66. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  67. }
  68. fileSystem.CreateShortcut(lnk, path);
  69. }
  70. }
  71. }