LibraryHelpers.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. public static void AddMediaPath(IFileSystem fileSystem, string virtualFolderName, string path, IServerApplicationPaths appPaths)
  52. {
  53. if (!fileSystem.DirectoryExists(path))
  54. {
  55. throw new DirectoryNotFoundException("The path does not exist.");
  56. }
  57. var rootFolderPath = appPaths.DefaultUserViewsPath;
  58. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  59. var shortcutFilename = fileSystem.GetFileNameWithoutExtension(path);
  60. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  61. while (fileSystem.FileExists(lnk))
  62. {
  63. shortcutFilename += "1";
  64. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  65. }
  66. fileSystem.CreateShortcut(lnk, path);
  67. }
  68. }
  69. }