diff --git a/YaeBlog/Exceptions/BlogCommandException.cs b/YaeBlog/Exceptions/BlogCommandException.cs new file mode 100644 index 0000000..0db929d --- /dev/null +++ b/YaeBlog/Exceptions/BlogCommandException.cs @@ -0,0 +1,12 @@ +namespace YaeBlog.Core.Exceptions; + +public class BlogCommandException : Exception +{ + public BlogCommandException(string message) : base(message) + { + } + + public BlogCommandException(string message, Exception innerException) : base(message, innerException) + { + } +} diff --git a/YaeBlog/Services/ImageCompressService.cs b/YaeBlog/Services/ImageCompressService.cs index 9d50661..831e230 100644 --- a/YaeBlog/Services/ImageCompressService.cs +++ b/YaeBlog/Services/ImageCompressService.cs @@ -56,7 +56,7 @@ public sealed class ImageCompressService(IEssayScanService essayScanService, ILo } CompressResult[] compressedImages = (await Task.WhenAll(from image in uncompressedImages - select Task.Run(async () => new CompressResult(image, await ConvertToWebp(image.Content))))).ToArray(); + select Task.Run(async () => new CompressResult(image, await ConvertToWebp(image))))).ToArray(); compressedSize += compressedImages.Select(i => i.CompressContent.Length).Sum(); @@ -65,7 +65,8 @@ public sealed class ImageCompressService(IEssayScanService essayScanService, ILo select r.ImageInfo with { File = new FileInfo(r.ImageInfo.File.FullName.Split('.')[0] + ".webp"), - Content = r.CompressContent + Content = r.CompressContent, + MineType = "image/webp" }).ToList(); // 修改文本 string blogContent = compressedImages.Aggregate(content.Content, (c, r) => @@ -88,21 +89,31 @@ public sealed class ImageCompressService(IEssayScanService essayScanService, ILo } } - private static async Task ConvertToWebp(byte[] image) + private static async Task ConvertToWebp(BlogImageInfo image) { using ImageJob job = new(); - BuildJobResult result = await job.Decode(MemorySource.Borrow(image)) + BuildJobResult result = await job.Decode(MemorySource.Borrow(image.Content)) + .Branch(f => f.EncodeToBytes(new WebPLosslessEncoder())) .EncodeToBytes(new WebPLossyEncoder(75)) .Finish() .InProcessAsync(); - ArraySegment? array = result.First?.TryGetBytes(); + // 超过128KB的图片使用有损压缩 + // 反之使用无损压缩 - if (array.HasValue) + ArraySegment? losslessImage = result.TryGet(1)?.TryGetBytes(); + ArraySegment? lossyImage = result.TryGet(2)?.TryGetBytes(); + + if (image.Size <= 128 * 1024 && losslessImage.HasValue) { - return array.Value.ToArray(); + return losslessImage.Value.ToArray(); } - throw new BlogFileException(); + if (lossyImage.HasValue) + { + return lossyImage.Value.ToArray(); + } + + throw new BlogCommandException($"Failed to convert {image.File.Name} to webp format: return value is null."); } }