diff --git a/src/Services/Catalog/Catalog.API/Controllers/PicController.cs b/src/Services/Catalog/Catalog.API/Controllers/PicController.cs index 2114f21e4..75029dc95 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/PicController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/PicController.cs @@ -22,19 +22,32 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers _catalogContext = catalogContext; } - [HttpGet("{filename}")] + [HttpGet("{id}")] // GET: // - public IActionResult GetImage(string filename) + public async Task GetImage(int id) { - var webRoot = _env.WebRootPath; - var path = Path.Combine(webRoot, filename); + if (id <= 0) + { + return BadRequest(); + } - string imageFileExtension = Path.GetExtension(filename); - string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension); + 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); + string imageFileExtension = Path.GetExtension(item.PictureFileName); + string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension); + + var buffer = System.IO.File.ReadAllBytes(path); + + return File(buffer, mimetype); + } - return File(buffer, mimetype); + return NotFound(); } private string GetImageMimeTypeFromImageFileExtension(string extension) @@ -43,29 +56,29 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers switch (extension) { - case "png": + case ".png": mimetype = "image/png"; break; - case "gif": + case ".gif": mimetype = "image/gif"; break; - case "jpg": - case "jpeg": + case ".jpg": + case ".jpeg": mimetype = "image/jpeg"; break; - case "bmp": + case ".bmp": mimetype = "image/bmp"; break; - case "tiff": + case ".tiff": mimetype = "image/tiff"; break; - case "wmf": + case ".wmf": mimetype = "image/wmf"; break; - case "jp2": + case ".jp2": mimetype = "image/jp2"; break; - case "svg": + case ".svg": mimetype = "image/svg+xml"; break; default: diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs index c83696293..e7dbe5452 100644 --- a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs +++ b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs @@ -189,7 +189,7 @@ string[] csvheaders; try { - string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureuri" }; + string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureFileName" }; string[] optionalheaders = { "availablestock", "restockthreshold", "maxstockthreshold", "onreorder" }; csvheaders = GetHeaders(csvFileCatalogItems, requiredHeaders, optionalheaders ); } @@ -320,18 +320,18 @@ { return new List() { - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureUri = "1.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "2.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "3.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureUri = "4.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "5.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureUri = "6.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "7.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureUri = "8.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup White Mug", Name = "Cup White Mug", Price = 12, PictureUri = "9.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "10.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup Sheet", Name = "Cup Sheet", Price = 8.5M, PictureUri = "11.png", AvailableStock = 100 }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "12.png", AvailableStock = 100 } + new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureFileName = "1.png" }, + new CatalogItem { CatalogTypeId = 1, CatalogBrandId = 2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureFileName = "2.png" }, + new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureFileName = "3.png" }, + new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureFileName = "4.png" }, + new CatalogItem { CatalogTypeId = 3, CatalogBrandId = 5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureFileName = "5.png" }, + new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureFileName = "6.png" }, + new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureFileName = "7.png" }, + new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureFileName = "8.png" }, + new CatalogItem { CatalogTypeId = 1, CatalogBrandId = 5, Description = "Cup White Mug", Name = "Cup White Mug", Price = 12, PictureFileName = "9.png" }, + new CatalogItem { CatalogTypeId = 3, CatalogBrandId = 2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureFileName = "10.png" }, + new CatalogItem { CatalogTypeId = 3, CatalogBrandId = 2, Description = "Cup Sheet", Name = "Cup Sheet", Price = 8.5M, PictureFileName = "11.png" }, + new CatalogItem { CatalogTypeId = 2, CatalogBrandId = 5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureFileName = "12.png" }, }; } diff --git a/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv b/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv index da0cd297f..0257216d4 100644 --- a/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv +++ b/src/Services/Catalog/Catalog.API/Setup/CatalogItems.csv @@ -1,4 +1,4 @@ -CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureUri,availablestock,onreorder +CatalogTypeName,CatalogBrandName,Description,Name,Price,PictureFileName,availablestock,onreorder T-Shirt,.NET,".NET Bot Black Hoodie, and more",.NET Bot Black Hoodie,19.5,1.png,100,false Mug,.NET,.NET Black & White Mug,.NET Black & White Mug,8.50,2.png,89,true T-Shirt,Other,Prism White T-Shirt,Prism White T-Shirt,12,3.png,56,false