add file controller

This commit is contained in:
Jicheng Lu 2024-05-06 02:59:03 -05:00
parent 78fcda634b
commit 8d90b3a7e2
4 changed files with 67 additions and 53 deletions

View file

@ -26,7 +26,7 @@ public class ConversationStorage : IConversationStorage
{
var agentId = dialog.CurrentAgentId;
var db = _services.GetRequiredService<IBotSharpRepository>();
var attachment = _services.GetRequiredService<IBotSharpFileService>();
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var dialogElements = new List<DialogElement>();
// 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();
}

View file

@ -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<IBotSharpFileService>();
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<OutputFileModel> GetConversationFiles([FromRoute] string conversationId, [FromRoute] string messageId)
{
var attachment = _services.GetRequiredService<IBotSharpFileService>();
return attachment.GetConversationFiles(conversationId, messageId);
}
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}/type/{type}")]
public async Task<IActionResult> GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId,
[FromRoute] string fileName, [FromRoute] string type)
{
var attachment = _services.GetRequiredService<IBotSharpFileService>();
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));
}
}

View file

@ -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<IBotSharpFileService>();
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<OutputFileModel> GetConversationFiles([FromRoute] string conversationId, [FromRoute] string messageId)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
return fileService.GetConversationFiles(conversationId, messageId);
}
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}/type/{type}")]
public async Task<IActionResult> GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId,
[FromRoute] string fileName, [FromRoute] string type)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
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));
}
}

View file

@ -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;