| | 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 | | internal class FixAudioData : IMigrationRoutine |
| | 20 | | { |
| | 21 | | private const string DbFilename = "library.db"; |
| | 22 | | private readonly ILogger<FixAudioData> _logger; |
| | 23 | | private readonly IServerApplicationPaths _applicationPaths; |
| | 24 | | private readonly IItemRepository _itemRepository; |
| | 25 | |
|
| | 26 | | public FixAudioData( |
| | 27 | | IServerApplicationPaths applicationPaths, |
| | 28 | | ILoggerFactory loggerFactory, |
| | 29 | | IItemRepository itemRepository) |
| | 30 | | { |
| 0 | 31 | | _applicationPaths = applicationPaths; |
| 0 | 32 | | _itemRepository = itemRepository; |
| 0 | 33 | | _logger = loggerFactory.CreateLogger<FixAudioData>(); |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <inheritdoc/> |
| 0 | 37 | | public Guid Id => Guid.Parse("{CF6FABC2-9FBE-4933-84A5-FFE52EF22A58}"); |
| | 38 | |
|
| | 39 | | /// <inheritdoc/> |
| 0 | 40 | | public string Name => "FixAudioData"; |
| | 41 | |
|
| | 42 | | /// <inheritdoc/> |
| 0 | 43 | | public bool PerformOnNewInstall => false; |
| | 44 | |
|
| | 45 | | /// <inheritdoc/> |
| | 46 | | public void Perform() |
| | 47 | | { |
| 0 | 48 | | var dbPath = Path.Combine(_applicationPaths.DataPath, DbFilename); |
| | 49 | |
|
| | 50 | | // Back up the database before modifying any entries |
| 0 | 51 | | for (int i = 1; ; i++) |
| | 52 | | { |
| 0 | 53 | | var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i); |
| 0 | 54 | | if (!File.Exists(bakPath)) |
| | 55 | | { |
| | 56 | | try |
| | 57 | | { |
| 0 | 58 | | _logger.LogInformation("Backing up {Library} to {BackupPath}", DbFilename, bakPath); |
| 0 | 59 | | File.Copy(dbPath, bakPath); |
| 0 | 60 | | _logger.LogInformation("{Library} backed up to {BackupPath}", DbFilename, bakPath); |
| 0 | 61 | | break; |
| | 62 | | } |
| 0 | 63 | | catch (Exception ex) |
| | 64 | | { |
| 0 | 65 | | _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPa |
| 0 | 66 | | throw; |
| | 67 | | } |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | _logger.LogInformation("Backfilling audio lyrics data to database."); |
| 0 | 72 | | var startIndex = 0; |
| 0 | 73 | | var records = _itemRepository.GetCount(new InternalItemsQuery |
| 0 | 74 | | { |
| 0 | 75 | | IncludeItemTypes = [BaseItemKind.Audio], |
| 0 | 76 | | }); |
| | 77 | |
|
| 0 | 78 | | while (startIndex < records) |
| | 79 | | { |
| 0 | 80 | | var results = _itemRepository.GetItemList(new InternalItemsQuery |
| 0 | 81 | | { |
| 0 | 82 | | IncludeItemTypes = [BaseItemKind.Audio], |
| 0 | 83 | | StartIndex = startIndex, |
| 0 | 84 | | Limit = 5000, |
| 0 | 85 | | SkipDeserialization = true |
| 0 | 86 | | }) |
| 0 | 87 | | .Cast<Audio>() |
| 0 | 88 | | .ToList(); |
| | 89 | |
|
| 0 | 90 | | foreach (var audio in results) |
| | 91 | | { |
| 0 | 92 | | var lyricMediaStreams = audio.GetMediaStreams().Where(s => s.Type == MediaStreamType.Lyric).Select(s |
| 0 | 93 | | if (lyricMediaStreams.Count > 0) |
| | 94 | | { |
| 0 | 95 | | audio.HasLyrics = true; |
| 0 | 96 | | audio.LyricFiles = lyricMediaStreams; |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | _itemRepository.SaveItems(results, CancellationToken.None); |
| 0 | 101 | | startIndex += results.Count; |
| 0 | 102 | | _logger.LogInformation("Backfilled data for {UpdatedRecords} of {TotalRecords} audio records", startInde |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | | } |
| | 106 | | } |