diff --git a/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor b/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor index 07f2b71..995fe49 100644 --- a/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor +++ b/src/BlazorAdmin/Pages/CatalogItemPage/Create.razor @@ -82,7 +82,7 @@ -
+ @*
@@ -96,7 +96,7 @@
@_badFileMessage
-
+
*@ diff --git a/src/BlazorAdmin/Pages/CatalogItemPage/Edit.razor b/src/BlazorAdmin/Pages/CatalogItemPage/Edit.razor index 415765f..cce6a52 100644 --- a/src/BlazorAdmin/Pages/CatalogItemPage/Edit.razor +++ b/src/BlazorAdmin/Pages/CatalogItemPage/Edit.razor @@ -85,7 +85,7 @@ -
+ @*
@@ -99,7 +99,7 @@
@_badFileMessage
-
+
*@ diff --git a/src/Infrastructure/Services/WebFileSystem.cs b/src/Infrastructure/Services/WebFileSystem.cs index b51ba54..f670079 100644 --- a/src/Infrastructure/Services/WebFileSystem.cs +++ b/src/Infrastructure/Services/WebFileSystem.cs @@ -10,11 +10,13 @@ using System.Threading.Tasks; namespace Microsoft.eShopWeb.Infrastructure.Services { + // This class never gets called. Modify it based on your need. + public class WebFileSystem : IFileSystem { private readonly HttpClient _httpClient; private readonly string _url; - public const string AUTH_KEY = "AuthKeyOfDoomThatMustBeAMinimumNumberOfBytes"; + public const string AUTH_KEY = ""; public WebFileSystem(string url) { @@ -50,13 +52,11 @@ namespace Microsoft.eShopWeb.Infrastructure.Services DataBase64 = Convert.ToBase64String(fileData), FileName = fileName }; + var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json"); - using var message = await _httpClient.PostAsync(_url, content, cancellationToken); - if (!message.IsSuccessStatusCode) - { - return false; - } + // TODO: Write the actual File image upload logic to web. + // Post this image binary content to an Image Upload API. return true; } diff --git a/src/PublicApi/CatalogItemEndpoints/Create.cs b/src/PublicApi/CatalogItemEndpoints/Create.cs index ef35707..a4f018f 100644 --- a/src/PublicApi/CatalogItemEndpoints/Create.cs +++ b/src/PublicApi/CatalogItemEndpoints/Create.cs @@ -19,13 +19,11 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints { private readonly IAsyncRepository _itemRepository; private readonly IUriComposer _uriComposer; - private readonly IFileSystem _webFileSystem; - public Create(IAsyncRepository itemRepository, IUriComposer uriComposer, IFileSystem webFileSystem) + public Create(IAsyncRepository itemRepository, IUriComposer uriComposer) { _itemRepository = itemRepository; _uriComposer = uriComposer; - _webFileSystem = webFileSystem; } [HttpPost("api/catalog-items")] @@ -45,12 +43,15 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints if (newItem.Id != 0) { - var picName = $"{newItem.Id}{Path.GetExtension(request.PictureName)}"; - if (await _webFileSystem.SavePicture(picName, request.PictureBase64, cancellationToken)) - { - newItem.UpdatePictureUri(picName); - await _itemRepository.UpdateAsync(newItem, cancellationToken); - } + // At this point time, the Admin application uses the default catalog item image for any new product item. + // But in the actual production scenario, you'll implement the image file upload mechanism in your application and set the image + // file the Uri accordingly. You can refer to fewlines of the boilerplate code are commented out and kept it in the following files. + // - BlazorAdmin project -> Create.razor and Edit.razor. + // - Infrastructure project -> Services/WebFileSystem.cs + + var picName = "eCatalog-item-default.png"; + newItem.UpdatePictureUri(picName); + await _itemRepository.UpdateAsync(newItem, cancellationToken); } var dto = new CatalogItemDto diff --git a/src/PublicApi/CatalogItemEndpoints/Update.cs b/src/PublicApi/CatalogItemEndpoints/Update.cs index 991c72e..97b45a4 100644 --- a/src/PublicApi/CatalogItemEndpoints/Update.cs +++ b/src/PublicApi/CatalogItemEndpoints/Update.cs @@ -17,15 +17,12 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints .WithResponse { private readonly IAsyncRepository _itemRepository; - private readonly IUriComposer _uriComposer; - private readonly IFileSystem _webFileSystem; + private readonly IUriComposer _uriComposer; - public Update(IAsyncRepository itemRepository, IUriComposer uriComposer, IFileSystem webFileSystem) + public Update(IAsyncRepository itemRepository, IUriComposer uriComposer) { _itemRepository = itemRepository; - _uriComposer = uriComposer; - _webFileSystem = webFileSystem; - + _uriComposer = uriComposer; } [HttpPut("api/catalog-items")] @@ -43,20 +40,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints existingItem.UpdateDetails(request.Name, request.Description, request.Price); existingItem.UpdateBrand(request.CatalogBrandId); - existingItem.UpdateType(request.CatalogTypeId); - - if (string.IsNullOrEmpty(request.PictureBase64) && string.IsNullOrEmpty(request.PictureUri)) - { - existingItem.UpdatePictureUri(string.Empty); - } - else - { - var picName = $"{existingItem.Id}{Path.GetExtension(request.PictureName)}"; - if (await _webFileSystem.SavePicture($"{picName}", request.PictureBase64, cancellationToken)) - { - existingItem.UpdatePictureUri(picName); - } - } + existingItem.UpdateType(request.CatalogTypeId); await _itemRepository.UpdateAsync(existingItem, cancellationToken); diff --git a/src/PublicApi/Startup.cs b/src/PublicApi/Startup.cs index e2d4e19..0747537 100644 --- a/src/PublicApi/Startup.cs +++ b/src/PublicApi/Startup.cs @@ -95,8 +95,7 @@ namespace Microsoft.eShopWeb.PublicApi services.AddScoped(); var baseUrlConfig = new BaseUrlConfiguration(); - Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig); - services.AddScoped(x => new WebFileSystem($"{baseUrlConfig.WebBase}File")); + Configuration.Bind(BaseUrlConfiguration.CONFIG_NAME, baseUrlConfig); services.AddMemoryCache(); diff --git a/src/Web/Controllers/FileController.cs b/src/Web/Controllers/FileController.cs deleted file mode 100644 index 1e3b9c3..0000000 --- a/src/Web/Controllers/FileController.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopWeb.Web.ViewModels.File; -using System; -using System.IO; - -namespace Microsoft.eShopWeb.Web.Controllers -{ - [Route("[controller]")] - [ApiController] - public class FileController : ControllerBase - { - [HttpPost] - [AllowAnonymous] - public IActionResult Upload(FileViewModel fileViewModel) - { - if (!Request.Headers.ContainsKey("auth-key") || Request.Headers["auth-key"].ToString() != ApplicationCore.Constants.AuthorizationConstants.AUTH_KEY) - { - return Unauthorized(); - } - - if(fileViewModel == null || string.IsNullOrEmpty(fileViewModel.DataBase64)) return BadRequest(); - - var fileData = Convert.FromBase64String(fileViewModel.DataBase64); - if (fileData.Length <= 0) return BadRequest(); - - var fullPath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot/images/products", fileViewModel.FileName); - if (System.IO.File.Exists(fullPath)) - { - System.IO.File.Delete(fullPath); - } - System.IO.File.WriteAllBytes(fullPath, fileData); - - return Ok(); - } - - } -} \ No newline at end of file diff --git a/src/Web/wwwroot/images/products/eCatalog-item-default.png b/src/Web/wwwroot/images/products/eCatalog-item-default.png new file mode 100644 index 0000000..5079b8b Binary files /dev/null and b/src/Web/wwwroot/images/products/eCatalog-item-default.png differ