PathManager.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using MediaBrowser.Common.Configuration;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.IO;
  8. namespace Emby.Server.Implementations.Library;
  9. /// <summary>
  10. /// IPathManager implementation.
  11. /// </summary>
  12. public class PathManager : IPathManager
  13. {
  14. private readonly IServerConfigurationManager _config;
  15. private readonly IApplicationPaths _appPaths;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="PathManager"/> class.
  18. /// </summary>
  19. /// <param name="config">The server configuration manager.</param>
  20. /// <param name="appPaths">The application paths.</param>
  21. public PathManager(
  22. IServerConfigurationManager config,
  23. IApplicationPaths appPaths)
  24. {
  25. _config = config;
  26. _appPaths = appPaths;
  27. }
  28. private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles");
  29. private string AttachmentCachePath => Path.Combine(_appPaths.DataPath, "attachments");
  30. /// <inheritdoc />
  31. public string GetAttachmentPath(string mediaSourceId, string fileName)
  32. {
  33. return Path.Join(GetAttachmentFolderPath(mediaSourceId), fileName);
  34. }
  35. /// <inheritdoc />
  36. public string GetAttachmentFolderPath(string mediaSourceId)
  37. {
  38. var id = Guid.Parse(mediaSourceId).ToString("D", CultureInfo.InvariantCulture).AsSpan();
  39. return Path.Join(AttachmentCachePath, id[..2], id);
  40. }
  41. /// <inheritdoc />
  42. public string GetSubtitleFolderPath(string mediaSourceId)
  43. {
  44. var id = Guid.Parse(mediaSourceId).ToString("D", CultureInfo.InvariantCulture).AsSpan();
  45. return Path.Join(SubtitleCachePath, id[..2], id);
  46. }
  47. /// <inheritdoc />
  48. public string GetSubtitlePath(string mediaSourceId, int streamIndex, string extension)
  49. {
  50. return Path.Join(GetSubtitleFolderPath(mediaSourceId), streamIndex.ToString(CultureInfo.InvariantCulture) + extension);
  51. }
  52. /// <inheritdoc />
  53. public string GetTrickplayDirectory(BaseItem item, bool saveWithMedia = false)
  54. {
  55. var id = item.Id.ToString("D", CultureInfo.InvariantCulture).AsSpan();
  56. return saveWithMedia
  57. ? Path.Combine(item.ContainingFolderPath, Path.ChangeExtension(item.Path, ".trickplay"))
  58. : Path.Join(_config.ApplicationPaths.TrickplayPath, id[..2], id);
  59. }
  60. }