From ccd1db47cfe475cb8c2068c218644f866cb098f2 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Tue, 19 Mar 2024 13:26:24 +0100 Subject: [PATCH] image processor which strips metadata --- .../Processors/StripMetadataWebProcessor.cs | 51 +++++++++++++++++++ zero/Media/MediaExtensions.cs | 3 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs diff --git a/zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs b/zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs new file mode 100644 index 00000000..f2d77384 --- /dev/null +++ b/zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Logging; +using SixLabors.ImageSharp.Web; +using SixLabors.ImageSharp.Web.Commands; +using SixLabors.ImageSharp.Web.Processors; +using System.Globalization; + +namespace zero.Media.ImageSharp.Processors; + +public class StripMetadataWebProcessor : IImageWebProcessor +{ + // + /// The command constant for quality. + /// + public const string Strip = "strip"; + + /// + /// The reusable collection of commands. + /// + private static readonly IEnumerable StripCommands + = new[] { Strip }; + + /// + public IEnumerable Commands { get; } = StripCommands; + + /// + public FormattedImage Process( + FormattedImage image, + ILogger logger, + CommandCollection commands, + CommandParser parser, + CultureInfo culture) + { + if (commands.Contains(Strip)) + { + bool strip = parser.ParseValue(commands.GetValueOrDefault(Strip), culture); + + if (strip) + { + image.Image.Metadata.ExifProfile = null; + image.Image.Metadata.XmpProfile = null; + image.Image.Metadata.IptcProfile = null; + image.Image.Metadata.IccProfile = null; + } + } + + return image; + } + + /// + public bool RequiresTrueColorPixelFormat(CommandCollection commands, CommandParser parser, CultureInfo culture) => true; +} diff --git a/zero/Media/MediaExtensions.cs b/zero/Media/MediaExtensions.cs index 7d9777b7..e3a5ab50 100644 --- a/zero/Media/MediaExtensions.cs +++ b/zero/Media/MediaExtensions.cs @@ -1,4 +1,5 @@ -using SixLabors.ImageSharp.Metadata.Profiles.Exif; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Web; using System.Numerics;