You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.0 KiB
124 lines
4.0 KiB
6 months ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
5 months ago
|
using System.Collections.Immutable;
|
||
6 months ago
|
using System.Linq;
|
||
|
using Jellyfin.Data.Entities;
|
||
5 months ago
|
using MediaBrowser.Controller.Chapters;
|
||
|
using MediaBrowser.Controller.Drawing;
|
||
|
using MediaBrowser.Model.Dto;
|
||
6 months ago
|
using MediaBrowser.Model.Entities;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
||
|
namespace Jellyfin.Server.Implementations.Item;
|
||
|
|
||
5 months ago
|
/// <summary>
|
||
|
/// The Chapter manager.
|
||
|
/// </summary>
|
||
5 months ago
|
public class ChapterRepository : IChapterRepository
|
||
6 months ago
|
{
|
||
|
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
|
||
5 months ago
|
private readonly IImageProcessor _imageProcessor;
|
||
6 months ago
|
|
||
5 months ago
|
/// <summary>
|
||
5 months ago
|
/// Initializes a new instance of the <see cref="ChapterRepository"/> class.
|
||
5 months ago
|
/// </summary>
|
||
|
/// <param name="dbProvider">The EFCore provider.</param>
|
||
|
/// <param name="imageProcessor">The Image Processor.</param>
|
||
5 months ago
|
public ChapterRepository(IDbContextFactory<JellyfinDbContext> dbProvider, IImageProcessor imageProcessor)
|
||
6 months ago
|
{
|
||
|
_dbProvider = dbProvider;
|
||
5 months ago
|
_imageProcessor = imageProcessor;
|
||
6 months ago
|
}
|
||
|
|
||
5 months ago
|
/// <inheritdoc cref="IChapterRepository"/>
|
||
5 months ago
|
public ChapterInfo? GetChapter(BaseItemDto baseItem, int index)
|
||
5 months ago
|
{
|
||
|
return GetChapter(baseItem.Id, index);
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc cref="IChapterRepository"/>
|
||
|
public IReadOnlyList<ChapterInfo> GetChapters(BaseItemDto baseItem)
|
||
|
{
|
||
|
return GetChapters(baseItem.Id);
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc cref="IChapterRepository"/>
|
||
|
public ChapterInfo? GetChapter(Guid baseItemId, int index)
|
||
6 months ago
|
{
|
||
|
using var context = _dbProvider.CreateDbContext();
|
||
5 months ago
|
var chapter = context.Chapters
|
||
|
.Select(e => new
|
||
|
{
|
||
|
chapter = e,
|
||
|
baseItemPath = e.Item.Path
|
||
|
})
|
||
|
.FirstOrDefault(e => e.chapter.ItemId.Equals(baseItemId) && e.chapter.ChapterIndex == index);
|
||
5 months ago
|
if (chapter is not null)
|
||
|
{
|
||
5 months ago
|
return Map(chapter.chapter, chapter.baseItemPath!);
|
||
5 months ago
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
5 months ago
|
/// <inheritdoc cref="IChapterRepository"/>
|
||
|
public IReadOnlyList<ChapterInfo> GetChapters(Guid baseItemId)
|
||
5 months ago
|
{
|
||
|
using var context = _dbProvider.CreateDbContext();
|
||
5 months ago
|
return context.Chapters.Where(e => e.ItemId.Equals(baseItemId))
|
||
|
.Select(e => new
|
||
|
{
|
||
|
chapter = e,
|
||
|
baseItemPath = e.Item.Path
|
||
|
})
|
||
5 months ago
|
.ToList()
|
||
5 months ago
|
.Select(e => Map(e.chapter, e.baseItemPath!))
|
||
5 months ago
|
.ToImmutableArray();
|
||
|
}
|
||
|
|
||
5 months ago
|
/// <inheritdoc cref="IChapterRepository"/>
|
||
5 months ago
|
public void SaveChapters(Guid itemId, IReadOnlyList<ChapterInfo> chapters)
|
||
|
{
|
||
|
using var context = _dbProvider.CreateDbContext();
|
||
|
using (var transaction = context.Database.BeginTransaction())
|
||
|
{
|
||
|
context.Chapters.Where(e => e.ItemId.Equals(itemId)).ExecuteDelete();
|
||
|
for (var i = 0; i < chapters.Count; i++)
|
||
|
{
|
||
|
var chapter = chapters[i];
|
||
|
context.Chapters.Add(Map(chapter, i, itemId));
|
||
|
}
|
||
|
|
||
|
context.SaveChanges();
|
||
|
transaction.Commit();
|
||
|
}
|
||
6 months ago
|
}
|
||
|
|
||
|
private Chapter Map(ChapterInfo chapterInfo, int index, Guid itemId)
|
||
|
{
|
||
|
return new Chapter()
|
||
|
{
|
||
|
ChapterIndex = index,
|
||
|
StartPositionTicks = chapterInfo.StartPositionTicks,
|
||
|
ImageDateModified = chapterInfo.ImageDateModified,
|
||
|
ImagePath = chapterInfo.ImagePath,
|
||
|
ItemId = itemId,
|
||
5 months ago
|
Name = chapterInfo.Name,
|
||
|
Item = null!
|
||
6 months ago
|
};
|
||
|
}
|
||
|
|
||
5 months ago
|
private ChapterInfo Map(Chapter chapterInfo, string baseItemPath)
|
||
6 months ago
|
{
|
||
5 months ago
|
var chapterEntity = new ChapterInfo()
|
||
6 months ago
|
{
|
||
|
StartPositionTicks = chapterInfo.StartPositionTicks,
|
||
|
ImageDateModified = chapterInfo.ImageDateModified.GetValueOrDefault(),
|
||
|
ImagePath = chapterInfo.ImagePath,
|
||
|
Name = chapterInfo.Name,
|
||
|
};
|
||
5 months ago
|
chapterEntity.ImageTag = _imageProcessor.GetImageCacheTag(baseItemPath, chapterEntity.ImageDateModified);
|
||
|
return chapterEntity;
|
||
6 months ago
|
}
|
||
|
}
|