LibraryHelpers.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }
  48. }