| | 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.Persistence; |
| | 9 | | using Microsoft.EntityFrameworkCore; |
| | 10 | |
|
| | 11 | | namespace Jellyfin.Server.Implementations.Item; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Repository for obtaining Keyframe data. |
| | 15 | | /// </summary> |
| | 16 | | public class KeyframeRepository : IKeyframeRepository |
| | 17 | | { |
| | 18 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Initializes a new instance of the <see cref="KeyframeRepository"/> class. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="dbProvider">The EFCore db factory.</param> |
| | 24 | | public KeyframeRepository(IDbContextFactory<JellyfinDbContext> dbProvider) |
| | 25 | | { |
| 21 | 26 | | _dbProvider = dbProvider; |
| 21 | 27 | | } |
| | 28 | |
|
| | 29 | | private static MediaEncoding.Keyframes.KeyframeData Map(KeyframeData entity) |
| | 30 | | { |
| 0 | 31 | | return new MediaEncoding.Keyframes.KeyframeData( |
| 0 | 32 | | entity.TotalDuration, |
| 0 | 33 | | (entity.KeyframeTicks ?? []).ToList()); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | private KeyframeData Map(MediaEncoding.Keyframes.KeyframeData dto, Guid itemId) |
| | 37 | | { |
| 0 | 38 | | return new() |
| 0 | 39 | | { |
| 0 | 40 | | ItemId = itemId, |
| 0 | 41 | | TotalDuration = dto.TotalDuration, |
| 0 | 42 | | KeyframeTicks = dto.KeyframeTicks.ToList() |
| 0 | 43 | | }; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | /// <inheritdoc /> |
| | 47 | | public IReadOnlyList<MediaEncoding.Keyframes.KeyframeData> GetKeyframeData(Guid itemId) |
| | 48 | | { |
| 0 | 49 | | using var context = _dbProvider.CreateDbContext(); |
| | 50 | |
|
| 0 | 51 | | return context.KeyframeData.AsNoTracking().Where(e => e.ItemId.Equals(itemId)).Select(e => Map(e)).ToList(); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <inheritdoc /> |
| | 55 | | public async Task SaveKeyframeDataAsync(Guid itemId, MediaEncoding.Keyframes.KeyframeData data, CancellationToken ca |
| | 56 | | { |
| | 57 | | using var context = _dbProvider.CreateDbContext(); |
| | 58 | | using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false); |
| | 59 | | await context.KeyframeData.Where(e => e.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAw |
| | 60 | | await context.KeyframeData.AddAsync(Map(data, itemId), cancellationToken).ConfigureAwait(false); |
| | 61 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | 62 | | await transaction.CommitAsync(cancellationToken).ConfigureAwait(false); |
| | 63 | | } |
| | 64 | | } |