404 lines
16 KiB
C#
404 lines
16 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using PSTW_CentralSystem.DBContext;
|
|
using PSTW_CentralSystem.Areas.MMS.Models;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator;
|
|
using QuestPDF.Fluent;
|
|
|
|
namespace PSTW_CentralSystem.Areas.MMS.Controllers
|
|
{
|
|
[Area("MMS")]
|
|
public class MarineController : Controller
|
|
{
|
|
private readonly MMSSystemContext _context;//Used in TarBallForm and GeneratePdfResponse to query the database.
|
|
private readonly NetworkShareAccess _networkAccessService;//used in GetImage and GeneratePdfResponse
|
|
private const string PhotoBasePath = @"\\192.168.12.42\images\marine\manual_tarball";//used in GetImage and GeneratePdfResponse
|
|
|
|
public MarineController(MMSSystemContext context, NetworkShareAccess networkAccessService)
|
|
{
|
|
_context = context;
|
|
_networkAccessService = networkAccessService;
|
|
}
|
|
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
public IActionResult TarBallForm()//Queries the database and returns a view with tarball data
|
|
{
|
|
try
|
|
{
|
|
var marineTarballs = _context.MarineTarballs
|
|
.Where(t => t.StationID != "1") // To remove unusable data with invalid stationID
|
|
.Select(t => new
|
|
{
|
|
t.Id,
|
|
Date = t.DateSample.ToString("yyyy/MM/dd"),
|
|
Station = t.StationID
|
|
})
|
|
.ToList();
|
|
|
|
Console.WriteLine($"Marine Tarballs Count: {marineTarballs.Count}");
|
|
return View(marineTarballs);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error: {ex.Message}");
|
|
return StatusCode(500, "An error occurred while loading the data.");
|
|
}
|
|
}
|
|
|
|
[HttpGet] // Explicitly mark as a GET endpoint
|
|
public IActionResult TestCredentials()
|
|
{
|
|
try
|
|
{
|
|
// Use the EXACT same path/credentials as in Program.cs
|
|
var testService = new NetworkShareAccess(
|
|
@"\\192.168.12.42\images\marine\manual_tarball",
|
|
"installer",
|
|
"mms@pstw"
|
|
);
|
|
|
|
testService.ConnectToNetworkPath();
|
|
testService.DisconnectFromNetworkShare();
|
|
|
|
return Ok("Network credentials and path are working correctly!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log the full error (including stack trace)
|
|
Console.WriteLine($"TestCredentials failed: {ex}");
|
|
return StatusCode(500, $"Credentials test failed: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public IActionResult GetImage(string fileName)
|
|
{
|
|
if (string.IsNullOrEmpty(fileName))
|
|
{
|
|
return BadRequest("Filename cannot be empty");
|
|
}
|
|
|
|
// Sanitize filename to prevent path traversal attacks
|
|
var sanitizedFileName = Path.GetFileName(fileName);
|
|
if (sanitizedFileName != fileName)
|
|
{
|
|
return BadRequest("Invalid filename");
|
|
}
|
|
|
|
int retryCount = 0;
|
|
const int maxRetries = 3;
|
|
bool connectionSuccess = false;
|
|
|
|
// Retry loop for network connection
|
|
while (retryCount < maxRetries && !connectionSuccess)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine($"Attempt {retryCount + 1} to connect to network share...");
|
|
|
|
// Connect to network share
|
|
_networkAccessService.ConnectToNetworkPath();
|
|
connectionSuccess = true;
|
|
|
|
Console.WriteLine("Network share connected successfully");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
retryCount++;
|
|
Console.WriteLine($"Connection attempt {retryCount} failed: {ex.Message}");
|
|
|
|
if (retryCount >= maxRetries)
|
|
{
|
|
Console.WriteLine($"Max connection attempts reached. Last error: {ex}");
|
|
return StatusCode(503, $"Could not establish connection to image server after {maxRetries} attempts");
|
|
}
|
|
|
|
// Wait before retrying (1s, 2s, 3s)
|
|
Thread.Sleep(1000 * retryCount);
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
string imagePath = Path.Combine(PhotoBasePath, sanitizedFileName);
|
|
Console.WriteLine($"Attempting to access image at: {imagePath}");
|
|
|
|
// Verify file exists
|
|
if (!System.IO.File.Exists(imagePath))
|
|
{
|
|
Console.WriteLine($"Image not found at path: {imagePath}");
|
|
return NotFound($"Image '{sanitizedFileName}' not found on server");
|
|
}
|
|
|
|
// Verify file is an image
|
|
if (!IsImageValid(imagePath))
|
|
{
|
|
Console.WriteLine($"Invalid image file at path: {imagePath}");
|
|
return BadRequest("The requested file is not a valid image");
|
|
}
|
|
|
|
// Read and return the image
|
|
byte[] imageBytes = System.IO.File.ReadAllBytes(imagePath);
|
|
Console.WriteLine($"Successfully read image: {sanitizedFileName} ({imageBytes.Length} bytes)");
|
|
|
|
// Determine content type based on extension
|
|
string contentType = "image/jpeg"; // default
|
|
string extension = Path.GetExtension(sanitizedFileName)?.ToLower();
|
|
|
|
if (extension == ".png")
|
|
{
|
|
contentType = "image/png";
|
|
}
|
|
else if (extension == ".gif")
|
|
{
|
|
contentType = "image/gif";
|
|
}
|
|
|
|
return File(imageBytes, contentType);
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
Console.WriteLine($"Access denied to image: {ex}");
|
|
return StatusCode(403, "Access to the image was denied");
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
Console.WriteLine($"IO error accessing image: {ex}");
|
|
return StatusCode(503, "Error accessing image file");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Unexpected error: {ex}");
|
|
return StatusCode(500, "An unexpected error occurred while processing the image");
|
|
}
|
|
finally
|
|
{
|
|
try
|
|
{
|
|
if (connectionSuccess)
|
|
{
|
|
Console.WriteLine("Disconnecting from network share...");
|
|
_networkAccessService.DisconnectFromNetworkShare();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Warning: Error disconnecting from share: {ex.Message}");
|
|
// Don't fail the request because of disconnect issues
|
|
}
|
|
}
|
|
}
|
|
|
|
public IActionResult GenerateReport(int id)//calls GeneratePdfResponse to generate a PDF for inline viewing
|
|
{
|
|
return GeneratePdfResponse(id, true);
|
|
}
|
|
|
|
public IActionResult ViewPDF(int id)
|
|
{
|
|
try
|
|
{
|
|
// Add timeout for safety
|
|
var task = Task.Run(() => GeneratePdfResponse(id, false));
|
|
if (task.Wait(TimeSpan.FromSeconds(30))) // 30 second timeout
|
|
{
|
|
return task.Result;
|
|
}
|
|
return StatusCode(500, "PDF generation took too long");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"PDF VIEW ERROR: {ex}");
|
|
return StatusCode(500, $"Error showing PDF: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private IActionResult GeneratePdfResponse(int id, bool forceDownload)
|
|
{
|
|
Console.WriteLine($"Requested ID in {(forceDownload ? "GenerateReport" : "ViewPDF")}: {id}");
|
|
|
|
// Add this temporary check at the start of GeneratePdfResponse
|
|
try
|
|
{
|
|
Console.WriteLine("Testing network connection...");
|
|
_networkAccessService.ConnectToNetworkPath();
|
|
Console.WriteLine("Network connected successfully!");
|
|
_networkAccessService.DisconnectFromNetworkShare();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"NETWORK ERROR: {ex.Message}");
|
|
return StatusCode(500, $"Cannot access network: {ex.Message}");
|
|
}
|
|
|
|
try
|
|
{
|
|
// Connect to the network path
|
|
_networkAccessService.ConnectToNetworkPath();//------------------
|
|
|
|
// 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
|
|
where marine.Id == id
|
|
select new
|
|
{
|
|
state.StateName,
|
|
marine.StationID,
|
|
station.LocationName,
|
|
marine.Longitude,
|
|
marine.Latitude,
|
|
marine.DateSample,
|
|
marine.TimeSample,
|
|
marine.ClassifyID,
|
|
TarBallYes = marine.ClassifyID != "NO",
|
|
TarBallNo = marine.ClassifyID == "NO",
|
|
IsSand = marine.ClassifyID == "SD",
|
|
IsNonSandy = marine.ClassifyID == "NS",
|
|
IsCoquina = marine.ClassifyID == "CO",
|
|
marine.OptionalName1,
|
|
marine.OptionalName2,
|
|
marine.OptionalName3,
|
|
marine.OptionalName4,
|
|
marine.FirstSampler
|
|
}).FirstOrDefault();
|
|
|
|
if (tarballData == null)
|
|
return NotFound("Record not found");
|
|
|
|
// 2. Get photos from station folder (with date matching)
|
|
var sampleDateString = tarballData.DateSample.ToString("yyyyMMdd");
|
|
var stationFolder = Path.Combine(PhotoBasePath, tarballData.StationID);
|
|
var stationImages = new Dictionary<string, string>();
|
|
|
|
if (Directory.Exists(stationFolder))
|
|
{
|
|
var allImages = Directory.GetFiles(stationFolder)
|
|
.Where(f =>
|
|
{
|
|
var fileName = Path.GetFileNameWithoutExtension(f);
|
|
var parts = fileName.Split('_');
|
|
|
|
return parts.Length >= 3 &&
|
|
parts[0] == tarballData.StationID &&
|
|
parts[1] == sampleDateString &&
|
|
(f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
|
|
f.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) &&
|
|
IsImageValid(f);
|
|
})
|
|
.ToList();
|
|
|
|
// Define image priority order
|
|
var imageTypesInOrder = new List<string>
|
|
{
|
|
"LEFTSIDECOASTALVIEW",
|
|
"RIGHTSIDECOASTALVIEW",
|
|
"DRAWINGVERTICALLINES",
|
|
"DRAWINGHORIZONTALLINES",
|
|
"OPTIONAL01",
|
|
"OPTIONAL02",
|
|
"OPTIONAL03",
|
|
"OPTIONAL04"
|
|
};
|
|
|
|
// Match images to their types
|
|
foreach (var imageType in imageTypesInOrder)
|
|
{
|
|
var matchedImage = allImages.FirstOrDefault(f =>
|
|
Path.GetFileNameWithoutExtension(f).ToUpper().Contains(imageType));
|
|
if (matchedImage != null)
|
|
{
|
|
stationImages[imageType] = matchedImage;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Validate mandatory images
|
|
var mandatoryImages = new List<string>
|
|
{
|
|
"LEFTSIDECOASTALVIEW",
|
|
"RIGHTSIDECOASTALVIEW",
|
|
"DRAWINGVERTICALLINES",
|
|
"DRAWINGHORIZONTALLINES"
|
|
};
|
|
|
|
foreach (var mandatoryImage in mandatoryImages)
|
|
{
|
|
if (!stationImages.ContainsKey(mandatoryImage))
|
|
{
|
|
return StatusCode(400, $"Missing mandatory image: {mandatoryImage}");
|
|
}
|
|
}
|
|
|
|
// 3. Generate PDF
|
|
var pdf = new TarBallPDF(
|
|
tarballData.StateName,
|
|
tarballData.StationID,
|
|
tarballData.LocationName,
|
|
tarballData.Longitude,
|
|
tarballData.Latitude,
|
|
tarballData.DateSample,
|
|
tarballData.TimeSample,
|
|
tarballData.ClassifyID,
|
|
tarballData.TarBallYes,
|
|
tarballData.TarBallNo,
|
|
tarballData.IsSand,
|
|
tarballData.IsNonSandy,
|
|
tarballData.IsCoquina,
|
|
stationImages.ContainsKey("LEFTSIDECOASTALVIEW") ? stationImages["LEFTSIDECOASTALVIEW"] : null,
|
|
stationImages.ContainsKey("RIGHTSIDECOASTALVIEW") ? stationImages["RIGHTSIDECOASTALVIEW"] : null,
|
|
stationImages.ContainsKey("DRAWINGVERTICALLINES") ? stationImages["DRAWINGVERTICALLINES"] : null,
|
|
stationImages.ContainsKey("DRAWINGHORIZONTALLINES") ? stationImages["DRAWINGHORIZONTALLINES"] : null,
|
|
stationImages.ContainsKey("OPTIONAL01") ? stationImages["OPTIONAL01"] : null,
|
|
stationImages.ContainsKey("OPTIONAL02") ? stationImages["OPTIONAL02"] : null,
|
|
stationImages.ContainsKey("OPTIONAL03") ? stationImages["OPTIONAL03"] : null,
|
|
stationImages.ContainsKey("OPTIONAL04") ? stationImages["OPTIONAL04"] : null,
|
|
tarballData.OptionalName1,
|
|
tarballData.OptionalName2,
|
|
tarballData.OptionalName3,
|
|
tarballData.OptionalName4,
|
|
tarballData.FirstSampler
|
|
).GeneratePdf();
|
|
|
|
// 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: {ex}");
|
|
// Include inner exception if available
|
|
string errorMessage = ex.InnerException != null
|
|
? $"{ex.Message} (Inner: {ex.InnerException.Message})"
|
|
: ex.Message;
|
|
return StatusCode(500, $"PDF generation failed: {errorMessage}");
|
|
}
|
|
finally
|
|
{
|
|
// Disconnect from the network path
|
|
_networkAccessService.DisconnectFromNetworkShare();
|
|
}
|
|
}
|
|
|
|
|
|
private bool IsImageValid(string imagePath)
|
|
{
|
|
try
|
|
{
|
|
using (var image = System.Drawing.Image.FromFile(imagePath))
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine($"Invalid image skipped: {imagePath}");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|