mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 13:06:50 +00:00
Add support for a time placeholder in URLs
This commit is contained in:
parent
c4679ccfb8
commit
3a3f42d755
1 changed files with 20 additions and 0 deletions
|
|
@ -14,6 +14,10 @@ public static class AvatarUtils
|
|||
|
||||
private static readonly string DiscordMediaUrlReplacement =
|
||||
"https://media.discordapp.net/attachments/$1/$2/$3.$4?width=256&height=256";
|
||||
|
||||
// Rewrite time "cachebuster" parameters for randomly generated/chosen avatars with custom URLs.
|
||||
// Number match uses `[1-9][0-9]{0,6}` rather than `[0-9]+` to avoid needing to deal with special cases for zero and limit to reasonable numbers.
|
||||
private static readonly Regex TimePlaceholder = new(@"\{time(?:/(?<divisor>[1-9][0-9]{0,6}))?(?:%(?<modulus>[1-9][0-9]{0,6}))?\}", RegexOptions.IgnoreCase);
|
||||
|
||||
public static string? TryRewriteCdnUrl(string? url)
|
||||
{
|
||||
|
|
@ -25,8 +29,24 @@ public static class AvatarUtils
|
|||
if (match.Groups["query"].Success)
|
||||
newUrl += "&" + match.Groups["query"].Value;
|
||||
|
||||
newUrl = TimePlaceholder.Replace(newUrl, ProcessTimePlaceholder);
|
||||
|
||||
return newUrl;
|
||||
}
|
||||
|
||||
private static string? ProcessTimePlaceholder(Match m) {
|
||||
// Minutes are the maximum accuracy to avoid too much cache thrashing
|
||||
// AND with the maximum positive value so it's always positive (as if this code will exist long enough for the 64-bit signed unix time to go negative...)
|
||||
var time = (DateTimeOffset.UtcNow.ToUnixTimeSeconds()/60)&Int64.MaxValue;
|
||||
|
||||
if (m.Groups["divisor"].Success)
|
||||
time /= Int32.Parse(m.Groups["divisor"].Value); // as above - guaranteed to not throw and be > 0
|
||||
|
||||
if (m.Groups["modulus"].Success)
|
||||
time %= Int32.Parse(m.Groups["modulus"].Value);
|
||||
|
||||
return time.ToString();
|
||||
}
|
||||
|
||||
public static bool IsDiscordCdnUrl(string? url) => url != null && DiscordCdnUrl.Match(url).Success;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue