| | 1 | | using System; |
| | 2 | | using System.Diagnostics; |
| | 3 | | using System.Diagnostics.CodeAnalysis; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.IO; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text.Json; |
| | 8 | | using Jellyfin.Data.Enums; |
| | 9 | | using Jellyfin.Database.Implementations; |
| | 10 | | using Jellyfin.Database.Implementations.Entities; |
| | 11 | | using Jellyfin.Extensions.Json; |
| | 12 | | using Jellyfin.Server.ServerSetupApp; |
| | 13 | | using MediaBrowser.Common.Configuration; |
| | 14 | | using MediaBrowser.Common.Extensions; |
| | 15 | | using Microsoft.EntityFrameworkCore; |
| | 16 | | using Microsoft.Extensions.Logging; |
| | 17 | |
|
| | 18 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Migration to move extracted files to the new directories. |
| | 22 | | /// </summary> |
| | 23 | | [JellyfinMigration("2025-04-21T00:00:00", nameof(MigrateKeyframeData))] |
| | 24 | | public class MigrateKeyframeData : IDatabaseMigrationRoutine |
| | 25 | | { |
| | 26 | | private readonly IStartupLogger _logger; |
| | 27 | | private readonly IApplicationPaths _appPaths; |
| | 28 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| 0 | 29 | | private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Initializes a new instance of the <see cref="MigrateKeyframeData"/> class. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="startupLogger">The startup logger for Startup UI intigration.</param> |
| | 35 | | /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param> |
| | 36 | | /// <param name="dbProvider">The EFCore db factory.</param> |
| | 37 | | public MigrateKeyframeData( |
| | 38 | | IStartupLogger startupLogger, |
| | 39 | | IApplicationPaths appPaths, |
| | 40 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | 41 | | { |
| 0 | 42 | | _logger = startupLogger; |
| 0 | 43 | | _appPaths = appPaths; |
| 0 | 44 | | _dbProvider = dbProvider; |
| 0 | 45 | | } |
| | 46 | |
|
| 0 | 47 | | private string KeyframeCachePath => Path.Combine(_appPaths.DataPath, "keyframes"); |
| | 48 | |
|
| | 49 | | /// <inheritdoc /> |
| | 50 | | public void Perform() |
| | 51 | | { |
| | 52 | | const int Limit = 5000; |
| 0 | 53 | | int itemCount = 0, offset = 0; |
| | 54 | |
|
| 0 | 55 | | var sw = Stopwatch.StartNew(); |
| | 56 | |
|
| 0 | 57 | | using var context = _dbProvider.CreateDbContext(); |
| 0 | 58 | | var baseQuery = context.BaseItems.Where(b => b.MediaType == MediaType.Video.ToString() && !b.IsVirtualItem && !b |
| 0 | 59 | | var records = baseQuery.Count(); |
| 0 | 60 | | _logger.LogInformation("Checking {Count} items for importable keyframe data.", records); |
| | 61 | |
|
| 0 | 62 | | context.KeyframeData.ExecuteDelete(); |
| 0 | 63 | | using var transaction = context.Database.BeginTransaction(); |
| | 64 | | do |
| | 65 | | { |
| 0 | 66 | | var results = baseQuery.Skip(offset).Take(Limit).Select(b => new Tuple<Guid, string?>(b.Id, b.Path)).ToList( |
| 0 | 67 | | foreach (var result in results) |
| | 68 | | { |
| 0 | 69 | | if (TryGetKeyframeData(result.Item1, result.Item2, out var data)) |
| | 70 | | { |
| 0 | 71 | | itemCount++; |
| 0 | 72 | | context.KeyframeData.Add(data); |
| | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | offset += Limit; |
| 0 | 77 | | if (offset > records) |
| | 78 | | { |
| 0 | 79 | | offset = records; |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | _logger.LogInformation("Checked: {Count} - Imported: {Items} - Time: {Time}", offset, itemCount, sw.Elapsed) |
| 0 | 83 | | } while (offset < records); |
| | 84 | |
|
| 0 | 85 | | context.SaveChanges(); |
| 0 | 86 | | transaction.Commit(); |
| | 87 | |
|
| 0 | 88 | | _logger.LogInformation("Imported keyframes for {Count} items in {Time}", itemCount, sw.Elapsed); |
| | 89 | |
|
| 0 | 90 | | if (Directory.Exists(KeyframeCachePath)) |
| | 91 | | { |
| 0 | 92 | | Directory.Delete(KeyframeCachePath, true); |
| | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | private bool TryGetKeyframeData(Guid id, string? path, [NotNullWhen(true)] out KeyframeData? data) |
| | 97 | | { |
| 0 | 98 | | data = null; |
| 0 | 99 | | if (!string.IsNullOrEmpty(path)) |
| | 100 | | { |
| 0 | 101 | | var cachePath = GetCachePath(KeyframeCachePath, path); |
| 0 | 102 | | if (TryReadFromCache(cachePath, out var keyframeData)) |
| | 103 | | { |
| 0 | 104 | | data = new() |
| 0 | 105 | | { |
| 0 | 106 | | ItemId = id, |
| 0 | 107 | | KeyframeTicks = keyframeData.KeyframeTicks.ToList(), |
| 0 | 108 | | TotalDuration = keyframeData.TotalDuration |
| 0 | 109 | | }; |
| | 110 | |
|
| 0 | 111 | | return true; |
| | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | return false; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | private string? GetCachePath(string keyframeCachePath, string filePath) |
| | 119 | | { |
| | 120 | | DateTime? lastWriteTimeUtc; |
| | 121 | | try |
| | 122 | | { |
| 0 | 123 | | lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath); |
| 0 | 124 | | } |
| 0 | 125 | | catch (IOException e) |
| | 126 | | { |
| 0 | 127 | | _logger.LogDebug("Skipping {Path}: {Exception}", filePath, e.Message); |
| | 128 | |
|
| 0 | 129 | | return null; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | ReadOnlySpan<char> filename = (filePath + "_" + lastWriteTimeUtc.Value.Ticks.ToString(CultureInfo.InvariantCultu |
| 0 | 133 | | var prefix = filename[..1]; |
| | 134 | |
|
| 0 | 135 | | return Path.Join(keyframeCachePath, prefix, filename); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private static bool TryReadFromCache(string? cachePath, [NotNullWhen(true)] out MediaEncoding.Keyframes.KeyframeData |
| | 139 | | { |
| 0 | 140 | | if (File.Exists(cachePath)) |
| | 141 | | { |
| 0 | 142 | | var bytes = File.ReadAllBytes(cachePath); |
| 0 | 143 | | cachedResult = JsonSerializer.Deserialize<MediaEncoding.Keyframes.KeyframeData>(bytes, _jsonOptions); |
| | 144 | |
|
| 0 | 145 | | return cachedResult is not null; |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | cachedResult = null; |
| | 149 | |
|
| 0 | 150 | | return false; |
| | 151 | | } |
| | 152 | | } |