mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 15:39:01 -07:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace RadicadeServer.API
|
|
{
|
|
public static class ChatPhotoMessages
|
|
{
|
|
public static readonly TimeSpan UploadWindow = TimeSpan.FromMinutes(15);
|
|
|
|
private static readonly Regex ImageReference = new(@"<Image:([^>]*)>", RegexOptions.Compiled);
|
|
|
|
private static readonly Regex PhotoEnvelope = new("\"Type\"\\s*:\\s*2(?![0-9])", RegexOptions.Compiled);
|
|
|
|
public static bool IsPhotoEnvelope(string? contents)
|
|
=> !string.IsNullOrEmpty(contents) && PhotoEnvelope.IsMatch(contents);
|
|
|
|
public static bool HasUnresolvedImageReference(string? contents)
|
|
{
|
|
if (string.IsNullOrEmpty(contents))
|
|
return false;
|
|
|
|
foreach (Match match in ImageReference.Matches(contents))
|
|
{
|
|
if (!ResolvesToAnImage(match.Groups[1].Value))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static string ResolveImageReference(string contents, long imageId)
|
|
{
|
|
return ImageReference.Replace(contents, match =>
|
|
ResolvesToAnImage(match.Groups[1].Value) ? match.Value : $"<Image:{imageId}>");
|
|
}
|
|
|
|
private static bool ResolvesToAnImage(string rawReference)
|
|
=> long.TryParse(rawReference, out var imageId) && imageId > 0;
|
|
}
|
|
}
|