aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Item/ChapterRepository.cs
blob: 98700f3224b772fed004579ae2ababd270b7adbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Entities;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using Microsoft.EntityFrameworkCore;

namespace Jellyfin.Server.Implementations.Item;

/// <summary>
/// The Chapter manager.
/// </summary>
public class ChapterRepository : IChapterRepository
{
    private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
    private readonly IImageProcessor _imageProcessor;

    /// <summary>
    /// Initializes a new instance of the <see cref="ChapterRepository"/> class.
    /// </summary>
    /// <param name="dbProvider">The EFCore provider.</param>
    /// <param name="imageProcessor">The Image Processor.</param>
    public ChapterRepository(IDbContextFactory<JellyfinDbContext> dbProvider, IImageProcessor imageProcessor)
    {
        _dbProvider = dbProvider;
        _imageProcessor = imageProcessor;
    }

    /// <inheritdoc />
    public ChapterInfo? GetChapter(Guid baseItemId, int index)
    {
        using var context = _dbProvider.CreateDbContext();
        var chapter = context.Chapters.AsNoTracking()
            .Select(e => new
            {
                chapter = e,
                baseItemPath = e.Item.Path
            })
            .FirstOrDefault(e => e.chapter.ItemId.Equals(baseItemId) && e.chapter.ChapterIndex == index);
        if (chapter is not null)
        {
            return Map(chapter.chapter, chapter.baseItemPath!);
        }

        return null;
    }

    /// <inheritdoc />
    public IReadOnlyList<ChapterInfo> GetChapters(Guid baseItemId)
    {
        using var context = _dbProvider.CreateDbContext();
        return context.Chapters.AsNoTracking().Where(e => e.ItemId.Equals(baseItemId))
            .Select(e => new
            {
                chapter = e,
                baseItemPath = e.Item.Path
            })
            .AsEnumerable()
            .Select(e => Map(e.chapter, e.baseItemPath!))
            .ToArray();
    }

    /// <inheritdoc />
    public void SaveChapters(Guid itemId, IReadOnlyList<ChapterInfo> chapters)
    {
        using var context = _dbProvider.CreateDbContext();
        using (var transaction = context.Database.BeginTransaction())
        {
            context.Chapters.Where(e => e.ItemId.Equals(itemId)).ExecuteDelete();
            for (var i = 0; i < chapters.Count; i++)
            {
                var chapter = chapters[i];
                context.Chapters.Add(Map(chapter, i, itemId));
            }

            context.SaveChanges();
            transaction.Commit();
        }
    }

    /// <inheritdoc />
    public async Task DeleteChaptersAsync(Guid itemId, CancellationToken cancellationToken)
    {
        var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
        await using (dbContext.ConfigureAwait(false))
        {
            await dbContext.Chapters.Where(c => c.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
            await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
        }
    }

    private Chapter Map(ChapterInfo chapterInfo, int index, Guid itemId)
    {
        return new Chapter()
        {
            ChapterIndex = index,
            StartPositionTicks = chapterInfo.StartPositionTicks,
            ImageDateModified = chapterInfo.ImageDateModified,
            ImagePath = chapterInfo.ImagePath,
            ItemId = itemId,
            Name = chapterInfo.Name,
            Item = null!
        };
    }

    private ChapterInfo Map(Chapter chapterInfo, string baseItemPath)
    {
        var chapterEntity = new ChapterInfo()
        {
            StartPositionTicks = chapterInfo.StartPositionTicks,
            ImageDateModified = chapterInfo.ImageDateModified.GetValueOrDefault(),
            ImagePath = chapterInfo.ImagePath,
            Name = chapterInfo.Name,
        };

        if (!string.IsNullOrEmpty(chapterInfo.ImagePath))
        {
            chapterEntity.ImageTag = _imageProcessor.GetImageCacheTag(baseItemPath, chapterEntity.ImageDateModified);
        }

        return chapterEntity;
    }
}