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