예제 #1
0
        async Task ShowAsync(HttpContext context, CancellationToken cancellationToken)
        {
            // generate
            var data      = new ArraySegment <byte>(new byte[0]);
            var size      = 300;
            var stopwatch = Stopwatch.StartNew();

            try
            {
                // prepare
                var query = context.GetRequestUri().ParseQuery();
                var value = query.ContainsKey("v") && !string.IsNullOrWhiteSpace(query["v"])
                                        ? query["v"].ToBase64(false, true).Decrypt(Global.EncryptionKey)
                                        : query.ContainsKey("d") ? query["d"] : null;
                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new InvalidRequestException();
                }

                if (query.ContainsKey("t"))
                {
                    var timestamp = query["t"].ToBase64(false, true).Decrypt(Global.EncryptionKey).CastAs <long>();
                    if (DateTime.Now.ToUnixTimestamp() - timestamp > 90)
                    {
                        throw new InvalidRequestException();
                    }
                }

                size = (query.ContainsKey("s") ? query["s"] : "300").CastAs <int>();

                if (!Enum.TryParse(query.ContainsKey("ecl") ? query["ecl"] : "M", out QRCodeGenerator.ECCLevel level))
                {
                    level = QRCodeGenerator.ECCLevel.M;
                }

                // generate QR code using QRCoder
                if ("QRCoder".IsEquals(UtilityService.GetAppSetting("Files:QRCodeProvider")))
                {
                    data = this.Generate(value, size, level);
                }

                // generate QR code using Google APIs
                else
                {
                    try
                    {
                        data = (await UtilityService.DownloadAsync($"https://chart.apis.google.com/chart?cht=qr&chs={size}x{size}&chl={value.UrlEncode()}", null, null, cancellationToken).ConfigureAwait(false)).ToArraySegment();
                    }
                    catch (Exception ex)
                    {
                        await Global.WriteLogsAsync(this.Logger, "Http.QRCodes", $"Error occurred while generating the QR Code by Google Chart APIs: {ex.Message}", ex).ConfigureAwait(false);

                        data = this.Generate(value, size, level);
                    }
                }

                stopwatch.Stop();
                if (Global.IsDebugLogEnabled)
                {
                    await Global.WriteLogsAsync(this.Logger, "Http.QRCodes", $"Generate QR Code successful: {value} - [Size: {size} - ECC Level: {level}] - Execution times: {stopwatch.GetElapsedTimes()}").ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                await Global.WriteLogsAsync(this.Logger, "Http.QRCodes", $"Error occurred while generating the QR Code: {ex.Message}", ex).ConfigureAwait(false);

                data = ThumbnailHandler.Generate(ex.Message, size, size, true);
            }

            // display
            context.SetResponseHeaders((int)HttpStatusCode.OK, "image/png", null, 0, "private, no-store, no-cache", TimeSpan.Zero, context.GetCorrelationID());
            await context.WriteAsync(data, cancellationToken).ConfigureAwait(false);
        }