diff --git a/src/Services/Catalog/Catalog.API/CatalogSettings.cs b/src/Services/Catalog/Catalog.API/CatalogSettings.cs index 62e92c6ce..03bb584f3 100644 --- a/src/Services/Catalog/Catalog.API/CatalogSettings.cs +++ b/src/Services/Catalog/Catalog.API/CatalogSettings.cs @@ -2,10 +2,12 @@ { public class CatalogSettings { - public string ExternalCatalogBaseUrl {get;set;} + public string PicBaseUrl { get;set;} public string EventBusConnection { get; set; } public string ServiceBusConnectionString { get; set; } + + public bool AzureStorageEnabled { get; set; } } } diff --git a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs index 374d8ec7c..ca1ef9e3b 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs @@ -200,7 +200,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers CatalogTypeId = product.CatalogTypeId, Description = product.Description, Name = product.Name, - PictureUri = product.PictureUri, + PictureFileName = product.PictureFileName, Price = product.Price }; _catalogContext.CatalogItems.Add(item); @@ -231,11 +231,14 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers private List ChangeUriPlaceholder(List items) { - var baseUri = _settings.ExternalCatalogBaseUrl; - - items.ForEach(x => + var baseUri = _settings.PicBaseUrl; + + items.ForEach(catalogItem => { - x.PictureUri = x.PictureUri.Replace("http://externalcatalogbaseurltobereplaced", baseUri); + catalogItem.PictureUri = _settings.AzureStorageEnabled + ? baseUri + catalogItem.PictureFileName + : baseUri + catalogItem.Id; + }); return items; diff --git a/src/Services/Catalog/Catalog.API/Controllers/PicController.cs b/src/Services/Catalog/Catalog.API/Controllers/PicController.cs index 8d8aaf9f2..a2106bead 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/PicController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/PicController.cs @@ -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: // - public IActionResult GetImage(int id) + public async Task 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(); } } } diff --git a/src/Services/Catalog/Catalog.API/settings.json b/src/Services/Catalog/Catalog.API/settings.json index 3cfe64f5e..b431cf8f0 100644 --- a/src/Services/Catalog/Catalog.API/settings.json +++ b/src/Services/Catalog/Catalog.API/settings.json @@ -1,6 +1,6 @@ { "ConnectionString": "Server=tcp:127.0.0.1,5433;Initial Catalog=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word", - "ExternalCatalogBaseUrl": "http://localhost:5101", + "PicBaseUrl": "http://localhost:5101", "Logging": { "IncludeScopes": false, "LogLevel": { @@ -11,5 +11,6 @@ }, "ServiceBusConnectionString": "Endpoint=sb://eshopsbez55a72p6wm62.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=oA6WpfCfCbScZbQa/DBOLfwl6oi5ezPsCYL7QsTb4PY=;EntityPath=eshop_event_bus", "AzureServiceBusEnabled": "true", + "AzureStorageEnabled": false, "SubscriptionClientName": "Catalog" }