ChapterRepository.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Database.Implementations;
  7. using Jellyfin.Database.Implementations.Entities;
  8. using MediaBrowser.Controller.Drawing;
  9. using MediaBrowser.Controller.Persistence;
  10. using MediaBrowser.Model.Entities;
  11. using Microsoft.EntityFrameworkCore;
  12. namespace Jellyfin.Server.Implementations.Item;
  13. /// <summary>
  14. /// The Chapter manager.
  15. /// </summary>
  16. public class ChapterRepository : IChapterRepository
  17. {
  18. private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
  19. private readonly IImageProcessor _imageProcessor;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="ChapterRepository"/> class.
  22. /// </summary>
  23. /// <param name="dbProvider">The EFCore provider.</param>
  24. /// <param name="imageProcessor">The Image Processor.</param>
  25. public ChapterRepository(IDbContextFactory<JellyfinDbContext> dbProvider, IImageProcessor imageProcessor)
  26. {
  27. _dbProvider = dbProvider;
  28. _imageProcessor = imageProcessor;
  29. }
  30. /// <inheritdoc />
  31. public ChapterInfo? GetChapter(Guid baseItemId, int index)
  32. {
  33. using var context = _dbProvider.CreateDbContext();
  34. var chapter = context.Chapters.AsNoTracking()
  35. .Select(e => new
  36. {
  37. chapter = e,
  38. baseItemPath = e.Item.Path
  39. })
  40. .FirstOrDefault(e => e.chapter.ItemId.Equals(baseItemId) && e.chapter.ChapterIndex == index);
  41. if (chapter is not null)
  42. {
  43. return Map(chapter.chapter, chapter.baseItemPath!);
  44. }
  45. return null;
  46. }
  47. /// <inheritdoc />
  48. public IReadOnlyList<ChapterInfo> GetChapters(Guid baseItemId)
  49. {
  50. using var context = _dbProvider.CreateDbContext();
  51. return context.Chapters.AsNoTracking().Where(e => e.ItemId.Equals(baseItemId))
  52. .Select(e => new
  53. {
  54. chapter = e,
  55. baseItemPath = e.Item.Path
  56. })
  57. .AsEnumerable()
  58. .Select(e => Map(e.chapter, e.baseItemPath!))
  59. .ToArray();
  60. }
  61. /// <inheritdoc />
  62. public void SaveChapters(Guid itemId, IReadOnlyList<ChapterInfo> chapters)
  63. {
  64. using var context = _dbProvider.CreateDbContext();
  65. using (var transaction = context.Database.BeginTransaction())
  66. {
  67. context.Chapters.Where(e => e.ItemId.Equals(itemId)).ExecuteDelete();
  68. for (var i = 0; i < chapters.Count; i++)
  69. {
  70. var chapter = chapters[i];
  71. context.Chapters.Add(Map(chapter, i, itemId));
  72. }
  73. context.SaveChanges();
  74. transaction.Commit();
  75. }
  76. }
  77. /// <inheritdoc />
  78. public async Task DeleteChaptersAsync(Guid itemId, CancellationToken cancellationToken)
  79. {
  80. var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
  81. await using (dbContext.ConfigureAwait(false))
  82. {
  83. await dbContext.Chapters.Where(c => c.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
  84. await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
  85. }
  86. }
  87. private Chapter Map(ChapterInfo chapterInfo, int index, Guid itemId)
  88. {
  89. return new Chapter()
  90. {
  91. ChapterIndex = index,
  92. StartPositionTicks = chapterInfo.StartPositionTicks,
  93. ImageDateModified = chapterInfo.ImageDateModified,
  94. ImagePath = chapterInfo.ImagePath,
  95. ItemId = itemId,
  96. Name = chapterInfo.Name,
  97. Item = null!
  98. };
  99. }
  100. private ChapterInfo Map(Chapter chapterInfo, string baseItemPath)
  101. {
  102. var chapterEntity = new ChapterInfo()
  103. {
  104. StartPositionTicks = chapterInfo.StartPositionTicks,
  105. ImageDateModified = chapterInfo.ImageDateModified.GetValueOrDefault(),
  106. ImagePath = chapterInfo.ImagePath,
  107. Name = chapterInfo.Name,
  108. };
  109. if (!string.IsNullOrEmpty(chapterInfo.ImagePath))
  110. {
  111. chapterEntity.ImageTag = _imageProcessor.GetImageCacheTag(baseItemPath, chapterEntity.ImageDateModified);
  112. }
  113. return chapterEntity;
  114. }
  115. }