From 8c060d12f473fe5c56a218335a1e4ead1591643a Mon Sep 17 00:00:00 2001 From: sivakumar <93644655+sivakumars3442@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:49:46 +0530 Subject: [PATCH 1/2] 902938: Provided Chunk upload support for File Manager component. --- Controllers/FileManagerAccessController.cs | 4 +- Controllers/FileManagerController.cs | 5 +- Controllers/VirtualizationController.cs | 4 +- Models/Base/FileProviderBase.cs | 2 +- Models/PhysicalFileProvider.cs | 58 ++++++++++++++++++---- 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/Controllers/FileManagerAccessController.cs b/Controllers/FileManagerAccessController.cs index 89ede20..74930f2 100644 --- a/Controllers/FileManagerAccessController.cs +++ b/Controllers/FileManagerAccessController.cs @@ -72,7 +72,7 @@ public object FileOperations([FromBody] FileManagerDirectoryContent args) // uploads the file(s) into a specified path [Route("Upload")] - public IActionResult Upload(string path, IList uploadFiles, string action) + public IActionResult Upload(string path, long size, IList uploadFiles, string action) { try { @@ -98,7 +98,7 @@ public IActionResult Upload(string path, IList uploadFiles, string ac } } } - uploadResponse = operation.Upload(path, uploadFiles, action, null); + uploadResponse = operation.Upload(path, uploadFiles, action, size, null); if (uploadResponse.Error != null) { Response.Clear(); diff --git a/Controllers/FileManagerController.cs b/Controllers/FileManagerController.cs index e8cfea8..a34659a 100644 --- a/Controllers/FileManagerController.cs +++ b/Controllers/FileManagerController.cs @@ -71,7 +71,8 @@ public object FileOperations([FromBody] FileManagerDirectoryContent args) // uploads the file(s) into a specified path [Route("Upload")] - public IActionResult Upload(string path, IList uploadFiles, string action) + [DisableRequestSizeLimit] + public IActionResult Upload(string path, long size, IList uploadFiles, string action) { try { @@ -97,7 +98,7 @@ public IActionResult Upload(string path, IList uploadFiles, string ac } } } - uploadResponse = operation.Upload(path, uploadFiles, action, null); + uploadResponse = operation.Upload(path, uploadFiles, action, size, null); if (uploadResponse.Error != null) { Response.Clear(); diff --git a/Controllers/VirtualizationController.cs b/Controllers/VirtualizationController.cs index 5031f5c..593c773 100644 --- a/Controllers/VirtualizationController.cs +++ b/Controllers/VirtualizationController.cs @@ -71,7 +71,7 @@ public object FileOperations([FromBody] FileManagerDirectoryContent args) // uploads the file(s) into a specified path [Route("Upload")] - public IActionResult Upload(string path, IList uploadFiles, string action) + public IActionResult Upload(string path, long size, IList uploadFiles, string action) { try { @@ -97,7 +97,7 @@ public IActionResult Upload(string path, IList uploadFiles, string ac } } } - uploadResponse = operation.Upload(path, uploadFiles, action, null); + uploadResponse = operation.Upload(path, uploadFiles, action, size, null); if (uploadResponse.Error != null) { Response.Clear(); diff --git a/Models/Base/FileProviderBase.cs b/Models/Base/FileProviderBase.cs index d971dcc..061b384 100644 --- a/Models/Base/FileProviderBase.cs +++ b/Models/Base/FileProviderBase.cs @@ -32,7 +32,7 @@ public interface FileProviderBase #if EJ2_DNX FileManagerResponse Upload(string path, IList uploadFiles, string action, params FileManagerDirectoryContent[] data); #else - FileManagerResponse Upload(string path, IList uploadFiles, string action, params FileManagerDirectoryContent[] data); + FileManagerResponse Upload(string path, IList uploadFiles, string action, long size, params FileManagerDirectoryContent[] data); #endif FileStreamResult GetImage(string path, string id, bool allowCompress, ImageSize size, params FileManagerDirectoryContent[] data); diff --git a/Models/PhysicalFileProvider.cs b/Models/PhysicalFileProvider.cs index a655eb7..d7abdd7 100644 --- a/Models/PhysicalFileProvider.cs +++ b/Models/PhysicalFileProvider.cs @@ -1276,7 +1276,7 @@ protected virtual ImageSize FindRatio(ImageSize originalSize, ImageSize targetSi #if EJ2_DNX public virtual FileManagerResponse Upload(string path, IList uploadFiles, string action, params FileManagerDirectoryContent[] data) #else - public virtual FileManagerResponse Upload(string path, IList uploadFiles, string action, params FileManagerDirectoryContent[] data) + public virtual FileManagerResponse Upload(string path, IList uploadFiles, string action, long size, params FileManagerDirectoryContent[] data) #endif { FileManagerResponse uploadResponse = new FileManagerResponse(); @@ -1317,15 +1317,27 @@ public virtual FileManagerResponse Upload(string path, IList uploadFi throw new UnauthorizedAccessException("Access denied for Directory-traversal"); } #endif + long fileLength = File.Exists(fullName) ? new FileInfo(fullName).Length : default; if (action == "save") { - if (!System.IO.File.Exists(fullName)) + bool isValidChunkUpload = file.ContentType == "application/octet-stream" && (fileLength != size); + if (!System.IO.File.Exists(fullName) || isValidChunkUpload) { #if !EJ2_DNX - using (FileStream fs = System.IO.File.Create(fullName)) + if (file.ContentType == "application/octet-stream") //Handle chunk upload { - file.CopyTo(fs); - fs.Flush(); + using (var fileStream = new FileStream(fullName, FileMode.Append)) + { + file.CopyTo(fileStream); + } + } + else //Handle normal upload + { + using (FileStream fs = System.IO.File.Create(fullName)) + { + file.CopyTo(fs); + fs.Flush(); + } } #else file.SaveAs(fullName); @@ -1356,11 +1368,22 @@ public virtual FileManagerResponse Upload(string path, IList uploadFi { System.IO.File.Delete(fullName); } + bool isValidChunkUpload = file.ContentType == "application/octet-stream" && (fileLength != size); #if !EJ2_DNX - using (FileStream fs = System.IO.File.Create(fullName)) + if (file.ContentType == "application/octet-stream") //Handle chunk upload { - file.CopyTo(fs); - fs.Flush(); + using (var fileStream = new FileStream(fullName, FileMode.Append)) + { + file.CopyTo(fileStream); + } + } + else //Handle normal upload + { + using (FileStream fs = System.IO.File.Create(fullName)) + { + file.CopyTo(fs); + fs.Flush(); + } } #else file.SaveAs(fullName); @@ -1379,10 +1402,23 @@ public virtual FileManagerResponse Upload(string path, IList uploadFi } newName = newName + (fileCount > 0 ? "(" + fileCount.ToString() + ")" : "") + Path.GetExtension(name); #if !EJ2_DNX - using (FileStream fs = System.IO.File.Create(newName)) + long newFileLength = File.Exists(newName) ? new FileInfo(newName).Length : default; + bool isValidChunkUpload = file.ContentType == "application/octet-stream" && (newFileLength != size); + + if (file.ContentType == "application/octet-stream") //Handle chunk upload { - file.CopyTo(fs); - fs.Flush(); + using (var fileStream = new FileStream(fullName, FileMode.Append)) + { + file.CopyTo(fileStream); + } + } + else //Handle normal upload + { + using (FileStream fs = System.IO.File.Create(newName)) + { + file.CopyTo(fs); + fs.Flush(); + } } #else file.SaveAs(newName); From 0e26becd58b08a34cf0fe7c5b88b2aa587ad5aed Mon Sep 17 00:00:00 2001 From: sivakumar <93644655+sivakumars3442@users.noreply.github.com> Date: Wed, 25 Sep 2024 20:51:07 +0530 Subject: [PATCH 2/2] 902938: Provided Chunk upload support for File Manager component. --- Models/PhysicalFileProvider.cs | 72 ++++++++++++---------------------- 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/Models/PhysicalFileProvider.cs b/Models/PhysicalFileProvider.cs index d7abdd7..5b76f9f 100644 --- a/Models/PhysicalFileProvider.cs +++ b/Models/PhysicalFileProvider.cs @@ -1324,21 +1324,7 @@ public virtual FileManagerResponse Upload(string path, IList uploadFi if (!System.IO.File.Exists(fullName) || isValidChunkUpload) { #if !EJ2_DNX - if (file.ContentType == "application/octet-stream") //Handle chunk upload - { - using (var fileStream = new FileStream(fullName, FileMode.Append)) - { - file.CopyTo(fileStream); - } - } - else //Handle normal upload - { - using (FileStream fs = System.IO.File.Create(fullName)) - { - file.CopyTo(fs); - fs.Flush(); - } - } + PerformUpload(file, fileLength, size, fullName); #else file.SaveAs(fullName); #endif @@ -1368,23 +1354,8 @@ public virtual FileManagerResponse Upload(string path, IList uploadFi { System.IO.File.Delete(fullName); } - bool isValidChunkUpload = file.ContentType == "application/octet-stream" && (fileLength != size); #if !EJ2_DNX - if (file.ContentType == "application/octet-stream") //Handle chunk upload - { - using (var fileStream = new FileStream(fullName, FileMode.Append)) - { - file.CopyTo(fileStream); - } - } - else //Handle normal upload - { - using (FileStream fs = System.IO.File.Create(fullName)) - { - file.CopyTo(fs); - fs.Flush(); - } - } + PerformUpload(file, fileLength, size, fullName); #else file.SaveAs(fullName); #endif @@ -1403,23 +1374,7 @@ public virtual FileManagerResponse Upload(string path, IList uploadFi newName = newName + (fileCount > 0 ? "(" + fileCount.ToString() + ")" : "") + Path.GetExtension(name); #if !EJ2_DNX long newFileLength = File.Exists(newName) ? new FileInfo(newName).Length : default; - bool isValidChunkUpload = file.ContentType == "application/octet-stream" && (newFileLength != size); - - if (file.ContentType == "application/octet-stream") //Handle chunk upload - { - using (var fileStream = new FileStream(fullName, FileMode.Append)) - { - file.CopyTo(fileStream); - } - } - else //Handle normal upload - { - using (FileStream fs = System.IO.File.Create(newName)) - { - file.CopyTo(fs); - fs.Flush(); - } - } + PerformUpload(file, newFileLength, size, newName); #else file.SaveAs(newName); #endif @@ -1447,6 +1402,27 @@ public virtual FileManagerResponse Upload(string path, IList uploadFi return uploadResponse; } } + + private void PerformUpload(IFormFile file, long fileLength, long size, string name) + { + bool isValidChunkUpload = file.ContentType == "application/octet-stream" && (fileLength != size); + + if (file.ContentType == "application/octet-stream") + { + using (var fileStream = new FileStream(name, FileMode.Append)) + { + file.CopyTo(fileStream); + } + } + else + { + using (FileStream fs = System.IO.File.Create(name)) + { + file.CopyTo(fs); + fs.Flush(); + } + } + } #if SyncfusionFramework4_0 public virtual void Download(string path, string[] names, params FileManagerDirectoryContent[] data) {