Skip to content

Commit

Permalink
Merge pull request #342 from GregFinzer/feature/FixSeeding
Browse files Browse the repository at this point in the history
Feature/fix seeding
  • Loading branch information
GregFinzer authored Sep 3, 2024
2 parents 38bf807 + a0ad03b commit e1a0c5b
Show file tree
Hide file tree
Showing 292 changed files with 1,235 additions and 598 deletions.
4 changes: 2 additions & 2 deletions BedBrigade.Client/Components/FileManager.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void objectSelected(FileSelectEventArgs<FileManagerDirectoryContent> args
bool isFile = false;
if (args.FileDetails != null && args.FileDetails.Name != null)
{
if (args.FileDetails.Name.ToString().ToLower() == "pages")
if (args.FileDetails.Name.ToString().ToLower() == Defaults.PagesDirectory.ToLower())
{
isPagesFolder = true;
}
Expand All @@ -155,7 +155,7 @@ public void OnMenuOpen(MenuOpenEventArgs<FileManagerDirectoryContent> args)
if (args.FileDetails != null)
{
isFile = args.FileDetails[0].IsFile;
if (args.FileDetails[0].Name.ToString() == "pages")
if (args.FileDetails[0].Name.ToString().ToLower() == Defaults.PagesDirectory.ToLower())
{
isPagesFolder = true;
}
Expand Down
3 changes: 0 additions & 3 deletions BedBrigade.Client/Components/LocationGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ private async Task Save(ActionEventArgs<Location> args)
await AddNewLocationAsync(Location);
}

// validate location folders
var bLocationFolderStatus = FileUtil.CreateOrValidateLocationFolders(Location.Route); // VS 8/25/2023

await Grid.CallStateHasChangedAsync();
await Grid.Refresh();
}
Expand Down
4 changes: 2 additions & 2 deletions BedBrigade.Client/Components/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ private async Task<bool> LoadLocationPage(string location, string pageName)
}
else
{
_navigationManager.NavigateTo("/Sorry", true);
_navigationManager.NavigateTo($"/Sorry/{location}/{pageName}", true);
return false;
}
}
else
{
_navigationManager.NavigateTo("/Sorry", true);
_navigationManager.NavigateTo($"/Sorry/{location}", true);
return false;
}
}
Expand Down
36 changes: 34 additions & 2 deletions BedBrigade.Client/Components/Pages/Sorry.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
@page "/sorry"
@page "/sorry/{mylocation}"
@page "/sorry/{mylocation}/{mypageName}"
@using Serilog
<PageTitle>Sorry</PageTitle>
<h3 class="text-center mt-5">Sorry - the page associated with this link was not found. </h3>
<h1 class="text-center mt-5">Sorry - the page associated with this link was not found. </h1>
@if (mylocation != null && mypageName != null)
{
<h2 class="text-center mt-5">You were trying to access the page: /@mylocation/@mypageName</h2>
}
else if (mylocation != null)
{
<h2 class="text-center mt-5">You were trying to access the page: /@mylocation</h2>
}

<div class="text-center">
<a href="/" class="btn btn-primary">Return to Home</a>
</div>

@code {
//TODO: Add notification code.
[Parameter] public string? mylocation { get; set; }
[Parameter] public string? mypageName { get; set; }

protected override void OnInitialized()
{
if (mylocation != null && mypageName != null)
{
Log.Logger.Warning($"404 for Page: /{mylocation}/{mypageName}");
}
else if (mylocation != null)
{
Log.Logger.Warning($"404 for Page: /{mylocation}");
}
else
{
Log.Logger.Warning($"404 for Page: /sorry");
}
}
}
6 changes: 3 additions & 3 deletions BedBrigade.Client/Components/PagesGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ protected async Task PdfExport()
{
PdfExportProperties ExportProperties = new PdfExportProperties
{
FileName = "Pages" + DateTime.Now.ToShortDateString() + ".pdf",
FileName = Defaults.PagesDirectory + DateTime.Now.ToShortDateString() + ".pdf",
PageOrientation = Syncfusion.Blazor.Grids.PageOrientation.Landscape
};
await Grid.PdfExport(ExportProperties);
Expand All @@ -318,7 +318,7 @@ protected async Task ExcelExport()
{
ExcelExportProperties ExportProperties = new ExcelExportProperties
{
FileName = "Pages " + DateTime.Now.ToShortDateString() + ".xlsx",
FileName = Defaults.PagesDirectory + DateTime.Now.ToShortDateString() + ".xlsx",

};

Expand All @@ -328,7 +328,7 @@ protected async Task CsvExportAsync()
{
ExcelExportProperties ExportProperties = new ExcelExportProperties
{
FileName = "Pages " + DateTime.Now.ToShortDateString() + ".csv",
FileName = Defaults.PagesDirectory + DateTime.Now.ToShortDateString() + ".csv",

};

Expand Down
6 changes: 6 additions & 0 deletions BedBrigade.Client/Services/LoadImagesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public string SetImagesForHtml(string path, string originalHtml)
var doc = new HtmlDocument();
doc.LoadHtml(originalHtml);
var nodes = doc.DocumentNode.SelectNodes("//img");

if (nodes == null)
{
return originalHtml;
}

foreach (var node in nodes)
{
if (node.Attributes[Id] != null)
Expand Down
1 change: 1 addition & 0 deletions BedBrigade.Common/Constants/Defaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public static class Defaults
public const string DefaultUserNameAndEmail = "Anonymous";
public const string GetFilesCacheKey = "Directory.GetFiles";
public const int MetroAreaNoneId = 1;
public const string PagesDirectory = "pages";
}
}
214 changes: 38 additions & 176 deletions BedBrigade.Common/Logic/FileUtil.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using BedBrigade.Common.Models;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BedBrigade.Common.Constants;

namespace BedBrigade.Common.Logic
{
Expand Down Expand Up @@ -37,164 +39,6 @@ private static string GetPathBeforeBin(string filePath)
}
}

/// <summary>
/// Returns the path to the file contained in the specified foldername
/// </summary>
/// <param name="fileName">The name of the file to find</param>
/// <param name="folderName">A string terminating in a folder to be searched (Note this may be a path of folders)</param>
/// <returns>A string containing where the file was found</returns>
public static string ToApplicationPath(string fileName, string folderName = "")
{
string appRoot = GetMediaDirectory(folderName);
return Path.Combine(appRoot, fileName);
}



public static bool CreateOrValidateLocationFolders(string locationRoute)
{

// LocationRoute = LocationRoute.Replace("/", String.Empty);

string templateLocationRoot = Path.Combine(GetMediaDirectory("grove-city"),"pages"); // temporary - should be replaced by configuration variable
// default pages List
string[] defaultPageFolders = Directory.GetDirectories(templateLocationRoot);
// Clear page List - remove path


try
{
// Step 1 - Location Root Folder & pages folder
string locationRoot = GetMediaDirectory(locationRoute);
string locationPagesPath = Path.Combine(locationRoot, "pages"); // location top default sub-folder
CreateFolderIfNotExists(locationRoot); // check location folder
CreateFolderIfNotExists(locationPagesPath); // check "pages" sub-folder

// step 2 - loop in default pages list
//foreach (string pageFolderName in defaultPageFolders)
Parallel.ForEach(defaultPageFolders, pageFolderName =>
{ // create location page folder
string pageName = Path.GetFileName(pageFolderName);
string pageFolderPath = Path.Combine(locationPagesPath, pageName);
CreateFolderIfNotExists(pageFolderPath);
if (pageName == "Home") // special structure
{
string homeRotatorPath = Path.Combine(pageFolderPath, "headerImageRotator");
CreateFolderIfNotExists(homeRotatorPath);
if (Directory.GetFiles(homeRotatorPath).Length == 0)
{
CopyRotatorImages(homeRotatorPath, true);
}
}
else
{
// Rotator Images Folder
CreatePageRotatorFolders(pageFolderPath);

} // Loop in default pages

} // end page loop
); // end Parallel process

return (true);

}
catch (Exception ex)
{
// Debug.WriteLine(ex.Message);
return(false);
}

} // Validate Location Folders


private static void CreatePageRotatorFolders(string pageFolderPath)
{
string[] RotatorFolders = ["leftImageRotator", "middleImageRotator", "rightImageRotator"];
// Rotator Images Folder
foreach (var rotatorFolderName in RotatorFolders)
{
string rotatorFolderPath = Path.Combine(pageFolderPath, rotatorFolderName);
CreateFolderIfNotExists(rotatorFolderPath);
if (Directory.GetFiles(rotatorFolderPath).Length == 0)
{
CopyRotatorImages(rotatorFolderPath);
}
} // loop in rotator folders
} // Create Page Rotator Folders

private static void CreateFolderIfNotExists(string folderPath)
{
try
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
//Debug.WriteLine($"Created folder: {folderPath}");
}
else
{
//Debug.WriteLine($"Folder already exists: {folderPath}");
}
}
catch (Exception ex)
{
//Debug.WriteLine($"Failed to create folder: {folderPath}. Error: {ex.Message}");
}
} // Create Folder if Not Exists

private static void CopyRotatorImages(string targetFolderPath, bool bHomePage = false)
{
string imagesFolderPath = Path.Combine(GetSeedingDirectory(), "SeedImages"); // seeding path
List<string> imagesToCopy = new List<string> { "middleHomeImage.jpg", "rightHomeImage.jpg" }; // for home paqe

if (!bHomePage) // Rotator Single Image in Folder
{
imagesToCopy.Clear();
imagesToCopy = new List<string> { "Default.jpg" };
}

foreach (string imageName in imagesToCopy)
{

var imageFileName = imageName;

if (imagesToCopy.Count == 1)
{
string rotatorFolderName = Path.GetFileName(targetFolderPath); // image rotator name without path
imageFileName = rotatorFolderName + imageFileName; // default rotator image
}

string sourceImagePath = Path.Combine(imagesFolderPath, imageFileName);
string destinationImagePath = Path.Combine(targetFolderPath, imageFileName);

try
{
if (File.Exists(sourceImagePath) && !File.Exists(destinationImagePath))
{
File.Copy(sourceImagePath, destinationImagePath);
//Debug.WriteLine($"Copied image: {imageFileName} to {targetFolderPath}");
}
else
{
//Debug.WriteLine($"Cannot copy image: {imageFileName} to {targetFolderPath}");
}
}
catch (Exception ex)
{
//Debug.WriteLine($"Failed to copy image: {imageFileName} to {targetFolderPath}. Error: {ex.Message}");
}
}

}//CopyRotatorImages



public static bool MediaSubDirectoryExists(string directoryName)
{
var appRoot = GetMediaDirectory(directoryName);
return Directory.Exists(appRoot);
}

public static DirectoryInfo CreateMediaSubDirectory(string directoryName)
{
Expand All @@ -208,22 +52,7 @@ public static void DeleteMediaSubDirectory(string directoryName, bool recursiveD
if (Directory.Exists(appRoot)) {
Directory.Delete(appRoot, recursiveDelete);
}
} // Delete Media SubDirectory

public static void DeleteMediaFiles(string directoryName)
{
var appRoot = GetMediaDirectory(directoryName);
string[] files = Directory.GetFiles(appRoot);
foreach (string file in files)
{
File.Delete(file);
}
}
public static void CreateDirectory(string targetDir)
{
Directory.CreateDirectory(targetDir);
}

}

public static void DeleteDirectory(string targetDir)
{
Expand Down Expand Up @@ -256,7 +85,10 @@ public static void CopyDirectory(string sourceDirectory, string targetDirectory)

private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);
if (!Directory.Exists(target.FullName))
{
Directory.CreateDirectory(target.FullName);
}

// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
Expand Down Expand Up @@ -296,5 +128,35 @@ public static string GetSeedingDirectory()

throw new DirectoryNotFoundException("Seeding directory not found. Current directory is : " + AppDomain.CurrentDomain.BaseDirectory);
}

public static void CreateLocationMediaDirectory(Location location)
{
var locationMediaDirectory = FileUtil.GetMediaDirectory(location.Route);

if (!Directory.Exists(locationMediaDirectory))
{
Directory.CreateDirectory(locationMediaDirectory);
}

var locationMediaPagesDirectory = Path.Combine(locationMediaDirectory, Defaults.PagesDirectory);

if (!Directory.Exists(locationMediaPagesDirectory))
{
Directory.CreateDirectory(locationMediaPagesDirectory);
}
}

public static void CopyMediaFromLocation(Location sourceLocation, Location destLocation, string directory)
{
var sourceDirectory = Path.Combine(FileUtil.GetMediaDirectory(sourceLocation.Route), Defaults.PagesDirectory, directory);
var destinationDirectory = Path.Combine(FileUtil.GetMediaDirectory(destLocation.Route), Defaults.PagesDirectory, directory);

if (!Directory.Exists(sourceDirectory))
{
throw new DirectoryNotFoundException($"Directory {sourceDirectory} does not exist");
}

FileUtil.CopyDirectory(sourceDirectory, destinationDirectory);
}
}
}
Loading

0 comments on commit e1a0c5b

Please sign in to comment.