LibraryHelpers.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  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. /// 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="user">The user.</param>
  29. /// <param name="appPaths">The app paths.</param>
  30. /// <exception cref="System.IO.DirectoryNotFoundException">The media folder does not exist</exception>
  31. public static void RemoveMediaPath(IFileSystem fileSystem, string virtualFolderName, string mediaPath, User user, IServerApplicationPaths appPaths)
  32. {
  33. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  34. var path = Path.Combine(rootFolderPath, virtualFolderName);
  35. if (!Directory.Exists(path))
  36. {
  37. throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
  38. }
  39. var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
  40. if (!string.IsNullOrEmpty(shortcut))
  41. {
  42. File.Delete(shortcut);
  43. }
  44. }
  45. /// <summary>
  46. /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  47. /// </summary>
  48. /// <param name="fileSystem">The file system.</param>
  49. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  50. /// <param name="path">The path.</param>
  51. /// <param name="user">The user.</param>
  52. /// <param name="appPaths">The app paths.</param>
  53. /// <exception cref="System.ArgumentException">The path is not valid.</exception>
  54. /// <exception cref="System.IO.DirectoryNotFoundException">The path does not exist.</exception>
  55. public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, User user, IServerApplicationPaths appPaths)
  56. {
  57. if (!Directory.Exists(path))
  58. {
  59. throw new DirectoryNotFoundException("The path does not exist.");
  60. }
  61. var rootFolderPath = user != null ? user.RootFolderPath : appPaths.DefaultUserViewsPath;
  62. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  63. var shortcutFilename = Path.GetFileNameWithoutExtension(path);
  64. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  65. while (File.Exists(lnk))
  66. {
  67. shortcutFilename += "1";
  68. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  69. }
  70. fileSystem.CreateShortcut(lnk, path);
  71. }
  72. }
  73. }