From 589063f36bccc007569744e1ed700662321ffa14 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 13 Feb 2024 02:02:42 +0100 Subject: [PATCH] [backend/drive] Calculate blurhash for images (ISH-54) --- .../Core/Services/DriveService.cs | 22 +++++++++++++++++-- Iceshrimp.Backend/Iceshrimp.Backend.csproj | 1 + Iceshrimp.NET.sln.DotSettings | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Iceshrimp.Backend/Core/Services/DriveService.cs b/Iceshrimp.Backend/Core/Services/DriveService.cs index 1c4e7676..11c66e11 100644 --- a/Iceshrimp.Backend/Core/Services/DriveService.cs +++ b/Iceshrimp.Backend/Core/Services/DriveService.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using Blurhash.ImageSharp; using Iceshrimp.Backend.Core.Configuration; using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database.Tables; @@ -7,6 +8,8 @@ using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Queues; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; namespace Iceshrimp.Backend.Core.Services; @@ -94,6 +97,21 @@ public class DriveService( } buf.Seek(0, SeekOrigin.Begin); + + string? blurhash = null; + + if (request.MimeType.StartsWith("image/")) { + try { + var image = await Image.LoadAsync(buf); + blurhash = Blurhasher.Encode(image, 7, 7); + } + catch { + logger.LogError("Failed to generate blurhash for image with mime type {type}", request.MimeType); + } + + buf.Seek(0, SeekOrigin.Begin); + } + var (filename, guid) = GenerateFilenameKeepingExtension(request.Filename); var shouldStore = storageConfig.Value.MediaRetention != null || user.Host == null; var storedInternal = storageConfig.Value.Mode == Enums.FileStorage.Local; @@ -138,8 +156,8 @@ public class DriveService( Comment = request.Comment, Type = request.MimeType, RequestHeaders = request.RequestHeaders, - RequestIp = request.RequestIp - //Blurhash = TODO, + RequestIp = request.RequestIp, + Blurhash = blurhash, //Properties = TODO, //ThumbnailUrl = TODO, //ThumbnailAccessKey = TODO, diff --git a/Iceshrimp.Backend/Iceshrimp.Backend.csproj b/Iceshrimp.Backend/Iceshrimp.Backend.csproj index c7603344..1dddfe66 100644 --- a/Iceshrimp.Backend/Iceshrimp.Backend.csproj +++ b/Iceshrimp.Backend/Iceshrimp.Backend.csproj @@ -18,6 +18,7 @@ + diff --git a/Iceshrimp.NET.sln.DotSettings b/Iceshrimp.NET.sln.DotSettings index 8967dc2b..8fb9faf5 100644 --- a/Iceshrimp.NET.sln.DotSettings +++ b/Iceshrimp.NET.sln.DotSettings @@ -344,6 +344,7 @@ True True True + True True True True