Files
qrcodeService/QRCodeService/Controllers/ImageController.cs
2021-02-25 15:40:58 +08:00

56 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using QRCoder;
using System.IO;
using QRCodeService.Application.Queries;
using QRCodeService.Models;
namespace QRCodeService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImageController : ControllerBase
{
readonly ILinkQueries linkQueries;
readonly IAppQueries appQueries;
public ImageController(ILinkQueries linkQueries, IAppQueries appQueries)
{
this.linkQueries = linkQueries;
this.appQueries = appQueries;
}
[Route("qrcode")]
[HttpPost]
public async Task<IActionResult> GetImage(GetQRCodeModel input)
{
var app = await appQueries.GetAppAsync(input.AppId);
if (app == null)
{
return BadRequest();
}
if (!await input.CheckValidAsync(app.Appkey))
{
return BadRequest();
}
var link = await linkQueries.GetLinkAsync(input.ShortCode);
if (link.AppId != input.AppId)
{
return BadRequest();
}
var qrCodeGenerator = new QRCodeGenerator();
var data = qrCodeGenerator.CreateQrCode($"http://localhost:5000/{link.ShortCode}", QRCodeGenerator.ECCLevel.Q);
var qrCode = new QRCode(data);
using (var stream = new MemoryStream())
{
qrCode.GetGraphic(20).Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return File(stream.ToArray(), "image/png");
}
}
}
}