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