ChapterRepository.cs 3.8 KB

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