👔 Adjust compression rate of webp uploaded

This commit is contained in:
LittleSheep 2025-06-03 00:30:35 +08:00
parent 09e4150294
commit 130ad8f186

View File

@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Minio; using Minio;
using Minio.DataModel.Args; using Minio.DataModel.Args;
using NetVips;
using NodaTime; using NodaTime;
using Quartz; using Quartz;
using tusdotnet.Stores; using tusdotnet.Stores;
@ -51,6 +52,7 @@ public class FileService(
} }
private static readonly string TempFilePrefix = "dyn-cloudfile"; private static readonly string TempFilePrefix = "dyn-cloudfile";
private static readonly string[] function = new[] { "image/gif", "image/apng", "image/webp", "image/avif" };
// The analysis file method no longer will remove the GPS EXIF data // The analysis file method no longer will remove the GPS EXIF data
// It should be handled on the client side, and for some specific cases it should be keep // It should be handled on the client side, and for some specific cases it should be keep
@ -110,7 +112,7 @@ public class FileService(
var format = vipsImage.Get("vips-loader") ?? "unknown"; var format = vipsImage.Get("vips-loader") ?? "unknown";
// Try to get orientation from exif data // Try to get orientation from exif data
int orientation = 1; var orientation = 1;
Dictionary<string, object> exif = []; Dictionary<string, object> exif = [];
foreach (var field in vipsImage.GetFields()) foreach (var field in vipsImage.GetFields())
@ -177,10 +179,12 @@ public class FileService(
if (contentType.Split('/')[0] == "image") if (contentType.Split('/')[0] == "image")
{ {
// Skip compression for animated image types // Skip compression for animated image types
var animatedMimeTypes = new[] { "image/gif", "image/apng", "image/webp", "image/avif" }; var animatedMimeTypes = function;
if (animatedMimeTypes.Contains(contentType)) if (animatedMimeTypes.Contains(contentType))
{ {
logger.LogInformation("File {fileId} is an animated image (MIME: {mime}), skipping WebP conversion.", fileId, contentType); logger.LogInformation(
"File {fileId} is an animated image (MIME: {mime}), skipping WebP conversion.", fileId,
contentType);
var tempFilePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}"); var tempFilePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}");
result.Add((tempFilePath, string.Empty)); result.Add((tempFilePath, string.Empty));
return; return;
@ -190,7 +194,8 @@ public class FileService(
using var vipsImage = NetVips.Image.NewFromFile(ogFilePath); using var vipsImage = NetVips.Image.NewFromFile(ogFilePath);
var imagePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}"); var imagePath = Path.Join(Path.GetTempPath(), $"{TempFilePrefix}#{file.Id}");
vipsImage.WriteToFile(imagePath + ".webp"); vipsImage.WriteToFile(imagePath + ".webp",
new VOption { { "lossless", true } });
result.Add((imagePath + ".webp", string.Empty)); result.Add((imagePath + ".webp", string.Empty));
if (vipsImage.Width * vipsImage.Height >= 1024 * 1024) if (vipsImage.Width * vipsImage.Height >= 1024 * 1024)
@ -201,7 +206,8 @@ public class FileService(
// Create and save image within the same synchronous block to avoid disposal issues // Create and save image within the same synchronous block to avoid disposal issues
using var compressedImage = vipsImage.Resize(scale); using var compressedImage = vipsImage.Resize(scale);
compressedImage.WriteToFile(imageCompressedPath + ".webp"); compressedImage.WriteToFile(imageCompressedPath + ".webp",
new VOption { { "Q", 80 } });
result.Add((imageCompressedPath + ".webp", ".compressed")); result.Add((imageCompressedPath + ".webp", ".compressed"));
file.HasCompression = true; file.HasCompression = true;
@ -408,7 +414,7 @@ public class FileService(
return client.Build(); return client.Build();
} }
// Helper method to purge the cache for a specific file // Helper method to purge the cache for a specific file
// Made internal to allow FileReferenceService to use it // Made internal to allow FileReferenceService to use it
internal async Task _PurgeCacheAsync(string fileId) internal async Task _PurgeCacheAsync(string fileId)
@ -492,4 +498,4 @@ public class FileService(
.Where(r => r.FileId == fileId) .Where(r => r.FileId == fileId)
.AnyAsync(); .AnyAsync();
} }
} }