< 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: 33
Coverable lines: 33
Total lines: 74
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
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(...)100%210%
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();
 028        context.AttachmentStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
 029        context.AttachmentStreamInfos.AddRange(attachments.Select(e => Map(e, id)));
 030        context.SaveChanges();
 031        transaction.Commit();
 032    }
 33
 34    /// <inheritdoc />
 35    public IReadOnlyList<MediaAttachment> GetMediaAttachments(MediaAttachmentQuery filter)
 36    {
 037        using var context = dbProvider.CreateDbContext();
 038        var query = context.AttachmentStreamInfos.AsNoTracking().Where(e => e.ItemId.Equals(filter.ItemId));
 039        if (filter.Index.HasValue)
 40        {
 041            query = query.Where(e => e.Index == filter.Index);
 42        }
 43
 044        return query.AsEnumerable().Select(Map).ToArray();
 045    }
 46
 47    private MediaAttachment Map(AttachmentStreamInfo attachment)
 48    {
 049        return new MediaAttachment()
 050        {
 051            Codec = attachment.Codec,
 052            CodecTag = attachment.CodecTag,
 053            Comment = attachment.Comment,
 054            FileName = attachment.Filename,
 055            Index = attachment.Index,
 056            MimeType = attachment.MimeType,
 057        };
 58    }
 59
 60    private AttachmentStreamInfo Map(MediaAttachment attachment, Guid id)
 61    {
 062        return new AttachmentStreamInfo()
 063        {
 064            Codec = attachment.Codec,
 065            CodecTag = attachment.CodecTag,
 066            Comment = attachment.Comment,
 067            Filename = attachment.FileName,
 068            Index = attachment.Index,
 069            MimeType = attachment.MimeType,
 070            ItemId = id,
 071            Item = null!
 072        };
 73    }
 74}