From 7e80ca5ef94c15db71fec0f3af961d12fd6a4cac Mon Sep 17 00:00:00 2001 From: misya Date: Wed, 30 Apr 2025 15:22:55 +0800 Subject: [PATCH] test accessing images --- Areas/MMS/Controllers/MarineController.cs | 102 ++++++++++++---------- Areas/MMS/Models/NetworkAccessService.cs | 17 +++- 2 files changed, 68 insertions(+), 51 deletions(-) diff --git a/Areas/MMS/Controllers/MarineController.cs b/Areas/MMS/Controllers/MarineController.cs index 56070f1..d638957 100644 --- a/Areas/MMS/Controllers/MarineController.cs +++ b/Areas/MMS/Controllers/MarineController.cs @@ -1,24 +1,54 @@ -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using PSTW_CentralSystem.DBContext; -using PSTW_CentralSystem.Models; -using PSTW_CentralSystem.Areas.MMS; -using System.Linq; -using QuestPDF.Fluent; -using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator; -using System.Globalization; +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")] - //[Authorize(Policy = "RoleModulePolicy")] - public class MarineController(MMSSystemContext context) : Controller + public class MarineController : Controller { - private readonly MMSSystemContext _context = context; + private readonly MMSSystemContext _context; + private readonly NetworkAccessService _networkAccessService; private const string PhotoBasePath = @"\\192.168.12.42\images\marine\manual_tarball"; + public MarineController(MMSSystemContext context, NetworkAccessService networkAccessService) + { + _context = context; + _networkAccessService = networkAccessService; + } + + private bool TryAccessNetworkPath() + { + try + { + _networkAccessService.ConnectToNetworkShare(PhotoBasePath); + + if (_networkAccessService.DirectoryExists(PhotoBasePath)) + { + Console.WriteLine("Network path is accessible."); + return true; + } + else + { + Console.WriteLine("Network path does not exist."); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error accessing network path: {ex.Message}"); + } + finally + { + _networkAccessService.DisconnectFromNetworkShare(PhotoBasePath); + } + + return false; + } + public IActionResult Index() { return View(); @@ -26,8 +56,13 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers public IActionResult TarBallForm() { + if (!TryAccessNetworkPath()) + { + return StatusCode(500, "Unable to access network path."); + } + var marineTarballs = _context.MarineTarballs - .Where(t => t.StationID != "1")//remove unusable data (to be modified later to delete) + .Where(t => t.StationID != "1") // Remove unusable data .Select(t => new { t.Id, @@ -36,12 +71,6 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers }) .ToList(); - // Debugging - foreach (var item in marineTarballs) - { - Console.WriteLine($"Date: {item.Date}, Station: {item.Station}"); - } - return View(marineTarballs); } @@ -94,7 +123,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers if (Directory.Exists(stationFolder)) { var allImages = Directory.GetFiles(stationFolder) - .Where(f => { + .Where(f => + { var fileName = Path.GetFileNameWithoutExtension(f); var parts = fileName.Split('_'); @@ -120,14 +150,15 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers "OPTIONAL04" }; - // Sort logic (same as before) + // Sort logic bool hasValidFormat = allImages.Any(f => imageTypesInOrder.Any(t => Path.GetFileNameWithoutExtension(f).ToUpper().Contains(t))); if (hasValidFormat) { stationImages = allImages - .OrderBy(f => { + .OrderBy(f => + { var fileName = Path.GetFileNameWithoutExtension(f).ToUpper(); var typeIndex = imageTypesInOrder.FindIndex(t => fileName.Contains(t)); return typeIndex >= 0 ? typeIndex : int.MaxValue; @@ -189,29 +220,6 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers } } - private static List GetStationPhotos(string folderPath) - { - try - { - if (!Directory.Exists(folderPath)) - { - Console.WriteLine($"Folder not found: {folderPath}"); - return []; // 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 (Exception ex) - { - Console.WriteLine($"Error fetching photos: {ex.Message}"); - return []; // Return empty list on error - } - } - private bool IsImageValid(string imagePath) { try @@ -226,6 +234,4 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers } } } - - -} \ No newline at end of file +} diff --git a/Areas/MMS/Models/NetworkAccessService.cs b/Areas/MMS/Models/NetworkAccessService.cs index 9880e27..df8f425 100644 --- a/Areas/MMS/Models/NetworkAccessService.cs +++ b/Areas/MMS/Models/NetworkAccessService.cs @@ -1,7 +1,8 @@ -using System.Runtime.InteropServices; -using System.Security; +using System; +using System.IO; +using System.Runtime.InteropServices; -public class NetworkAccessService +public class NetworkAccessService : IDisposable { private readonly IConfiguration _config; @@ -35,6 +36,16 @@ public class NetworkAccessService WNetCancelConnection2(networkPath, 0, true); } + public bool DirectoryExists(string path) + { + return Directory.Exists(path); + } + + public void Dispose() + { + // No impersonation context to clean up, but you can add cleanup logic here if needed. + } + [StructLayout(LayoutKind.Sequential)] private class NetResource {