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