diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs index 16197b9c..7f861c1f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs @@ -6,7 +6,7 @@ public interface IBotSharpFileService IEnumerable GetChatImages(string conversationId, List conversations, int offset = 2); IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, bool imageOnly = false); string GetMessageFile(string conversationId, string messageId, string fileName); - bool SaveMessageFiles(string conversationId, string messageId, List files); + Task SaveMessageFiles(string conversationId, string messageId, List files); string GetUserAvatar(); bool SaveUserAvatar(BotSharpFile file); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index d6940fe1..eef8d7dd 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(); - fileService.SaveMessageFiles(_conversationId, message.MessageId, message.Files); + await fileService.SaveMessageFiles(_conversationId, message.MessageId, 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 71e34549..50c7f487 100644 --- a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs @@ -1,4 +1,6 @@ +using BotSharp.Abstraction.Browsing; using Microsoft.AspNetCore.StaticFiles; +using Microsoft.EntityFrameworkCore; using System.IO; using System.Threading; @@ -44,7 +46,7 @@ public partial class BotSharpFileService foreach (var file in Directory.GetFiles(dir)) { var contentType = GetFileContentType(file); - if (imageOnly && !_allowedTypes.Contains(contentType)) + if (imageOnly && !_allowedImageTypes.Contains(contentType)) { continue; } @@ -81,15 +83,27 @@ public partial class BotSharpFileService return found; } - public bool SaveMessageFiles(string conversationId, string messageId, List files) + public async Task SaveMessageFiles(string conversationId, string messageId, 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); + try { + 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++) { var file = files[i]; @@ -103,7 +117,21 @@ public partial class BotSharpFileService var fileName = $"{i + 1}{fileType}"; Thread.Sleep(100); File.WriteAllBytes(Path.Combine(dir, fileName), bytes); + + if (isNeedScreenShot && _allowScreenShotTypes.Contains(fileType)) + { + var path = Path.Combine(preFixPath, fileName); + await web.GoToPage(contextId, path); + path = Path.Combine(preFixPath, $"{Guid.NewGuid()}.png"); + await web.ScreenshotAsync(contextId, path); + } } + + if (isNeedScreenShot) + { + await web.CloseBrowser(contextId); + } + return true; } catch (Exception ex) diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs index 76d26dbc..9f5e4fe9 100644 --- a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs @@ -12,7 +12,8 @@ public partial class BotSharpFileService : IBotSharpFileService private readonly IUserIdentity _user; private readonly ILogger _logger; private readonly string _baseDir; - private readonly IEnumerable _allowedTypes = new List { "image/png", "image/jpeg" }; + private readonly IEnumerable _allowedImageTypes = new List { "image/png", "image/jpeg" }; + private readonly IEnumerable _allowScreenShotTypes = new List { ".pdf" }; private const string CONVERSATION_FOLDER = "conversations"; private const string FILE_FOLDER = "files"; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index c8f573a6..0cbaf0c9 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -374,7 +374,7 @@ public class ConversationController : ControllerBase public IEnumerable GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId) { var fileService = _services.GetRequiredService(); - var files = fileService.GetMessageFiles(conversationId, new List { messageId }); + var files = fileService.GetMessageFiles(conversationId, new List { messageId }, imageOnly: true); return files?.Select(x => MessageFileViewModel.Transform(x))?.ToList() ?? new List(); }