| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using Jellyfin.Data.Enums; |
| | 7 | | using MediaBrowser.Controller; |
| | 8 | | using MediaBrowser.Controller.Entities; |
| | 9 | | using MediaBrowser.Controller.Entities.Audio; |
| | 10 | | using MediaBrowser.Controller.Persistence; |
| | 11 | | using MediaBrowser.Model.Entities; |
| | 12 | | using Microsoft.Extensions.Logging; |
| | 13 | |
|
| | 14 | | namespace Jellyfin.Server.Migrations.Routines |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Fixes the data column of audio types to be deserializable. |
| | 18 | | /// </summary> |
| | 19 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | 20 | | [JellyfinMigration("2025-04-20T18:00:00", nameof(FixAudioData), "CF6FABC2-9FBE-4933-84A5-FFE52EF22A58")] |
| | 21 | | internal class FixAudioData : IMigrationRoutine |
| | 22 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | 23 | | { |
| | 24 | | private const string DbFilename = "library.db"; |
| | 25 | | private readonly ILogger<FixAudioData> _logger; |
| | 26 | | private readonly IServerApplicationPaths _applicationPaths; |
| | 27 | | private readonly IItemRepository _itemRepository; |
| | 28 | |
|
| | 29 | | public FixAudioData( |
| | 30 | | IServerApplicationPaths applicationPaths, |
| | 31 | | ILoggerFactory loggerFactory, |
| | 32 | | IItemRepository itemRepository) |
| | 33 | | { |
| 0 | 34 | | _applicationPaths = applicationPaths; |
| 0 | 35 | | _itemRepository = itemRepository; |
| 0 | 36 | | _logger = loggerFactory.CreateLogger<FixAudioData>(); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <inheritdoc/> |
| | 40 | | public void Perform() |
| | 41 | | { |
| 0 | 42 | | var dbPath = Path.Combine(_applicationPaths.DataPath, DbFilename); |
| | 43 | |
|
| | 44 | | // Back up the database before modifying any entries |
| 0 | 45 | | for (int i = 1; ; i++) |
| | 46 | | { |
| 0 | 47 | | var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i); |
| 0 | 48 | | if (!File.Exists(bakPath)) |
| | 49 | | { |
| | 50 | | try |
| | 51 | | { |
| 0 | 52 | | _logger.LogInformation("Backing up {Library} to {BackupPath}", DbFilename, bakPath); |
| 0 | 53 | | File.Copy(dbPath, bakPath); |
| 0 | 54 | | _logger.LogInformation("{Library} backed up to {BackupPath}", DbFilename, bakPath); |
| 0 | 55 | | break; |
| | 56 | | } |
| 0 | 57 | | catch (Exception ex) |
| | 58 | | { |
| 0 | 59 | | _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPa |
| 0 | 60 | | throw; |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | _logger.LogInformation("Backfilling audio lyrics data to database."); |
| 0 | 66 | | var startIndex = 0; |
| 0 | 67 | | var records = _itemRepository.GetCount(new InternalItemsQuery |
| 0 | 68 | | { |
| 0 | 69 | | IncludeItemTypes = [BaseItemKind.Audio], |
| 0 | 70 | | }); |
| | 71 | |
|
| 0 | 72 | | while (startIndex < records) |
| | 73 | | { |
| 0 | 74 | | var results = _itemRepository.GetItemList(new InternalItemsQuery |
| 0 | 75 | | { |
| 0 | 76 | | IncludeItemTypes = [BaseItemKind.Audio], |
| 0 | 77 | | StartIndex = startIndex, |
| 0 | 78 | | Limit = 5000, |
| 0 | 79 | | SkipDeserialization = true |
| 0 | 80 | | }) |
| 0 | 81 | | .Cast<Audio>() |
| 0 | 82 | | .ToList(); |
| | 83 | |
|
| 0 | 84 | | foreach (var audio in results) |
| | 85 | | { |
| 0 | 86 | | var lyricMediaStreams = audio.GetMediaStreams().Where(s => s.Type == MediaStreamType.Lyric).Select(s |
| 0 | 87 | | if (lyricMediaStreams.Count > 0) |
| | 88 | | { |
| 0 | 89 | | audio.HasLyrics = true; |
| 0 | 90 | | audio.LyricFiles = lyricMediaStreams; |
| | 91 | | } |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | _itemRepository.SaveItems(results, CancellationToken.None); |
| 0 | 95 | | startIndex += results.Count; |
| 0 | 96 | | _logger.LogInformation("Backfilled data for {UpdatedRecords} of {TotalRecords} audio records", startInde |
| | 97 | | } |
| 0 | 98 | | } |
| | 99 | | } |
| | 100 | | } |