diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/Enums/FileSourceType.cs b/src/Infrastructure/BotSharp.Abstraction/Files/Enums/FileSourceType.cs new file mode 100644 index 00000000..04c92df6 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Files/Enums/FileSourceType.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Abstraction.Files.Enums; + +public static class FileSourceType +{ + public const string User = "user"; + public const string Bot = "bot"; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs index b0e0209c..1da42cc4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs @@ -3,11 +3,11 @@ namespace BotSharp.Abstraction.Files; public interface IBotSharpFileService { string GetDirectory(string conversationId); - IEnumerable GetChatImages(string conversationId, List conversations, int? offset = null); - IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, bool imageOnly = false); - string GetMessageFile(string conversationId, string messageId, string fileName); - bool HasConversationFiles(string conversationId); - Task SaveMessageFiles(string conversationId, string messageId, List files); + IEnumerable GetChatImages(string conversationId, string source, List conversations, int? offset = null); + IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, string source, bool imageOnly = false); + string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName); + bool HasConversationUserFiles(string conversationId); + bool SaveMessageFiles(string conversationId, string messageId, string source, List files); string GetUserAvatar(); bool SaveUserAvatar(BotSharpFile file); diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/Models/MessageFileModel.cs b/src/Infrastructure/BotSharp.Abstraction/Files/Models/MessageFileModel.cs index 3ec63fd8..999f76de 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/Models/MessageFileModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/Models/MessageFileModel.cs @@ -20,6 +20,9 @@ public class MessageFileModel [JsonPropertyName("content_type")] public string ContentType { get; set; } + [JsonPropertyName("file_source")] + public string FileSource { get; set; } = FileSourceType.User; + public MessageFileModel() { diff --git a/src/Infrastructure/BotSharp.Abstraction/Using.cs b/src/Infrastructure/BotSharp.Abstraction/Using.cs index a5bbc188..89c9f7db 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Using.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Using.cs @@ -16,4 +16,5 @@ global using BotSharp.Abstraction.Routing.Planning; global using BotSharp.Abstraction.Templating; global using BotSharp.Abstraction.Translation.Attributes; global using BotSharp.Abstraction.Messaging.Enums; -global using BotSharp.Abstraction.Files.Models; \ No newline at end of file +global using BotSharp.Abstraction.Files.Models; +global using BotSharp.Abstraction.Files.Enums; \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index eef8d7dd..e4d65452 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -48,7 +48,7 @@ public partial class ConversationService // Save message files var fileService = _services.GetRequiredService(); - await fileService.SaveMessageFiles(_conversationId, message.MessageId, message.Files); + fileService.SaveMessageFiles(_conversationId, message.MessageId, FileSourceType.User, message.Files); message.Files?.Clear(); // Save payload diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs index 330eeae5..212dba8b 100644 --- a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs @@ -8,7 +8,7 @@ namespace BotSharp.Core.Files; public partial class BotSharpFileService { - public IEnumerable GetChatImages(string conversationId, List conversations, int? offset = null) + public IEnumerable GetChatImages(string conversationId, string source, List conversations, int? offset = null) { var files = new List(); if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty()) @@ -35,54 +35,60 @@ public partial class BotSharpFileService messageIds = conversations.Select(x => x.MessageId).Distinct().ToList(); } - files = GetMessageFiles(conversationId, messageIds, imageOnly: true).ToList(); + files = GetMessageFiles(conversationId, messageIds, source, imageOnly: true).ToList(); return files; } - public IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, bool imageOnly = false) + public IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, + string source, bool imageOnly = false) { var files = new List(); if (messageIds.IsNullOrEmpty()) return files; foreach (var messageId in messageIds) { - var dir = GetConversationFileDirectory(conversationId, messageId); + var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId, source); if (!ExistDirectory(dir)) { continue; } - foreach (var file in Directory.GetFiles(dir)) + foreach (var subDir in Directory.GetDirectories(dir)) { - var contentType = GetFileContentType(file); - if (imageOnly && !_allowedImageTypes.Contains(contentType)) + var index = subDir.Split(Path.DirectorySeparatorChar).Last(); + + foreach (var file in Directory.GetFiles(subDir)) { - continue; + var contentType = GetFileContentType(file); + if (imageOnly && !_allowedImageTypes.Contains(contentType)) + { + continue; + } + + var fileName = Path.GetFileNameWithoutExtension(file); + var extension = Path.GetExtension(file); + var fileType = extension.Substring(1); + + var model = new MessageFileModel() + { + MessageId = messageId, + FileUrl = $"/conversation/{conversationId}/message/{messageId}/{source}/file/{index}/{fileName}", + FileStorageUrl = file, + FileName = fileName, + FileType = fileType, + ContentType = contentType + }; + files.Add(model); } - - var fileName = Path.GetFileNameWithoutExtension(file); - var extension = Path.GetExtension(file); - var fileType = extension.Substring(1); - - var model = new MessageFileModel() - { - MessageId = messageId, - FileUrl = $"/conversation/{conversationId}/message/{messageId}/file/{fileName}", - FileStorageUrl = file, - FileName = fileName, - FileType = fileType, - ContentType = contentType - }; - files.Add(model); } } return files; } - public string GetMessageFile(string conversationId, string messageId, string fileName) + public string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName) { - var dir = GetConversationFileDirectory(conversationId, messageId); + var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId, source, index); if (!ExistDirectory(dir)) { return string.Empty; @@ -92,36 +98,36 @@ public partial class BotSharpFileService return found; } - public bool HasConversationFiles(string conversationId) + public bool HasConversationUserFiles(string conversationId) { if (string.IsNullOrEmpty(conversationId)) return false; - var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER); + var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, USER_FILE_FOLDER); if (!ExistDirectory(dir)) return false; return Directory.GetDirectories(dir).Count() > 0; } - public async Task SaveMessageFiles(string conversationId, string messageId, List files) + public bool SaveMessageFiles(string conversationId, string messageId, string source, List files) { if (files.IsNullOrEmpty()) return false; var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true); if (!ExistDirectory(dir)) return false; - var contextId = string.Empty; - var web = _services.GetRequiredService(); - var isNeedScreenShot = files.Any(x => _allowScreenShotTypes.Contains(Path.GetExtension(x.FileName))); - var preFixPath = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId); + //var contextId = string.Empty; + //var web = _services.GetRequiredService(); + //var isNeedScreenShot = files.Any(x => _allowScreenShotTypes.Contains(Path.GetExtension(x.FileName))); + //var preFixPath = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId); try { - if (isNeedScreenShot) - { - var state = _services.GetRequiredService(); - contextId = state.GetConversationId() ?? Guid.NewGuid().ToString(); - await web.LaunchBrowser(contextId, string.Empty); - } + //if (isNeedScreenShot) + //{ + // var state = _services.GetRequiredService(); + // contextId = state.GetConversationId() ?? Guid.NewGuid().ToString(); + // await web.LaunchBrowser(contextId, string.Empty); + //} for (int i = 0; i < files.Count; i++) { @@ -132,24 +138,28 @@ public partial class BotSharpFileService } var (_, bytes) = GetFileInfoFromData(file.FileData); - var fileType = Path.GetExtension(file.FileName); - var fileName = $"{i + 1}{fileType}"; Thread.Sleep(100); - File.WriteAllBytes(Path.Combine(dir, fileName), bytes); - - if (isNeedScreenShot && _allowScreenShotTypes.Contains(fileType)) + var subDir = Path.Combine(dir, source, $"{i + 1}"); + if (!ExistDirectory(subDir)) { - var path = Path.Combine(preFixPath, fileName); - await web.GoToPage(contextId, path); - path = Path.Combine(preFixPath, $"{Guid.NewGuid()}.{i + 1}.png"); - await web.ScreenshotAsync(contextId, path); + Directory.CreateDirectory(subDir); } + + File.WriteAllBytes(Path.Combine(subDir, file.FileName), bytes); + + //if (isNeedScreenShot && _allowScreenShotTypes.Contains(fileType)) + //{ + // var path = Path.Combine(preFixPath, fileName); + // await web.GoToPage(contextId, path); + // path = Path.Combine(preFixPath, $"{Guid.NewGuid()}.{i + 1}.png"); + // await web.ScreenshotAsync(contextId, path); + //} } - if (isNeedScreenShot) - { - await web.CloseBrowser(contextId); - } + //if (isNeedScreenShot) + //{ + // await web.CloseBrowser(contextId); + //} return true; } @@ -161,7 +171,6 @@ public partial class BotSharpFileService } - public bool DeleteMessageFiles(string conversationId, IEnumerable messageIds, string targetMessageId, string? newMessageId = null) { if (string.IsNullOrEmpty(conversationId) || messageIds == null) return false; @@ -179,6 +188,13 @@ public partial class BotSharpFileService } Directory.Move(prevDir, newDir); + Thread.Sleep(100); + + var botDir = Path.Combine(newDir, BOT_FILE_FOLDER); + if (ExistDirectory(botDir)) + { + Directory.Delete(botDir, true); + } } } diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs index 9f5e4fe9..a195af01 100644 --- a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs @@ -17,6 +17,8 @@ public partial class BotSharpFileService : IBotSharpFileService private const string CONVERSATION_FOLDER = "conversations"; private const string FILE_FOLDER = "files"; + private const string USER_FILE_FOLDER = "user"; + private const string BOT_FILE_FOLDER = "bot"; private const string USERS_FOLDER = "users"; private const string USER_AVATAR_FOLDER = "avatar"; diff --git a/src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs b/src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs index d62e331b..9c657f7c 100644 --- a/src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs @@ -9,7 +9,7 @@ public class FilePlugin : IBotSharpPlugin public string Name => "File"; - public string Description => "Provides file processing funcationality."; + public string Description => "Provides file analysis."; public void RegisterDI(IServiceCollection services, IConfiguration config) diff --git a/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs b/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs index 4bf7a0f0..3db691e2 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Functions/LoadAttachmentFn.cs @@ -50,7 +50,7 @@ public class LoadAttachmentFn : IFunctionCallback } var fileService = _services.GetRequiredService(); - var files = fileService.GetChatImages(conversationId, dialogs); + var files = fileService.GetChatImages(conversationId, FileSourceType.User, dialogs); foreach (var dialog in dialogs) { diff --git a/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs b/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs index 9cbd0d3f..b8062acc 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs @@ -18,7 +18,7 @@ public class AttachmentProcessingHook : AgentHookBase { var fileService = _services.GetRequiredService(); var conv = _services.GetRequiredService(); - var hasConvFiles = fileService.HasConversationFiles(conv.ConversationId); + var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId); if (hasConvFiles) { diff --git a/src/Infrastructure/BotSharp.Core/Using.cs b/src/Infrastructure/BotSharp.Core/Using.cs index a54df964..af7bc560 100644 --- a/src/Infrastructure/BotSharp.Core/Using.cs +++ b/src/Infrastructure/BotSharp.Core/Using.cs @@ -25,6 +25,7 @@ global using BotSharp.Abstraction.Repositories.Filters; global using BotSharp.Abstraction.Translation; global using BotSharp.Abstraction.Files; global using BotSharp.Abstraction.Files.Models; +global using BotSharp.Abstraction.Files.Enums; global using BotSharp.Abstraction.Translation.Attributes; global using BotSharp.Abstraction.Messaging.Enums; global using BotSharp.Abstraction.Http.Settings; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 0cbaf0c9..3f3ba8ed 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -370,19 +370,19 @@ public class ConversationController : ControllerBase return BadRequest(new { message = "Invalid file." }); } - [HttpGet("/conversation/{conversationId}/files/{messageId}")] - public IEnumerable GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId) + [HttpGet("/conversation/{conversationId}/files/{messageId}/{source}")] + public IEnumerable GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source) { var fileService = _services.GetRequiredService(); - var files = fileService.GetMessageFiles(conversationId, new List { messageId }, imageOnly: true); + var files = fileService.GetMessageFiles(conversationId, new List { messageId }, source, imageOnly: false); return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List(); } - [HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")] - public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName) + [HttpGet("/conversation/{conversationId}/message/{messageId}/{source}/file/{index}/{fileName}")] + public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source, [FromRoute] string index, [FromRoute] string fileName) { var fileService = _services.GetRequiredService(); - var file = fileService.GetMessageFile(conversationId, messageId, fileName); + var file = fileService.GetMessageFile(conversationId, messageId, source, index, fileName); if (string.IsNullOrEmpty(file)) { return NotFound(); diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs b/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs index 79cb803a..77370893 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs @@ -36,7 +36,7 @@ public class WebSocketsMiddleware { var regexes = new List { - new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/file/[a-z0-9-]+", RegexOptions.IgnoreCase), + new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/[a-z]+/file/[a-z0-9-]+/[a-z0-9-]+", RegexOptions.IgnoreCase), new Regex(@"/user/avatar", RegexOptions.IgnoreCase) };