|
|
@ -1,6 +1,9 @@ |
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; |
|
|
|
using System.IO; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
|
|
|
@ -10,21 +13,38 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
public class PicController : Controller |
|
|
|
{ |
|
|
|
private readonly IHostingEnvironment _env; |
|
|
|
public PicController(IHostingEnvironment env) |
|
|
|
private readonly CatalogContext _catalogContext; |
|
|
|
|
|
|
|
public PicController(IHostingEnvironment env, |
|
|
|
CatalogContext catalogContext) |
|
|
|
{ |
|
|
|
_env = env; |
|
|
|
_catalogContext = catalogContext; |
|
|
|
} |
|
|
|
|
|
|
|
[HttpGet("{id}")] |
|
|
|
// GET: /<controller>/
|
|
|
|
public IActionResult GetImage(int id) |
|
|
|
public async Task<IActionResult> GetImage(int id) |
|
|
|
{ |
|
|
|
var webRoot = _env.WebRootPath; |
|
|
|
var path = Path.Combine(webRoot, id + ".png"); |
|
|
|
if (id <= 0) |
|
|
|
{ |
|
|
|
return BadRequest(); |
|
|
|
} |
|
|
|
|
|
|
|
var item = await _catalogContext.CatalogItems |
|
|
|
.SingleOrDefaultAsync(ci => ci.Id == id); |
|
|
|
|
|
|
|
if (item != null) |
|
|
|
{ |
|
|
|
var webRoot = _env.WebRootPath; |
|
|
|
var path = Path.Combine(webRoot, item.PictureFileName); |
|
|
|
|
|
|
|
var buffer = System.IO.File.ReadAllBytes(path); |
|
|
|
|
|
|
|
return File(buffer, "image/png"); |
|
|
|
} |
|
|
|
|
|
|
|
var buffer = System.IO.File.ReadAllBytes(path); |
|
|
|
|
|
|
|
return File(buffer, "image/png"); |
|
|
|
return NotFound(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |