e81c127514
- Add JSON-based config loading with XML fallback for DB and library options - Implement LibraryOptionsRepository with EF Core, migrations, and entity - Update CollectionFolder to use DB-backed options with XML fallback/backfill - Register repository in DI and initialize at startup - Use EF execution strategy for transactional DB operations - Suppress code analysis warnings in .csproj and test files - Add DATABASE_MIGRATION.md, LIBRARY_OPTIONS_DB_DESIGN.md, and WEBSOCKET_AUTHENTICATION.md - Add database.json.example and improve migration docs - Add tests for JSON config loader and update test naming warnings
107 lines
3.7 KiB
C#
107 lines
3.7 KiB
C#
// <copyright file="MediaAttachmentRepository.cs" company="PlaceholderCompany">
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
// </copyright>
|
|
|
|
namespace Jellyfin.Server.Implementations.Item;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Jellyfin.Database.Implementations;
|
|
using Jellyfin.Database.Implementations.Entities;
|
|
using MediaBrowser.Controller.Persistence;
|
|
using MediaBrowser.Model.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
/// <summary>
|
|
/// Manager for handling Media Attachments.
|
|
/// </summary>
|
|
/// <param name="dbProvider">Efcore Factory.</param>
|
|
public class MediaAttachmentRepository(IDbContextFactory<JellyfinDbContext> dbProvider) : IMediaAttachmentRepository
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task SaveMediaAttachmentsAsync(
|
|
Guid id,
|
|
IReadOnlyList<MediaAttachment> attachments,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
await using var context = dbProvider.CreateDbContext();
|
|
|
|
// Use the execution strategy to handle transactional consistency and retries
|
|
// This is required for PostgreSQL NpgsqlRetryingExecutionStrategy
|
|
var executionStrategy = context.Database.CreateExecutionStrategy();
|
|
await executionStrategy.ExecuteAsync(async () =>
|
|
{
|
|
// 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.
|
|
await context.AttachmentStreamInfos
|
|
.Where(e => e.ItemId.Equals(id))
|
|
.ExecuteDeleteAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
if (attachments.Any())
|
|
{
|
|
await context.AttachmentStreamInfos
|
|
.AddRangeAsync(attachments.Select(e => Map(e, id)), cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
|
|
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
|
}).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<MediaAttachment>> GetMediaAttachmentsAsync(
|
|
MediaAttachmentQuery filter,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
await 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);
|
|
}
|
|
|
|
var attachments = await query
|
|
.ToArrayAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return attachments.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!
|
|
};
|
|
}
|
|
}
|