diff options
| author | Joshua M. Boniface <joshua@boniface.me> | 2025-01-25 02:08:44 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-25 02:08:44 -0500 |
| commit | 93b8eade617dbce16979bbada63b7faf44c5ce82 (patch) | |
| tree | 5ef83a80b4ef1f713961d32ee6311c6033a3a9a9 /Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs | |
| parent | 679ee960d346f24d7df559cbbaf95cf1c9567345 (diff) | |
| parent | 64cf67f1ac758acd36db61432c97e434d9f9225d (diff) | |
Merge pull request #12798 from JPVenson/feature/EFUserData
Refactor library.db into jellyfin.db and EFCore
Diffstat (limited to 'Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs')
| -rw-r--r-- | Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs b/Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs new file mode 100644 index 000000000..155798209 --- /dev/null +++ b/Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Threading; +using Jellyfin.Data.Entities; +using MediaBrowser.Controller.Persistence; +using MediaBrowser.Model.Entities; +using Microsoft.EntityFrameworkCore; + +namespace Jellyfin.Server.Implementations.Item; + +/// <summary> +/// Manager for handling Media Attachments. +/// </summary> +/// <param name="dbProvider">Efcore Factory.</param> +public class MediaAttachmentRepository(IDbContextFactory<JellyfinDbContext> dbProvider) : IMediaAttachmentRepository +{ + /// <inheritdoc /> + public void SaveMediaAttachments( + Guid id, + IReadOnlyList<MediaAttachment> attachments, + CancellationToken cancellationToken) + { + using var context = dbProvider.CreateDbContext(); + using var transaction = context.Database.BeginTransaction(); + context.AttachmentStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete(); + context.AttachmentStreamInfos.AddRange(attachments.Select(e => Map(e, id))); + context.SaveChanges(); + transaction.Commit(); + } + + /// <inheritdoc /> + public IReadOnlyList<MediaAttachment> GetMediaAttachments(MediaAttachmentQuery filter) + { + using var context = dbProvider.CreateDbContext(); + var query = context.AttachmentStreamInfos.AsNoTracking().Where(e => e.ItemId.Equals(filter.ItemId)); + if (filter.Index.HasValue) + { + query = query.Where(e => e.Index == filter.Index); + } + + return query.AsEnumerable().Select(Map).ToArray(); + } + + private MediaAttachment Map(AttachmentStreamInfo attachment) + { + return new MediaAttachment() + { + Codec = attachment.Codec, + CodecTag = attachment.CodecTag, + Comment = attachment.Comment, + FileName = attachment.Filename, + Index = attachment.Index, + MimeType = attachment.MimeType, + }; + } + + private AttachmentStreamInfo Map(MediaAttachment attachment, Guid id) + { + return new AttachmentStreamInfo() + { + Codec = attachment.Codec, + CodecTag = attachment.CodecTag, + Comment = attachment.Comment, + Filename = attachment.FileName, + Index = attachment.Index, + MimeType = attachment.MimeType, + ItemId = id, + Item = null! + }; + } +} |
