From caf31b44b88ecbd26934edcb092e140b7e46f378 Mon Sep 17 00:00:00 2001 From: Musa Ahmed Date: Mon, 2 Dec 2024 14:39:48 -0500 Subject: [PATCH] - added .env file & made the projects use it - using tailwindcli now --- WardrobeManager.Api/Program.cs | 5 +- .../Implementation/DataDirectoryService.cs | 2 +- .../Services/Implementation/FileService.cs | 38 +- .../appsettings.Development.json | 6 +- .../appsettings.Production.json | 12 - .../Components/Clothing/ItemSummary.razor | 2 +- .../Clothing/NewOrEditItemSection.razor | 9 +- .../Components/Shared/Image.razor | 2 +- .../Components/Shared/Modal.razor | 4 +- WardrobeManager.Presentation/Pages/Test.razor | 2 +- WardrobeManager.Presentation/Program.cs | 2 +- .../package-lock.json | 1431 ++++++++ WardrobeManager.Presentation/package.json | 6 + .../tailwind.config.js | 16 + WardrobeManager.Presentation/tailwind.css | 3 + .../wwwroot/appsettings.Development.json | 2 +- .../wwwroot/appsettings.Production.json | 3 - .../wwwroot/css/tailwind.css | 3077 +++++++++++++++++ .../wwwroot/index.html | 6 +- .../Misc/ProjectConstants.cs | 5 +- docker-compose.dev.yml | 8 +- docker-compose.yml | 10 +- .../WardrobeManager.Presentation/Dockerfile | 1 + .../appsettings.Production.json | 3 - .../update-appsettings.sh | 15 +- docker/config.env | 21 + 26 files changed, 4629 insertions(+), 62 deletions(-) delete mode 100644 WardrobeManager.Api/appsettings.Production.json create mode 100644 WardrobeManager.Presentation/package-lock.json create mode 100644 WardrobeManager.Presentation/package.json create mode 100644 WardrobeManager.Presentation/tailwind.config.js create mode 100644 WardrobeManager.Presentation/tailwind.css delete mode 100644 WardrobeManager.Presentation/wwwroot/appsettings.Production.json create mode 100644 WardrobeManager.Presentation/wwwroot/css/tailwind.css delete mode 100644 docker/WardrobeManager.Presentation/appsettings.Production.json create mode 100644 docker/config.env diff --git a/WardrobeManager.Api/Program.cs b/WardrobeManager.Api/Program.cs index ff76ff7..691137c 100644 --- a/WardrobeManager.Api/Program.cs +++ b/WardrobeManager.Api/Program.cs @@ -23,8 +23,8 @@ // Configuration is setup by default to read from (in order of precedence) Environment Variables, appsettings.json // https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0 -string BackendUrl = builder.Configuration["BackendUrl"] ?? throw new Exception("BackendUrl: configuration value not set"); -string FrontendUrl = builder.Configuration["FrontendUrl"] ?? throw new Exception("Frontend: configuration value not set"); +string BackendUrl = builder.Configuration["WM_BACKEND_URL"] ?? throw new Exception("BackendUrl: configuration value not set"); +string FrontendUrl = builder.Configuration["WM_FRONTEND_URL"] ?? throw new Exception("Frontend: configuration value not set"); builder.Services.AddAuthentication(IdentityConstants.ApplicationScheme).AddIdentityCookies(); // Configure app cookie (THIS ALLOWS THE BACKEND AND FRONTEND RUN ON DIFFERENT DOMAINS) @@ -101,6 +101,7 @@ app.UseSwaggerUI(); } +// Adds a bunch of auto generated Identity-related routes (/register, /login, etc) app.MapIdentityApi(); app.UseCors("wasm"); diff --git a/WardrobeManager.Api/Services/Implementation/DataDirectoryService.cs b/WardrobeManager.Api/Services/Implementation/DataDirectoryService.cs index 3f82390..a8081b8 100644 --- a/WardrobeManager.Api/Services/Implementation/DataDirectoryService.cs +++ b/WardrobeManager.Api/Services/Implementation/DataDirectoryService.cs @@ -13,7 +13,7 @@ public class DataDirectoryService : IDataDirectoryService public DataDirectoryService(IConfiguration config, IWebHostEnvironment webHostEnvironment) { - string DataDirectory = config["DataDirectory"] ?? throw new Exception("Data Directory: configuration value not set"); + string DataDirectory = config["WM_DATA_DIRECTORY"] ?? throw new Exception("Data Directory: configuration value not set"); _baseDirectory = webHostEnvironment.IsDevelopment() ? Path.GetFullPath(Path.Combine(webHostEnvironment.ContentRootPath, DataDirectory)) : DataDirectory; } diff --git a/WardrobeManager.Api/Services/Implementation/FileService.cs b/WardrobeManager.Api/Services/Implementation/FileService.cs index 08f5a2c..916269d 100644 --- a/WardrobeManager.Api/Services/Implementation/FileService.cs +++ b/WardrobeManager.Api/Services/Implementation/FileService.cs @@ -2,17 +2,12 @@ namespace WardrobeManager.Api.Services.Implementation; -public class FileService : IFileService +public class FileService( + IDataDirectoryService dataDirectoryService, + IWebHostEnvironment webHostEnvironment, + IConfiguration configuration) + : IFileService { - private readonly IDataDirectoryService _dataDirectoryService; - private readonly IWebHostEnvironment _webHostEnvironment; - - public FileService(IDataDirectoryService dataDirectoryService, IWebHostEnvironment webHostEnvironment) - { - _dataDirectoryService = dataDirectoryService; - _webHostEnvironment = webHostEnvironment; - } - public string ParseGuid(Guid guid) { return guid.ToString().Replace("{", "").Replace("}", ""); @@ -31,26 +26,37 @@ public async Task SaveImage(Guid? guid, string ImageBase64) byte[] imageBytes = Convert.FromBase64String(ImageBase64); - string path = Path.Combine(_dataDirectoryService.GetUploadsDirectory(), ParseGuid(properGuid)); + // 5MB default max file size + var max_file_size = configuration["WM_MAX_IMAGE_UPLOAD_SIZE_IN_MB"] ?? "5"; + int max_file_size_num = Convert.ToInt32(max_file_size); + max_file_size_num *= 1024; + + if (imageBytes.Length > max_file_size_num) + { + throw new Exception( + $"File size too large! Received file size: {imageBytes.Length / 1024} MB. Max file size: {max_file_size_num / 1024} MB"); + } + + string path = Path.Combine(dataDirectoryService.GetUploadsDirectory(), ParseGuid(properGuid)); await File.WriteAllBytesAsync(path, imageBytes); } public async Task GetImage(string guid) { - string path = Path.Combine(_dataDirectoryService.GetUploadsDirectory(), guid); - string notFound = Path.Combine(_webHostEnvironment.WebRootPath, "images", "notfound.jpg"); + string path = Path.Combine(dataDirectoryService.GetUploadsDirectory(), guid); + string notFound = Path.Combine(webHostEnvironment.WebRootPath, "images", "notfound.jpg"); if (File.Exists(path)) { - byte[] imageBytes = await File.ReadAllBytesAsync(path); // 6. Serve the file + byte[] imageBytes = await File.ReadAllBytesAsync(path); // 6. Serve the file return imageBytes; } else { - byte[] imageBytes = await File.ReadAllBytesAsync(notFound); // 6. Serve the file + byte[] imageBytes = await File.ReadAllBytesAsync(notFound); // 6. Serve the file return imageBytes; } } -} +} \ No newline at end of file diff --git a/WardrobeManager.Api/appsettings.Development.json b/WardrobeManager.Api/appsettings.Development.json index 14eff59..2abf572 100644 --- a/WardrobeManager.Api/appsettings.Development.json +++ b/WardrobeManager.Api/appsettings.Development.json @@ -6,7 +6,7 @@ } }, - "BackendUrl": "https://localhost:7026", - "FrontendUrl": "https://localhost:7147", - "DataDirectory": "../data" + "WM_BACKEND_URL": "https://localhost:7026", + "WM_FRONTEND_URL": "https://localhost:7147", + "WM_DATA_DIRECTORY": "../data" } diff --git a/WardrobeManager.Api/appsettings.Production.json b/WardrobeManager.Api/appsettings.Production.json deleted file mode 100644 index 0ce0c5a..0000000 --- a/WardrobeManager.Api/appsettings.Production.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Information" - } - }, - - "BackendUrl": "https://localhost:7026", - "FrontendUrl": "https://localhost:7147", - "DataDirectory": "/data" -} diff --git a/WardrobeManager.Presentation/Components/Clothing/ItemSummary.razor b/WardrobeManager.Presentation/Components/Clothing/ItemSummary.razor index eb38070..2211908 100644 --- a/WardrobeManager.Presentation/Components/Clothing/ItemSummary.razor +++ b/WardrobeManager.Presentation/Components/Clothing/ItemSummary.razor @@ -47,7 +47,7 @@ @* Modal that pops up when user clicks 'edit' *@ - + diff --git a/WardrobeManager.Presentation/Components/Clothing/NewOrEditItemSection.razor b/WardrobeManager.Presentation/Components/Clothing/NewOrEditItemSection.razor index 10a622a..50f712e 100644 --- a/WardrobeManager.Presentation/Components/Clothing/NewOrEditItemSection.razor +++ b/WardrobeManager.Presentation/Components/Clothing/NewOrEditItemSection.razor @@ -121,8 +121,13 @@ try { img = new MemoryStream(); - // 5MB max file size - await e.File.OpenReadStream(maxAllowedSize: 1024 * 5000).CopyToAsync(img); + + // 5MB default max file size + var max_file_size = _config["WM_MAX_IMAGE_UPLOAD_SIZE_IN_MB"] ?? "5"; + int max_file_size_num = Convert.ToInt32(max_file_size); + max_file_size_num *= 1024; + + await e.File.OpenReadStream(maxAllowedSize: max_file_size_num).CopyToAsync(img); newItem.ImageBase64 = Convert.ToBase64String(img.ToArray()); diff --git a/WardrobeManager.Presentation/Components/Shared/Image.razor b/WardrobeManager.Presentation/Components/Shared/Image.razor index 852c9a1..337f93b 100644 --- a/WardrobeManager.Presentation/Components/Shared/Image.razor +++ b/WardrobeManager.Presentation/Components/Shared/Image.razor @@ -1,6 +1,6 @@ @namespace WardrobeManager.Presentation.Components.Shared - + @code { [Parameter] public required Guid? ImageGuid { get; set; } diff --git a/WardrobeManager.Presentation/Components/Shared/Modal.razor b/WardrobeManager.Presentation/Components/Shared/Modal.razor index 98121f1..fe4125b 100644 --- a/WardrobeManager.Presentation/Components/Shared/Modal.razor +++ b/WardrobeManager.Presentation/Components/Shared/Modal.razor @@ -5,7 +5,7 @@ @* !! When using this component make sure you put onclick="id.showModal()" in a button or something !! *@ -