diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index eacc7211..5490daae 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -26,7 +26,7 @@ public class ConversationStorage : IConversationStorage { var agentId = dialog.CurrentAgentId; var db = _services.GetRequiredService(); - var attachment = _services.GetRequiredService(); + var fileService = _services.GetRequiredService(); var dialogElements = new List(); // Prevent duplicate record to be inserted @@ -78,7 +78,7 @@ public class ConversationStorage : IConversationStorage } db.AppendConversationDialogs(conversationId, dialogElements); - attachment.SaveConversationFiles(conversationId, dialog.Files); + fileService.SaveConversationFiles(conversationId, dialog.Files); dialog.Files.Clear(); } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 082b594c..af7c44b3 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -2,7 +2,6 @@ using BotSharp.Abstraction.Routing; using Newtonsoft.Json.Serialization; using Newtonsoft.Json; using BotSharp.Abstraction.Files.Models; -using Microsoft.AspNetCore.Hosting; using BotSharp.Abstraction.Files; namespace BotSharp.OpenAPI.Controllers; @@ -292,54 +291,4 @@ public class ConversationController : ControllerBase buffer = Encoding.UTF8.GetBytes("\n"); await response.Body.WriteAsync(buffer, 0, buffer.Length); } - - [HttpPost("/conversation/{conversationId}/attachments")] - public IActionResult UploadAttachments([FromRoute] string conversationId, - IFormFile[] files) - { - if (files != null && files.Length > 0) - { - var attachmentService = _services.GetRequiredService(); - var dir = attachmentService.GetDirectory(conversationId); - foreach (var file in files) - { - // Save the file, process it, etc. - 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); - } - } - - return Ok(new { message = "File uploaded successfully." }); - } - - return BadRequest(new { message = "Invalid file." }); - } - - [HttpGet("/conversation/{conversationId}/files/{messageId}")] - public IEnumerable GetConversationFiles([FromRoute] string conversationId, [FromRoute] string messageId) - { - var attachment = _services.GetRequiredService(); - return attachment.GetConversationFiles(conversationId, messageId); - } - - [HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}/type/{type}")] - public async Task GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, - [FromRoute] string fileName, [FromRoute] string type) - { - var attachment = _services.GetRequiredService(); - var file = attachment.GetMessageFile(conversationId, messageId, fileName, type); - if (string.IsNullOrEmpty(file)) - { - return NotFound(); - } - - 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); - return File(bytes, "application/octet-stream", Path.GetFileName(file)); - } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs new file mode 100644 index 00000000..84bb65b0 --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs @@ -0,0 +1,63 @@ +namespace BotSharp.OpenAPI.Controllers; + +[Authorize] +[ApiController] +public class FileController : ControllerBase +{ + private readonly IServiceProvider _services; + + public FileController(IServiceProvider services) + { + _services = services; + } + + [HttpPost("/conversation/{conversationId}/attachments")] + public IActionResult UploadAttachments([FromRoute] string conversationId, + IFormFile[] files) + { + if (files != null && files.Length > 0) + { + var fileService = _services.GetRequiredService(); + var dir = fileService.GetDirectory(conversationId); + foreach (var file in files) + { + // Save the file, process it, etc. + 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); + } + } + + return Ok(new { message = "File uploaded successfully." }); + } + + return BadRequest(new { message = "Invalid file." }); + } + + [HttpGet("/conversation/{conversationId}/files/{messageId}")] + public IEnumerable GetConversationFiles([FromRoute] string conversationId, [FromRoute] string messageId) + { + var fileService = _services.GetRequiredService(); + return fileService.GetConversationFiles(conversationId, messageId); + } + + [HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}/type/{type}")] + public async Task GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, + [FromRoute] string fileName, [FromRoute] string type) + { + var fileService = _services.GetRequiredService(); + var file = fileService.GetMessageFile(conversationId, messageId, fileName, type); + if (string.IsNullOrEmpty(file)) + { + return NotFound(); + } + + 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); + return File(bytes, "application/octet-stream", Path.GetFileName(file)); + } +} diff --git a/src/Infrastructure/BotSharp.OpenAPI/Using.cs b/src/Infrastructure/BotSharp.OpenAPI/Using.cs index 201bf7b1..f3ba775f 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Using.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Using.cs @@ -24,6 +24,8 @@ global using BotSharp.Abstraction.Conversations.Enums; global using BotSharp.Abstraction.Conversations.Models; global using BotSharp.Abstraction.Models; global using BotSharp.Abstraction.Repositories.Filters; +global using BotSharp.Abstraction.Files.Models; +global using BotSharp.Abstraction.Files; global using BotSharp.OpenAPI.ViewModels.Conversations; global using BotSharp.OpenAPI.ViewModels.Users; global using BotSharp.OpenAPI.ViewModels.Agents; \ No newline at end of file