[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

@ -197,6 +197,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...");
return instanceConfig; return instanceConfig;

View file

@ -168,6 +168,8 @@ public class DriveService(
using var processed = image.Autorot(); using var processed = image.Autorot();
buf.Seek(0, SeekOrigin.Begin); buf.Seek(0, SeekOrigin.Begin);
try
{
// Calculate blurhash using a x200px image for improved performance // Calculate blurhash using a x200px image for improved performance
using var blurhashImage = processed.ThumbnailImage(200, 200, NetVips.Enums.Size.Down); using var blurhashImage = processed.ThumbnailImage(200, 200, NetVips.Enums.Size.Down);
var blurBuf = blurhashImage.WriteToMemory(); var blurBuf = blurhashImage.WriteToMemory();
@ -179,12 +181,19 @@ public class DriveService(
{ {
for (var j = 0; j < blurhashImage.Width; j++) for (var j = 0; j < blurhashImage.Width; j++)
{ {
blurArr[j, i] = new Pixel(blurBuf[idx++] / 255d, blurBuf[idx++] / 255d, blurBuf[idx++] / 255d); blurArr[j, i] = new Pixel(blurBuf[idx++] / 255d, blurBuf[idx++] / 255d,
blurBuf[idx++] / 255d);
idx += incr; idx += incr;
} }
} }
blurhash = Blurhash.Core.Encode(blurArr, 7, 7, new Progress<int>()); 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)
{ {
@ -208,10 +217,10 @@ public class DriveService(
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;