Browse Source

Add AzureStorageEnabled environment variable to get the origin of the product image from PicBaseUrl environment variable

pull/235/head
Christian Arenas 7 years ago
parent
commit
1a385a2179
4 changed files with 40 additions and 14 deletions
  1. +3
    -1
      src/Services/Catalog/Catalog.API/CatalogSettings.cs
  2. +8
    -5
      src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs
  3. +27
    -7
      src/Services/Catalog/Catalog.API/Controllers/PicController.cs
  4. +2
    -1
      src/Services/Catalog/Catalog.API/settings.json

+ 3
- 1
src/Services/Catalog/Catalog.API/CatalogSettings.cs View File

@ -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; }
}
}

+ 8
- 5
src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs View File

@ -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<CatalogItem> ChangeUriPlaceholder(List<CatalogItem> 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;


+ 27
- 7
src/Services/Catalog/Catalog.API/Controllers/PicController.cs View File

@ -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();
}
}
}

+ 2
- 1
src/Services/Catalog/Catalog.API/settings.json View File

@ -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"
}

Loading…
Cancel
Save