Commit 9ee0ab0d authored by Krishna Reddy Tamatam's avatar Krishna Reddy Tamatam

* Fixed Saving Patient Documents function Issues.

* Addded new params to Patient Documents function.
* Added New function for Move files from one folder to another.
parent 2edf1dfd
...@@ -70,6 +70,71 @@ namespace FTP_Services.Services.Controllers ...@@ -70,6 +70,71 @@ namespace FTP_Services.Services.Controllers
} }
} }
private async Task<bool> FileExistsAsync(string fileUrl, string username, string password)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fileUrl);
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Timeout = 2400000; // Increased timeout
request.ReadWriteTimeout = 2400000; // Increased read/write timeout
using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
{
return response.StatusCode == FtpStatusCode.FileStatus;
}
}
catch (WebException ex)
{
if (ex.Response is FtpWebResponse response)
{
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
// File does not exist
return false;
}
response.Close();
}
throw; // Rethrow other exceptions
}
}
private async Task DeleteFileAsync(string fileUrl, string username, string password)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fileUrl);
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.DeleteFile;
request.Timeout = 2400000; // Increased timeout
request.ReadWriteTimeout = 2400000; // Increased read/write timeout
using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
{
// If file deletion is successful, status code will be 'FileActionOK'
if (response.StatusCode != FtpStatusCode.FileActionOK)
{
// throw new InvalidOperationException("Failed to delete file: {response.StatusDescription}");
}
}
}
catch (WebException ex)
{
if (ex.Response is FtpWebResponse response)
{
// Log the status code if file deletion fails
// Console.WriteLine(
// $"Error: {response.StatusCode} - {response.StatusDescription}"
// );
response.Close();
}
// Rethrow the exception so calling code can handle it
// throw new InvalidOperationException("Failed to delete file", ex);
}
}
[HttpGet("LoadDocumentCategories")] [HttpGet("LoadDocumentCategories")]
public IActionResult LoadDocumentCategories() public IActionResult LoadDocumentCategories()
{ {
...@@ -377,8 +442,6 @@ namespace FTP_Services.Services.Controllers ...@@ -377,8 +442,6 @@ namespace FTP_Services.Services.Controllers
public async Task<string> UploadFileWithStreamLocal(string fileName, Stream stream) public async Task<string> UploadFileWithStreamLocal(string fileName, Stream stream)
{ {
// WriteLog("UploadFileWithStreamLocal Start.");
if (stream == null) if (stream == null)
{ {
throw new ArgumentNullException(nameof(stream), "Stream cannot be null."); throw new ArgumentNullException(nameof(stream), "Stream cannot be null.");
...@@ -400,10 +463,6 @@ namespace FTP_Services.Services.Controllers ...@@ -400,10 +463,6 @@ namespace FTP_Services.Services.Controllers
string strFullPath = Path.Combine(strDestinationPath, strMfN); string strFullPath = Path.Combine(strDestinationPath, strMfN);
string strDirectoryPath = Path.GetDirectoryName(strFullPath); string strDirectoryPath = Path.GetDirectoryName(strFullPath);
// WriteLog("DestinationPath: " + strDestinationPath);
// WriteLog("FullPath: " + strFullPath);
// WriteLog("DirectoryPath: " + strDirectoryPath);
if (!Directory.Exists(strDirectoryPath)) if (!Directory.Exists(strDirectoryPath))
{ {
Directory.CreateDirectory(strDirectoryPath); Directory.CreateDirectory(strDirectoryPath);
...@@ -411,11 +470,19 @@ namespace FTP_Services.Services.Controllers ...@@ -411,11 +470,19 @@ namespace FTP_Services.Services.Controllers
if (System.IO.File.Exists(strFullPath)) if (System.IO.File.Exists(strFullPath))
{ {
return "File already exists."; try
{
System.GC.Collect();
// System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(strFullPath);
}
catch (Exception ex)
{
return "Error deleting existing file: " + ex.Message.ToString();
}
} }
string filePath = Path.Combine(strDirectoryPath, Path.GetFileName(strFullPath)); string filePath = Path.Combine(strDirectoryPath, Path.GetFileName(strFullPath));
// WriteLog("filePath: " + filePath);
try try
{ {
...@@ -423,11 +490,11 @@ namespace FTP_Services.Services.Controllers ...@@ -423,11 +490,11 @@ namespace FTP_Services.Services.Controllers
{ {
await stream.CopyToAsync(fileStream); await stream.CopyToAsync(fileStream);
} }
return string.Empty; // Success return string.Empty;
} }
catch (Exception ex) catch (Exception ex)
{ {
return ex.Message.ToString(); // Return error message if an exception occurs return ex.Message.ToString();
} }
} }
...@@ -616,6 +683,13 @@ namespace FTP_Services.Services.Controllers ...@@ -616,6 +683,13 @@ namespace FTP_Services.Services.Controllers
if (CheckFolderExists(strFolderPath, ftpUserName, ftpPassword) == false) if (CheckFolderExists(strFolderPath, ftpUserName, ftpPassword) == false)
CreateFolder(strFolderPath, ftpUserName, ftpPassword); CreateFolder(strFolderPath, ftpUserName, ftpPassword);
// Check if the file already exists, and if so, delete it
if (await FileExistsAsync(uploadUrl, ftpUserName, ftpPassword))
{
await DeleteFileAsync(uploadUrl, ftpUserName, ftpPassword);
}
// Create the FTP request to upload the file
FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(uploadUrl); FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(uploadUrl);
ftp.Credentials = new NetworkCredential(ftpUserName, ftpPassword); ftp.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
ftp.KeepAlive = true; ftp.KeepAlive = true;
...@@ -625,12 +699,10 @@ namespace FTP_Services.Services.Controllers ...@@ -625,12 +699,10 @@ namespace FTP_Services.Services.Controllers
ftp.Timeout = 2400000; // Increased timeout for large files (40 minutes) ftp.Timeout = 2400000; // Increased timeout for large files (40 minutes)
ftp.ReadWriteTimeout = 2400000; // Increased read/write timeout for large files ftp.ReadWriteTimeout = 2400000; // Increased read/write timeout for large files
//Console.WriteLine($"Requesting upload to {uploadUrl} with method {ftp.Method}"); // Upload the file in chunks
using (Stream ftpStream = await ftp.GetRequestStreamAsync()) using (Stream ftpStream = await ftp.GetRequestStreamAsync())
{ {
// byte[] buffer = new byte[2048];
byte[] buffer = new byte[1048576]; // 1 MB buffer byte[] buffer = new byte[1048576]; // 1 MB buffer
int bytesRead; int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{ {
...@@ -640,7 +712,7 @@ namespace FTP_Services.Services.Controllers ...@@ -640,7 +712,7 @@ namespace FTP_Services.Services.Controllers
using (FtpWebResponse response = (FtpWebResponse)await ftp.GetResponseAsync()) using (FtpWebResponse response = (FtpWebResponse)await ftp.GetResponseAsync())
{ {
return ""; return ""; // Return empty string for successful upload
} }
} }
catch (WebException ex) catch (WebException ex)
...@@ -1034,6 +1106,115 @@ namespace FTP_Services.Services.Controllers ...@@ -1034,6 +1106,115 @@ namespace FTP_Services.Services.Controllers
// } // }
// } // }
[HttpPost("MoveFileAsync")]
public async Task<string> MoveFileAsync(string sourceFilePath, string destinationFilePath)
{
if (string.IsNullOrWhiteSpace(sourceFilePath) || string.IsNullOrWhiteSpace(destinationFilePath))
{
return "Source and destination file paths cannot be null or empty.";
}
bool isFTPMODE = _appSettings.FTPConfiguration.FTPMODE; // Check the FTP mode setting
string result = string.Empty;
try
{
if (isFTPMODE) // FTP Mode
{
// If FTP Mode is enabled, you can implement FTP file transfer logic
//result = await MoveFileToFtpAsync(sourceFilePath, destinationFilePath);
result = await MoveFileLocallyAsync(sourceFilePath, destinationFilePath);
}
else // Local File Mode
{
// In Local File Mode, perform the file move locally
result = await MoveFileLocallyAsync(sourceFilePath, destinationFilePath);
}
return result;
}
catch (Exception ex)
{
return "Error moving file: " + ex.Message;
}
}
private async Task<string> MoveFileLocallyAsync(string sourceFilePath, string destinationFilePath)
{
string strSourceFullPath = "";
string strDestinationFullPath ="";
try
{
string strBasePath = _appSettings
.FTPConfiguration.DestinationPath.ToString()
.Trim();
sourceFilePath=sourceFilePath.Replace("/", "\\").TrimStart('\\');
destinationFilePath=destinationFilePath.Replace("/", "\\").TrimStart('\\');
strSourceFullPath = Path.Combine(strBasePath, sourceFilePath);
strDestinationFullPath =Path.Combine(strBasePath, destinationFilePath );
// string strDestinationDirectory = Path.GetDirectoryName(strDestinationFullPath);
// Ensure the source file exists
if (!System.IO.File.Exists(strSourceFullPath))
{
return "Source file does not exist.";
}
if (!Directory.Exists(strDestinationFullPath))
{
Directory.CreateDirectory(strDestinationFullPath);
}
strDestinationFullPath = Path.Combine(strDestinationFullPath, Path.GetFileName(strSourceFullPath));
// Move the file from source to destination
System.IO.File.Move(strSourceFullPath, strDestinationFullPath);
return ""; // Return empty string if move was successful
}
catch (Exception ex)
{
return "Error moving file : " + ex.Message;
}
}
// private async Task<string> MoveFileToFtpAsync(string sourceFilePath, string destinationFilePath)
// {
// try
// {
// // FTP file move logic will go here
// // Example: Upload the file to FTP and delete the source file after upload
// var ftpClient = new FtpClient( _appSettings.FTPConfiguration.FtpURL,_appSettings.FTPConfiguration.Username, _appSettings.FTPConfiguration.Password); //_appSettings.FTPConfiguration.ServerUrl,
// // Upload the file to FTP (assuming you have an FTP client setup)
// bool uploadResult = await ftpClient.UploadFileAsync(sourceFilePath, destinationFilePath);
// if (!uploadResult)
// {
// return "Error uploading file to FTP.";
// }
// // Once uploaded, delete the source file locally if the upload is successful
// if (System.IO.File.Exists(sourceFilePath))
// {
// System.IO.File.Delete(sourceFilePath);
// }
// return string.Empty; // Return empty string if move was successful
// }
// catch (Exception ex)
// {
// return "Error moving file to FTP: " + ex.Message;
// }
// }
public void WriteLog(string SndException) public void WriteLog(string SndException)
{ {
......
...@@ -22,7 +22,4 @@ ...@@ -22,7 +22,4 @@
<PackageReference Include="Npgsql" Version="8.0.3" /> <PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.1" /> <PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.1" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="log4net.config" CopyToOutputDirectory="Always" />
</ItemGroup>
</Project> </Project>
...@@ -12,6 +12,8 @@ namespace FTP_Services.Core.Models ...@@ -12,6 +12,8 @@ namespace FTP_Services.Core.Models
public string DocumentUrl { get; set; } public string DocumentUrl { get; set; }
public string ThumbnailUrl { get; set; } public string ThumbnailUrl { get; set; }
public int SplID { get; set; } public int SplID { get; set; }
public int PatDocId { get; set; }
public string UploadedDate { get; set; }
public PatientDocumentDetailsModel() public PatientDocumentDetailsModel()
{ {
...@@ -25,6 +27,8 @@ namespace FTP_Services.Core.Models ...@@ -25,6 +27,8 @@ namespace FTP_Services.Core.Models
DocumentUrl = ""; DocumentUrl = "";
ThumbnailUrl = ""; ThumbnailUrl = "";
SplID = -1; SplID = -1;
PatDocId = 0;
UploadedDate = "";
} }
} }
...@@ -154,6 +158,9 @@ namespace FTP_Services.Core.Models ...@@ -154,6 +158,9 @@ namespace FTP_Services.Core.Models
public string UploadedByRole { get; set; } public string UploadedByRole { get; set; }
public string FullName { get; set; } public string FullName { get; set; }
public string DocumentUrl { get; set; } public string DocumentUrl { get; set; }
public int SplID { get; set; }
public string ThumbnailUrl { get; set; }
public PatientDocuments() public PatientDocuments()
{ {
...@@ -173,6 +180,8 @@ namespace FTP_Services.Core.Models ...@@ -173,6 +180,8 @@ namespace FTP_Services.Core.Models
UploadedByRole = ""; UploadedByRole = "";
FullName = ""; FullName = "";
DocumentUrl = ""; DocumentUrl = "";
SplID = -1;
ThumbnailUrl ="";
} }
} }
......
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
...@@ -132,21 +132,21 @@ app.UseEndpoints(endpoints => ...@@ -132,21 +132,21 @@ app.UseEndpoints(endpoints =>
// }); // });
// }); // });
// if (app.Environment.IsDevelopment())
// {
// app.Run("http://localhost:5603");
// }
// else
// {
// app.Run();
// }
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.Run("http://localhost:5603"); app.Run("http://localhost:5603");
} }
else else
{ {
app.Run("http://*:4708"); app.Run();
} }
// if (app.Environment.IsDevelopment())
// {
// app.Run("http://localhost:5603");
// }
// else
// {
// app.Run("http://*:4708");
// }
...@@ -187,11 +187,15 @@ namespace FTP_Services.Services ...@@ -187,11 +187,15 @@ namespace FTP_Services.Services
using (var tx = _repository.GetTransaction()) using (var tx = _repository.GetTransaction())
{ {
string SQLStr = string SQLStr =
"SELECT * FROM \"SPScanTool_SavePatientDocument\"(0, " "SELECT * FROM \"SPScanTool_SavePatientDocument\"("
+ SndPatDocRec.PatDocId
+ ",'"
+ SndPatDocRec.UploadedDate
+"',"
+ SndPatDocRec.PatientId + SndPatDocRec.PatientId
+ ","
+ SndPatDocRec.UploadedBy
+ ",'" + ",'"
+ SndPatDocRec.UploadedBy
+ "','"
+ SndPatDocRec.DocumentName + SndPatDocRec.DocumentName
+ "','" + "','"
+ SndPatDocRec.DocumentType + SndPatDocRec.DocumentType
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment