Skip to content

Commit

Permalink
Merge pull request #83 from msigley/main
Browse files Browse the repository at this point in the history
Added Added ApplyDefaultCommandsToQuerylessUrls route option
  • Loading branch information
lilith authored Aug 11, 2023
2 parents 51faea9 + 72bb560 commit 9c12ab3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Imageflow.Server.Configuration/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public ImageflowMiddlewareOptions GetImageflowMiddlewareOptions(){
}

}

if (config.RouteDefaults?.ApplyDefaultCommandsToQuerylessUrls ?? false){
options.SetApplyDefaultCommandsToQuerylessUrls(true);
}

// set security options
var securityOptions = new SecurityOptions();
if (config.Security?.MaxDecodeResolution != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ internal class RouteBase : ITomlMetadataProvider, IValidationCapable
// public RouteSource? Source { get; set; }
public string? CacheControl { get; set; }
public string? ApplyDefaultCommands { get; set; }
public bool? ApplyDefaultCommandsToQuerylessUrls { get; set; }
// public string? ApplyOverrideCommands { get; set; }
// public string? RequireSignature { get; set; }

Expand Down
1 change: 1 addition & 0 deletions src/Imageflow.Server.Configuration/minimal.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lowercase_path_remainder = false
allow_extensionless_urls = false
cache_control = "public, max-age=2592000"
apply_default_commands = "quality=76&webp.quality=70&f.sharpen=23&down.filter=mitchell"
apply_default_commands_to_queryless_urls = false

[[routes]]
prefix = '/images/'
Expand Down
2 changes: 1 addition & 1 deletion src/Imageflow.Server/ImageJobInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private bool ProcessRewritesAndAuthorization(HttpContext context, ImageflowMiddl
}

// Set defaults if keys are missing, but at least 1 supported key is present
if (PathHelpers.SupportedQuerystringKeys.Any(args.Query.ContainsKey))
if (middlewareOptions.ApplyDefaultCommandsToQuerylessUrls || PathHelpers.SupportedQuerystringKeys.Any(args.Query.ContainsKey))
{
foreach (var pair in middlewareOptions.CommandDefaults)
{
Expand Down
14 changes: 13 additions & 1 deletion src/Imageflow.Server/ImageflowMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class ImageflowMiddlewareOptions


internal readonly Dictionary<string, string> CommandDefaults = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public bool ApplyDefaultCommandsToQuerylessUrls { get; set; } = false;

internal readonly Dictionary<string, PresetOptions> Presets = new Dictionary<string, PresetOptions>(StringComparer.OrdinalIgnoreCase);

internal readonly List<ExtensionlessPath> ExtensionlessPaths = new List<ExtensionlessPath>();
Expand Down Expand Up @@ -242,5 +243,16 @@ public ImageflowMiddlewareOptions SetDefaultCacheControlString(string cacheContr
DefaultCacheControlString = cacheControlString;
return this;
}

/// <summary>
/// Set to true have the command defaults to all urls, not just ones containing a valid query command.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ImageflowMiddlewareOptions SetApplyDefaultCommandsToQuerylessUrls(bool value)
{
this.ApplyDefaultCommandsToQuerylessUrls = value;
return this;
}
}
}
4 changes: 4 additions & 0 deletions tests/Imageflow.Server.Configuration.Tests/TestMinimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void TestComputedOptions(DeploymentEnvironment environment)
{"ImageflowMiddlewareOptions.MapWebRoot", "False"},
{"ImageflowMiddlewareOptions.UsePresetsExclusively", "False"},
{"ImageflowMiddlewareOptions.DefaultCacheControlString", "public, max-age=20"},
{"ImageflowMiddlewareOptions.ApplyDefaultCommandsToQuerylessUrls", "True"},
{"ImageflowMiddlewareOptions.RequestSignatureOptions", "null"},
{"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxWidth", "12000"},
{"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxHeight", "12000"},
Expand Down Expand Up @@ -135,6 +136,7 @@ public void TestComputedOptions(DeploymentEnvironment environment)
{"ImageflowMiddlewareOptions.MapWebRoot", "False"},
{"ImageflowMiddlewareOptions.UsePresetsExclusively", "False"},
{"ImageflowMiddlewareOptions.DefaultCacheControlString", "public, max-age=20"},
{"ImageflowMiddlewareOptions.ApplyDefaultCommandsToQuerylessUrls", "True"},
{"ImageflowMiddlewareOptions.RequestSignatureOptions", "null"},
{"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxWidth", "12000"},
{"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxHeight", "12000"},
Expand Down Expand Up @@ -183,6 +185,7 @@ public void TestComputedOptions(DeploymentEnvironment environment)
{"ImageflowMiddlewareOptions.MapWebRoot", "False"},
{"ImageflowMiddlewareOptions.UsePresetsExclusively", "False"},
{"ImageflowMiddlewareOptions.DefaultCacheControlString", "public, max-age=20"},
{"ImageflowMiddlewareOptions.ApplyDefaultCommandsToQuerylessUrls", "True"},
{"ImageflowMiddlewareOptions.RequestSignatureOptions", "null"},
{"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxWidth", "12000"},
{"ImageflowMiddlewareOptions.JobSecurityOptions.MaxDecodeSize.MaxHeight", "12000"},
Expand Down Expand Up @@ -232,6 +235,7 @@ public void TestComputedOptions(DeploymentEnvironment environment)
lowercase_path_remainder = true
cache_control = "public, max-age=20"
apply_default_commands = "quality=76"
apply_default_commands_to_queryless_urls = true
[[routes]]
prefix = '/images/'
Expand Down

0 comments on commit 9c12ab3

Please sign in to comment.