summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.Cdn/Mimes.cs
diff options
context:
space:
mode:
Diffstat (limited to 'extra/admin-api/Spacebar.Cdn/Mimes.cs')
-rw-r--r--extra/admin-api/Spacebar.Cdn/Mimes.cs15
1 files changed, 15 insertions, 0 deletions
diff --git a/extra/admin-api/Spacebar.Cdn/Mimes.cs b/extra/admin-api/Spacebar.Cdn/Mimes.cs

index 508e9a3b..5a281432 100644 --- a/extra/admin-api/Spacebar.Cdn/Mimes.cs +++ b/extra/admin-api/Spacebar.Cdn/Mimes.cs
@@ -1,3 +1,4 @@ +using System.ComponentModel; using ImageMagick; namespace Spacebar.AdminApi.TestClient.Services; @@ -8,6 +9,20 @@ public static class Mimes { return mime; } + public static MagickFormat GetFormatForExtension(string extension) { + extension = extension.ToLower(); + // ban some values... + if (extension == "screenshot") throw new AccessViolationException("Disallowed extension: " + extension); + + var matchingFormat = Enum.GetNames(typeof(MagickFormat)).FirstOrDefault(f => f.ToLower() == extension); + if (string.IsNullOrWhiteSpace(matchingFormat)) throw new InvalidEnumArgumentException("Unknown format: " + extension); + if (Enum.TryParse(matchingFormat, out MagickFormat fmt)) { + return fmt; + } + + throw new InvalidEnumArgumentException("Unknown format: " + extension); + } + public static string GetMime(MagickFormat fmt) => fmt switch { MagickFormat.Png => "image/png", MagickFormat.Jpeg => "image/jpeg",