[backend/drive] Better error handling & logging

This commit is contained in:
Laura Hausmann 2024-05-01 01:20:08 +02:00
parent 7976854190
commit eab5e02b70
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 34 additions and 19 deletions

View file

@ -196,6 +196,12 @@ public static class WebApplicationExtensions
SixLabors.ImageSharp.Configuration.Default.MemoryAllocator = SixLabors.ImageSharp.Configuration.Default.MemoryAllocator =
MemoryAllocator.Create(new MemoryAllocatorOptions { AllocationLimitMegabytes = 20 }); MemoryAllocator.Create(new MemoryAllocatorOptions { AllocationLimitMegabytes = 20 });
NetVips.Log.SetLogHandler("VIPS", NetVips.Enums.LogLevelFlags.Warning, null);
NetVips.Log.SetLogHandler("VIPS", NetVips.Enums.LogLevelFlags.Error, (domain, _, message) =>
{
app.Logger.LogWarning("libvips error: {domain} - {message}", domain, message);
});
app.Logger.LogInformation("Initializing application, please wait..."); app.Logger.LogInformation("Initializing application, please wait...");

View file

@ -168,23 +168,32 @@ public class DriveService(
using var processed = image.Autorot(); using var processed = image.Autorot();
buf.Seek(0, SeekOrigin.Begin); buf.Seek(0, SeekOrigin.Begin);
// Calculate blurhash using a x200px image for improved performance try
using var blurhashImage = processed.ThumbnailImage(200, 200, NetVips.Enums.Size.Down);
var blurBuf = blurhashImage.WriteToMemory();
var blurArr = new Pixel[blurhashImage.Width, blurhashImage.Height];
var idx = 0;
var incr = image.Bands - 3;
for (var i = 0; i < blurhashImage.Height; i++)
{ {
for (var j = 0; j < blurhashImage.Width; j++) // Calculate blurhash using a x200px image for improved performance
{ using var blurhashImage = processed.ThumbnailImage(200, 200, NetVips.Enums.Size.Down);
blurArr[j, i] = new Pixel(blurBuf[idx++] / 255d, blurBuf[idx++] / 255d, blurBuf[idx++] / 255d); var blurBuf = blurhashImage.WriteToMemory();
idx += incr; var blurArr = new Pixel[blurhashImage.Width, blurhashImage.Height];
}
}
blurhash = Blurhash.Core.Encode(blurArr, 7, 7, new Progress<int>()); var idx = 0;
var incr = image.Bands - 3;
for (var i = 0; i < blurhashImage.Height; i++)
{
for (var j = 0; j < blurhashImage.Width; j++)
{
blurArr[j, i] = new Pixel(blurBuf[idx++] / 255d, blurBuf[idx++] / 255d,
blurBuf[idx++] / 255d);
idx += incr;
}
}
blurhash = Blurhash.Core.Encode(blurArr, 7, 7, new Progress<int>());
}
catch (Exception e)
{
logger.LogWarning("Failed to generate blurhash for image with mime type {type}: {e}",
request.MimeType, e.Message);
}
if (shouldStore) if (shouldStore)
{ {
@ -205,13 +214,13 @@ public class DriveService(
webpublic.Seek(0, SeekOrigin.Begin); webpublic.Seek(0, SeekOrigin.Begin);
} }
} }
logger.LogTrace("Image processing took {ms} ms", (int)(DateTime.Now - pre).TotalMilliseconds); logger.LogTrace("Image processing took {ms} ms", (int)(DateTime.Now - pre).TotalMilliseconds);
} }
catch catch (Exception e)
{ {
logger.LogError("Failed to generate blurhash & thumbnail for image with mime type {type}", logger.LogError("Failed to generate thumbnails for image with mime type {type}: {e}",
request.MimeType); request.MimeType, e.Message);
// We want to make sure no images are federated out without stripping metadata & converting to webp // We want to make sure no images are federated out without stripping metadata & converting to webp
if (user.Host == null) throw; if (user.Host == null) throw;