From 97f454213e9ac0111f296622cadf8117ed338e66 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 18 Mar 2021 16:51:30 +0330 Subject: [PATCH] #95 poet images api --- RMuseum/Controllers/GanjoorController.cs | 57 ++++++++++++++++++- RMuseum/RMuseum.xml | 28 ++++++++- RMuseum/Services/IGanjoorService.cs | 7 +++ .../Services/Implementation/GanjoorService.cs | 32 ++++++++++- 4 files changed, 119 insertions(+), 5 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 82eb479b..7c19bb22 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -9,9 +9,11 @@ using RMuseum.Models.GanjoorAudio.ViewModels; using RMuseum.Models.GanjoorIntegration.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Generic; +using RSecurityBackend.Models.Image; using RSecurityBackend.Services; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; @@ -86,6 +88,53 @@ namespace RMuseum.Controllers return Ok(res.Result); } + /// + /// get poet image + /// + /// sample: hafez + /// + [HttpGet("poet/image/{url}.png")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(FileStreamResult))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task Get(string url) + { + + RServiceResult poet = await _ganjoorService.GetPoetImageIdByUrl($"/{url}"); + if (!string.IsNullOrEmpty(poet.ExceptionString)) + return BadRequest(poet.ExceptionString); + + if (poet.Result == Guid.Empty) + return NotFound(); + + + RServiceResult img = + await _imageFileService.GetImage(poet.Result); + + if (!string.IsNullOrEmpty(img.ExceptionString)) + return BadRequest(img.ExceptionString); + + if (img.Result == null) + return NotFound(); + + Response.GetTypedHeaders().LastModified = img.Result.LastModified; + + var requestHeaders = Request.GetTypedHeaders(); + if (requestHeaders.IfModifiedSince.HasValue && + requestHeaders.IfModifiedSince.Value >= img.Result.LastModified) + { + return StatusCode(StatusCodes.Status304NotModified); + } + + RServiceResult imgPath = _imageFileService.GetImagePath(img.Result); + if (!string.IsNullOrEmpty(imgPath.ExceptionString)) + return BadRequest(imgPath.ExceptionString); + + + return new FileStreamResult(new FileStream(imgPath.Result, FileMode.Open, FileAccess.Read), "image/png"); + + } + /// /// cat by id /// @@ -750,17 +799,23 @@ namespace RMuseum.Controllers /// protected IHttpContextAccessor _httpContextAccessor; + /// + /// Image Service + /// + protected readonly IImageFileService _imageFileService; + /// /// constructor /// /// /// /// - public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor) + public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor, IImageFileService imageFileService) { _ganjoorService = ganjoorService; _appUserService = appUserService; _httpContextAccessor = httpContextAccessor; + _imageFileService = imageFileService; } } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 98b8a6bc..7164373e 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -637,6 +637,13 @@ + + + get poet image + + sample: hafez + + cat by id @@ -852,7 +859,12 @@ for client IP resolution - + + + Image Service + + + constructor @@ -6167,6 +6179,13 @@ + + + poet image id by url + + + + get cat by id @@ -7139,6 +7158,13 @@ + + + poet image id by url + + + + get cat by url diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index b0ec034b..5ba7af99 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -35,6 +35,13 @@ namespace RMuseum.Services /// Task> GetPoetByUrl(string url); + /// + /// poet image id by url + /// + /// + /// + Task> GetPoetImageIdByUrl(string url); + /// /// get cat by id /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 4fb69ddf..b035a6b2 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -52,7 +52,7 @@ namespace RMuseum.Services.Implementation RootCatId = cat.Id, Nickname = poet.Nickname, Published = poet.Published, - ImageUrl = poet.RImageId == null ? "" : $"/api/rimages/{poet.RImageId}.jpg" + ImageUrl = poet.RImageId == null ? "" : $"/api/ganjoor/poet/image{cat.FullUrl}.png" } ) .ToListAsync(); @@ -117,6 +117,32 @@ namespace RMuseum.Services.Implementation } } + /// + /// poet image id by url + /// + /// + /// + public async Task> GetPoetImageIdByUrl(string url) + { + try + { + // /hafez/ => /hafez : + if (url.LastIndexOf('/') == url.Length - 1) + { + url = url.Substring(0, url.Length - 1); + } + var cat = await _context.GanjoorCategories.Where(c => c.FullUrl == url && c.ParentId == null).SingleOrDefaultAsync(); + if (cat == null) + return new RServiceResult(Guid.Empty); + var poet = await _context.GanjoorPoets.Where(p => p.Id == cat.PoetId).SingleOrDefaultAsync(); + return new RServiceResult((Guid)poet.RImageId); + } + catch (Exception exp) + { + return new RServiceResult(Guid.Empty, exp.ToString()); + } + } + /// /// get cat by url /// @@ -264,7 +290,7 @@ namespace RMuseum.Services.Implementation RootCatId = _context.GanjoorCategories.Where(c => c.PoetId == p.Id && c.ParentId == null).Single().Id, Nickname = p.Nickname, Published = p.Published, - ImageUrl = p.RImageId == null ? "" : $"/api/rimages/{p.RImageId}.jpg" + ImageUrl = p.RImageId == null ? "" : $"/api/ganjoor/poet/image{_context.GanjoorCategories.Where(c => c.PoetId == p.Id && c.ParentId == null).Single().FullUrl}.png" }).FirstOrDefaultAsync(), Cat = catViewModel } @@ -336,7 +362,7 @@ namespace RMuseum.Services.Implementation RootCatId = cat.Id, Nickname = poet.Nickname, Published = poet.Published, - ImageUrl = poet.RImageId == null ? "" : $"/api/rimages/{poet.RImageId}.jpg" + ImageUrl = poet.RImageId == null ? "" : $"/api/ganjoor/poet/image{cat.FullUrl}.png" } ) .SingleAsync();