From 138d88918f0d4d67567d55f7b63fe49b7119fb3a Mon Sep 17 00:00:00 2001 From: misya Date: Wed, 23 Apr 2025 12:18:04 +0800 Subject: [PATCH] generate images (1-4; no naming format, only alph.) --- Areas/MMS/Controllers/MarineController.cs | 87 +++++++++++---------- Areas/MMS/Models/PDFGenerator/TarBallPDF.cs | 40 +++++++--- Models/MarineTarball.cs | 8 +- PSTW_CentralSystem.csproj | 1 + 4 files changed, 79 insertions(+), 57 deletions(-) diff --git a/Areas/MMS/Controllers/MarineController.cs b/Areas/MMS/Controllers/MarineController.cs index 7fa199c..009517d 100644 --- a/Areas/MMS/Controllers/MarineController.cs +++ b/Areas/MMS/Controllers/MarineController.cs @@ -61,11 +61,10 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers try { + // 1. Fetch core data from database var tarballData = (from marine in _context.MarineTarballs - join station in _context.MarineStations - on marine.StationID equals station.StationID - join state in _context.States - on station.StateID equals state.StateID + join station in _context.MarineStations on marine.StationID equals station.StationID + join state in _context.States on station.StateID equals state.StateID where marine.Id == id select new { @@ -85,18 +84,25 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers }).FirstOrDefault(); if (tarballData == null) + return NotFound("Record not found"); + + // 2. Get photos from station folder + var stationFolder = Path.Combine(PhotoBasePath, tarballData.StationID); + var stationImages = new List(); + + if (Directory.Exists(stationFolder)) { - return NotFound("The specified record was not found."); + stationImages = Directory.GetFiles(stationFolder) + .Where(f => f.EndsWith(".jpg") || f.EndsWith(".png")) + .OrderBy(f => f) // Alphabetical order + .Take(4) // Only need 4 photos max + .ToList(); } - // Generate photo paths - var photoBaseName = $"{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}"; - var photoPath1 = GetValidPhotoPath(photoBaseName + "_1.jpg"); - var photoPath2 = GetValidPhotoPath(photoBaseName + "_2.jpg"); - var photoPath3 = GetValidPhotoPath(photoBaseName + "_3.jpg"); - var photoPath4 = GetValidPhotoPath(photoBaseName + "_4.jpg"); + Console.WriteLine($"Found {stationImages.Count} images for {tarballData.StationID}"); - var pdfDocument = new TarBallPDF( + // 3. Generate PDF + var pdf = new TarBallPDF( tarballData.StateName, tarballData.StationID, tarballData.LocationName, @@ -110,48 +116,47 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers tarballData.IsSand, tarballData.IsNonSandy, tarballData.IsCoquina, - photoPath1, - photoPath2, - photoPath3, - photoPath4 - ); + stationImages.Count > 0 ? stationImages[0] : null, + stationImages.Count > 1 ? stationImages[1] : null, + stationImages.Count > 2 ? stationImages[2] : null, + stationImages.Count > 3 ? stationImages[3] : null + ).GeneratePdf(); - var pdf = pdfDocument.GeneratePdf(); - Console.WriteLine("PDF generation completed."); - - if (forceDownload) - { - var fileName = $"TbReport_{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}.pdf"; - return File(pdf, "application/pdf", fileName); - } - - return File(pdf, "application/pdf"); + // 4. Return file + return forceDownload + ? File(pdf, "application/pdf", $"TbReport_{tarballData.StationID}_{tarballData.DateSample:yyyyMMdd}.pdf") + : File(pdf, "application/pdf"); } catch (Exception ex) { - Console.WriteLine($"Error in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {ex.Message}"); - return StatusCode(StatusCodes.Status500InternalServerError, - $"An error occurred while {(forceDownload ? "generating" : "viewing")} the PDF. {ex.Message}"); + Console.WriteLine($"Error: {ex.Message}"); + return StatusCode(500, $"PDF generation failed: {ex.Message}"); } } - private string GetValidPhotoPath(string photoName) - { - var fullPath = Path.Combine(PhotoBasePath, photoName); - return System.IO.File.Exists(fullPath) ? fullPath : null; - } - - private string? FindPhoto(string folderPath, string searchPattern) + private static List GetStationPhotos(string folderPath) { try { - var files = Directory.GetFiles(folderPath, searchPattern); - return files.Length > 0 ? files[0] : null; + if (!Directory.Exists(folderPath)) + { + Console.WriteLine($"Folder not found: {folderPath}"); + return new List(); // Return empty list + } + + // Get ALL .jpg/.png files (no date sorting) + return Directory.GetFiles(folderPath) + .Where(f => f.EndsWith(".jpg") || f.EndsWith(".png")) + .OrderBy(f => f) // Optional: Sort alphabetically + .ToList(); } - catch + catch (Exception ex) { - return null; + Console.WriteLine($"Error fetching photos: {ex.Message}"); + return new List(); // Return empty list on error } } + + } } \ No newline at end of file diff --git a/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs b/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs index 3d8f3ec..94c80c6 100644 --- a/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs +++ b/Areas/MMS/Models/PDFGenerator/TarBallPDF.cs @@ -25,19 +25,27 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator private readonly bool _isSand = isSand; private readonly bool _isNonSandy = isNonSandy; private readonly bool _isCoquina = isCoquina; - private readonly string _photoPath1 = photoPath1; - private readonly string _photoPath2 = photoPath2; - private readonly string _photoPath3 = photoPath3; - private readonly string _photoPath4 = photoPath4; + private readonly string? _photoPath1 = photoPath1; + private readonly string? _photoPath2 = photoPath2; + private readonly string? _photoPath3 = photoPath3; + private readonly string? _photoPath4 = photoPath4; //INSERT LOADIMAGE() HERE - private static Image? LoadImage(string path) + private Image? LoadImage(string? imagePath) { - if (!string.IsNullOrEmpty(path) && File.Exists(path)) + if (string.IsNullOrEmpty(imagePath)) // Fixed: Added closing ) { - return Image.FromFile(path); // Load image if the file exists + return null; + } + + try + { + return Image.FromFile(imagePath); + } + catch + { + return null; } - return Image.FromFile("default-placeholder.jpg"); // Return null if the file is missing } // Metadata for the PDF document @@ -101,10 +109,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator innerRow.RelativeItem(1).Element(CellStyle).Text("02") .AlignLeft(); }); + }); static IContainer CellStyle(IContainer container) => container.Border(0.5f).Padding(5); + }); // Content Section @@ -214,10 +224,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator // Row 1: Photos table.Cell().Element(CellStyle).Height(150) - .Image(LoadImage(_photoPath1)); // Left Side View photo space + .Image(LoadImage(_photoPath1) ?? null) // Just pass null if no image + .FitArea(); table.Cell().Element(CellStyle).Height(150) - .Image(LoadImage(_photoPath2)); // Right Side View photo space + .Image(LoadImage(_photoPath2) ?? null) // Just pass null if no image + .FitArea(); // Row 1: Captions table.Cell().Element(CellStyle) @@ -230,10 +242,12 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator // Row 2: Photos table.Cell().Element(CellStyle).Height(150) - .Image(LoadImage(_photoPath3)); // Vertical Lines photo space + .Image(LoadImage(_photoPath3) ?? null) // Just pass null if no image + .FitArea(); table.Cell().Element(CellStyle).Height(150) - .Image(LoadImage(_photoPath4)); // Horizontal Lines photo space + .Image(LoadImage(_photoPath4) ?? null) // Just pass null if no image + .FitArea(); // Row 2: Captions table.Cell().Element(CellStyle) @@ -248,6 +262,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator // Page Break column.Item().PageBreak(); + column.Spacing(3); + // Additional Photos Section column.Item().Table(table => { diff --git a/Models/MarineTarball.cs b/Models/MarineTarball.cs index b81c69d..87b9c07 100644 --- a/Models/MarineTarball.cs +++ b/Models/MarineTarball.cs @@ -18,10 +18,10 @@ namespace PSTW_CentralSystem.Models public double GetLongitude { get; set; } // Maps to 'getLongitude' public DateTime Timestamp { get; set; } // Maps to 'timestamp' - public string PhotoPath1 { get; set; } // Left Side Coastal View - public string PhotoPath2 { get; set; } // Right Side Coastal View - public string PhotoPath3 { get; set; } // Vertical Lines - public string PhotoPath4 { get; set; } // Horizontal Lines + public required string PhotoPath1 { get; set; } // Left Side Coastal View + public required string PhotoPath2 { get; set; } // Right Side Coastal View + public required string PhotoPath3 { get; set; } // Vertical Lines + public required string PhotoPath4 { get; set; } // Horizontal Lines [ForeignKey("StationID")] public required MarineStation MarineStation { get; set; } diff --git a/PSTW_CentralSystem.csproj b/PSTW_CentralSystem.csproj index da776fe..92012c8 100644 --- a/PSTW_CentralSystem.csproj +++ b/PSTW_CentralSystem.csproj @@ -28,6 +28,7 @@ +