| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Collections.Immutable; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading; |
| | | 6 | | using Jellyfin.Database.Implementations; |
| | | 7 | | using Jellyfin.Database.Implementations.Entities; |
| | | 8 | | using MediaBrowser.Controller.Persistence; |
| | | 9 | | using MediaBrowser.Model.Entities; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Manager for handling Media Attachments. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="dbProvider">Efcore Factory.</param> |
| | | 18 | | public class MediaAttachmentRepository(IDbContextFactory<JellyfinDbContext> dbProvider) : IMediaAttachmentRepository |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public void SaveMediaAttachments( |
| | | 22 | | Guid id, |
| | | 23 | | IReadOnlyList<MediaAttachment> attachments, |
| | | 24 | | CancellationToken cancellationToken) |
| | | 25 | | { |
| | 0 | 26 | | using var context = dbProvider.CreateDbContext(); |
| | 0 | 27 | | using var transaction = context.Database.BeginTransaction(); |
| | | 28 | | |
| | | 29 | | // Users may replace a media with a version that includes attachments to one without them. |
| | | 30 | | // So when saving attachments is triggered by a library scan, we always unconditionally |
| | | 31 | | // clear the old ones, and then add the new ones if given. |
| | 0 | 32 | | context.AttachmentStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete(); |
| | 0 | 33 | | if (attachments.Any()) |
| | | 34 | | { |
| | 0 | 35 | | context.AttachmentStreamInfos.AddRange(attachments.Select(e => Map(e, id))); |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | context.SaveChanges(); |
| | 0 | 39 | | transaction.Commit(); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public IReadOnlyList<MediaAttachment> GetMediaAttachments(MediaAttachmentQuery filter) |
| | | 44 | | { |
| | 0 | 45 | | using var context = dbProvider.CreateDbContext(); |
| | 0 | 46 | | var query = context.AttachmentStreamInfos.AsNoTracking().Where(e => e.ItemId.Equals(filter.ItemId)); |
| | 0 | 47 | | if (filter.Index.HasValue) |
| | | 48 | | { |
| | 0 | 49 | | query = query.Where(e => e.Index == filter.Index); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | return query.AsEnumerable().Select(Map).ToArray(); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | private MediaAttachment Map(AttachmentStreamInfo attachment) |
| | | 56 | | { |
| | 0 | 57 | | return new MediaAttachment() |
| | 0 | 58 | | { |
| | 0 | 59 | | Codec = attachment.Codec, |
| | 0 | 60 | | CodecTag = attachment.CodecTag, |
| | 0 | 61 | | Comment = attachment.Comment, |
| | 0 | 62 | | FileName = attachment.Filename, |
| | 0 | 63 | | Index = attachment.Index, |
| | 0 | 64 | | MimeType = attachment.MimeType, |
| | 0 | 65 | | }; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private AttachmentStreamInfo Map(MediaAttachment attachment, Guid id) |
| | | 69 | | { |
| | 0 | 70 | | return new AttachmentStreamInfo() |
| | 0 | 71 | | { |
| | 0 | 72 | | Codec = attachment.Codec, |
| | 0 | 73 | | CodecTag = attachment.CodecTag, |
| | 0 | 74 | | Comment = attachment.Comment, |
| | 0 | 75 | | Filename = attachment.FileName, |
| | 0 | 76 | | Index = attachment.Index, |
| | 0 | 77 | | MimeType = attachment.MimeType, |
| | 0 | 78 | | ItemId = id, |
| | 0 | 79 | | Item = null! |
| | 0 | 80 | | }; |
| | | 81 | | } |
| | | 82 | | } |