move files out of webrootpath.

This commit is contained in:
DESKTOP-T0O5CDB\DESK-555BD
2025-01-08 10:10:41 -07:00
parent e13707305b
commit 2cc471b944
6 changed files with 145 additions and 50 deletions

10
.gitignore vendored
View File

@@ -1,15 +1,7 @@
.vs/ .vs/
bin/ bin/
obj/ obj/
wwwroot/images/ data/
cartracker.db
data/cartracker.db
wwwroot/documents/
wwwroot/temp/
wwwroot/imports/
wwwroot/translations/
config/userConfig.json config/userConfig.json
CarCareTracker.csproj.user CarCareTracker.csproj.user
Properties/launchSettings.json Properties/launchSettings.json
data/cartracker-log.db
data/widgets.html

View File

@@ -81,7 +81,7 @@ namespace CarCareTracker.Controllers
private string UploadFile(IFormFile fileToUpload) private string UploadFile(IFormFile fileToUpload)
{ {
string uploadDirectory = "temp/"; string uploadDirectory = "temp/";
string uploadPath = Path.Combine(_webEnv.WebRootPath, uploadDirectory); string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", uploadDirectory);
if (!Directory.Exists(uploadPath)) if (!Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath); Directory.CreateDirectory(uploadPath);
string fileName = Guid.NewGuid() + Path.GetExtension(fileToUpload.FileName); string fileName = Guid.NewGuid() + Path.GetExtension(fileToUpload.FileName);
@@ -95,7 +95,7 @@ namespace CarCareTracker.Controllers
public IActionResult UploadCoordinates(List<string> coordinates) public IActionResult UploadCoordinates(List<string> coordinates)
{ {
string uploadDirectory = "temp/"; string uploadDirectory = "temp/";
string uploadPath = Path.Combine(_webEnv.WebRootPath, uploadDirectory); string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", uploadDirectory);
if (!Directory.Exists(uploadPath)) if (!Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath); Directory.CreateDirectory(uploadPath);
string fileName = Guid.NewGuid() + ".csv"; string fileName = Guid.NewGuid() + ".csv";

View File

@@ -25,7 +25,7 @@ namespace CarCareTracker.Controllers
return Json(false); return Json(false);
} }
string uploadDirectory = "temp/"; string uploadDirectory = "temp/";
string uploadPath = Path.Combine(_webEnv.WebRootPath, uploadDirectory); string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", uploadDirectory);
if (!Directory.Exists(uploadPath)) if (!Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath); Directory.CreateDirectory(uploadPath);
var fileNameToExport = $"temp/{Guid.NewGuid()}.csv"; var fileNameToExport = $"temp/{Guid.NewGuid()}.csv";

View File

@@ -34,7 +34,7 @@ namespace CarCareTracker.Helper
} }
public List<string> GetLanguages() public List<string> GetLanguages()
{ {
var languagePath = Path.Combine(_webEnv.WebRootPath, "translations"); var languagePath = Path.Combine(_webEnv.ContentRootPath, "data", "translations");
var defaultList = new List<string>() { "en_US" }; var defaultList = new List<string>() { "en_US" };
if (Directory.Exists(languagePath)) if (Directory.Exists(languagePath))
{ {
@@ -72,7 +72,7 @@ namespace CarCareTracker.Helper
{ {
currentFilePath = currentFilePath.Substring(1); 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)) if (File.Exists(oldFilePath))
{ {
return oldFilePath; return oldFilePath;
@@ -94,7 +94,7 @@ namespace CarCareTracker.Helper
} }
try try
{ {
var tempPath = Path.Combine(_webEnv.WebRootPath, $"temp/{Guid.NewGuid()}"); var tempPath = Path.Combine(_webEnv.ContentRootPath, "data", $"temp/{Guid.NewGuid()}");
if (!Directory.Exists(tempPath)) if (!Directory.Exists(tempPath))
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);
//extract zip file //extract zip file
@@ -108,7 +108,7 @@ namespace CarCareTracker.Helper
var configPath = Path.Combine(tempPath, StaticHelper.UserConfigPath); var configPath = Path.Combine(tempPath, StaticHelper.UserConfigPath);
if (Directory.Exists(imagePath)) if (Directory.Exists(imagePath))
{ {
var existingPath = Path.Combine(_webEnv.WebRootPath, "images"); var existingPath = Path.Combine(_webEnv.ContentRootPath, "data", "images");
if (!Directory.Exists(existingPath)) if (!Directory.Exists(existingPath))
{ {
Directory.CreateDirectory(existingPath); Directory.CreateDirectory(existingPath);
@@ -130,7 +130,7 @@ namespace CarCareTracker.Helper
} }
if (Directory.Exists(documentPath)) if (Directory.Exists(documentPath))
{ {
var existingPath = Path.Combine(_webEnv.WebRootPath, "documents"); var existingPath = Path.Combine(_webEnv.ContentRootPath, "data", "documents");
if (!Directory.Exists(existingPath)) if (!Directory.Exists(existingPath))
{ {
Directory.CreateDirectory(existingPath); Directory.CreateDirectory(existingPath);
@@ -152,7 +152,7 @@ namespace CarCareTracker.Helper
} }
if (Directory.Exists(translationPath)) if (Directory.Exists(translationPath))
{ {
var existingPath = Path.Combine(_webEnv.WebRootPath, "translations"); var existingPath = Path.Combine(_webEnv.ContentRootPath, "data", "translations");
if (!Directory.Exists(existingPath)) if (!Directory.Exists(existingPath))
{ {
Directory.CreateDirectory(existingPath); Directory.CreateDirectory(existingPath);
@@ -203,7 +203,7 @@ namespace CarCareTracker.Helper
public string MakeAttachmentsExport(List<GenericReportModel> exportData) public string MakeAttachmentsExport(List<GenericReportModel> exportData)
{ {
var folderName = Guid.NewGuid(); 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)) if (!Directory.Exists(tempPath))
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);
int fileIndex = 0; int fileIndex = 0;
@@ -227,10 +227,10 @@ namespace CarCareTracker.Helper
public string MakeBackup() public string MakeBackup()
{ {
var folderName = $"db_backup_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}"; var folderName = $"db_backup_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}";
var tempPath = Path.Combine(_webEnv.WebRootPath, $"temp/{folderName}"); var tempPath = Path.Combine(_webEnv.ContentRootPath, "data", $"temp/{folderName}");
var imagePath = Path.Combine(_webEnv.WebRootPath, "images"); var imagePath = Path.Combine(_webEnv.ContentRootPath, "data", "images");
var documentPath = Path.Combine(_webEnv.WebRootPath, "documents"); var documentPath = Path.Combine(_webEnv.ContentRootPath, "data", "documents");
var translationPath = Path.Combine(_webEnv.WebRootPath, "translations"); var translationPath = Path.Combine(_webEnv.ContentRootPath, "data", "translations");
var dataPath = StaticHelper.DbName; var dataPath = StaticHelper.DbName;
var widgetPath = StaticHelper.AdditionalWidgetsPath; var widgetPath = StaticHelper.AdditionalWidgetsPath;
var configPath = StaticHelper.UserConfigPath; var configPath = StaticHelper.UserConfigPath;
@@ -301,8 +301,8 @@ namespace CarCareTracker.Helper
{ {
currentFilePath = currentFilePath.Substring(1); currentFilePath = currentFilePath.Substring(1);
} }
string uploadPath = Path.Combine(_webEnv.WebRootPath, newFolder); string uploadPath = Path.Combine(_webEnv.ContentRootPath, "data", newFolder);
string oldFilePath = Path.Combine(_webEnv.WebRootPath, currentFilePath); string oldFilePath = Path.Combine(_webEnv.ContentRootPath, "data", currentFilePath);
if (!Directory.Exists(uploadPath)) if (!Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath); Directory.CreateDirectory(uploadPath);
string newFileUploadPath = oldFilePath.Replace(tempPath, newFolder); string newFileUploadPath = oldFilePath.Replace(tempPath, newFolder);
@@ -319,7 +319,7 @@ namespace CarCareTracker.Helper
{ {
currentFilePath = currentFilePath.Substring(1); currentFilePath = currentFilePath.Substring(1);
} }
string filePath = Path.Combine(_webEnv.WebRootPath, currentFilePath); string filePath = Path.Combine(_webEnv.ContentRootPath, "data", currentFilePath);
if (File.Exists(filePath)) if (File.Exists(filePath))
{ {
File.Delete(filePath); File.Delete(filePath);

View File

@@ -240,7 +240,8 @@ namespace CarCareTracker.Helper
public static List<ExtraField> AddExtraFields(List<ExtraField> recordExtraFields, List<ExtraField> templateExtraFields) public static List<ExtraField> AddExtraFields(List<ExtraField> recordExtraFields, List<ExtraField> templateExtraFields)
{ {
if (!templateExtraFields.Any()) { if (!templateExtraFields.Any())
{
return new List<ExtraField>(); return new List<ExtraField>();
} }
if (!recordExtraFields.Any()) if (!recordExtraFields.Any())
@@ -261,7 +262,7 @@ namespace CarCareTracker.Helper
extraField.IsRequired = templateExtraFields.Where(x => x.Name == extraField.Name).First().IsRequired; extraField.IsRequired = templateExtraFields.Where(x => x.Name == extraField.Name).First().IsRequired;
} }
//append extra fields //append extra fields
foreach(ExtraField extraField in templateExtraFields) foreach (ExtraField extraField in templateExtraFields)
{ {
if (!recordFieldNames.Contains(extraField.Name)) if (!recordFieldNames.Contains(extraField.Name))
{ {
@@ -309,7 +310,8 @@ namespace CarCareTracker.Helper
if (mailConfig != null && !string.IsNullOrWhiteSpace(mailConfig.EmailServer)) if (mailConfig != null && !string.IsNullOrWhiteSpace(mailConfig.EmailServer))
{ {
Console.WriteLine($"SMTP Configured for {mailConfig.EmailServer}"); Console.WriteLine($"SMTP Configured for {mailConfig.EmailServer}");
} else }
else
{ {
Console.WriteLine("SMTP Not Configured"); Console.WriteLine("SMTP Not Configured");
} }
@@ -319,6 +321,82 @@ namespace CarCareTracker.Helper
{ {
Console.WriteLine("No Locale or Culture Configured for LubeLogger, Check Environment Variables"); 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) public static async void NotifyAsync(string webhookURL, WebHookPayload webHookPayload)
{ {
@@ -332,7 +410,8 @@ namespace CarCareTracker.Helper
webhookURL = webhookURL.Replace("discord://", "https://"); //cleanurl webhookURL = webhookURL.Replace("discord://", "https://"); //cleanurl
//format to discord //format to discord
httpClient.PostAsJsonAsync(webhookURL, DiscordWebHook.FromWebHookPayload(webHookPayload)); httpClient.PostAsJsonAsync(webhookURL, DiscordWebHook.FromWebHookPayload(webHookPayload));
} else }
else
{ {
httpClient.PostAsJsonAsync(webhookURL, webHookPayload); httpClient.PostAsJsonAsync(webhookURL, webHookPayload);
} }
@@ -370,12 +449,14 @@ namespace CarCareTracker.Helper
if (vehicle.VehicleIdentifier == "LicensePlate") if (vehicle.VehicleIdentifier == "LicensePlate")
{ {
return vehicle.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; return vehicle.ExtraFields?.FirstOrDefault(x => x.Name == vehicle.VehicleIdentifier)?.Value;
} else }
else
{ {
return "N/A"; return "N/A";
} }
@@ -402,10 +483,11 @@ namespace CarCareTracker.Helper
//Translations //Translations
public static string GetTranslationDownloadPath(string continent, string name) 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; return string.Empty;
} }
else else
{ {
switch (continent) switch (continent)
{ {
@@ -424,8 +506,9 @@ namespace CarCareTracker.Helper
if (string.IsNullOrWhiteSpace(name)) if (string.IsNullOrWhiteSpace(name))
{ {
return string.Empty; return string.Empty;
} else }
{ else
{
try try
{ {
string cleanedName = name.Contains("_") ? name.Replace("_", "-") : name; string cleanedName = name.Contains("_") ? name.Replace("_", "-") : name;
@@ -438,7 +521,8 @@ namespace CarCareTracker.Helper
{ {
return displayName; return displayName;
} }
} catch (Exception ex) }
catch (Exception ex)
{ {
return name; return name;
} }
@@ -609,7 +693,8 @@ namespace CarCareTracker.Helper
if (input == 0M.ToString("C2") && hideZero) if (input == 0M.ToString("C2") && hideZero)
{ {
return "---"; return "---";
} else }
else
{ {
return string.IsNullOrWhiteSpace(decorations) ? input : $"{input}{decorations}"; return string.IsNullOrWhiteSpace(decorations) ? input : $"{input}{decorations}";
} }

View File

@@ -7,11 +7,14 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
//Print Messages //Print Messages
StaticHelper.InitMessage(builder.Configuration); StaticHelper.InitMessage(builder.Configuration);
//Check Migration
StaticHelper.CheckMigration(builder.Environment.WebRootPath);
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
@@ -75,15 +78,6 @@ builder.Services.AddSingleton<IUserLogic, UserLogic>();
builder.Services.AddSingleton<IOdometerLogic, OdometerLogic>(); builder.Services.AddSingleton<IOdometerLogic, OdometerLogic>();
builder.Services.AddSingleton<IVehicleLogic, VehicleLogic>(); builder.Services.AddSingleton<IVehicleLogic, VehicleLogic>();
if (!Directory.Exists("data"))
{
Directory.CreateDirectory("data");
}
if (!Directory.Exists("config"))
{
Directory.CreateDirectory("config");
}
//Additional JsonFile //Additional JsonFile
builder.Configuration.AddJsonFile(StaticHelper.UserConfigPath, optional: true, reloadOnChange: true); 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(); app.UseRouting();