Skip to content

Commit 1c9161e

Browse files
authored
Merge pull request #476 from iceljc/bugfix/refine-file-controller
clean file controller
2 parents 3233ac7 + 89eb725 commit 1c9161e

File tree

3 files changed

+101
-82
lines changed

3 files changed

+101
-82
lines changed

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

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,7 @@ public async Task<bool> DeleteConversationMessage([FromRoute] string conversatio
219219
return response;
220220
}
221221

222-
private void SetStates(IConversationService conv, NewMessageModel input)
223-
{
224-
conv.States.SetState("channel", input.Channel, source: StateSource.External)
225-
.SetState("provider", input.Provider, source: StateSource.External)
226-
.SetState("model", input.Model, source: StateSource.External)
227-
.SetState("temperature", input.Temperature, source: StateSource.External)
228-
.SetState("sampling_factor", input.SamplingFactor, source: StateSource.External);
229-
}
230-
222+
#region Send message
231223
[HttpPost("/conversation/{agentId}/{conversationId}")]
232224
public async Task<ChatResponseModel> SendMessage([FromRoute] string agentId,
233225
[FromRoute] string conversationId,
@@ -350,6 +342,73 @@ await conv.SendMessage(agentId, inputMsg,
350342

351343
// await OnEventCompleted(Response);
352344
}
345+
#endregion
346+
347+
#region Files and attachments
348+
[HttpPost("/conversation/{conversationId}/attachments")]
349+
public IActionResult UploadAttachments([FromRoute] string conversationId,
350+
IFormFile[] files)
351+
{
352+
if (files != null && files.Length > 0)
353+
{
354+
var fileService = _services.GetRequiredService<IBotSharpFileService>();
355+
var dir = fileService.GetDirectory(conversationId);
356+
foreach (var file in files)
357+
{
358+
// Save the file, process it, etc.
359+
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
360+
var filePath = Path.Combine(dir, fileName);
361+
362+
using (var stream = new FileStream(filePath, FileMode.Create))
363+
{
364+
file.CopyTo(stream);
365+
}
366+
}
367+
368+
return Ok(new { message = "File uploaded successfully." });
369+
}
370+
371+
return BadRequest(new { message = "Invalid file." });
372+
}
373+
374+
[HttpGet("/conversation/{conversationId}/files/{messageId}")]
375+
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId)
376+
{
377+
var fileService = _services.GetRequiredService<IBotSharpFileService>();
378+
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId });
379+
return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List<MessageFileViewModel>();
380+
}
381+
382+
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")]
383+
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
384+
{
385+
var fileService = _services.GetRequiredService<IBotSharpFileService>();
386+
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
387+
if (string.IsNullOrEmpty(file))
388+
{
389+
return NotFound();
390+
}
391+
return BuildFileResult(file);
392+
}
393+
#endregion
394+
395+
#region Private methods
396+
private void SetStates(IConversationService conv, NewMessageModel input)
397+
{
398+
conv.States.SetState("channel", input.Channel, source: StateSource.External)
399+
.SetState("provider", input.Provider, source: StateSource.External)
400+
.SetState("model", input.Model, source: StateSource.External)
401+
.SetState("temperature", input.Temperature, source: StateSource.External)
402+
.SetState("sampling_factor", input.SamplingFactor, source: StateSource.External);
403+
}
404+
405+
private FileContentResult BuildFileResult(string file)
406+
{
407+
using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
408+
var bytes = new byte[stream.Length];
409+
stream.Read(bytes, 0, (int)stream.Length);
410+
return File(bytes, "application/octet-stream", Path.GetFileName(file));
411+
}
353412

354413
private async Task OnChunkReceived(HttpResponse response, ChatResponseModel message)
355414
{
@@ -391,4 +450,5 @@ private JsonSerializerOptions InitJsonOptions(BotSharpOptions options)
391450

392451
return jsonOption;
393452
}
453+
#endregion
394454
}

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

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,77 +10,4 @@ public FileController(IServiceProvider services)
1010
{
1111
_services = services;
1212
}
13-
14-
[HttpPost("/conversation/{conversationId}/attachments")]
15-
public IActionResult UploadAttachments([FromRoute] string conversationId,
16-
IFormFile[] files)
17-
{
18-
if (files != null && files.Length > 0)
19-
{
20-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
21-
var dir = fileService.GetDirectory(conversationId);
22-
foreach (var file in files)
23-
{
24-
// Save the file, process it, etc.
25-
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
26-
var filePath = Path.Combine(dir, fileName);
27-
28-
using (var stream = new FileStream(filePath, FileMode.Create))
29-
{
30-
file.CopyTo(stream);
31-
}
32-
}
33-
34-
return Ok(new { message = "File uploaded successfully." });
35-
}
36-
37-
return BadRequest(new { message = "Invalid file." });
38-
}
39-
40-
[HttpGet("/conversation/{conversationId}/files/{messageId}")]
41-
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId)
42-
{
43-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
44-
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId });
45-
return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List<MessageFileViewModel>();
46-
}
47-
48-
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")]
49-
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
50-
{
51-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
52-
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
53-
if (string.IsNullOrEmpty(file))
54-
{
55-
return NotFound();
56-
}
57-
return BuildFileResult(file);
58-
}
59-
60-
[HttpPost("/user/avatar")]
61-
public bool UploadUserAvatar([FromBody] BotSharpFile file)
62-
{
63-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
64-
return fileService.SaveUserAvatar(file);
65-
}
66-
67-
[HttpGet("/user/avatar")]
68-
public IActionResult GetUserAvatar()
69-
{
70-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
71-
var file = fileService.GetUserAvatar();
72-
if (string.IsNullOrEmpty(file))
73-
{
74-
return NotFound();
75-
}
76-
return BuildFileResult(file);
77-
}
78-
79-
private FileContentResult BuildFileResult(string file)
80-
{
81-
using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
82-
var bytes = new byte[stream.Length];
83-
stream.Read(bytes, 0, (int)stream.Length);
84-
return File(bytes, "application/octet-stream", Path.GetFileName(file));
85-
}
8613
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,36 @@ public async Task<bool> VerifyEmailUnique([FromQuery] string email)
106106
{
107107
return await _userService.VerifyEmailUnique(email);
108108
}
109+
110+
#region Avatar
111+
[HttpPost("/user/avatar")]
112+
public bool UploadUserAvatar([FromBody] BotSharpFile file)
113+
{
114+
var fileService = _services.GetRequiredService<IBotSharpFileService>();
115+
return fileService.SaveUserAvatar(file);
116+
}
117+
118+
[HttpGet("/user/avatar")]
119+
public IActionResult GetUserAvatar()
120+
{
121+
var fileService = _services.GetRequiredService<IBotSharpFileService>();
122+
var file = fileService.GetUserAvatar();
123+
if (string.IsNullOrEmpty(file))
124+
{
125+
return NotFound();
126+
}
127+
return BuildFileResult(file);
128+
}
129+
#endregion
130+
131+
132+
#region Private methods
133+
private FileContentResult BuildFileResult(string file)
134+
{
135+
using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
136+
var bytes = new byte[stream.Length];
137+
stream.Read(bytes, 0, (int)stream.Length);
138+
return File(bytes, "application/octet-stream", Path.GetFileName(file));
139+
}
140+
#endregion
109141
}

0 commit comments

Comments
 (0)