Skip to content

Commit

Permalink
make service used by QRCodeImage control configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephMDavis committed May 17, 2024
1 parent 785b56c commit d1709da
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 137 deletions.
136 changes: 0 additions & 136 deletions Web/Controls/Google/QRCodeImage.cs

This file was deleted.

95 changes: 95 additions & 0 deletions Web/Controls/QRCodeImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using mojoPortal.Web.Framework;

namespace mojoPortal.Web.UI;

public class QRCodeImage : Image
{
private static readonly ILog log = LogManager.GetLogger(typeof(QRCodeImage));

private readonly string QRCodeGeneratorUrl = ConfigHelper.GetStringProperty("QRCodeGeneratorUrl", "https://api.qrserver.com/v1/create-qr-code/?size={0}x{1}&format={2}&data={3}");

public int SizeInPixels { get; set; } = 150;
public string TextToEncode { get; set; } = string.Empty;
public bool AutoDetectPageUrl { get; set; } = false;
public bool RemoveSkinParam { get; set; } = true;
public string ImageFormat { get; set; } = "jpg";

private void SetTextFromPageUrl()
{
if (RemoveSkinParam)
{
try
{
TextToEncode = FilteredUrl(WebUtils.GetSiteRoot() + Page.Request.RawUrl);
}
catch (Exception ex)
{
log.Error("handled error ", ex);
}
}
else
{
TextToEncode = WebUtils.GetSiteRoot() + Page.Request.RawUrl;
}
}


private string FilteredUrl(string rawUrl)
{
if (string.IsNullOrEmpty(rawUrl) || !rawUrl.Contains("?"))
{
return rawUrl;
}

if (rawUrl.IndexOf("?") == (rawUrl.Length - 1))
{
return rawUrl.Replace("?", string.Empty);
}

var baseUrl = rawUrl.Substring(0, rawUrl.IndexOf("?"));
var queryString = rawUrl.Substring(rawUrl.IndexOf("?"), rawUrl.Length - rawUrl.IndexOf("?"));
queryString = WebUtils.RemoveQueryStringParam(queryString, "skin");

if (queryString.Length > 0)
{
return $"{baseUrl}?{queryString}";
}

return baseUrl;
}


protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

if (AutoDetectPageUrl)
{
SetTextFromPageUrl();
}

if (string.IsNullOrWhiteSpace(TextToEncode))
{
return;
}

ImageUrl = string.Format(CultureInfo.InvariantCulture, QRCodeGeneratorUrl, SizeInPixels, SizeInPixels, ImageFormat, Page.Server.UrlEncode(TextToEncode));
AlternateText = TextToEncode;
ToolTip = TextToEncode;
}


protected override void Render(HtmlTextWriter writer)
{
// don't render if the text to encode has not been set
if (TextToEncode.Length > 0)
{
base.Render(writer);
}
}
}
2 changes: 1 addition & 1 deletion Web/mojoPortal.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@
<Compile Include="Controls\Google\GoogleTranslatePanel.cs" />
<Compile Include="Controls\Google\GoogleCustomSearchControl.cs" />
<Compile Include="Controls\Google\LocationMap.cs" />
<Compile Include="Controls\Google\QRCodeImage.cs" />
<Compile Include="Controls\QRCodeImage.cs" />
<Compile Include="Controls\Gravatar\Avatar.cs" />
<Compile Include="Controls\Panels\jPlayerPanel.cs" />
<Compile Include="Controls\jQuery\bxSlider.cs" />
Expand Down

0 comments on commit d1709da

Please sign in to comment.