aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Item/KeyframeRepository.cs
blob: a2267700fbd618adc7ba9e714b9ba38fe14c72d3 (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
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.Persistence;
using Microsoft.EntityFrameworkCore;

namespace Jellyfin.Server.Implementations.Item;

/// <summary>
/// Repository for obtaining Keyframe data.
/// </summary>
public class KeyframeRepository : IKeyframeRepository
{
    private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;

    /// <summary>
    /// Initializes a new instance of the <see cref="KeyframeRepository"/> class.
    /// </summary>
    /// <param name="dbProvider">The EFCore db factory.</param>
    public KeyframeRepository(IDbContextFactory<JellyfinDbContext> dbProvider)
    {
        _dbProvider = dbProvider;
    }

    private static MediaEncoding.Keyframes.KeyframeData Map(KeyframeData entity)
    {
        return new MediaEncoding.Keyframes.KeyframeData(
            entity.TotalDuration,
            (entity.KeyframeTicks ?? []).ToList());
    }

    private KeyframeData Map(MediaEncoding.Keyframes.KeyframeData dto, Guid itemId)
    {
        return new()
        {
            ItemId = itemId,
            TotalDuration = dto.TotalDuration,
            KeyframeTicks = dto.KeyframeTicks.ToList()
        };
    }

    /// <inheritdoc />
    public IReadOnlyList<MediaEncoding.Keyframes.KeyframeData> GetKeyframeData(Guid itemId)
    {
        using var context = _dbProvider.CreateDbContext();

        return context.KeyframeData.AsNoTracking().Where(e => e.ItemId.Equals(itemId)).Select(e => Map(e)).ToList();
    }

    /// <inheritdoc />
    public async Task SaveKeyframeDataAsync(Guid itemId, MediaEncoding.Keyframes.KeyframeData data, CancellationToken cancellationToken)
    {
        using var context = _dbProvider.CreateDbContext();
        using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
        await context.KeyframeData.Where(e => e.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
        await context.KeyframeData.AddAsync(Map(data, itemId), cancellationToken).ConfigureAwait(false);
        await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
        await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
    }
}