Commit b51b6e56 authored by Kundena Mohan 's avatar Kundena Mohan

Added Upload method with large files

Signed-off-by: Kundena Mohan 's avatarkmohan <kmohan@sujainfo.net>
parent 5390b81f
...@@ -37,7 +37,6 @@ namespace FTP_Services.Services.Controllers ...@@ -37,7 +37,6 @@ namespace FTP_Services.Services.Controllers
[HttpPost("GetLoginDetails")] [HttpPost("GetLoginDetails")]
public IActionResult GetLoginDetails([FromBody] LoginResponseModel model) public IActionResult GetLoginDetails([FromBody] LoginResponseModel model)
{ {
// _hostingEnvironment.log("GetLoginDetails ==> "); // _hostingEnvironment.log("GetLoginDetails ==> ");
try try
{ {
...@@ -134,7 +133,6 @@ namespace FTP_Services.Services.Controllers ...@@ -134,7 +133,6 @@ namespace FTP_Services.Services.Controllers
} }
} }
[HttpGet("LoadDocumentCategories")] [HttpGet("LoadDocumentCategories")]
public IActionResult LoadDocumentCategories() public IActionResult LoadDocumentCategories()
{ {
...@@ -317,6 +315,33 @@ namespace FTP_Services.Services.Controllers ...@@ -317,6 +315,33 @@ namespace FTP_Services.Services.Controllers
return base64; 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")] [HttpGet("GetPDFBase64")]
public async Task<string> GetPDFBase64(string remoteFilePath) public async Task<string> GetPDFBase64(string remoteFilePath)
{ {
...@@ -440,6 +465,141 @@ namespace FTP_Services.Services.Controllers ...@@ -440,6 +465,141 @@ namespace FTP_Services.Services.Controllers
return uploadResponse; 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) public async Task<string> UploadFileWithStreamLocal(string fileName, Stream stream)
{ {
if (stream == null) if (stream == null)
...@@ -498,6 +658,81 @@ namespace FTP_Services.Services.Controllers ...@@ -498,6 +658,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) public async Task<string> UploadFileWithStreamToNetworkShare(string fileName, Stream stream)
{ {
if (stream == null) if (stream == null)
...@@ -1109,13 +1344,14 @@ namespace FTP_Services.Services.Controllers ...@@ -1109,13 +1344,14 @@ namespace FTP_Services.Services.Controllers
[HttpPost("MoveFileAsync")] [HttpPost("MoveFileAsync")]
public async Task<string> MoveFileAsync(string sourceFilePath, string destinationFilePath) 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."; return "Source and destination file paths cannot be null or empty.";
} }
bool isFTPMODE = _appSettings.FTPConfiguration.FTPMODE; // Check the FTP mode setting bool isFTPMODE = _appSettings.FTPConfiguration.FTPMODE; // Check the FTP mode setting
string result = string.Empty; string result = string.Empty;
...@@ -1141,26 +1377,27 @@ namespace FTP_Services.Services.Controllers ...@@ -1141,26 +1377,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 strSourceFullPath = "";
string strDestinationFullPath =""; string strDestinationFullPath = "";
try try
{ {
string strBasePath = _appSettings string strBasePath = _appSettings
.FTPConfiguration.DestinationPath.ToString() .FTPConfiguration.DestinationPath.ToString()
.Trim(); .Trim();
sourceFilePath=sourceFilePath.Replace("/", "\\").TrimStart('\\'); sourceFilePath = sourceFilePath.Replace("/", "\\").TrimStart('\\');
destinationFilePath=destinationFilePath.Replace("/", "\\").TrimStart('\\'); destinationFilePath = destinationFilePath.Replace("/", "\\").TrimStart('\\');
strSourceFullPath = Path.Combine(strBasePath, sourceFilePath); strSourceFullPath = Path.Combine(strBasePath, sourceFilePath);
strDestinationFullPath =Path.Combine(strBasePath, destinationFilePath ); strDestinationFullPath = Path.Combine(strBasePath, destinationFilePath);
// string strDestinationDirectory = Path.GetDirectoryName(strDestinationFullPath); // string strDestinationDirectory = Path.GetDirectoryName(strDestinationFullPath);
// Ensure the source file exists // Ensure the source file exists
if (!System.IO.File.Exists(strSourceFullPath)) if (!System.IO.File.Exists(strSourceFullPath))
{ {
...@@ -1171,7 +1408,10 @@ namespace FTP_Services.Services.Controllers ...@@ -1171,7 +1408,10 @@ namespace FTP_Services.Services.Controllers
Directory.CreateDirectory(strDestinationFullPath); Directory.CreateDirectory(strDestinationFullPath);
} }
strDestinationFullPath = Path.Combine(strDestinationFullPath, Path.GetFileName(strSourceFullPath)); strDestinationFullPath = Path.Combine(
strDestinationFullPath,
Path.GetFileName(strSourceFullPath)
);
// Move the file from source to destination // Move the file from source to destination
System.IO.File.Move(strSourceFullPath, strDestinationFullPath); 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);
}
}
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