Commit 52c766a8 authored by Krishna Reddy Tamatam's avatar Krishna Reddy Tamatam

resolved conflicts

parents 6e5e5a38 b51b6e56
......@@ -37,7 +37,6 @@ namespace FTP_Services.Services.Controllers
[HttpPost("GetLoginDetails")]
public IActionResult GetLoginDetails([FromBody] LoginResponseModel model)
{
// _hostingEnvironment.log("GetLoginDetails ==> ");
try
{
......@@ -334,6 +333,33 @@ namespace FTP_Services.Services.Controllers
return base64;
}
[HttpPost("DownloadBase64DataAsync_New")]
public IActionResult DownloadBase64DataAsync_New(string fileNameWithPath)
{
string strDestinationPath = _appSettings.FTPConfiguration.DestinationPath.ToString().Trim();
string strMfN = fileNameWithPath.Replace("/", "\\").TrimStart('\\');
string strFullPath = Path.Combine(strDestinationPath, strMfN);
if (string.IsNullOrEmpty(strFullPath) || !System.IO.File.Exists(strFullPath))
{
return BadRequest("File not found");
}
try
{
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)
};
}
catch (Exception ex)
{
return StatusCode(500, "Error processing file: " + ex.Message.ToString());
}
}
[HttpGet("GetPDFBase64")]
public async Task<string> GetPDFBase64(string remoteFilePath)
{
......@@ -457,6 +483,141 @@ namespace FTP_Services.Services.Controllers
return uploadResponse;
}
[HttpPost("UploadLargeFileWithStreamLocal")]
public async Task<string> UploadLargeFileWithStreamLocal(string fileName, Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream), "Stream cannot be null.");
}
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentNullException(
nameof(fileName),
"File name cannot be null or empty."
);
}
string strDestinationPath = _appSettings
.FTPConfiguration.DestinationPath.ToString()
.Trim();
string strMfN = fileName.Replace("/", "\\").TrimStart('\\');
string strFullPath = Path.Combine(strDestinationPath, strMfN);
string strDirectoryPath = Path.GetDirectoryName(strFullPath);
// Ensure the directory exists
if (!Directory.Exists(strDirectoryPath))
{
Directory.CreateDirectory(strDirectoryPath);
}
// If file already exists, delete it
if (System.IO.File.Exists(strFullPath))
{
try
{
System.GC.Collect();
System.IO.File.Delete(strFullPath);
}
catch (Exception ex)
{
return "Error deleting existing file: " + ex.Message;
}
}
string filePath = Path.Combine(strDirectoryPath, Path.GetFileName(strFullPath));
const int bufferSize = 1048576; // 80 KB buffer, you can adjust the size based on your system
byte[] buffer = new byte[bufferSize];
try
{
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
}
}
return string.Empty;
}
catch (Exception ex)
{
return "Error uploading file: " + ex.Message;
}
}
[HttpPost("UploadFileByPath")]
public async Task<string> UploadFileByPath(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentNullException(
nameof(filePath),
"File path cannot be null or empty."
);
}
if (!System.IO.File.Exists(filePath))
{
throw new FileNotFoundException("The specified file does not exist.", filePath);
}
string strDestinationPath = _appSettings
.FTPConfiguration.DestinationPath.ToString()
.Trim();
string strMfN = Path.GetFileName(filePath);
string strFullPath = Path.Combine(strDestinationPath, strMfN);
string strDirectoryPath = Path.GetDirectoryName(strFullPath);
if (!Directory.Exists(strDirectoryPath))
{
Directory.CreateDirectory(strDirectoryPath);
}
if (System.IO.File.Exists(strFullPath))
{
try
{
System.GC.Collect();
System.IO.File.Delete(strFullPath);
}
catch (Exception ex)
{
return "Error deleting existing file: " + ex.Message;
}
}
const int bufferSize = 81920;
byte[] buffer = new byte[bufferSize];
try
{
using (var sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (
var destinationStream = new FileStream(
strFullPath,
FileMode.Create,
FileAccess.Write
)
)
{
int bytesRead;
while ((bytesRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await destinationStream.WriteAsync(buffer, 0, bytesRead);
}
}
return string.Empty;
}
catch (Exception ex)
{
return "Error uploading file: " + ex.Message;
}
}
public async Task<string> UploadFileWithStreamLocal(string fileName, Stream stream)
{
if (stream == null)
......@@ -515,6 +676,81 @@ namespace FTP_Services.Services.Controllers
}
}
[HttpPost("UploadFileWithFilePath")]
public async Task<string> UploadFileWithFilePath(string fileName, string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentNullException(
nameof(filePath),
"File path cannot be null or empty."
);
}
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentNullException(
nameof(fileName),
"File name cannot be null or empty."
);
}
string strDestinationPath = _appSettings
.FTPConfiguration.DestinationPath.ToString()
.Trim();
string strMfN = fileName.Replace("/", "\\").TrimStart('\\');
string strFullPath = Path.Combine(strDestinationPath, strMfN);
string strDirectoryPath = Path.GetDirectoryName(strFullPath);
if (!Directory.Exists(strDirectoryPath))
{
Directory.CreateDirectory(strDirectoryPath);
}
if (System.IO.File.Exists(strFullPath))
{
try
{
System.GC.Collect();
System.IO.File.Delete(strFullPath);
}
catch (Exception ex)
{
return "Error deleting existing file: " + ex.Message;
}
}
// const int bufferSize = 81920; // 80KB buffer size
const int bufferSize = 1048576; // 80KB buffer size
byte[] buffer = new byte[bufferSize];
try
{
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (
var destinationStream = new FileStream(
strFullPath,
FileMode.Create,
FileAccess.Write
)
)
{
int bytesRead;
while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await destinationStream.WriteAsync(buffer, 0, bytesRead);
}
}
return string.Empty;
}
catch (Exception ex)
{
return ex.Message;
}
}
public async Task<string> UploadFileWithStreamToNetworkShare(string fileName, Stream stream)
{
if (stream == null)
......@@ -1126,13 +1362,14 @@ namespace FTP_Services.Services.Controllers
[HttpPost("MoveFileAsync")]
public async Task<string> MoveFileAsync(string sourceFilePath, string destinationFilePath)
{
if (string.IsNullOrWhiteSpace(sourceFilePath) || string.IsNullOrWhiteSpace(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;
......@@ -1158,26 +1395,27 @@ namespace FTP_Services.Services.Controllers
}
}
private async Task<string> MoveFileLocallyAsync(string sourceFilePath, string destinationFilePath)
private async Task<string> MoveFileLocallyAsync(
string sourceFilePath,
string destinationFilePath
)
{
string strSourceFullPath = "";
string strDestinationFullPath ="";
string strSourceFullPath = "";
string strDestinationFullPath = "";
try
{
string strBasePath = _appSettings
.FTPConfiguration.DestinationPath.ToString()
.Trim();
.FTPConfiguration.DestinationPath.ToString()
.Trim();
sourceFilePath=sourceFilePath.Replace("/", "\\").TrimStart('\\');
destinationFilePath=destinationFilePath.Replace("/", "\\").TrimStart('\\');
sourceFilePath = sourceFilePath.Replace("/", "\\").TrimStart('\\');
destinationFilePath = destinationFilePath.Replace("/", "\\").TrimStart('\\');
strSourceFullPath = Path.Combine(strBasePath, sourceFilePath);
strDestinationFullPath =Path.Combine(strBasePath, destinationFilePath );
strDestinationFullPath = Path.Combine(strBasePath, destinationFilePath);
// string strDestinationDirectory = Path.GetDirectoryName(strDestinationFullPath);
// Ensure the source file exists
if (!System.IO.File.Exists(strSourceFullPath))
{
......@@ -1188,7 +1426,10 @@ namespace FTP_Services.Services.Controllers
Directory.CreateDirectory(strDestinationFullPath);
}
strDestinationFullPath = Path.Combine(strDestinationFullPath, Path.GetFileName(strSourceFullPath));
strDestinationFullPath = Path.Combine(
strDestinationFullPath,
Path.GetFileName(strSourceFullPath)
);
// Move the file from source to destination
System.IO.File.Move(strSourceFullPath, strDestinationFullPath);
......
using System.Text;
public class Base64EncodingStream : Stream
{
private readonly Stream _innerStream;
private readonly MemoryStream _base64BufferStream = new MemoryStream();
private bool _endOfStream = false;
public Base64EncodingStream(Stream innerStream)
{
_innerStream = innerStream ?? throw new ArgumentNullException(nameof(innerStream));
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override void Flush() => throw new NotSupportedException();
public override int Read(byte[] buffer, int offset, int count)
{
if (_endOfStream && _base64BufferStream.Length == 0)
return 0; // End of stream
// Fill the Base64 buffer if it's empty
if (_base64BufferStream.Length == 0)
{
byte[] readBuffer = new byte[81920]; // 80 KB
int bytesRead = _innerStream.Read(readBuffer, 0, readBuffer.Length);
if (bytesRead > 0)
{
string base64Chunk = Convert.ToBase64String(readBuffer, 0, bytesRead);
byte[] base64Bytes = Encoding.UTF8.GetBytes(base64Chunk);
_base64BufferStream.Write(base64Bytes, 0, base64Bytes.Length);
_base64BufferStream.Position = 0; // Reset position for reading
}
else
{
_endOfStream = true;
}
}
// Read Base64 data from the buffer
int bytesToCopy = Math.Min(count, (int)(_base64BufferStream.Length - _base64BufferStream.Position));
int bytesCopied = _base64BufferStream.Read(buffer, offset, bytesToCopy);
// If the Base64 buffer is exhausted, reset it
if (_base64BufferStream.Position == _base64BufferStream.Length)
{
_base64BufferStream.SetLength(0);
_base64BufferStream.Position = 0;
}
return bytesCopied;
}
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
if (disposing)
{
_innerStream?.Dispose();
_base64BufferStream?.Dispose();
}
base.Dispose(disposing);
}
}
......@@ -132,15 +132,6 @@ app.UseEndpoints(endpoints =>
// });
// });
// if (app.Environment.IsDevelopment())
// {
// app.Run("http://localhost:5603");
// }
// else
// {
// app.Run();
// }
if (app.Environment.IsDevelopment())
{
app.Run("http://localhost:5603");
......@@ -149,5 +140,6 @@ else
{
//app.Run();
app.Run("http://*:4708");
}
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