< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.MoveExtractedFiles
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/MoveExtractedFiles.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 80
Coverable lines: 80
Total lines: 295
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_SubtitleCachePath()100%210%
get_AttachmentCachePath()100%210%
MoveSubtitleAndAttachmentFiles(...)0%930300%
GetOldAttachmentDataPath(...)0%2040%
GetOldAttachmentCachePath(...)0%2040%
GetOldSubtitleCachePath(...)0%620%
GetSubtitleExtension(...)0%4260%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/MoveExtractedFiles.cs

#LineLine coverage
 1#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Globalization;
 7using System.IO;
 8using System.Linq;
 9using System.Security.Cryptography;
 10using System.Text;
 11using System.Threading;
 12using System.Threading.Tasks;
 13using Jellyfin.Data.Enums;
 14using Jellyfin.Database.Implementations;
 15using Jellyfin.Database.Implementations.Entities;
 16using Jellyfin.Server.ServerSetupApp;
 17using MediaBrowser.Common.Configuration;
 18using MediaBrowser.Common.Extensions;
 19using MediaBrowser.Controller.IO;
 20using MediaBrowser.Model.IO;
 21using Microsoft.EntityFrameworkCore;
 22using Microsoft.Extensions.Logging;
 23
 24namespace Jellyfin.Server.Migrations.Routines;
 25
 26/// <summary>
 27/// Migration to move extracted files to the new directories.
 28/// </summary>
 29[JellyfinMigration("2025-04-20T21:00:00", nameof(MoveExtractedFiles))]
 30public class MoveExtractedFiles : IAsyncMigrationRoutine
 31{
 32    private readonly IApplicationPaths _appPaths;
 33    private readonly ILogger _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="startupLogger">The startup logger for Startup UI intigration.</param>
 44    /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 45    /// <param name="pathManager">Instance of the <see cref="IPathManager"/> interface.</param>
 46    /// <param name="dbProvider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param>
 47    public MoveExtractedFiles(
 48        IApplicationPaths appPaths,
 49        ILogger<MoveExtractedFiles> logger,
 50        IStartupLogger startupLogger,
 51        IPathManager pathManager,
 52        IFileSystem fileSystem,
 53        IDbContextFactory<JellyfinDbContext> dbProvider)
 54    {
 055        _appPaths = appPaths;
 056        _logger = startupLogger.With(logger);
 057        _pathManager = pathManager;
 058        _fileSystem = fileSystem;
 059        _dbProvider = dbProvider;
 060    }
 61
 062    private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles");
 63
 064    private string AttachmentCachePath => Path.Combine(_appPaths.DataPath, "attachments");
 65
 66    /// <inheritdoc />
 67    public async Task PerformAsync(CancellationToken cancellationToken)
 68    {
 69        const int Limit = 5000;
 70        int itemCount = 0;
 71
 72        var sw = Stopwatch.StartNew();
 73
 74        using var context = _dbProvider.CreateDbContext();
 75        var records = context.BaseItems.Count(b => b.MediaType == MediaType.Video.ToString() && !b.IsVirtualItem && !b.I
 76        _logger.LogInformation("Checking {Count} items for movable extracted files.", records);
 77
 78        // Make sure directories exist
 79        Directory.CreateDirectory(SubtitleCachePath);
 80        Directory.CreateDirectory(AttachmentCachePath);
 81
 82        await foreach (var result in context.BaseItems
 83                          .Include(e => e.MediaStreams!.Where(s => s.StreamType == MediaStreamTypeEntity.Subtitle && !s.
 84                          .Where(b => b.MediaType == MediaType.Video.ToString() && !b.IsVirtualItem && !b.IsFolder)
 85                          .Select(b => new
 86                          {
 87                              b.Id,
 88                              b.Path,
 89                              b.MediaStreams
 90                          })
 91                          .OrderBy(e => e.Id)
 92                          .WithPartitionProgress((partition) => _logger.LogInformation("Checked: {Count} - Moved: {Items
 93                          .PartitionEagerAsync(Limit, cancellationToken)
 94                          .WithCancellation(cancellationToken)
 95                          .ConfigureAwait(false))
 96        {
 97            if (MoveSubtitleAndAttachmentFiles(result.Id, result.Path, result.MediaStreams, context))
 98            {
 99                itemCount++;
 100            }
 101        }
 102
 103        _logger.LogInformation("Moved files for {Count} items in {Time}", itemCount, sw.Elapsed);
 104
 105        // Get all subdirectories with 1 character names (those are the legacy directories)
 106        var subdirectories = Directory.GetDirectories(SubtitleCachePath, "*", SearchOption.AllDirectories).Where(s => s.
 107        subdirectories.AddRange(Directory.GetDirectories(AttachmentCachePath, "*", SearchOption.AllDirectories).Where(s 
 108
 109        // Remove all legacy subdirectories
 110        foreach (var subdir in subdirectories)
 111        {
 112            Directory.Delete(subdir, true);
 113        }
 114
 115        // Remove old cache path
 116        var attachmentCachePath = Path.Join(_appPaths.CachePath, "attachments");
 117        if (Directory.Exists(attachmentCachePath))
 118        {
 119            Directory.Delete(attachmentCachePath, true);
 120        }
 121
 122        _logger.LogInformation("Cleaned up left over subtitles and attachments.");
 123    }
 124
 125    private bool MoveSubtitleAndAttachmentFiles(Guid id, string? path, ICollection<MediaStreamInfo>? mediaStreams, Jelly
 126    {
 0127        var itemIdString = id.ToString("N", CultureInfo.InvariantCulture);
 0128        var modified = false;
 0129        if (mediaStreams is not null)
 130        {
 0131            foreach (var mediaStream in mediaStreams)
 132            {
 0133                if (mediaStream.Codec is null)
 134                {
 135                    continue;
 136                }
 137
 0138                var mediaStreamIndex = mediaStream.StreamIndex;
 0139                var extension = GetSubtitleExtension(mediaStream.Codec);
 0140                var oldSubtitleCachePath = GetOldSubtitleCachePath(path, mediaStreamIndex, extension);
 0141                if (string.IsNullOrEmpty(oldSubtitleCachePath) || !File.Exists(oldSubtitleCachePath))
 142                {
 143                    continue;
 144                }
 145
 0146                var newSubtitleCachePath = _pathManager.GetSubtitlePath(itemIdString, mediaStreamIndex, extension);
 0147                if (File.Exists(newSubtitleCachePath))
 148                {
 0149                    File.Delete(oldSubtitleCachePath);
 150                }
 151                else
 152                {
 0153                    var newDirectory = Path.GetDirectoryName(newSubtitleCachePath);
 0154                    if (newDirectory is not null)
 155                    {
 0156                        Directory.CreateDirectory(newDirectory);
 0157                        File.Move(oldSubtitleCachePath, newSubtitleCachePath, false);
 0158                        _logger.LogDebug("Moved subtitle {Index} for {Item} from {Source} to {Destination}", mediaStream
 159
 0160                        modified = true;
 161                    }
 162                }
 163            }
 164        }
 165
 166#pragma warning disable CA1309 // Use ordinal string comparison
 0167        var attachments = context.AttachmentStreamInfos.Where(a => a.ItemId.Equals(id) && !string.Equals(a.Codec, "mjpeg
 168#pragma warning restore CA1309 // Use ordinal string comparison
 0169        var shouldExtractOneByOne = attachments.Any(a => !string.IsNullOrEmpty(a.Filename)
 0170                                                                              && (a.Filename.Contains('/', StringCompari
 0171        foreach (var attachment in attachments)
 172        {
 0173            var attachmentIndex = attachment.Index;
 0174            var oldAttachmentPath = GetOldAttachmentDataPath(path, attachmentIndex);
 0175            if (string.IsNullOrEmpty(oldAttachmentPath) || !File.Exists(oldAttachmentPath))
 176            {
 0177                oldAttachmentPath = GetOldAttachmentCachePath(itemIdString, attachment, shouldExtractOneByOne);
 0178                if (string.IsNullOrEmpty(oldAttachmentPath) || !File.Exists(oldAttachmentPath))
 179                {
 180                    continue;
 181                }
 182            }
 183
 0184            var newAttachmentPath = _pathManager.GetAttachmentPath(itemIdString, attachment.Filename ?? attachmentIndex.
 0185            if (File.Exists(newAttachmentPath))
 186            {
 0187                File.Delete(oldAttachmentPath);
 188            }
 189            else
 190            {
 0191                var newDirectory = Path.GetDirectoryName(newAttachmentPath);
 0192                if (newDirectory is not null)
 193                {
 0194                    Directory.CreateDirectory(newDirectory);
 0195                    File.Move(oldAttachmentPath, newAttachmentPath, false);
 0196                    _logger.LogDebug("Moved attachment {Index} for {Item} from {Source} to {Destination}", attachmentInd
 197
 0198                    modified = true;
 199                }
 200            }
 201        }
 202
 0203        return modified;
 204    }
 205
 206    private string? GetOldAttachmentDataPath(string? mediaPath, int attachmentStreamIndex)
 207    {
 0208        if (mediaPath is null)
 209        {
 0210            return null;
 211        }
 212
 213        string filename;
 0214        if (_fileSystem.IsPathFile(mediaPath))
 215        {
 216            DateTime? date;
 217            try
 218            {
 0219                date = File.GetLastWriteTimeUtc(mediaPath);
 0220            }
 0221            catch (IOException e)
 222            {
 0223                _logger.LogDebug("Skipping attachment at index {Index} for {Path}: {Exception}", attachmentStreamIndex, 
 224
 0225                return null;
 226            }
 227
 0228            filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Value.Tick
 229        }
 230        else
 231        {
 0232            filename = (mediaPath + attachmentStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5().ToString("D",
 233        }
 234
 0235        return Path.Join(_appPaths.DataPath, "attachments", filename[..1], filename);
 0236    }
 237
 238    private string? GetOldAttachmentCachePath(string mediaSourceId, AttachmentStreamInfo attachment, bool shouldExtractO
 239    {
 0240        var attachmentFolderPath = Path.Join(_appPaths.CachePath, "attachments", mediaSourceId);
 0241        if (shouldExtractOneByOne)
 242        {
 0243            return Path.Join(attachmentFolderPath, attachment.Index.ToString(CultureInfo.InvariantCulture));
 244        }
 245
 0246        if (string.IsNullOrEmpty(attachment.Filename))
 247        {
 0248            return null;
 249        }
 250
 0251        return Path.Join(attachmentFolderPath, attachment.Filename);
 252    }
 253
 254    private string? GetOldSubtitleCachePath(string? path, int streamIndex, string outputSubtitleExtension)
 255    {
 0256        if (path is null)
 257        {
 0258            return null;
 259        }
 260
 261        DateTime? date;
 262        try
 263        {
 0264            date = File.GetLastWriteTimeUtc(path);
 0265        }
 0266        catch (IOException e)
 267        {
 0268            _logger.LogDebug("Skipping subtitle at index {Index} for {Path}: {Exception}", streamIndex, path, e.Message)
 269
 0270            return null;
 271        }
 272
 0273        var ticksParam = string.Empty;
 0274        ReadOnlySpan<char> filename = new Guid(MD5.HashData(Encoding.Unicode.GetBytes(path + "_" + streamIndex.ToString(
 275
 0276        return Path.Join(SubtitleCachePath, filename[..1], filename);
 0277    }
 278
 279    private static string GetSubtitleExtension(string codec)
 280    {
 0281        if (codec.ToLower(CultureInfo.InvariantCulture).Equals("ass", StringComparison.OrdinalIgnoreCase)
 0282            || codec.ToLower(CultureInfo.InvariantCulture).Equals("ssa", StringComparison.OrdinalIgnoreCase))
 283        {
 0284            return "." + codec;
 285        }
 0286        else if (codec.Contains("pgs", StringComparison.OrdinalIgnoreCase))
 287        {
 0288            return ".sup";
 289        }
 290        else
 291        {
 0292            return ".srt";
 293        }
 294    }
 295}