From c759fcb1a74b1657eef7a03c6cba10a1911e9f52 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Tue, 2 Apr 2024 11:46:27 +0200 Subject: [PATCH] add blur web processor; set image cache defaults --- .../ImageSharp/Processors/BlurWebProcessor.cs | 48 +++++++++++++++++++ zero/Media/Indexes/Media_ByChildren.cs | 38 --------------- zero/Media/Indexes/Media_ByHierarchy.cs | 45 ----------------- zero/Media/MediaOptions.cs | 2 +- zero/Media/ZeroMediaModule.cs | 18 ++++--- 5 files changed, 60 insertions(+), 91 deletions(-) create mode 100644 zero/Media/ImageSharp/Processors/BlurWebProcessor.cs delete mode 100644 zero/Media/Indexes/Media_ByChildren.cs delete mode 100644 zero/Media/Indexes/Media_ByHierarchy.cs diff --git a/zero/Media/ImageSharp/Processors/BlurWebProcessor.cs b/zero/Media/ImageSharp/Processors/BlurWebProcessor.cs new file mode 100644 index 00000000..ddb5e49f --- /dev/null +++ b/zero/Media/ImageSharp/Processors/BlurWebProcessor.cs @@ -0,0 +1,48 @@ +using Microsoft.Extensions.Logging; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Web; +using SixLabors.ImageSharp.Web.Commands; +using SixLabors.ImageSharp.Web.Processors; +using System.Globalization; + +namespace zero.Media.ImageSharp.Processors; + +public class BlurWebProcessor : IImageWebProcessor +{ + // + /// The command constant for the gaussian blur sigma. + /// + public const string Blur = "blur"; + + /// + /// The reusable collection of commands. + /// + private static readonly IEnumerable BlurCommands = new[] { Blur }; + + /// + public IEnumerable Commands { get; } = BlurCommands; + + /// + public FormattedImage Process( + FormattedImage image, + ILogger logger, + CommandCollection commands, + CommandParser parser, + CultureInfo culture) + { + if (commands.Contains(Blur)) + { + float sigma = parser.ParseValue(commands.GetValueOrDefault(Blur), culture); + + if (sigma != 0) + { + image.Image.Mutate(x => x.GaussianBlur(sigma)); + } + } + + return image; + } + + /// + public bool RequiresTrueColorPixelFormat(CommandCollection commands, CommandParser parser, CultureInfo culture) => true; +} diff --git a/zero/Media/Indexes/Media_ByChildren.cs b/zero/Media/Indexes/Media_ByChildren.cs deleted file mode 100644 index 0f32893b..00000000 --- a/zero/Media/Indexes/Media_ByChildren.cs +++ /dev/null @@ -1,38 +0,0 @@ -// using Raven.Client.Documents.Indexes; -// -// namespace zero.Media; -// -// public class Media_ByChildren : ZeroMultiMapIndex -// { -// public class Result : ZeroIdEntity, ISupportsDbConventions -// { -// public string ParentId { get; set; } -// -// public int ChildrenCount { get; set; } -// -// public string[] ChildrenIds { get; set; } -// } -// -// -// protected override void Create() -// { -// AddMap(items => items.Select(item => new Result() -// { -// Id = item.Id, -// ParentId = item.ParentId, -// ChildrenCount = 1, -// ChildrenIds = new string[] { } -// })); -// -// Reduce = results => results.GroupBy(x => new { x.ParentId }).Select(group => new Result() -// { -// Id = null, -// ParentId = group.Key.ParentId, -// ChildrenCount = group.Sum(x => x.ChildrenCount), -// ChildrenIds = group.Select(x => x.Id).ToArray() -// }); -// -// StoreAllFields(FieldStorage.Yes); -// Index(x => x.ParentId, FieldIndexing.Exact); -// } -// } \ No newline at end of file diff --git a/zero/Media/Indexes/Media_ByHierarchy.cs b/zero/Media/Indexes/Media_ByHierarchy.cs deleted file mode 100644 index 46ce620a..00000000 --- a/zero/Media/Indexes/Media_ByHierarchy.cs +++ /dev/null @@ -1,45 +0,0 @@ -// using Raven.Client.Documents.Indexes; -// -// namespace zero.Media; -// -// public class Media_ByHierarchy : ZeroIndex -// { -// public class Result : ZeroIdEntity, ISupportsDbConventions -// { -// public string Name { get; set; } -// -// public List Path { get; set; } = new List(); -// -// public string[] PathIds { get; set; } = Array.Empty(); -// } -// -// -// public class PathResult -// { -// public string Id { get; set; } -// -// public string Name { get; set; } -// } -// -// -// protected override void Create() -// { -// Map = items => items -// .Select(item => new -// { -// Item = item, -// Path = Recurse(item, x => LoadDocument(x.ParentId)).Where(x => x != null && x.Id != null && x.Id != item.Id).Reverse() -// }) -// .Select(item => new Result -// { -// Id = item.Item.Id, -// Name = item.Item.Name, -// Path = item.Path.Select(current => new PathResult() { Id = current.Id, Name = current.Name }).ToList(), -// PathIds = item.Path.Select(current => current.Id).ToArray() -// }); -// -// StoreAllFields(FieldStorage.Yes); -// Index("PathIds", FieldIndexing.Exact); -// //Index(x => x.ChannelId, FieldIndexing.Exact); -// } -// } diff --git a/zero/Media/MediaOptions.cs b/zero/Media/MediaOptions.cs index c74e48a7..f0a88dc9 100644 --- a/zero/Media/MediaOptions.cs +++ b/zero/Media/MediaOptions.cs @@ -5,7 +5,7 @@ namespace zero.Media; public class MediaOptions { - public string FolderPath { get; set; } + public string FolderPath { get; set; } = string.Empty; public string PublicPathPrefix { get; set; } = string.Empty; diff --git a/zero/Media/ZeroMediaModule.cs b/zero/Media/ZeroMediaModule.cs index fdb9571e..60c258d8 100644 --- a/zero/Media/ZeroMediaModule.cs +++ b/zero/Media/ZeroMediaModule.cs @@ -1,15 +1,13 @@ -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using SixLabors.ImageSharp.Processing; -using System.IO; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Routing; using SixLabors.ImageSharp.Web.Caching; using SixLabors.ImageSharp.Web.DependencyInjection; +using System.IO; using zero.Media.ImageSharp; -using zero.Numbers; using zero.Media.ImageSharp.Processors; namespace zero.Media; @@ -24,7 +22,13 @@ internal class ZeroMediaModule : ZeroModule .AddProvider() .AddProcessor() .AddProcessor() - .Configure(configuration.GetSection("Zero:Media:ImageSharp:Cache")); + .AddProcessor(); + + services.AddOptions().Configure(opts => + { + opts.CacheRootPath = "~/"; + opts.CacheFolder = "cache"; + }).Bind(configuration.GetSection("Zero:Media:ImageSharp:Cache")); //configuration.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Config/imaging.json"), true, true); //configuration.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), $"Config/imaging.{builder.Environment.EnvironmentName}.json"), true);