| | 1 | | #pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Diagnostics; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.IO; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Security.Cryptography; |
| | 9 | | using System.Text; |
| | 10 | | using Jellyfin.Data.Enums; |
| | 11 | | using MediaBrowser.Common.Configuration; |
| | 12 | | using MediaBrowser.Common.Extensions; |
| | 13 | | using MediaBrowser.Controller.Entities; |
| | 14 | | using MediaBrowser.Controller.IO; |
| | 15 | | using MediaBrowser.Controller.Library; |
| | 16 | | using MediaBrowser.Model.Entities; |
| | 17 | | using MediaBrowser.Model.MediaInfo; |
| | 18 | | using Microsoft.Extensions.Logging; |
| | 19 | |
|
| | 20 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Migration to move extracted files to the new directories. |
| | 24 | | /// </summary> |
| | 25 | | public class MoveExtractedFiles : IDatabaseMigrationRoutine |
| | 26 | | { |
| | 27 | | private readonly IApplicationPaths _appPaths; |
| | 28 | | private readonly ILibraryManager _libraryManager; |
| | 29 | | private readonly ILogger<MoveExtractedFiles> _logger; |
| | 30 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | 31 | | private readonly IPathManager _pathManager; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="MoveExtractedFiles"/> class. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param> |
| | 37 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | 38 | | /// <param name="logger">The logger.</param> |
| | 39 | | /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param> |
| | 40 | | /// <param name="pathManager">Instance of the <see cref="IPathManager"/> interface.</param> |
| | 41 | | public MoveExtractedFiles( |
| | 42 | | IApplicationPaths appPaths, |
| | 43 | | ILibraryManager libraryManager, |
| | 44 | | ILogger<MoveExtractedFiles> logger, |
| | 45 | | IMediaSourceManager mediaSourceManager, |
| | 46 | | IPathManager pathManager) |
| | 47 | | { |
| 0 | 48 | | _appPaths = appPaths; |
| 0 | 49 | | _libraryManager = libraryManager; |
| 0 | 50 | | _logger = logger; |
| 0 | 51 | | _mediaSourceManager = mediaSourceManager; |
| 0 | 52 | | _pathManager = pathManager; |
| 0 | 53 | | } |
| | 54 | |
|
| 0 | 55 | | private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles"); |
| | 56 | |
|
| 0 | 57 | | private string AttachmentCachePath => Path.Combine(_appPaths.DataPath, "attachments"); |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| 0 | 60 | | public Guid Id => new("9063b0Ef-CFF1-4EDC-9A13-74093681A89B"); |
| | 61 | |
|
| | 62 | | /// <inheritdoc /> |
| 0 | 63 | | public string Name => "MoveExtractedFiles"; |
| | 64 | |
|
| | 65 | | /// <inheritdoc /> |
| 0 | 66 | | public bool PerformOnNewInstall => false; |
| | 67 | |
|
| | 68 | | /// <inheritdoc /> |
| | 69 | | public void Perform() |
| | 70 | | { |
| | 71 | | const int Limit = 500; |
| 0 | 72 | | int itemCount = 0, offset = 0; |
| | 73 | |
|
| 0 | 74 | | var sw = Stopwatch.StartNew(); |
| 0 | 75 | | var itemsQuery = new InternalItemsQuery |
| 0 | 76 | | { |
| 0 | 77 | | MediaTypes = [MediaType.Video], |
| 0 | 78 | | SourceTypes = [SourceType.Library], |
| 0 | 79 | | IsVirtualItem = false, |
| 0 | 80 | | IsFolder = false, |
| 0 | 81 | | Limit = Limit, |
| 0 | 82 | | StartIndex = offset, |
| 0 | 83 | | EnableTotalRecordCount = true, |
| 0 | 84 | | }; |
| | 85 | |
|
| 0 | 86 | | var records = _libraryManager.GetItemsResult(itemsQuery).TotalRecordCount; |
| 0 | 87 | | _logger.LogInformation("Checking {Count} items for movable extracted files.", records); |
| | 88 | |
|
| | 89 | | // Make sure directories exist |
| 0 | 90 | | Directory.CreateDirectory(SubtitleCachePath); |
| 0 | 91 | | Directory.CreateDirectory(AttachmentCachePath); |
| | 92 | |
|
| 0 | 93 | | itemsQuery.EnableTotalRecordCount = false; |
| | 94 | | do |
| | 95 | | { |
| 0 | 96 | | itemsQuery.StartIndex = offset; |
| 0 | 97 | | var result = _libraryManager.GetItemsResult(itemsQuery); |
| | 98 | |
|
| 0 | 99 | | var items = result.Items; |
| 0 | 100 | | foreach (var item in items) |
| | 101 | | { |
| 0 | 102 | | if (MoveSubtitleAndAttachmentFiles(item)) |
| | 103 | | { |
| 0 | 104 | | itemCount++; |
| | 105 | | } |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | offset += Limit; |
| 0 | 109 | | if (offset % 5_000 == 0) |
| | 110 | | { |
| 0 | 111 | | _logger.LogInformation("Checked extracted files for {Count} items in {Time}.", offset, sw.Elapsed); |
| | 112 | | } |
| 0 | 113 | | } while (offset < records); |
| | 114 | |
|
| 0 | 115 | | _logger.LogInformation("Checked {Checked} items - Moved files for {Items} items in {Time}.", records, itemCount, |
| | 116 | |
|
| | 117 | | // Get all subdirectories with 1 character names (those are the legacy directories) |
| 0 | 118 | | var subdirectories = Directory.GetDirectories(SubtitleCachePath, "*", SearchOption.AllDirectories).Where(s => s. |
| 0 | 119 | | subdirectories.AddRange(Directory.GetDirectories(AttachmentCachePath, "*", SearchOption.AllDirectories).Where(s |
| | 120 | |
|
| | 121 | | // Remove all legacy subdirectories |
| 0 | 122 | | foreach (var subdir in subdirectories) |
| | 123 | | { |
| 0 | 124 | | Directory.Delete(subdir, true); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | // Remove old cache path |
| 0 | 128 | | var attachmentCachePath = Path.Join(_appPaths.CachePath, "attachments"); |
| 0 | 129 | | if (Directory.Exists(attachmentCachePath)) |
| | 130 | | { |
| 0 | 131 | | Directory.Delete(attachmentCachePath, true); |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | _logger.LogInformation("Cleaned up left over subtitles and attachments."); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | private bool MoveSubtitleAndAttachmentFiles(BaseItem item) |
| | 138 | | { |
| 0 | 139 | | var mediaStreams = item.GetMediaStreams().Where(s => s.Type == MediaStreamType.Subtitle && !s.IsExternal); |
| 0 | 140 | | var itemIdString = item.Id.ToString("N", CultureInfo.InvariantCulture); |
| 0 | 141 | | var modified = false; |
| 0 | 142 | | foreach (var mediaStream in mediaStreams) |
| | 143 | | { |
| 0 | 144 | | if (mediaStream.Codec is null) |
| | 145 | | { |
| | 146 | | continue; |
| | 147 | | } |
| | 148 | |
|
| 0 | 149 | | var mediaStreamIndex = mediaStream.Index; |
| 0 | 150 | | var extension = GetSubtitleExtension(mediaStream.Codec); |
| 0 | 151 | | var oldSubtitleCachePath = GetOldSubtitleCachePath(item.Path, mediaStream.Index, extension); |
| 0 | 152 | | if (string.IsNullOrEmpty(oldSubtitleCachePath) || !File.Exists(oldSubtitleCachePath)) |
| | 153 | | { |
| | 154 | | continue; |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | var newSubtitleCachePath = _pathManager.GetSubtitlePath(itemIdString, mediaStreamIndex, extension); |
| 0 | 158 | | if (File.Exists(newSubtitleCachePath)) |
| | 159 | | { |
| 0 | 160 | | File.Delete(oldSubtitleCachePath); |
| | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 0 | 164 | | var newDirectory = Path.GetDirectoryName(newSubtitleCachePath); |
| 0 | 165 | | if (newDirectory is not null) |
| | 166 | | { |
| 0 | 167 | | Directory.CreateDirectory(newDirectory); |
| 0 | 168 | | File.Move(oldSubtitleCachePath, newSubtitleCachePath, false); |
| 0 | 169 | | _logger.LogDebug("Moved subtitle {Index} for {Item} from {Source} to {Destination}", mediaStreamInde |
| | 170 | |
|
| 0 | 171 | | modified = true; |
| | 172 | | } |
| | 173 | | } |
| | 174 | | } |
| | 175 | |
|
| 0 | 176 | | var attachments = _mediaSourceManager.GetMediaAttachments(item.Id).Where(a => !string.Equals(a.Codec, "mjpeg", S |
| 0 | 177 | | var shouldExtractOneByOne = attachments.Any(a => !string.IsNullOrEmpty(a.FileName) |
| 0 | 178 | | && (a.FileName.Contains('/', StringCompari |
| 0 | 179 | | foreach (var attachment in attachments) |
| | 180 | | { |
| 0 | 181 | | var attachmentIndex = attachment.Index; |
| 0 | 182 | | var oldAttachmentPath = GetOldAttachmentDataPath(item.Path, attachmentIndex); |
| 0 | 183 | | if (string.IsNullOrEmpty(oldAttachmentPath) || !File.Exists(oldAttachmentPath)) |
| | 184 | | { |
| 0 | 185 | | oldAttachmentPath = GetOldAttachmentCachePath(itemIdString, attachment, shouldExtractOneByOne); |
| 0 | 186 | | if (string.IsNullOrEmpty(oldAttachmentPath) || !File.Exists(oldAttachmentPath)) |
| | 187 | | { |
| | 188 | | continue; |
| | 189 | | } |
| | 190 | | } |
| | 191 | |
|
| 0 | 192 | | var newAttachmentPath = _pathManager.GetAttachmentPath(itemIdString, attachment.FileName ?? attachmentIndex. |
| 0 | 193 | | if (File.Exists(newAttachmentPath)) |
| | 194 | | { |
| 0 | 195 | | File.Delete(oldAttachmentPath); |
| | 196 | | } |
| | 197 | | else |
| | 198 | | { |
| 0 | 199 | | var newDirectory = Path.GetDirectoryName(newAttachmentPath); |
| 0 | 200 | | if (newDirectory is not null) |
| | 201 | | { |
| 0 | 202 | | Directory.CreateDirectory(newDirectory); |
| 0 | 203 | | File.Move(oldAttachmentPath, newAttachmentPath, false); |
| 0 | 204 | | _logger.LogDebug("Moved attachment {Index} for {Item} from {Source} to {Destination}", attachmentInd |
| | 205 | |
|
| 0 | 206 | | modified = true; |
| | 207 | | } |
| | 208 | | } |
| | 209 | | } |
| | 210 | |
|
| 0 | 211 | | return modified; |
| | 212 | | } |
| | 213 | |
|
| | 214 | | private string? GetOldAttachmentDataPath(string? mediaPath, int attachmentStreamIndex) |
| | 215 | | { |
| 0 | 216 | | if (mediaPath is null) |
| | 217 | | { |
| 0 | 218 | | return null; |
| | 219 | | } |
| | 220 | |
|
| | 221 | | string filename; |
| 0 | 222 | | var protocol = _mediaSourceManager.GetPathProtocol(mediaPath); |
| 0 | 223 | | if (protocol == MediaProtocol.File) |
| | 224 | | { |
| | 225 | | DateTime? date; |
| | 226 | | try |
| | 227 | | { |
| 0 | 228 | | date = File.GetLastWriteTimeUtc(mediaPath); |
| 0 | 229 | | } |
| 0 | 230 | | catch (IOException e) |
| | 231 | | { |
| 0 | 232 | | _logger.LogDebug("Skipping attachment at index {Index} for {Path}: {Exception}", attachmentStreamIndex, |
| | 233 | |
|
| 0 | 234 | | return null; |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Value.Tick |
| | 238 | | } |
| | 239 | | else |
| | 240 | | { |
| 0 | 241 | | filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D", |
| | 242 | | } |
| | 243 | |
|
| 0 | 244 | | return Path.Join(_appPaths.DataPath, "attachments", filename[..1], filename); |
| 0 | 245 | | } |
| | 246 | |
|
| | 247 | | private string? GetOldAttachmentCachePath(string mediaSourceId, MediaAttachment attachment, bool shouldExtractOneByO |
| | 248 | | { |
| 0 | 249 | | var attachmentFolderPath = Path.Join(_appPaths.CachePath, "attachments", mediaSourceId); |
| 0 | 250 | | if (shouldExtractOneByOne) |
| | 251 | | { |
| 0 | 252 | | return Path.Join(attachmentFolderPath, attachment.Index.ToString(CultureInfo.InvariantCulture)); |
| | 253 | | } |
| | 254 | |
|
| 0 | 255 | | if (string.IsNullOrEmpty(attachment.FileName)) |
| | 256 | | { |
| 0 | 257 | | return null; |
| | 258 | | } |
| | 259 | |
|
| 0 | 260 | | return Path.Join(attachmentFolderPath, attachment.FileName); |
| | 261 | | } |
| | 262 | |
|
| | 263 | | private string? GetOldSubtitleCachePath(string path, int streamIndex, string outputSubtitleExtension) |
| | 264 | | { |
| | 265 | | DateTime? date; |
| | 266 | | try |
| | 267 | | { |
| 0 | 268 | | date = File.GetLastWriteTimeUtc(path); |
| 0 | 269 | | } |
| 0 | 270 | | catch (IOException e) |
| | 271 | | { |
| 0 | 272 | | _logger.LogDebug("Skipping subtitle at index {Index} for {Path}: {Exception}", streamIndex, path, e.Message) |
| | 273 | |
|
| 0 | 274 | | return null; |
| | 275 | | } |
| | 276 | |
|
| 0 | 277 | | var ticksParam = string.Empty; |
| 0 | 278 | | ReadOnlySpan<char> filename = new Guid(MD5.HashData(Encoding.Unicode.GetBytes(path + "_" + streamIndex.ToString( |
| | 279 | |
|
| 0 | 280 | | return Path.Join(SubtitleCachePath, filename[..1], filename); |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | private static string GetSubtitleExtension(string codec) |
| | 284 | | { |
| 0 | 285 | | if (codec.ToLower(CultureInfo.InvariantCulture).Equals("ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 286 | | || codec.ToLower(CultureInfo.InvariantCulture).Equals("ssa", StringComparison.OrdinalIgnoreCase)) |
| | 287 | | { |
| 0 | 288 | | return "." + codec; |
| | 289 | | } |
| 0 | 290 | | else if (codec.Contains("pgs", StringComparison.OrdinalIgnoreCase)) |
| | 291 | | { |
| 0 | 292 | | return ".sup"; |
| | 293 | | } |
| | 294 | | else |
| | 295 | | { |
| 0 | 296 | | return ".srt"; |
| | 297 | | } |
| | 298 | | } |
| | 299 | | } |