< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.Item.MediaAttachmentRepository
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 82
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SaveMediaAttachments(...)0%620%
GetMediaAttachments(...)0%620%
Map(...)100%210%
Map(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/Item/MediaAttachmentRepository.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Collections.Immutable;
 4using System.Linq;
 5using System.Threading;
 6using Jellyfin.Database.Implementations;
 7using Jellyfin.Database.Implementations.Entities;
 8using MediaBrowser.Controller.Persistence;
 9using MediaBrowser.Model.Entities;
 10using Microsoft.EntityFrameworkCore;
 11
 12namespace Jellyfin.Server.Implementations.Item;
 13
 14/// <summary>
 15/// Manager for handling Media Attachments.
 16/// </summary>
 17/// <param name="dbProvider">Efcore Factory.</param>
 18public class MediaAttachmentRepository(IDbContextFactory<JellyfinDbContext> dbProvider) : IMediaAttachmentRepository
 19{
 20    /// <inheritdoc />
 21    public void SaveMediaAttachments(
 22        Guid id,
 23        IReadOnlyList<MediaAttachment> attachments,
 24        CancellationToken cancellationToken)
 25    {
 026        using var context = dbProvider.CreateDbContext();
 027        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.
 032        context.AttachmentStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
 033        if (attachments.Any())
 34        {
 035            context.AttachmentStreamInfos.AddRange(attachments.Select(e => Map(e, id)));
 36        }
 37
 038        context.SaveChanges();
 039        transaction.Commit();
 040    }
 41
 42    /// <inheritdoc />
 43    public IReadOnlyList<MediaAttachment> GetMediaAttachments(MediaAttachmentQuery filter)
 44    {
 045        using var context = dbProvider.CreateDbContext();
 046        var query = context.AttachmentStreamInfos.AsNoTracking().Where(e => e.ItemId.Equals(filter.ItemId));
 047        if (filter.Index.HasValue)
 48        {
 049            query = query.Where(e => e.Index == filter.Index);
 50        }
 51
 052        return query.AsEnumerable().Select(Map).ToArray();
 053    }
 54
 55    private MediaAttachment Map(AttachmentStreamInfo attachment)
 56    {
 057        return new MediaAttachment()
 058        {
 059            Codec = attachment.Codec,
 060            CodecTag = attachment.CodecTag,
 061            Comment = attachment.Comment,
 062            FileName = attachment.Filename,
 063            Index = attachment.Index,
 064            MimeType = attachment.MimeType,
 065        };
 66    }
 67
 68    private AttachmentStreamInfo Map(MediaAttachment attachment, Guid id)
 69    {
 070        return new AttachmentStreamInfo()
 071        {
 072            Codec = attachment.Codec,
 073            CodecTag = attachment.CodecTag,
 074            Comment = attachment.Comment,
 075            Filename = attachment.FileName,
 076            Index = attachment.Index,
 077            MimeType = attachment.MimeType,
 078            ItemId = id,
 079            Item = null!
 080        };
 81    }
 82}