LibraryHelpers.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (string.IsNullOrWhiteSpace(mediaPath))
  33. {
  34. throw new ArgumentNullException("mediaPath");
  35. }
  36. var rootFolderPath = appPaths.DefaultUserViewsPath;
  37. var path = Path.Combine(rootFolderPath, virtualFolderName);
  38. if (!fileSystem.DirectoryExists(path))
  39. {
  40. throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName));
  41. }
  42. var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase));
  43. if (!string.IsNullOrEmpty(shortcut))
  44. {
  45. fileSystem.DeleteFile(shortcut);
  46. }
  47. }
  48. /// <summary>
  49. /// Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  50. /// </summary>
  51. /// <param name="fileSystem">The file system.</param>
  52. /// <param name="virtualFolderName">Name of the virtual folder.</param>
  53. /// <param name="path">The path.</param>
  54. /// <param name="appPaths">The app paths.</param>
  55. public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, IServerApplicationPaths appPaths)
  56. {
  57. if (string.IsNullOrWhiteSpace(path))
  58. {
  59. throw new ArgumentNullException("path");
  60. }
  61. if (!fileSystem.DirectoryExists(path))
  62. {
  63. throw new DirectoryNotFoundException("The path does not exist.");
  64. }
  65. var rootFolderPath = appPaths.DefaultUserViewsPath;
  66. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  67. var shortcutFilename = fileSystem.GetFileNameWithoutExtension(path);
  68. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  69. while (fileSystem.FileExists(lnk))
  70. {
  71. shortcutFilename += "1";
  72. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  73. }
  74. fileSystem.CreateShortcut(lnk, path);
  75. }
  76. }
  77. }