fix image generation when null
This commit is contained in:
parent
d9fe781343
commit
a2ee35d462
@ -3,31 +3,38 @@ using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
|
||||
public class NetworkShareAccess : IDisposable
|
||||
{
|
||||
private readonly string _networkPath;
|
||||
private readonly string _username;
|
||||
private readonly string _password;
|
||||
|
||||
private bool _isConnected = false;
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
public NetworkShareAccess(string networkPath, string username, string password)
|
||||
{
|
||||
// Validate and normalize the network path
|
||||
_networkPath = networkPath?.Trim().Replace('/', '\\') ?? throw new ArgumentNullException(nameof(networkPath));
|
||||
if (!_networkPath.StartsWith(@"\\"))
|
||||
{
|
||||
throw new ArgumentException("Network path must start with \\\\");
|
||||
}
|
||||
|
||||
_username = username ?? throw new ArgumentNullException(nameof(username));
|
||||
_password = password ?? throw new ArgumentNullException(nameof(password));
|
||||
}
|
||||
|
||||
public void ConnectToNetworkPath()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_isConnected) return;
|
||||
|
||||
int retries = 0;
|
||||
while (retries < 3)
|
||||
{
|
||||
try
|
||||
{
|
||||
var netResource = new NetResource
|
||||
{
|
||||
Scope = ResourceScope.GlobalNetwork,
|
||||
@ -38,18 +45,35 @@ public class NetworkShareAccess : IDisposable
|
||||
|
||||
int result = WNetAddConnection2(netResource, _password, _username, 0);
|
||||
|
||||
if (result != 0)
|
||||
if (result == 0)
|
||||
{
|
||||
string errorMessage = GetNetworkErrorDescription(result);
|
||||
Console.WriteLine($"Failed to connect to {_networkPath}. Error {result}: {errorMessage}");
|
||||
throw new IOException($"Failed to connect to network path. Error {result}: {errorMessage}");
|
||||
_isConnected = true;
|
||||
Console.WriteLine($"Connected to {_networkPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
_isConnected = true;
|
||||
Console.WriteLine($"Successfully connected to {_networkPath}");
|
||||
if (result == 1219) // Multiple connections
|
||||
{
|
||||
WNetCancelConnection2(_networkPath, 0, true);
|
||||
Thread.Sleep(1000);
|
||||
retries++;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new IOException($"Failed to connect. Error {result}: {GetNetworkErrorDescription(result)}");
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (++retries >= 3) throw;
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DisconnectFromNetworkShare()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_isConnected) return;
|
||||
|
||||
@ -57,36 +81,18 @@ public class NetworkShareAccess : IDisposable
|
||||
{
|
||||
int result = WNetCancelConnection2(_networkPath, 0, true);
|
||||
if (result != 0)
|
||||
{
|
||||
string errorMessage = GetNetworkErrorDescription(result);
|
||||
Console.WriteLine($"Failed to disconnect from {_networkPath}. Error {result}: {errorMessage}");
|
||||
throw new IOException($"Failed to disconnect from network path. Error {result}: {errorMessage}");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Successfully disconnected from {_networkPath}");
|
||||
Console.WriteLine($"Warning: Disconnect failed (Error {result})");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isConnected = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
DisconnectFromNetworkShare();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error during disposal: {ex.Message}");
|
||||
// Suppress disposal errors
|
||||
}
|
||||
}
|
||||
|
||||
private string GetNetworkErrorDescription(int errorCode)
|
||||
{
|
||||
return errorCode switch
|
||||
public void Dispose() => DisconnectFromNetworkShare();
|
||||
|
||||
private string GetNetworkErrorDescription(int errorCode) => errorCode switch
|
||||
{
|
||||
5 => "Access denied",
|
||||
53 => "Network path not found",
|
||||
@ -96,7 +102,6 @@ public class NetworkShareAccess : IDisposable
|
||||
1219 => "Multiple connections to a server or shared resource not allowed",
|
||||
_ => new Win32Exception(errorCode).Message
|
||||
};
|
||||
}
|
||||
|
||||
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
|
||||
@ -104,9 +109,6 @@ public class NetworkShareAccess : IDisposable
|
||||
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern int WNetCancelConnection2(string name, int flags, bool force);
|
||||
|
||||
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern int WNetGetConnection(string localName, StringBuilder remoteName, ref int length);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private class NetResource
|
||||
{
|
||||
|
||||
@ -49,18 +49,18 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
|
||||
private Image? LoadImage(string? imagePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(imagePath))
|
||||
if (string.IsNullOrEmpty(imagePath)) //check if photo is missing
|
||||
{
|
||||
return null;
|
||||
return null; // returns 'nothing' (safe)
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return Image.FromFile(imagePath);
|
||||
return Image.FromFile(imagePath); //load photo
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
return null; //if loading fails, returns 'nothing' (safe)
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,7 +208,7 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
{
|
||||
text.Span(_classifyID == "SD" ? "☑ Sand " : "☐ Sand ").Style(TextStyle.Default.FontSize(10));
|
||||
text.Span(_classifyID == "NS" ? " ☑ Non-sandy " : " ☐ Non-sandy ").Style(TextStyle.Default.FontSize(10));
|
||||
text.Span(_classifyID == "CO" ? "☑ Coquina" : "☐ Coquina").Style(TextStyle.Default.FontSize(10));
|
||||
text.Span(_classifyID == "CQ" ? "☑ Coquina" : "☐ Coquina").Style(TextStyle.Default.FontSize(10));
|
||||
});
|
||||
|
||||
column.Item()
|
||||
@ -237,7 +237,7 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
|
||||
// Row 1: Photos ====================================
|
||||
table.Cell().Element(CellStyle).Height(150)
|
||||
.Image(LoadImage(_photoPath1) ?? null) // Just pass null if no image
|
||||
.Image(LoadImage(_photoPath1) ?? null) // Loads image or uses null. if exist, fill cell. if null, cell stay empty
|
||||
.FitArea();
|
||||
|
||||
table.Cell().Element(CellStyle).Height(150)
|
||||
@ -288,28 +288,44 @@ namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
|
||||
columns.RelativeColumn(1);
|
||||
});
|
||||
|
||||
table.Cell().Element(CellStyle).Height(150)
|
||||
.Image(LoadImage(_photoPath5) ?? null) // Just pass null if no image
|
||||
.FitArea();
|
||||
table.Cell().Element(CellStyle).Height(150)
|
||||
.Image(LoadImage(_photoPath6) ?? null) // Just pass null if no image
|
||||
.FitArea();
|
||||
// Helper function to safely add image cells
|
||||
void AddOptionalImageCell(string imagePath)
|
||||
{
|
||||
table.Cell().Element(CellStyle).Height(150).Element(cell =>
|
||||
{
|
||||
var image = LoadImage(imagePath);
|
||||
if (image != null)
|
||||
{
|
||||
cell.Image(image).FitArea();
|
||||
}
|
||||
// If null, leaves an empty cell
|
||||
});
|
||||
}
|
||||
|
||||
table.Cell().Element(CellStyle).Text($"Figure 5: {_optionalName1}")
|
||||
.FontSize(12).AlignLeft();
|
||||
table.Cell().Element(CellStyle).Text($"Figure 6: {_optionalName2}")
|
||||
// Row 1: Optional images 5 & 6
|
||||
AddOptionalImageCell(_photoPath5);
|
||||
AddOptionalImageCell(_photoPath6);
|
||||
|
||||
// Row 2: Captions
|
||||
table.Cell().Element(CellStyle)
|
||||
.Text(string.IsNullOrEmpty(_photoPath5) ? "" : $"Figure 5: {_optionalName1}")
|
||||
.FontSize(12).AlignLeft();
|
||||
|
||||
table.Cell().Element(CellStyle).Height(150)
|
||||
.Image(LoadImage(_photoPath7) ?? null) // Just pass null if no image
|
||||
.FitArea();
|
||||
table.Cell().Element(CellStyle).Height(150)
|
||||
.Image(LoadImage(_photoPath8) ?? null) // Just pass null if no image
|
||||
.FitArea();
|
||||
|
||||
table.Cell().Element(CellStyle).Text($"Figure 7: {_optionalName3}")
|
||||
table.Cell().Element(CellStyle)
|
||||
.Text(string.IsNullOrEmpty(_photoPath6) ? "" : $"Figure 6: {_optionalName2}")
|
||||
.FontSize(12).AlignLeft();
|
||||
table.Cell().Element(CellStyle).Text($"Figure 8: {_optionalName4}")
|
||||
|
||||
// Row 3: Optional images 7 & 8
|
||||
AddOptionalImageCell(_photoPath7);
|
||||
AddOptionalImageCell(_photoPath8);
|
||||
|
||||
// Row 4: Captions
|
||||
table.Cell().Element(CellStyle)
|
||||
.Text(string.IsNullOrEmpty(_photoPath7) ? "" : $"Figure 7: {_optionalName3}")
|
||||
.FontSize(12).AlignLeft();
|
||||
|
||||
table.Cell().Element(CellStyle)
|
||||
.Text(string.IsNullOrEmpty(_photoPath8) ? "" : $"Figure 8: {_optionalName4}")
|
||||
.FontSize(12).AlignLeft();
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user