test accessing images

This commit is contained in:
misya 2025-04-30 15:22:55 +08:00
parent b31d54bd8e
commit 7e80ca5ef9
2 changed files with 68 additions and 51 deletions

View File

@ -1,24 +1,54 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.DBContext;
using PSTW_CentralSystem.Models; using PSTW_CentralSystem.Areas.MMS.Models;
using PSTW_CentralSystem.Areas.MMS;
using System.Linq;
using QuestPDF.Fluent;
using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator;
using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator;
using QuestPDF.Fluent;
namespace PSTW_CentralSystem.Areas.MMS.Controllers namespace PSTW_CentralSystem.Areas.MMS.Controllers
{ {
[Area("MMS")] [Area("MMS")]
//[Authorize(Policy = "RoleModulePolicy")] public class MarineController : Controller
public class MarineController(MMSSystemContext context) : 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"; 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() public IActionResult Index()
{ {
return View(); return View();
@ -26,8 +56,13 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
public IActionResult TarBallForm() public IActionResult TarBallForm()
{ {
if (!TryAccessNetworkPath())
{
return StatusCode(500, "Unable to access network path.");
}
var marineTarballs = _context.MarineTarballs 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 .Select(t => new
{ {
t.Id, t.Id,
@ -36,12 +71,6 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
}) })
.ToList(); .ToList();
// Debugging
foreach (var item in marineTarballs)
{
Console.WriteLine($"Date: {item.Date}, Station: {item.Station}");
}
return View(marineTarballs); return View(marineTarballs);
} }
@ -94,7 +123,8 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
if (Directory.Exists(stationFolder)) if (Directory.Exists(stationFolder))
{ {
var allImages = Directory.GetFiles(stationFolder) var allImages = Directory.GetFiles(stationFolder)
.Where(f => { .Where(f =>
{
var fileName = Path.GetFileNameWithoutExtension(f); var fileName = Path.GetFileNameWithoutExtension(f);
var parts = fileName.Split('_'); var parts = fileName.Split('_');
@ -120,14 +150,15 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
"OPTIONAL04" "OPTIONAL04"
}; };
// Sort logic (same as before) // Sort logic
bool hasValidFormat = allImages.Any(f => bool hasValidFormat = allImages.Any(f =>
imageTypesInOrder.Any(t => Path.GetFileNameWithoutExtension(f).ToUpper().Contains(t))); imageTypesInOrder.Any(t => Path.GetFileNameWithoutExtension(f).ToUpper().Contains(t)));
if (hasValidFormat) if (hasValidFormat)
{ {
stationImages = allImages stationImages = allImages
.OrderBy(f => { .OrderBy(f =>
{
var fileName = Path.GetFileNameWithoutExtension(f).ToUpper(); var fileName = Path.GetFileNameWithoutExtension(f).ToUpper();
var typeIndex = imageTypesInOrder.FindIndex(t => fileName.Contains(t)); var typeIndex = imageTypesInOrder.FindIndex(t => fileName.Contains(t));
return typeIndex >= 0 ? typeIndex : int.MaxValue; return typeIndex >= 0 ? typeIndex : int.MaxValue;
@ -189,29 +220,6 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
} }
} }
private static List<string> 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) private bool IsImageValid(string imagePath)
{ {
try try
@ -226,6 +234,4 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
} }
} }
} }
}
}

View File

@ -1,7 +1,8 @@
using System.Runtime.InteropServices; using System;
using System.Security; using System.IO;
using System.Runtime.InteropServices;
public class NetworkAccessService public class NetworkAccessService : IDisposable
{ {
private readonly IConfiguration _config; private readonly IConfiguration _config;
@ -35,6 +36,16 @@ public class NetworkAccessService
WNetCancelConnection2(networkPath, 0, true); 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)] [StructLayout(LayoutKind.Sequential)]
private class NetResource private class NetResource
{ {