KeyframeRepository.cs 2.8 KB

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