diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs index b6e4e1e0..dd91a2bb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs @@ -1,3 +1,5 @@ +using System.IO; + namespace BotSharp.Abstraction.Files; public interface IBotSharpFileService @@ -74,5 +76,7 @@ public interface IBotSharpFileService (string, byte[]) GetFileInfoFromData(string data); string GetDirectory(string conversationId); string GetFileContentType(string filePath); + byte[] GetFileBytes(string fileStorageUrl); + bool SavefileToPath(string filePath, Stream stream); #endregion } diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Common.cs b/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Common.cs index 6f3341d6..be5f3180 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Common.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Common.cs @@ -43,4 +43,21 @@ public partial class BotSharpFileService return contentType; } + + public byte[] GetFileBytes(string fileStorageUrl) + { + using var stream = File.OpenRead(fileStorageUrl); + var bytes = new byte[stream.Length]; + stream.Read(bytes, 0, (int)stream.Length); + return bytes; + } + + public bool SavefileToPath(string filePath, Stream stream) + { + using (var fileStream = new FileStream(filePath, FileMode.Create)) + { + stream.CopyTo(fileStream); + } + return true; + } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index effc0392..22649d72 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -356,10 +356,7 @@ public class ConversationController : ControllerBase var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var filePath = Path.Combine(dir, fileName); - using (var stream = new FileStream(filePath, FileMode.Create)) - { - file.CopyTo(stream); - } + fileService.SavefileToPath(filePath, file.OpenReadStream()); } return Ok(new { message = "File uploaded successfully." }); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index 03b33a95..966f51c9 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -158,9 +158,8 @@ public class UserController : ControllerBase #region Private methods private FileContentResult BuildFileResult(string file) { - using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read); - var bytes = new byte[stream.Length]; - stream.Read(bytes, 0, (int)stream.Length); + var fileService = _services.GetRequiredService(); + var bytes = fileService.GetFileBytes(file); return File(bytes, "application/octet-stream", Path.GetFileName(file)); } #endregion