Merge pull request #476 from iceljc/bugfix/refine-file-controller
clean file controller
This commit is contained in:
commit
1c9161e4ea
|
|
@ -219,15 +219,7 @@ public class ConversationController : ControllerBase
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetStates(IConversationService conv, NewMessageModel input)
|
#region Send message
|
||||||
{
|
|
||||||
conv.States.SetState("channel", input.Channel, source: StateSource.External)
|
|
||||||
.SetState("provider", input.Provider, source: StateSource.External)
|
|
||||||
.SetState("model", input.Model, source: StateSource.External)
|
|
||||||
.SetState("temperature", input.Temperature, source: StateSource.External)
|
|
||||||
.SetState("sampling_factor", input.SamplingFactor, source: StateSource.External);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost("/conversation/{agentId}/{conversationId}")]
|
[HttpPost("/conversation/{agentId}/{conversationId}")]
|
||||||
public async Task<ChatResponseModel> SendMessage([FromRoute] string agentId,
|
public async Task<ChatResponseModel> SendMessage([FromRoute] string agentId,
|
||||||
[FromRoute] string conversationId,
|
[FromRoute] string conversationId,
|
||||||
|
|
@ -350,6 +342,73 @@ public class ConversationController : ControllerBase
|
||||||
|
|
||||||
// await OnEventCompleted(Response);
|
// await OnEventCompleted(Response);
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Files and attachments
|
||||||
|
[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<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId)
|
||||||
|
{
|
||||||
|
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||||
|
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId });
|
||||||
|
return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List<MessageFileViewModel>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")]
|
||||||
|
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
|
||||||
|
{
|
||||||
|
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||||
|
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
|
||||||
|
if (string.IsNullOrEmpty(file))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
return BuildFileResult(file);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Private methods
|
||||||
|
private void SetStates(IConversationService conv, NewMessageModel input)
|
||||||
|
{
|
||||||
|
conv.States.SetState("channel", input.Channel, source: StateSource.External)
|
||||||
|
.SetState("provider", input.Provider, source: StateSource.External)
|
||||||
|
.SetState("model", input.Model, source: StateSource.External)
|
||||||
|
.SetState("temperature", input.Temperature, source: StateSource.External)
|
||||||
|
.SetState("sampling_factor", input.SamplingFactor, source: StateSource.External);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
return File(bytes, "application/octet-stream", Path.GetFileName(file));
|
||||||
|
}
|
||||||
|
|
||||||
private async Task OnChunkReceived(HttpResponse response, ChatResponseModel message)
|
private async Task OnChunkReceived(HttpResponse response, ChatResponseModel message)
|
||||||
{
|
{
|
||||||
|
|
@ -391,4 +450,5 @@ public class ConversationController : ControllerBase
|
||||||
|
|
||||||
return jsonOption;
|
return jsonOption;
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,77 +10,4 @@ public class FileController : ControllerBase
|
||||||
{
|
{
|
||||||
_services = 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<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId)
|
|
||||||
{
|
|
||||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
|
||||||
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId });
|
|
||||||
return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List<MessageFileViewModel>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")]
|
|
||||||
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
|
|
||||||
{
|
|
||||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
|
||||||
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
|
|
||||||
if (string.IsNullOrEmpty(file))
|
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
return File(bytes, "application/octet-stream", Path.GetFileName(file));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -106,4 +106,36 @@ public class UserController : ControllerBase
|
||||||
{
|
{
|
||||||
return await _userService.VerifyEmailUnique(email);
|
return await _userService.VerifyEmailUnique(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Avatar
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#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);
|
||||||
|
return File(bytes, "application/octet-stream", Path.GetFileName(file));
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue