PathManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.Join(_appPaths.DataPath, "subtitles");
  29. private string AttachmentCachePath => Path.Join(_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.Join(item.ContainingFolderPath, Path.ChangeExtension(item.Path, ".trickplay"))
  58. : Path.Join(_config.ApplicationPaths.TrickplayPath, id[..2], id);
  59. }
  60. /// <inheritdoc/>
  61. public string GetChapterImageFolderPath(BaseItem item)
  62. {
  63. return Path.Join(item.GetInternalMetadataPath(), "chapters");
  64. }
  65. /// <inheritdoc/>
  66. public string GetChapterImagePath(BaseItem item, long chapterPositionTicks)
  67. {
  68. var filename = item.DateModified.Ticks.ToString(CultureInfo.InvariantCulture) + "_" + chapterPositionTicks.ToString(CultureInfo.InvariantCulture) + ".jpg";
  69. return Path.Join(GetChapterImageFolderPath(item), filename);
  70. }
  71. }