Commit dbf15746 authored by Krishna Reddy Tamatam's avatar Krishna Reddy Tamatam

Added Delete documnet function

parent e8b037e4
......@@ -159,7 +159,7 @@ namespace FTP_Services.Services.Controllers
string DatabaseName = _appSettings.ConnectionString;
_appSettings.ConnectionString = _appSettings.AdminConnectionString;
FTPDataAdapter adapter = new FTPDataAdapter(_appSettings);
List<ConfigValues>? RequestsList = adapter.ConfigValues(CompanyId,DatabaseName);
List<ConfigValues>? RequestsList = adapter.ConfigValues(CompanyId, DatabaseName);
return Ok(RequestsList);
}
catch (Exception ex)
......@@ -272,7 +272,7 @@ namespace FTP_Services.Services.Controllers
}
[HttpPost("DownloadBase64DataAsync")]
public async Task<string> DownloadBase64DataAsync(string fileNameWithPath,bool IsExternal)
public async Task<string> DownloadBase64DataAsync(string fileNameWithPath, bool IsExternal)
{
bool isFTPMODE = _appSettings.FTPConfiguration.FTPMODE;
var base64 = string.Empty;
......@@ -348,11 +348,11 @@ namespace FTP_Services.Services.Controllers
try
{
var fileStream = new FileStream(strFullPath, FileMode.Open, FileAccess.Read,FileShare.Read);
var fileStream = new FileStream(strFullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
var base64Stream = new Base64EncodingStream(fileStream);
return new FileStreamResult(base64Stream, "application/octet-stream")
{
FileDownloadName = Path.GetFileName(fileNameWithPath)
FileDownloadName = Path.GetFileName(fileNameWithPath)
};
}
catch (Exception ex)
......@@ -446,7 +446,7 @@ namespace FTP_Services.Services.Controllers
|| response.StatusCode == FtpStatusCode.FileActionOK;
return success;
}
//Using for Scan upload
......@@ -1122,16 +1122,38 @@ namespace FTP_Services.Services.Controllers
}
[HttpPost("DeleteFTPFile")]
public async Task<int> DeleteFTPFile(string fileNameWithDestinationPath)
public async Task<string> DeleteFTPFile(string fileNameWithDestinationPath)
{
bool isFTPMODE = _appSettings.FTPConfiguration.FTPMODE;
string result = string.Empty;
if (isFTPMODE == true)
{
result = await DeleteFileFTPAsync(fileNameWithDestinationPath);
}
else
{
result = await DeleteFileLocallyAsync(fileNameWithDestinationPath);
}
return result;
}
private async Task<string> DeleteFileFTPAsync(string sourceFilePath)
{
try
{
string FTPURL = _appSettings.FTPConfiguration.FtpURL.ToString();
string FTPUserName = _appSettings.FTPConfiguration.Username.ToString();
string FTPPassword = _appSettings.FTPConfiguration.Password.ToString();
string strSourceFullPath = "";
string strBasePath = _appSettings
.FTPConfiguration.DestinationPathFtp.ToString()
.Trim();
sourceFilePath = sourceFilePath.Replace("/", "\\").TrimStart('\\');
strSourceFullPath = Path.Combine(strBasePath, sourceFilePath);
FtpWebRequest ftp = (FtpWebRequest)
WebRequest.Create(FTPURL + fileNameWithDestinationPath);
WebRequest.Create(FTPURL + strSourceFullPath);
ftp.Credentials = new NetworkCredential(FTPUserName, FTPPassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
......@@ -1140,21 +1162,36 @@ namespace FTP_Services.Services.Controllers
FtpWebResponse response = (FtpWebResponse)await ftp.GetResponseAsync();
var statuscode = (int)response.StatusCode;
response.Close();
return statuscode;
return "";
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
response.Close();
return 1;
}
else
return "Error deleting file: " + ex.Message.ToString();
}
}
private async Task<string> DeleteFileLocallyAsync(string sourceFilePath)
{
string strSourceFullPath = "";
try
{
string strBasePath = _appSettings
.FTPConfiguration.DestinationPathFile.ToString()
.Trim();
sourceFilePath = sourceFilePath.Replace("/", "\\").TrimStart('\\');
strSourceFullPath = Path.Combine(strBasePath, sourceFilePath);
if (!System.IO.File.Exists(strSourceFullPath))
{
response.Close();
return 0;
return "Source file does not exist.";
}
System.IO.File.Delete(strSourceFullPath);
return "";
}
catch (Exception ex)
{
return "Error deleting file: " + ex.Message;
}
}
......
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