Files
qrcodeService/QRCodeService/Controllers/ImageController.cs

50 lines
1.6 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;
using QRCodeService.Options;
using QRCodeService.Infrastructure.Middlewares;
namespace QRCodeService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImageController : ControllerBase
{
readonly ILinkQueries linkQueries;
readonly IAppQueries appQueries;
readonly ServiceOption option;
public ImageController(ILinkQueries linkQueries, IAppQueries appQueries,ServiceOption option)
{
this.linkQueries = linkQueries;
this.appQueries = appQueries;
this.option = option;
}
[CheckSign(typeof(GetQRCodeModel))]
[Route("qrcode")]
[HttpPost]
public async Task<IActionResult> GetImage(GetQRCodeModel input)
{
var link = await linkQueries.GetLinkAsync(input.ShortCode);
if (link.AppId != input.AppId)
{
return BadRequest();
}
var qrCodeGenerator = new QRCodeGenerator();
var data = qrCodeGenerator.CreateQrCode($"{option.BaseUrl}r/{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");
}
}
}
}