aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs
blob: e75dda439276d09416fc0780d02f083d1244222b (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.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();

        // Users may replace a media with a version that includes attachments to one without them.
        // So when saving attachments is triggered by a library scan, we always unconditionally
        // clear the old ones, and then add the new ones if given.
        context.AttachmentStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
        if (attachments.Any())
        {
            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!
        };
    }
}