Add ImageInfo index

This commit is contained in:
Shadowghost
2026-01-18 17:39:48 +01:00
parent 99440f8432
commit 342846e4fc
6 changed files with 1881 additions and 19 deletions

View File

@@ -1698,7 +1698,8 @@ public class ImageController : BaseJellyfinApiController
return await GetImageResult(
options,
cacheDuration,
ImmutableDictionary<string, string>.Empty)
ImmutableDictionary<string, string>.Empty,
tag)
.ConfigureAwait(false);
}
@@ -1913,7 +1914,8 @@ public class ImageController : BaseJellyfinApiController
return await GetImageResult(
options,
cacheDuration,
responseHeaders).ConfigureAwait(false);
responseHeaders,
tag).ConfigureAwait(false);
}
private ImageFormat[] GetOutputFormats(ImageFormat? format)
@@ -1992,18 +1994,13 @@ public class ImageController : BaseJellyfinApiController
private async Task<ActionResult> GetImageResult(
ImageProcessingOptions imageProcessingOptions,
TimeSpan? cacheDuration,
IDictionary<string, string> headers)
IDictionary<string, string> headers,
string? tag)
{
var (imagePath, imageContentType, dateImageModified) = await _imageProcessor.ProcessImage(imageProcessingOptions).ConfigureAwait(false);
var disableCaching = Request.Headers[HeaderNames.CacheControl].Contains("no-cache");
var parsingSuccessful = DateTime.TryParse(Request.Headers[HeaderNames.IfModifiedSince], out var ifModifiedSinceHeader);
// if the parsing of the IfModifiedSince header was not successful, disable caching
if (!parsingSuccessful)
{
// disableCaching = true;
}
var hasTag = !string.IsNullOrEmpty(tag);
foreach (var (key, value) in headers)
{
@@ -2025,7 +2022,8 @@ public class ImageController : BaseJellyfinApiController
{
if (cacheDuration.HasValue)
{
Response.Headers.Append(HeaderNames.CacheControl, "public, max-age=" + cacheDuration.Value.TotalSeconds);
// When tag is provided, the URL is effectively immutable - the tag changes when the image changes
Response.Headers.Append(HeaderNames.CacheControl, "public, max-age=" + cacheDuration.Value.TotalSeconds + ", immutable");
}
else
{
@@ -2034,10 +2032,25 @@ public class ImageController : BaseJellyfinApiController
Response.Headers.Append(HeaderNames.LastModified, dateImageModified.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss \"GMT\"", CultureInfo.InvariantCulture));
// if the image was not modified since "ifModifiedSinceHeader"-header, return a HTTP status code 304 not modified
if (!(dateImageModified > ifModifiedSinceHeader) && cacheDuration.HasValue)
// Add ETag header for stronger cache validation when tag is provided
if (hasTag)
{
if (ifModifiedSinceHeader.Add(cacheDuration.Value) < DateTime.UtcNow)
Response.Headers.Append(HeaderNames.ETag, $"\"{tag}\"");
// Check If-None-Match header for ETag-based validation (preferred over If-Modified-Since)
var ifNoneMatch = Request.Headers[HeaderNames.IfNoneMatch].ToString();
if (!string.IsNullOrEmpty(ifNoneMatch) && (ifNoneMatch == $"\"{tag}\"" || ifNoneMatch == tag))
{
Response.StatusCode = StatusCodes.Status304NotModified;
return new ContentResult();
}
}
// Check If-Modified-Since header for time-based validation
if (DateTime.TryParse(Request.Headers[HeaderNames.IfModifiedSince], out var ifModifiedSinceHeader))
{
// Return 304 if the image has not been modified since the client's cached version
if (dateImageModified <= ifModifiedSinceHeader)
{
Response.StatusCode = StatusCodes.Status304NotModified;
return new ContentResult();

View File

@@ -908,17 +908,17 @@ public sealed class BaseItemRepository
var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(filter);
if (enableGroupByPresentationUniqueKey && filter.GroupBySeriesPresentationUniqueKey)
{
var tempQuery = dbQuery.GroupBy(e => new { e.PresentationUniqueKey, e.SeriesPresentationUniqueKey }).Select(e => e.FirstOrDefault()).Select(e => e!.Id);
var tempQuery = dbQuery.GroupBy(e => new { e.PresentationUniqueKey, e.SeriesPresentationUniqueKey }).Select(e => e.OrderBy(x => x.Id).FirstOrDefault()).Select(e => e!.Id);
dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id));
}
else if (enableGroupByPresentationUniqueKey)
{
var tempQuery = dbQuery.GroupBy(e => e.PresentationUniqueKey).Select(e => e.FirstOrDefault()).Select(e => e!.Id);
var tempQuery = dbQuery.GroupBy(e => e.PresentationUniqueKey).Select(e => e.OrderBy(x => x.Id).FirstOrDefault()).Select(e => e!.Id);
dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id));
}
else if (filter.GroupBySeriesPresentationUniqueKey)
{
var tempQuery = dbQuery.GroupBy(e => e.SeriesPresentationUniqueKey).Select(e => e.FirstOrDefault()).Select(e => e!.Id);
var tempQuery = dbQuery.GroupBy(e => e.SeriesPresentationUniqueKey).Select(e => e.OrderBy(x => x.Id).FirstOrDefault()).Select(e => e!.Id);
dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id));
}
else
@@ -2113,7 +2113,7 @@ public sealed class BaseItemRepository
var masterQuery = TranslateQuery(innerQuery, context, outerQueryFilter)
.GroupBy(e => e.PresentationUniqueKey)
.Select(e => e.FirstOrDefault())
.Select(e => e.OrderBy(x => x.Id).FirstOrDefault())
.Select(e => e!.Id);
var query = context.BaseItems
@@ -2866,7 +2866,7 @@ public sealed class BaseItemRepository
if (filter.ImageTypes.Length > 0)
{
var imgTypes = filter.ImageTypes.Select(e => (ImageInfoImageType)e).ToArray();
baseQuery = baseQuery.Where(e => imgTypes.Any(f => e.Images!.Any(w => w.ImageType == f)));
baseQuery = baseQuery.Where(e => e.Images!.Any(w => imgTypes.Contains(w.ImageType)));
}
if (filter.IsLiked.HasValue)
@@ -3667,6 +3667,7 @@ public sealed class BaseItemRepository
var result = query
.Select(b => b.UserData!.Any(u => u.UserId == userId && u.Played))
.GroupBy(_ => 1)
.OrderBy(g => g.Key)
.Select(g => new
{
Total = g.Count(),

View File

@@ -0,0 +1,24 @@
using Jellyfin.Database.Implementations.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Database.Implementations.ModelConfiguration;
/// <summary>
/// FluentAPI configuration for the BaseItemImageInfo entity.
/// </summary>
public class BaseItemImageInfoConfiguration : IEntityTypeConfiguration<BaseItemImageInfo>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<BaseItemImageInfo> builder)
{
builder.HasKey(e => e.Id);
builder.HasOne(e => e.Item).WithMany(e => e.Images).HasForeignKey(e => e.ItemId);
// Index for efficient lookups and deletes by ItemId
builder.HasIndex(e => e.ItemId);
// Composite index for filtering by item and image type
builder.HasIndex(e => new { e.ItemId, e.ImageType });
}
}

View File

@@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Jellyfin.Database.Providers.Sqlite.Migrations
{
/// <inheritdoc />
public partial class AddIndicesToImageInfo : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_BaseItemImageInfos_ItemId_ImageType",
table: "BaseItemImageInfos",
columns: new[] { "ItemId", "ImageType" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_BaseItemImageInfos_ItemId_ImageType",
table: "BaseItemImageInfos");
}
}
}

View File

@@ -450,6 +450,8 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasIndex("ItemId");
b.HasIndex("ItemId", "ImageType");
b.ToTable("BaseItemImageInfos");
b.HasAnnotation("Sqlite:UseSqlReturningClause", false);