diff --git a/QRCodeService/Controllers/Api/LinkController.cs b/QRCodeService/Controllers/Api/LinkController.cs index 5d21f26..27da907 100644 --- a/QRCodeService/Controllers/Api/LinkController.cs +++ b/QRCodeService/Controllers/Api/LinkController.cs @@ -11,6 +11,11 @@ using MediatR; using QRCodeService.Application.Queries; using QRCodeService.Application.Commands; using QRCodeService.Infrastructure.Middlewares; +using QRCodeService.Options; +using QRCoder; +using System.Drawing; +using System.IO; +using Microsoft.Extensions.Options; namespace QRCodeService.Controllers.Api { @@ -20,22 +25,31 @@ namespace QRCodeService.Controllers.Api { readonly IMediator mediator; readonly ILinkQueries linkQueries; + readonly IOptions option; - public LinkController(IMediator mediator, ILinkQueries queries) + public LinkController(IMediator mediator, ILinkQueries queries, IOptions option) { this.mediator = mediator; this.linkQueries = queries; + this.option = option; } - [Route("{shortCode}")] + [Route("i/{shortCode}/")] [HttpGet] - public async Task Get(string shortCode) + public async Task GetQrCode(string shortCode,int img=0) { var link = await linkQueries.GetLinkAsync(shortCode); if (link == null) { return NotFound(); } - return Ok(link); + var qrCodeGenerator = new QRCodeGenerator(); + var data = qrCodeGenerator.CreateQrCode($"{option.Value.BaseUrl}r/{link.ShortCode}", QRCodeGenerator.ECCLevel.Q); + var qrCode = new QRCode(data); + using (var stream = new MemoryStream()) + { + qrCode.GetGraphic(10, Color.Black, Color.White, null, 15, 6, false).Save(stream, System.Drawing.Imaging.ImageFormat.Png); + return File(stream.ToArray(), "image/png"); + } } [CheckSign(typeof(CreateLinkModel))] diff --git a/QRCodeService/Controllers/ImageController.cs b/QRCodeService/Controllers/ImageController.cs deleted file mode 100644 index b78cf83..0000000 --- a/QRCodeService/Controllers/ImageController.cs +++ /dev/null @@ -1,49 +0,0 @@ -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 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"); - } - } - } -} diff --git a/QRCodeService/Controllers/RedirectController.cs b/QRCodeService/Controllers/RedirectController.cs index 296a319..1dfb0d5 100644 --- a/QRCodeService/Controllers/RedirectController.cs +++ b/QRCodeService/Controllers/RedirectController.cs @@ -1,5 +1,8 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http.Extensions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.WebUtilities; using QRCodeService.Application.Queries; +using QRCodeService.Extensions; using System; using System.Collections.Generic; using System.Linq; @@ -9,18 +12,35 @@ using System.Threading.Tasks; namespace QRCodeService.Controllers { [Route("r/{shortCode}")] - public class RedirectController:Controller + public class RedirectController : Controller { - private readonly ILinkQueries queries; + private readonly ILinkQueries linkQueries; + private readonly IAppQueries appQueries; - public RedirectController(ILinkQueries queries) + public RedirectController(ILinkQueries queries, IAppQueries appQueries) { - this.queries = queries; + this.linkQueries = queries; + this.appQueries = appQueries; } [HttpGet] public async Task Index(string shortCode) { - var link = await queries.GetLinkAsync(shortCode); + var link = await linkQueries.GetLinkAsync(shortCode); + if (link == null) + { + return BadRequest(); + } + var app = await appQueries.GetAppAsync(link.AppId); + if (app == null) + { + return BadRequest(); + } + var t = DateTime.Now.ToString("yyyyMMddHHmmss"); + var sign = BitConverter.ToString($"{t}{app.Appkey}".ToMD5()).Replace("-", ""); + link.FullUrl = QueryHelpers.AddQueryString(link.FullUrl, new Dictionary { + { "s",sign }, + { "t",t} + }); return View(link); } } diff --git a/QRCodeService/QRCodeService.csproj b/QRCodeService/QRCodeService.csproj index 83cb3b3..f641906 100644 --- a/QRCodeService/QRCodeService.csproj +++ b/QRCodeService/QRCodeService.csproj @@ -49,6 +49,10 @@ + + + + diff --git a/QRCodeService/Startup.cs b/QRCodeService/Startup.cs index 99ac022..8a4f39a 100644 --- a/QRCodeService/Startup.cs +++ b/QRCodeService/Startup.cs @@ -51,7 +51,7 @@ namespace QRCodeService c.SwaggerDoc("v1", new OpenApiInfo { Title = "QRCodeService", Version = "v1" }); }); //ÅäÖà - services.Configure(Configuration.GetSection("hosting")); + services.Configure(Configuration.GetSection("Hosting")); //validator services.AddTransient, CreateLinkCommandValidator>(); diff --git a/QRCodeService/Views/Redirect/Index.cshtml b/QRCodeService/Views/Redirect/Index.cshtml index 1887b40..74e80a7 100644 --- a/QRCodeService/Views/Redirect/Index.cshtml +++ b/QRCodeService/Views/Redirect/Index.cshtml @@ -14,7 +14,7 @@ loading... \ No newline at end of file