ChapterRepository.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Linq;
  5. using Jellyfin.Database.Implementations;
  6. using Jellyfin.Database.Implementations.Entities;
  7. using MediaBrowser.Controller.Chapters;
  8. using MediaBrowser.Controller.Drawing;
  9. using MediaBrowser.Model.Dto;
  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 cref="IChapterRepository"/>
  31. public ChapterInfo? GetChapter(BaseItemDto baseItem, int index)
  32. {
  33. return GetChapter(baseItem.Id, index);
  34. }
  35. /// <inheritdoc cref="IChapterRepository"/>
  36. public IReadOnlyList<ChapterInfo> GetChapters(BaseItemDto baseItem)
  37. {
  38. return GetChapters(baseItem.Id);
  39. }
  40. /// <inheritdoc cref="IChapterRepository"/>
  41. public ChapterInfo? GetChapter(Guid baseItemId, int index)
  42. {
  43. using var context = _dbProvider.CreateDbContext();
  44. var chapter = context.Chapters.AsNoTracking()
  45. .Select(e => new
  46. {
  47. chapter = e,
  48. baseItemPath = e.Item.Path
  49. })
  50. .FirstOrDefault(e => e.chapter.ItemId.Equals(baseItemId) && e.chapter.ChapterIndex == index);
  51. if (chapter is not null)
  52. {
  53. return Map(chapter.chapter, chapter.baseItemPath!);
  54. }
  55. return null;
  56. }
  57. /// <inheritdoc cref="IChapterRepository"/>
  58. public IReadOnlyList<ChapterInfo> GetChapters(Guid baseItemId)
  59. {
  60. using var context = _dbProvider.CreateDbContext();
  61. return context.Chapters.AsNoTracking().Where(e => e.ItemId.Equals(baseItemId))
  62. .Select(e => new
  63. {
  64. chapter = e,
  65. baseItemPath = e.Item.Path
  66. })
  67. .AsEnumerable()
  68. .Select(e => Map(e.chapter, e.baseItemPath!))
  69. .ToArray();
  70. }
  71. /// <inheritdoc cref="IChapterRepository"/>
  72. public void SaveChapters(Guid itemId, IReadOnlyList<ChapterInfo> chapters)
  73. {
  74. using var context = _dbProvider.CreateDbContext();
  75. using (var transaction = context.Database.BeginTransaction())
  76. {
  77. context.Chapters.Where(e => e.ItemId.Equals(itemId)).ExecuteDelete();
  78. for (var i = 0; i < chapters.Count; i++)
  79. {
  80. var chapter = chapters[i];
  81. context.Chapters.Add(Map(chapter, i, itemId));
  82. }
  83. context.SaveChanges();
  84. transaction.Commit();
  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. chapterEntity.ImageTag = _imageProcessor.GetImageCacheTag(baseItemPath, chapterEntity.ImageDateModified);
  110. return chapterEntity;
  111. }
  112. }