BotSharp/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs

87 lines
3.1 KiB
C#
Raw Normal View History

2024-05-06 07:59:03 +00:00
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}")]
2024-05-13 21:53:41 +00:00
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId)
2024-05-06 07:59:03 +00:00
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
2024-05-13 21:53:41 +00:00
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId });
return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List<MessageFileViewModel>();
2024-05-06 07:59:03 +00:00
}
2024-05-06 22:31:52 +00:00
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")]
2024-05-22 04:33:31 +00:00
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
2024-05-06 07:59:03 +00:00
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
2024-05-06 22:31:52 +00:00
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
2024-05-06 07:59:03 +00:00
if (string.IsNullOrEmpty(file))
{
return NotFound();
}
2024-05-22 04:33:31 +00:00
return BuildFileResult(file);
}
[HttpPost("/user/avatar")]
public bool UploadUserAvatar([FromBody] BotSharpFile file)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
return fileService.SaveUserAvatar(file);
}
[HttpGet("/user/avatar")]
public IActionResult GetUserAvatar()
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var file = fileService.GetUserAvatar();
if (string.IsNullOrEmpty(file))
{
return NotFound();
}
return BuildFileResult(file);
}
2024-05-06 07:59:03 +00:00
2024-05-22 04:33:31 +00:00
private FileContentResult BuildFileResult(string file)
{
2024-05-06 07:59:03 +00:00
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));
}
}