-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PathTemplates handles assetPath formatting
Known formatting list only, can be expanded as required
- Loading branch information
1 parent
70a308a
commit a8f87d1
Showing
6 changed files
with
153 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# PathTemplates | ||
|
||
The default path template for requests is `/{prefix}/{customer}/{space}/{assetPath}`, where: | ||
|
||
* `prefix` is route path (e.g. `iiif-manifest`, `iiif-av`, `iiif-img`) and includes version. | ||
* `customer` and `space` are self explanatory | ||
* `assetPath` is the asset identifier plus any specific elements for the current request - e.g. for image requests it will contain the full IIIF image request. | ||
|
||
By default the above format is reflected on info.json (from Thumbs and Orchestrator). | ||
|
||
To facilitate using proxy servers to receive alternative URLs that are then rewritten to standard DLCS URLs, overrides to the default rules can be specified. These are used when outputting any self-referencing URIs (e.g. info.json `id` element). | ||
|
||
> [!IMPORTANT] | ||
> For the below to work the expectation is that the `x-forwarded-host` header is set in the proxy. | ||
``` | ||
"PathRules": { | ||
"Default": "/{prefix}/{customer}/{space}/{assetPath}", | ||
"Overrides": { | ||
"exclude-space.com": "/{prefix}/{customer}/extra/{assetPath}/", | ||
"customer-specific.io": "/{prefix}/{assetPath}" | ||
"i-have-ark.io": "/{prefix}/ark:{assetPath:US}" | ||
} | ||
} | ||
``` | ||
|
||
As an convenience you can specify `"PathRules:OverridesAsJson"` appSetting, for Orchestrator only, that includes a string-based config. This makes it easier to configure via environment variables etc | ||
|
||
## Formatters | ||
|
||
`assetPath` supports formatting via a known formatting parameter, e.g. `{assetPath}` can be formatted with `{assetPath:FMT}`. | ||
|
||
Supported format parameter values are: | ||
|
||
* `3US` - replaces triple _U_nderscores with _S_lashes (e.g. assetPath `"foo___bar_baz"` -> `"foo/bar_baz"`). | ||
|
||
## Auth PathTemplates | ||
|
||
There is a similar config block availabe for authentication under the `"Auth"` key for Orchestrator. | ||
|
||
For auth the path replacements are simpler: | ||
* `customer` is the customer the auth service is for | ||
* `behaviour` is the name of the auth service. | ||
|
||
``` | ||
"Auth": { | ||
"AuthPathRules": { | ||
"Default": "/auth/{customer}/{behaviour}", | ||
"Overrides": { | ||
"exclude-space.com": "/auth/{behaviour}" | ||
} | ||
} | ||
}, | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace DLCS.Core.Formats; | ||
|
||
/// <summary> | ||
/// Helper function for formatting {assetPath} template value, handling replacements | ||
/// </summary> | ||
internal static class AssetPathFormatter | ||
{ | ||
// match {assetPath} or {assetPath:FMT} | ||
private static readonly Regex AssetPath = new("({assetPath:?.*})", RegexOptions.Compiled); | ||
|
||
public static string ReplaceAssetPath(this string template, string assetPath) | ||
{ | ||
var match = AssetPath.Match(template); | ||
if (!match.Success) return template; | ||
|
||
for (var x = 0; x < match.Captures.Count; x++) | ||
{ | ||
var capture = match.Captures[x].Value; | ||
var forFormat = capture.Replace("assetPath", "0"); | ||
template = template.Replace(capture, string.Format(AssetPathFormat.Instance, forFormat, assetPath)); | ||
} | ||
|
||
return template; | ||
} | ||
} | ||
|
||
internal class AssetPathFormat : IFormatProvider, ICustomFormatter | ||
{ | ||
public static AssetPathFormat Instance { get; } = new(); | ||
|
||
// Replace "___" with "/" | ||
private const string UnderscoreToSlash = "3US"; | ||
|
||
public object? GetFormat(Type? formatType) | ||
=> formatType == typeof(ICustomFormatter) ? this : null; | ||
|
||
public string Format(string? format, object? arg, IFormatProvider? formatProvider) | ||
{ | ||
if (string.IsNullOrEmpty(format) || arg == null) return arg?.ToString() ?? string.Empty; | ||
|
||
var result = arg.ToString(); | ||
if (string.IsNullOrEmpty(result)) return string.Empty; | ||
|
||
if (format == UnderscoreToSlash) | ||
{ | ||
return result.Replace("___", "/"); | ||
} | ||
|
||
throw new ArgumentException($"'{format}' is not a known assetPath format", nameof(format)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters