CS-CDN changes from worker stuff

This commit is contained in:
Rory&
2026-04-06 01:24:54 +02:00
parent 64f635010d
commit b11ac9b4b4
5 changed files with 60 additions and 60 deletions
@@ -3,28 +3,33 @@ using Spacebar.AdminApi.TestClient.Services.Services;
namespace Spacebar.Cdn.Extensions;
public class ImageController : ControllerBase {
protected DiscordImageResizeParams GetResizeParams() {
return new() {
Size = Request.Query.ContainsKey("size") && uint.TryParse(Request.Query["size"], out uint size) ? size : null,
Quality = Request.Query.ContainsKey("quality") && Enum.TryParse<DiscordImageResizeQuality>(Request.Query["quality"], true, out var quality)
? quality
: DiscordImageResizeQuality.High,
KeepAspectRatio = !Request.Query.ContainsKey("keepAspectRatio") || !bool.TryParse(Request.Query["keepAspectRatio"], out bool kar) || kar,
Passthrough = Request.Query.ContainsKey("passthrough") && bool.TryParse(Request.Query["passthrough"], out bool pt) && pt,
Animated = Request.Query.ContainsKey("animated") && bool.TryParse(Request.Query["animated"], out bool an) && an,
SpacebarAllowUpscale = Request.Query.ContainsKey("allowUpscale") && bool.TryParse(Request.Query["allowUpscale"], out bool au) && au,
SpacebarOptimiseGif = Request.Query.ContainsKey("optimiseGif") && bool.TryParse(Request.Query["optimiseGif"], out bool og) && og,
};
}
protected void SetSuccessCacheHeader() {
int cacheDuration = (int)TimeSpan.FromHours(6).TotalSeconds;
Response.Headers.CacheControl = $"public, max-age={cacheDuration}, s-maxage={cacheDuration}, immutable";
public static class HttpRequestExtensions {
extension(HttpRequest request) {
public DiscordImageResizeParams GetResizeParams() {
return new() {
Size = request.Query.ContainsKey("size") && uint.TryParse(request.Query["size"], out uint size) ? size : null,
Quality = request.Query.ContainsKey("quality") && Enum.TryParse<DiscordImageResizeQuality>(request.Query["quality"], true, out var quality)
? quality
: DiscordImageResizeQuality.High,
KeepAspectRatio = !request.Query.ContainsKey("keepAspectRatio") || !bool.TryParse(request.Query["keepAspectRatio"], out bool kar) || kar,
Passthrough = request.Query.ContainsKey("passthrough") && bool.TryParse(request.Query["passthrough"], out bool pt) && pt,
Animated = request.Query.ContainsKey("animated") && bool.TryParse(request.Query["animated"], out bool an) && an,
SpacebarAllowUpscale = request.Query.ContainsKey("allowUpscale") && bool.TryParse(request.Query["allowUpscale"], out bool au) && au,
SpacebarOptimiseGif = request.Query.ContainsKey("optimiseGif") && bool.TryParse(request.Query["optimiseGif"], out bool og) && og,
};
}
}
extension (HttpResponse response) {
public void SetSuccessCacheHeader() {
int cacheDuration = (int)TimeSpan.FromHours(6).TotalSeconds;
response.Headers.CacheControl = $"public, max-age={cacheDuration}, s-maxage={cacheDuration}, immutable";
}
protected void SetFailureCacheHeader() {
int cacheDuration = (int)TimeSpan.FromMinutes(5).TotalSeconds;
Response.Headers.CacheControl = $"public, max-age={cacheDuration}, s-maxage={cacheDuration}, immutable";
public void SetFailureCacheHeader() {
int cacheDuration = (int)TimeSpan.FromMinutes(5).TotalSeconds;
response.Headers.CacheControl = $"public, max-age={cacheDuration}, s-maxage={cacheDuration}, immutable";
}
}
}
@@ -1,24 +0,0 @@
using ArcaneLibs.Extensions;
using ImageMagick;
namespace Spacebar.AdminApi.TestClient.Services.Helpers;
public class DefaultAvatarRenderer {
// Slower at runtime, but doesnt depend on filesystem
private static string GetDefaultAvatarSvg(byte r, byte g, byte b) {
return $"""
<svg width="512" height="512" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect fill="#000115" width="512" height="512"/>
<path fill="#{r:X2}{g:X2}{b:X2}" fill-rule="evenodd" clip-rule="evenodd" d="M419.395 296.205C424.003 317.631 418.678 339.993 404.912 357.042C391.143 374.092 370.407 384 348.497 384H163.949C142.039 384 121.303 374.092 107.536 357.042C93.7677 339.993 88.4431 317.631 93.0509 296.205L125.547 145.08C127.06 138.042 131.891 132.176 138.508 129.344C145.124 126.511 152.703 127.065 158.837 130.829L256.223 190.583L353.609 130.829C359.743 127.065 367.322 126.511 373.938 129.344C380.555 132.176 385.386 138.042 386.899 145.08L419.395 296.205ZM344.729 261.348C344.729 250.399 335.855 241.523 324.91 241.523H286.727C275.782 241.523 266.91 250.399 266.91 261.348V299.542C266.91 310.491 275.782 319.365 286.727 319.365H324.91C335.855 319.365 344.729 310.491 344.729 299.542V261.348ZM245.536 261.348C245.536 250.399 236.664 241.523 225.719 241.523H187.536C176.591 241.523 167.717 250.399 167.717 261.348V299.542C167.717 310.491 176.591 319.365 187.536 319.365H225.719C236.664 319.365 245.536 310.491 245.536 299.542V261.348Z"/>
</svg>
""";
}
public static async Task<MemoryStream> GetDefaultAvatar(byte r, byte g, byte b, MagickFormat format = MagickFormat.Png, int size = 4096) {
var img = new MagickImageCollection(GetDefaultAvatarSvg(r, g, b).AsBytes().ToArray());
var ms = new MemoryStream();
await img.WriteAsync(ms);
ms.Position = 0;
return ms;
}
}
+23 -8
View File
@@ -1,7 +1,7 @@
using System.ComponentModel;
using ImageMagick;
namespace Spacebar.AdminApi.TestClient.Services;
namespace Spacebar.Cdn;
public static class Mimes {
private static string PrintLogged(string msg, string mime) {
@@ -12,15 +12,30 @@ public static class Mimes {
public static MagickFormat GetFormatForExtension(string extension) {
extension = extension.ToLower();
// ban some values...
if (extension == "screenshot") throw new AccessViolationException("Disallowed extension: " + extension);
// TODO: look for more
if (extension
// screen capture/write
is "screenshot"
or "win"
or "clipboard"
or "x" // read from/write to x11 server
or "xwd" // x11 window dump
or "dds" // MS DirectDraw surface
or "open" // display image on screen, OSX only
// printer stuff
or "print"
or "scan"
or "scanx"
// special
or "dmr" // MagicCache media library, let's not...
or "emf" // some microsoft meta format, windows only
or "mpr" // Magick Persistent Registry - basically a resident in-memory image
) throw new AccessViolationException("Disallowed extension: " + extension);
var matchingFormat = Enum.GetNames(typeof(MagickFormat)).FirstOrDefault(f => f.ToLower() == extension);
MagickFormat
var matchingFormat = Enum.GetNames<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);
return Enum.TryParse(matchingFormat, out MagickFormat fmt) ? fmt : throw new InvalidEnumArgumentException("Unknown format: " + extension);
}
public static string GetMime(MagickFormat fmt) => fmt switch {
@@ -8,7 +8,7 @@
"applicationUrl": "http://localhost:5114",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"LD_LIBRARY_PATH": "/home/Rory/git/spacebar/server-master/extra/admin-api/Spacebar.Cdn/result-lib/lib/"
// "LD_LIBRARY_PATH": "/home/Rory/git/spacebar/server-master/extra/admin-api/Spacebar.Cdn/result-lib/lib/"
}
},
"Local": {
@@ -18,7 +18,7 @@
"applicationUrl": "http://localhost:5114",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Local",
// "LD_LIBRARY_PATH": "/home/Rory/git/spacebar/server-master/extra/admin-api/Spacebar.Cdn/result-lib/lib/",
// "LD_LIBRARY_PATH": "/home/Rory/git/spacebar/server-master/extra/admin-api/Spacebar.Cdn/result-lib/lib/",
"STORAGE_PATH": "./files"
}
}
@@ -7,17 +7,21 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ArcaneLibs" Version="1.0.1-preview.20260126-091403" />
<!-- <PackageReference Include="Magick.NET-Q16-HDRI-AnyCPU" Version="14.10.1" />-->
<PackageReference Include="Magick.NET-Q16-HDRI-OpenMP-x64" Version="14.11.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.5" />
<PackageReference Include="ArcaneLibs" Version="1.0.1-preview.20260126-091403"/>
<!-- i dont think native performance here is worth it? -->
<PackageReference Include="Magick.NET-Q16-HDRI-AnyCPU" Version="14.11.1"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.5"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Interop\Spacebar.Interop.Cdn.Abstractions\Spacebar.Interop.Cdn.Abstractions.csproj" Condition="'$(ContinuousIntegrationBuild)'!='true'"/>
<ProjectReference Include="..\Interop\Spacebar.Interop.Cdn.Abstractions\Spacebar.Interop.Cdn.Abstractions.csproj" Condition="'$(ContinuousIntegrationBuild)'!='true'"/>
<PackageReference Include="Spacebar.Interop.Cdn.Abstractions" Version="*-preview*" Condition="'$(ContinuousIntegrationBuild)'=='true'"/>
<ProjectReference Include="..\Models\Spacebar.Models.Db\Spacebar.Models.Db.csproj" Condition="'$(ContinuousIntegrationBuild)'!='true'"/>
<ProjectReference Include="..\Models\Spacebar.Models.Db\Spacebar.Models.Db.csproj" Condition="'$(ContinuousIntegrationBuild)'!='true'"/>
<PackageReference Include="Spacebar.Models.Db" Version="*-preview*" Condition="'$(ContinuousIntegrationBuild)'=='true'"/>
</ItemGroup>
<ItemGroup>
<Folder Include="Helpers\" />
</ItemGroup>
</Project>