Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add chat file download #723

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace BotSharp.Abstraction.Files.Models;
public class FileInformation
{
/// <summary>
/// External file url
/// External file url for display
/// </summary>
[JsonPropertyName("file_url")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down Expand Up @@ -35,4 +35,11 @@ public class FileInformation
[JsonPropertyName("file_extension")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FileExtension { get; set; } = string.Empty;

/// <summary>
/// External file url for download
/// </summary>
[JsonPropertyName("file_download_url")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FileDownloadUrl { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<Agent> CreateAgent(Agent agent)
{
UserId = user.Id,
AgentId = agentRecord.Id,
Actions = new List<string> { UserAction.Edit },
Actions = new List<string> { UserAction.Edit, UserAction.Chat },
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnu
{
MessageId = messageId,
FileUrl = $"/conversation/{conversationId}/message/{messageId}/{source}/file/{index}/{fileName}",
FileDownloadUrl = $"/conversation/{conversationId}/message/{messageId}/{source}/file/{index}/{fileName}/download",
FileStorageUrl = file,
FileName = fileName,
FileExtension = fileExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Azure;
using BotSharp.Abstraction.Files.Constants;
using BotSharp.Abstraction.Files.Enums;
using BotSharp.Abstraction.Files.Utilities;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Users.Enums;
Expand Down Expand Up @@ -488,6 +489,26 @@ public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRout
}
return BuildFileResult(file);
}

[HttpGet("/conversation/{conversationId}/message/{messageId}/{source}/file/{index}/{fileName}/download")]
public IActionResult DownloadMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source, [FromRoute] string index, [FromRoute] string fileName)
{
var fileStorage = _services.GetRequiredService<IFileStorageService>();
var file = fileStorage.GetMessageFile(conversationId, messageId, source, index, fileName);
if (string.IsNullOrEmpty(file))
{
return NotFound();
}

var fName = file.Split(Path.DirectorySeparatorChar).Last();
var contentType = FileUtility.GetFileContentType(fName);
var 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);
stream.Position = 0;

return new FileStreamResult(stream, contentType) { FileDownloadName = fName };
}
#endregion

#region Private methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class MessageFileViewModel
[JsonPropertyName("file_source")]
public string FileSource { get; set; }

[JsonPropertyName("file_download_url")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FileDownloadUrl { get; set; }

public MessageFileViewModel()
{

Expand All @@ -32,7 +36,8 @@ public static MessageFileViewModel Transform(MessageFileModel model)
FileName = model.FileName,
FileExtension = model.FileExtension,
ContentType = model.ContentType,
FileSource = model.FileSource
FileSource = model.FileSource,
FileDownloadUrl = model.FileDownloadUrl
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private bool VerifyGetRequest(HttpRequest request)
var regexes = new List<Regex>
{
new Regex(@"/conversation/(.*?)/message/(.*?)/(.*?)/file/(.*?)/(.*?)", RegexOptions.IgnoreCase),
new Regex(@"/conversation/(.*?)/message/(.*?)/(.*?)/file/(.*?)/(.*?)/download", RegexOptions.IgnoreCase),
new Regex(@"/user/avatar", RegexOptions.IgnoreCase),
new Regex(@"/knowledge/document/(.*?)/file/(.*?)", RegexOptions.IgnoreCase)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnu
{
MessageId = messageId,
FileUrl = BuilFileUrl(file),
FileDownloadUrl = BuilFileUrl(file),
FileStorageUrl = file,
FileName = fileName,
FileExtension = fileExtension,
Expand Down