#95 poet images api
This commit is contained in:
parent
f53cda20fd
commit
97f454213e
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get poet image
|
||||
/// </summary>
|
||||
/// <param name="url">sample: hafez</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("poet/image/{url}.png")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(FileStreamResult))]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
||||
public async Task<IActionResult> Get(string url)
|
||||
{
|
||||
|
||||
RServiceResult<Guid> poet = await _ganjoorService.GetPoetImageIdByUrl($"/{url}");
|
||||
if (!string.IsNullOrEmpty(poet.ExceptionString))
|
||||
return BadRequest(poet.ExceptionString);
|
||||
|
||||
if (poet.Result == Guid.Empty)
|
||||
return NotFound();
|
||||
|
||||
|
||||
RServiceResult<RImage> 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<string> 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");
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// cat by id
|
||||
/// </summary>
|
||||
@ -750,17 +799,23 @@ namespace RMuseum.Controllers
|
||||
/// </summary>
|
||||
protected IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
/// <summary>
|
||||
/// Image Service
|
||||
/// </summary>
|
||||
protected readonly IImageFileService _imageFileService;
|
||||
|
||||
/// <summary>
|
||||
/// constructor
|
||||
/// </summary>
|
||||
/// <param name="ganjoorService"></param>
|
||||
/// <param name="appUserService"></param>
|
||||
/// <param name="httpContextAccessor"></param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -637,6 +637,13 @@
|
||||
<param name="url"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.Get(System.String)">
|
||||
<summary>
|
||||
get poet image
|
||||
</summary>
|
||||
<param name="url">sample: hafez</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.GetCatById(System.Int32,System.Boolean)">
|
||||
<summary>
|
||||
cat by id
|
||||
@ -852,7 +859,12 @@
|
||||
for client IP resolution
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.#ctor(RMuseum.Services.IGanjoorService,RSecurityBackend.Services.IAppUserService,Microsoft.AspNetCore.Http.IHttpContextAccessor)">
|
||||
<member name="F:RMuseum.Controllers.GanjoorController._imageFileService">
|
||||
<summary>
|
||||
Image Service
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.#ctor(RMuseum.Services.IGanjoorService,RSecurityBackend.Services.IAppUserService,Microsoft.AspNetCore.Http.IHttpContextAccessor,RSecurityBackend.Services.IImageFileService)">
|
||||
<summary>
|
||||
constructor
|
||||
</summary>
|
||||
@ -6167,6 +6179,13 @@
|
||||
<param name="url"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IGanjoorService.GetPoetImageIdByUrl(System.String)">
|
||||
<summary>
|
||||
poet image id by url
|
||||
</summary>
|
||||
<param name="url"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IGanjoorService.GetCatById(System.Int32,System.Boolean)">
|
||||
<summary>
|
||||
get cat by id
|
||||
@ -7139,6 +7158,13 @@
|
||||
<param name="url"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoetImageIdByUrl(System.String)">
|
||||
<summary>
|
||||
poet image id by url
|
||||
</summary>
|
||||
<param name="url"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetCatByUrl(System.String,System.Boolean)">
|
||||
<summary>
|
||||
get cat by url
|
||||
|
||||
@ -35,6 +35,13 @@ namespace RMuseum.Services
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetPoetByUrl(string url);
|
||||
|
||||
/// <summary>
|
||||
/// poet image id by url
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<Guid>> GetPoetImageIdByUrl(string url);
|
||||
|
||||
/// <summary>
|
||||
/// get cat by id
|
||||
/// </summary>
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// poet image id by url
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<Guid>> 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>(Guid.Empty);
|
||||
var poet = await _context.GanjoorPoets.Where(p => p.Id == cat.PoetId).SingleOrDefaultAsync();
|
||||
return new RServiceResult<Guid>((Guid)poet.RImageId);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return new RServiceResult<Guid>(Guid.Empty, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get cat by url
|
||||
/// </summary>
|
||||
@ -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();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user