| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using MediaBrowser.Common.Configuration; |
| | | 6 | | using MediaBrowser.Controller.Configuration; |
| | | 7 | | using MediaBrowser.Controller.Entities; |
| | | 8 | | using MediaBrowser.Controller.IO; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | |
| | | 11 | | namespace Emby.Server.Implementations.Library; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// IPathManager implementation. |
| | | 15 | | /// </summary> |
| | | 16 | | public class PathManager : IPathManager |
| | | 17 | | { |
| | | 18 | | private readonly ILogger<PathManager> _logger; |
| | | 19 | | private readonly IServerConfigurationManager _config; |
| | | 20 | | private readonly IApplicationPaths _appPaths; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="PathManager"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="logger">The logger.</param> |
| | | 26 | | /// <param name="config">The server configuration manager.</param> |
| | | 27 | | /// <param name="appPaths">The application paths.</param> |
| | | 28 | | public PathManager( |
| | | 29 | | ILogger<PathManager> logger, |
| | | 30 | | IServerConfigurationManager config, |
| | | 31 | | IApplicationPaths appPaths) |
| | | 32 | | { |
| | 21 | 33 | | _logger = logger; |
| | 21 | 34 | | _config = config; |
| | 21 | 35 | | _appPaths = appPaths; |
| | 21 | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles"); |
| | | 39 | | |
| | 0 | 40 | | private string AttachmentCachePath => Path.Combine(_appPaths.DataPath, "attachments"); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public string? GetAttachmentPath(string mediaSourceId, string fileName) |
| | | 44 | | { |
| | 0 | 45 | | var folder = GetAttachmentFolderPath(mediaSourceId); |
| | 0 | 46 | | return folder is null ? null : Path.Combine(folder, fileName); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public string? GetAttachmentFolderPath(string mediaSourceId) |
| | | 51 | | { |
| | 0 | 52 | | if (!Guid.TryParse(mediaSourceId, out var parsed)) |
| | | 53 | | { |
| | 0 | 54 | | _logger.LogDebug("MediaSource Id '{MediaSourceId}' is not a GUID; no on-disk attachment folder.", mediaSourc |
| | 0 | 55 | | return null; |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | var id = parsed.ToString("D", CultureInfo.InvariantCulture).AsSpan(); |
| | 0 | 59 | | return Path.Join(AttachmentCachePath, id[..2], id); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public string? GetSubtitleFolderPath(string mediaSourceId) |
| | | 64 | | { |
| | 0 | 65 | | if (!Guid.TryParse(mediaSourceId, out var parsed)) |
| | | 66 | | { |
| | 0 | 67 | | _logger.LogDebug("MediaSource Id '{MediaSourceId}' is not a GUID; no on-disk subtitle folder.", mediaSourceI |
| | 0 | 68 | | return null; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | var id = parsed.ToString("D", CultureInfo.InvariantCulture).AsSpan(); |
| | 0 | 72 | | return Path.Join(SubtitleCachePath, id[..2], id); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public string? GetSubtitlePath(string mediaSourceId, int streamIndex, string extension) |
| | | 77 | | { |
| | 0 | 78 | | var folder = GetSubtitleFolderPath(mediaSourceId); |
| | 0 | 79 | | return folder is null ? null : Path.Combine(folder, streamIndex.ToString(CultureInfo.InvariantCulture) + extensi |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public string GetTrickplayDirectory(BaseItem item, bool saveWithMedia = false) |
| | | 84 | | { |
| | 0 | 85 | | var id = item.Id.ToString("D", CultureInfo.InvariantCulture).AsSpan(); |
| | | 86 | | |
| | 0 | 87 | | return saveWithMedia |
| | 0 | 88 | | ? Path.Combine(item.ContainingFolderPath, Path.ChangeExtension(Path.GetFileName(item.Path), ".trickplay")) |
| | 0 | 89 | | : Path.Join(_config.ApplicationPaths.TrickplayPath, id[..2], id); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc/> |
| | | 93 | | public string GetChapterImageFolderPath(BaseItem item) |
| | | 94 | | { |
| | 0 | 95 | | return Path.Combine(item.GetInternalMetadataPath(), "chapters"); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc/> |
| | | 99 | | public string GetChapterImagePath(BaseItem item, long chapterPositionTicks) |
| | | 100 | | { |
| | 0 | 101 | | var filename = item.DateModified.Ticks.ToString(CultureInfo.InvariantCulture) + "_" + chapterPositionTicks.ToStr |
| | | 102 | | |
| | 0 | 103 | | return Path.Combine(GetChapterImageFolderPath(item), filename); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <inheritdoc/> |
| | | 107 | | public IReadOnlyList<string> GetExtractedDataPaths(BaseItem item) |
| | | 108 | | { |
| | 0 | 109 | | var mediaSourceId = item.Id.ToString("N", CultureInfo.InvariantCulture); |
| | 0 | 110 | | List<string> paths = []; |
| | 0 | 111 | | var attachmentFolder = GetAttachmentFolderPath(mediaSourceId); |
| | 0 | 112 | | if (attachmentFolder is not null) |
| | | 113 | | { |
| | 0 | 114 | | paths.Add(attachmentFolder); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | var subtitleFolder = GetSubtitleFolderPath(mediaSourceId); |
| | 0 | 118 | | if (subtitleFolder is not null) |
| | | 119 | | { |
| | 0 | 120 | | paths.Add(subtitleFolder); |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | paths.Add(GetTrickplayDirectory(item, false)); |
| | 0 | 124 | | paths.Add(GetTrickplayDirectory(item, true)); |
| | 0 | 125 | | paths.Add(GetChapterImageFolderPath(item)); |
| | | 126 | | |
| | 0 | 127 | | return paths; |
| | | 128 | | } |
| | | 129 | | } |