diff --git a/.gitignore b/.gitignore index 1122c14..703c4b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,7 @@ .vs/ bin/ obj/ -wwwroot/images/ -cartracker.db -data/cartracker.db -wwwroot/documents/ -wwwroot/temp/ -wwwroot/imports/ -wwwroot/translations/ +data/ config/userConfig.json CarCareTracker.csproj.user Properties/launchSettings.json -data/cartracker-log.db -data/widgets.html diff --git a/Controllers/FilesController.cs b/Controllers/FilesController.cs index 3780bd0..4c2f491 100644 --- a/Controllers/FilesController.cs +++ b/Controllers/FilesController.cs @@ -81,7 +81,7 @@ namespace CarCareTracker.Controllers private string UploadFile(IFormFile fileToUpload) { string uploadDirectory = "temp/"; - string uploadPath = Path.Combine(_webEnv.WebRootPath, uploadDirectory); + string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", uploadDirectory); if (!Directory.Exists(uploadPath)) Directory.CreateDirectory(uploadPath); string fileName = Guid.NewGuid() + Path.GetExtension(fileToUpload.FileName); @@ -95,7 +95,7 @@ namespace CarCareTracker.Controllers public IActionResult UploadCoordinates(List coordinates) { string uploadDirectory = "temp/"; - string uploadPath = Path.Combine(_webEnv.WebRootPath, uploadDirectory); + string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", uploadDirectory); if (!Directory.Exists(uploadPath)) Directory.CreateDirectory(uploadPath); string fileName = Guid.NewGuid() + ".csv"; diff --git a/Controllers/Vehicle/ImportController.cs b/Controllers/Vehicle/ImportController.cs index 2f9cc1e..03fbb2a 100644 --- a/Controllers/Vehicle/ImportController.cs +++ b/Controllers/Vehicle/ImportController.cs @@ -25,7 +25,7 @@ namespace CarCareTracker.Controllers return Json(false); } string uploadDirectory = "temp/"; - string uploadPath = Path.Combine(_webEnv.WebRootPath, uploadDirectory); + string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", uploadDirectory); if (!Directory.Exists(uploadPath)) Directory.CreateDirectory(uploadPath); var fileNameToExport = $"temp/{Guid.NewGuid()}.csv"; diff --git a/Helper/FileHelper.cs b/Helper/FileHelper.cs index e62aaa2..02d5d82 100644 --- a/Helper/FileHelper.cs +++ b/Helper/FileHelper.cs @@ -34,7 +34,7 @@ namespace CarCareTracker.Helper } public List GetLanguages() { - var languagePath = Path.Combine(_webEnv.WebRootPath, "translations"); + var languagePath = Path.Combine(_webEnv.ContentRootPath, "data", "translations"); var defaultList = new List() { "en_US" }; if (Directory.Exists(languagePath)) { @@ -72,7 +72,7 @@ namespace CarCareTracker.Helper { currentFilePath = currentFilePath.Substring(1); } - string oldFilePath = Path.Combine(_webEnv.WebRootPath, currentFilePath); + string oldFilePath = currentFilePath.StartsWith("defaults/") ? Path.Combine(_webEnv.WebRootPath, currentFilePath) : Path.Combine(_webEnv.ContentRootPath, "data", currentFilePath); if (File.Exists(oldFilePath)) { return oldFilePath; @@ -94,7 +94,7 @@ namespace CarCareTracker.Helper } try { - var tempPath = Path.Combine(_webEnv.WebRootPath, $"temp/{Guid.NewGuid()}"); + var tempPath = Path.Combine(_webEnv.ContentRootPath, "data", $"temp/{Guid.NewGuid()}"); if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); //extract zip file @@ -108,7 +108,7 @@ namespace CarCareTracker.Helper var configPath = Path.Combine(tempPath, StaticHelper.UserConfigPath); if (Directory.Exists(imagePath)) { - var existingPath = Path.Combine(_webEnv.WebRootPath, "images"); + var existingPath = Path.Combine(_webEnv.ContentRootPath, "data", "images"); if (!Directory.Exists(existingPath)) { Directory.CreateDirectory(existingPath); @@ -130,7 +130,7 @@ namespace CarCareTracker.Helper } if (Directory.Exists(documentPath)) { - var existingPath = Path.Combine(_webEnv.WebRootPath, "documents"); + var existingPath = Path.Combine(_webEnv.ContentRootPath, "data", "documents"); if (!Directory.Exists(existingPath)) { Directory.CreateDirectory(existingPath); @@ -152,7 +152,7 @@ namespace CarCareTracker.Helper } if (Directory.Exists(translationPath)) { - var existingPath = Path.Combine(_webEnv.WebRootPath, "translations"); + var existingPath = Path.Combine(_webEnv.ContentRootPath, "data", "translations"); if (!Directory.Exists(existingPath)) { Directory.CreateDirectory(existingPath); @@ -203,7 +203,7 @@ namespace CarCareTracker.Helper public string MakeAttachmentsExport(List exportData) { var folderName = Guid.NewGuid(); - var tempPath = Path.Combine(_webEnv.WebRootPath, $"temp/{folderName}"); + var tempPath = Path.Combine(_webEnv.ContentRootPath, "data", $"temp/{folderName}"); if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); int fileIndex = 0; @@ -227,10 +227,10 @@ namespace CarCareTracker.Helper public string MakeBackup() { var folderName = $"db_backup_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}"; - var tempPath = Path.Combine(_webEnv.WebRootPath, $"temp/{folderName}"); - var imagePath = Path.Combine(_webEnv.WebRootPath, "images"); - var documentPath = Path.Combine(_webEnv.WebRootPath, "documents"); - var translationPath = Path.Combine(_webEnv.WebRootPath, "translations"); + var tempPath = Path.Combine(_webEnv.ContentRootPath, "data", $"temp/{folderName}"); + var imagePath = Path.Combine(_webEnv.ContentRootPath, "data", "images"); + var documentPath = Path.Combine(_webEnv.ContentRootPath, "data", "documents"); + var translationPath = Path.Combine(_webEnv.ContentRootPath, "data", "translations"); var dataPath = StaticHelper.DbName; var widgetPath = StaticHelper.AdditionalWidgetsPath; var configPath = StaticHelper.UserConfigPath; @@ -301,8 +301,8 @@ namespace CarCareTracker.Helper { currentFilePath = currentFilePath.Substring(1); } - string uploadPath = Path.Combine(_webEnv.WebRootPath, newFolder); - string oldFilePath = Path.Combine(_webEnv.WebRootPath, currentFilePath); + string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", newFolder); + string oldFilePath = Path.Combine(_webEnv.ContentRootPath, "data", currentFilePath); if (!Directory.Exists(uploadPath)) Directory.CreateDirectory(uploadPath); string newFileUploadPath = oldFilePath.Replace(tempPath, newFolder); @@ -319,7 +319,7 @@ namespace CarCareTracker.Helper { currentFilePath = currentFilePath.Substring(1); } - string filePath = Path.Combine(_webEnv.WebRootPath, currentFilePath); + string filePath = Path.Combine(_webEnv.ContentRootPath, "data", currentFilePath); if (File.Exists(filePath)) { File.Delete(filePath); diff --git a/Helper/StaticHelper.cs b/Helper/StaticHelper.cs index 5749e7a..8041553 100644 --- a/Helper/StaticHelper.cs +++ b/Helper/StaticHelper.cs @@ -11,7 +11,7 @@ namespace CarCareTracker.Helper /// public static class StaticHelper { - public const string VersionNumber = "1.4.2"; + public const string VersionNumber = "1.4.3"; public const string DbName = "data/cartracker.db"; public const string UserConfigPath = "config/userConfig.json"; public const string AdditionalWidgetsPath = "data/widgets.html"; @@ -241,7 +241,8 @@ namespace CarCareTracker.Helper public static List AddExtraFields(List recordExtraFields, List templateExtraFields) { - if (!templateExtraFields.Any()) { + if (!templateExtraFields.Any()) + { return new List(); } if (!recordExtraFields.Any()) @@ -262,7 +263,7 @@ namespace CarCareTracker.Helper extraField.IsRequired = templateExtraFields.Where(x => x.Name == extraField.Name).First().IsRequired; } //append extra fields - foreach(ExtraField extraField in templateExtraFields) + foreach (ExtraField extraField in templateExtraFields) { if (!recordFieldNames.Contains(extraField.Name)) { @@ -310,7 +311,8 @@ namespace CarCareTracker.Helper if (mailConfig != null && !string.IsNullOrWhiteSpace(mailConfig.EmailServer)) { Console.WriteLine($"SMTP Configured for {mailConfig.EmailServer}"); - } else + } + else { Console.WriteLine("SMTP Not Configured"); } @@ -320,6 +322,82 @@ namespace CarCareTracker.Helper { Console.WriteLine("No Locale or Culture Configured for LubeLogger, Check Environment Variables"); } + //Create folders if they don't exist. + if (!Directory.Exists("data")) + { + Console.WriteLine("Created data directory"); + Directory.CreateDirectory("data"); + } + if (!Directory.Exists("data/images")) + { + Console.WriteLine("Created images directory"); + Directory.CreateDirectory("data/images"); + } + if (!Directory.Exists("data/documents")) + { + Console.WriteLine("Created documents directory"); + Directory.CreateDirectory("data/documents"); + } + if (!Directory.Exists("data/translations")) + { + Console.WriteLine("Created translations directory"); + Directory.CreateDirectory("data/translations"); + } + if (!Directory.Exists("data/temp")) + { + Console.WriteLine("Created translations directory"); + Directory.CreateDirectory("data/temp"); + } + if (!Directory.Exists("config")) + { + Directory.CreateDirectory("config"); + Console.WriteLine("Created config directory"); + } + } + public static void CheckMigration(string webRootPath) + { + //migrates all user-uploaded files from webroot to new data folder + //images + var imagePath = Path.Combine(webRootPath, "images"); + var docsPath = Path.Combine(webRootPath, "documents"); + var translationPath = Path.Combine(webRootPath, "translations"); + var tempPath = Path.Combine(webRootPath, "temp"); + if (Directory.Exists(imagePath)) + { + foreach (string fileToMove in Directory.GetFiles(imagePath)) + { + var newFilePath = $"data/images/{Path.GetFileName(fileToMove)}"; + File.Move(fileToMove, newFilePath, true); + Console.WriteLine($"Migrated Image: {Path.GetFileName(fileToMove)}"); + } + } + if (Directory.Exists(docsPath)) + { + foreach (string fileToMove in Directory.GetFiles(docsPath)) + { + var newFilePath = $"data/documents/{Path.GetFileName(fileToMove)}"; + File.Move(fileToMove, newFilePath, true); + Console.WriteLine($"Migrated Document: {Path.GetFileName(fileToMove)}"); + } + } + if (Directory.Exists(translationPath)) + { + foreach (string fileToMove in Directory.GetFiles(translationPath)) + { + var newFilePath = $"data/translations/{Path.GetFileName(fileToMove)}"; + File.Move(fileToMove, newFilePath, true); + Console.WriteLine($"Migrated Translation: {Path.GetFileName(fileToMove)}"); + } + } + if (Directory.Exists(tempPath)) + { + foreach (string fileToMove in Directory.GetFiles(tempPath)) + { + var newFilePath = $"data/temp/{Path.GetFileName(fileToMove)}"; + File.Move(fileToMove, newFilePath, true); + Console.WriteLine($"Migrated Temp File: {Path.GetFileName(fileToMove)}"); + } + } } public static async void NotifyAsync(string webhookURL, WebHookPayload webHookPayload) { @@ -333,7 +411,8 @@ namespace CarCareTracker.Helper webhookURL = webhookURL.Replace("discord://", "https://"); //cleanurl //format to discord httpClient.PostAsJsonAsync(webhookURL, DiscordWebHook.FromWebHookPayload(webHookPayload)); - } else + } + else { httpClient.PostAsJsonAsync(webhookURL, webHookPayload); } @@ -371,12 +450,14 @@ namespace CarCareTracker.Helper if (vehicle.VehicleIdentifier == "LicensePlate") { return vehicle.LicensePlate; - } else + } + else { - if (vehicle.ExtraFields.Any(x=>x.Name == vehicle.VehicleIdentifier)) + if (vehicle.ExtraFields.Any(x => x.Name == vehicle.VehicleIdentifier)) { - return vehicle.ExtraFields?.FirstOrDefault(x=>x.Name == vehicle.VehicleIdentifier)?.Value; - } else + return vehicle.ExtraFields?.FirstOrDefault(x => x.Name == vehicle.VehicleIdentifier)?.Value; + } + else { return "N/A"; } @@ -403,10 +484,11 @@ namespace CarCareTracker.Helper //Translations public static string GetTranslationDownloadPath(string continent, string name) { - if (string.IsNullOrWhiteSpace(continent) || string.IsNullOrWhiteSpace(name)){ + if (string.IsNullOrWhiteSpace(continent) || string.IsNullOrWhiteSpace(name)) + { return string.Empty; - } - else + } + else { switch (continent) { @@ -425,8 +507,9 @@ namespace CarCareTracker.Helper if (string.IsNullOrWhiteSpace(name)) { return string.Empty; - } else - { + } + else + { try { string cleanedName = name.Contains("_") ? name.Replace("_", "-") : name; @@ -439,7 +522,8 @@ namespace CarCareTracker.Helper { return displayName; } - } catch (Exception ex) + } + catch (Exception ex) { return name; } @@ -610,7 +694,8 @@ namespace CarCareTracker.Helper if (input == 0M.ToString("C2") && hideZero) { return "---"; - } else + } + else { return string.IsNullOrWhiteSpace(decorations) ? input : $"{input}{decorations}"; } diff --git a/Program.cs b/Program.cs index fb3d840..ce9346e 100644 --- a/Program.cs +++ b/Program.cs @@ -7,11 +7,14 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.Extensions.FileProviders; var builder = WebApplication.CreateBuilder(args); //Print Messages StaticHelper.InitMessage(builder.Configuration); +//Check Migration +StaticHelper.CheckMigration(builder.Environment.WebRootPath); // Add services to the container. builder.Services.AddControllersWithViews(); @@ -75,15 +78,6 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); -if (!Directory.Exists("data")) -{ - Directory.CreateDirectory("data"); -} -if (!Directory.Exists("config")) -{ - Directory.CreateDirectory("config"); -} - //Additional JsonFile builder.Configuration.AddJsonFile(StaticHelper.UserConfigPath, optional: true, reloadOnChange: true); @@ -126,6 +120,30 @@ app.UseStaticFiles(new StaticFileOptions } } }); +app.UseStaticFiles(new StaticFileOptions +{ + FileProvider = new PhysicalFileProvider( + Path.Combine(builder.Environment.ContentRootPath, "data", "images")), + RequestPath = "/images" +}); +app.UseStaticFiles(new StaticFileOptions +{ + FileProvider = new PhysicalFileProvider( + Path.Combine(builder.Environment.ContentRootPath, "data", "documents")), + RequestPath = "/documents" +}); +app.UseStaticFiles(new StaticFileOptions +{ + FileProvider = new PhysicalFileProvider( + Path.Combine(builder.Environment.ContentRootPath, "data", "translations")), + RequestPath = "/translations" +}); +app.UseStaticFiles(new StaticFileOptions +{ + FileProvider = new PhysicalFileProvider( + Path.Combine(builder.Environment.ContentRootPath, "data", "temp")), + RequestPath = "/temp" +}); app.UseRouting();