LibraryHelpers.cs 3.4 KB

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