2
0

LibraryHelpers.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 (!string.IsNullOrWhiteSpace(path))
  54. {
  55. throw new ArgumentNullException("path");
  56. }
  57. if (!fileSystem.DirectoryExists(path))
  58. {
  59. throw new DirectoryNotFoundException("The path does not exist.");
  60. }
  61. var rootFolderPath = appPaths.DefaultUserViewsPath;
  62. var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName);
  63. var shortcutFilename = fileSystem.GetFileNameWithoutExtension(path);
  64. var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  65. while (fileSystem.FileExists(lnk))
  66. {
  67. shortcutFilename += "1";
  68. lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension);
  69. }
  70. fileSystem.CreateShortcut(lnk, path);
  71. }
  72. }
  73. }