ChapterRepository.cs 4.0 KB

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