ChapterManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 ChapterManager : IChapterManager
  16. {
  17. private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
  18. private readonly IImageProcessor _imageProcessor;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="ChapterManager"/> class.
  21. /// </summary>
  22. /// <param name="dbProvider">The EFCore provider.</param>
  23. /// <param name="imageProcessor">The Image Processor.</param>
  24. public ChapterManager(IDbContextFactory<JellyfinDbContext> dbProvider, IImageProcessor imageProcessor)
  25. {
  26. _dbProvider = dbProvider;
  27. _imageProcessor = imageProcessor;
  28. }
  29. /// <inheritdoc cref="IChapterManager"/>
  30. public ChapterInfo? GetChapter(BaseItemDto baseItem, int index)
  31. {
  32. using var context = _dbProvider.CreateDbContext();
  33. var chapter = context.Chapters.FirstOrDefault(e => e.ItemId.Equals(baseItem.Id) && e.ChapterIndex == index);
  34. if (chapter is not null)
  35. {
  36. return Map(chapter, baseItem);
  37. }
  38. return null;
  39. }
  40. /// <inheritdoc cref="IChapterManager"/>
  41. public IReadOnlyList<ChapterInfo> GetChapters(BaseItemDto baseItem)
  42. {
  43. using var context = _dbProvider.CreateDbContext();
  44. return context.Chapters.Where(e => e.ItemId.Equals(baseItem.Id))
  45. .ToList()
  46. .Select(e => Map(e, baseItem))
  47. .ToImmutableArray();
  48. }
  49. /// <inheritdoc cref="IChapterManager"/>
  50. public void SaveChapters(Guid itemId, IReadOnlyList<ChapterInfo> chapters)
  51. {
  52. using var context = _dbProvider.CreateDbContext();
  53. using (var transaction = context.Database.BeginTransaction())
  54. {
  55. context.Chapters.Where(e => e.ItemId.Equals(itemId)).ExecuteDelete();
  56. for (var i = 0; i < chapters.Count; i++)
  57. {
  58. var chapter = chapters[i];
  59. context.Chapters.Add(Map(chapter, i, itemId));
  60. }
  61. context.SaveChanges();
  62. transaction.Commit();
  63. }
  64. }
  65. private Chapter Map(ChapterInfo chapterInfo, int index, Guid itemId)
  66. {
  67. return new Chapter()
  68. {
  69. ChapterIndex = index,
  70. StartPositionTicks = chapterInfo.StartPositionTicks,
  71. ImageDateModified = chapterInfo.ImageDateModified,
  72. ImagePath = chapterInfo.ImagePath,
  73. ItemId = itemId,
  74. Name = chapterInfo.Name
  75. };
  76. }
  77. private ChapterInfo Map(Chapter chapterInfo, BaseItemDto baseItem)
  78. {
  79. var info = new ChapterInfo()
  80. {
  81. StartPositionTicks = chapterInfo.StartPositionTicks,
  82. ImageDateModified = chapterInfo.ImageDateModified.GetValueOrDefault(),
  83. ImagePath = chapterInfo.ImagePath,
  84. Name = chapterInfo.Name,
  85. };
  86. info.ImageTag = _imageProcessor.GetImageCacheTag(baseItem, info);
  87. return info;
  88. }
  89. }