示例#1
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "api/sample")] HttpRequestMessage req, TraceWriter log, ExecutionContext context)
        {
            var host = req.GetHost();

            var queryLogo = req.GetQueryValue("logo");

            var isBool = Boolean.TryParse(queryLogo, out bool hasLogo);

            if (!isBool && queryLogo != null)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            var generator = new UrlGenerator(new Uri(host + path), 1);

            var uri = generator.ToUri().First();

            var bitmap = new QrCode().GenerateImage(uri, 50);

            var wallId = generator.Guids.First().ToString("N");

            byte[] hmac;

            var key = SampleAESKey.Key;

            hmac = AES.EncryptStringToBytes_Aes(wallId, key);

            var ms = new MemoryStream();

            if (hasLogo)
            {
                var logoPath = context.FunctionAppDirectory + LOGO_DIR;
                var logo     = new Logo(logoPath);

                ms.Position = 0;

                var qrCodeWithLogo = logo.Image.AppendImage(Image.FromStream(ms), 100, 100);
                ms = new MemoryStream();
                qrCodeWithLogo.Save(ms, ImageFormat.Jpeg);
            }

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new ByteArrayContent(ms.ToArray());
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaType);
            response.Headers.Add("Access-Control-Expose-Headers", HeaderName);
            response.Headers.Add(HeaderName, AES.ByteToString(hmac));

            return(response);
        }