LibraryHelpers.cs 3.5 KB

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