add file source type
This commit is contained in:
parent
a66df13706
commit
c1687e8c45
|
|
@ -0,0 +1,7 @@
|
|||
namespace BotSharp.Abstraction.Files.Enums;
|
||||
|
||||
public static class FileSourceType
|
||||
{
|
||||
public const string User = "user";
|
||||
public const string Bot = "bot";
|
||||
}
|
||||
|
|
@ -3,11 +3,11 @@ namespace BotSharp.Abstraction.Files;
|
|||
public interface IBotSharpFileService
|
||||
{
|
||||
string GetDirectory(string conversationId);
|
||||
IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int? offset = null);
|
||||
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, bool imageOnly = false);
|
||||
string GetMessageFile(string conversationId, string messageId, string fileName);
|
||||
bool HasConversationFiles(string conversationId);
|
||||
Task<bool> SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files);
|
||||
IEnumerable<MessageFileModel> GetChatImages(string conversationId, string source, List<RoleDialogModel> conversations, int? offset = null);
|
||||
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> 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<BotSharpFile> files);
|
||||
|
||||
string GetUserAvatar();
|
||||
bool SaveUserAvatar(BotSharpFile file);
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
global using BotSharp.Abstraction.Files.Models;
|
||||
global using BotSharp.Abstraction.Files.Enums;
|
||||
|
|
@ -48,7 +48,7 @@ public partial class ConversationService
|
|||
|
||||
// Save message files
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
await fileService.SaveMessageFiles(_conversationId, message.MessageId, message.Files);
|
||||
fileService.SaveMessageFiles(_conversationId, message.MessageId, FileSourceType.User, message.Files);
|
||||
message.Files?.Clear();
|
||||
|
||||
// Save payload
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ namespace BotSharp.Core.Files;
|
|||
|
||||
public partial class BotSharpFileService
|
||||
{
|
||||
public IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int? offset = null)
|
||||
public IEnumerable<MessageFileModel> GetChatImages(string conversationId, string source, List<RoleDialogModel> conversations, int? offset = null)
|
||||
{
|
||||
var files = new List<MessageFileModel>();
|
||||
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<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, bool imageOnly = false)
|
||||
public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds,
|
||||
string source, bool imageOnly = false)
|
||||
{
|
||||
var files = new List<MessageFileModel>();
|
||||
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<bool> SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files)
|
||||
public bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> 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<IWebBrowser>();
|
||||
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<IWebBrowser>();
|
||||
//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<IConversationStateService>();
|
||||
contextId = state.GetConversationId() ?? Guid.NewGuid().ToString();
|
||||
await web.LaunchBrowser(contextId, string.Empty);
|
||||
}
|
||||
//if (isNeedScreenShot)
|
||||
//{
|
||||
// var state = _services.GetRequiredService<IConversationStateService>();
|
||||
// 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<string> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public class LoadAttachmentFn : IFunctionCallback
|
|||
}
|
||||
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
var files = fileService.GetChatImages(conversationId, dialogs);
|
||||
var files = fileService.GetChatImages(conversationId, FileSourceType.User, dialogs);
|
||||
|
||||
foreach (var dialog in dialogs)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ public class AttachmentProcessingHook : AgentHookBase
|
|||
{
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var hasConvFiles = fileService.HasConversationFiles(conv.ConversationId);
|
||||
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
|
||||
|
||||
if (hasConvFiles)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -370,19 +370,19 @@ public class ConversationController : ControllerBase
|
|||
return BadRequest(new { message = "Invalid file." });
|
||||
}
|
||||
|
||||
[HttpGet("/conversation/{conversationId}/files/{messageId}")]
|
||||
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId)
|
||||
[HttpGet("/conversation/{conversationId}/files/{messageId}/{source}")]
|
||||
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source)
|
||||
{
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId }, imageOnly: true);
|
||||
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId }, source, imageOnly: false);
|
||||
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)
|
||||
[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<IBotSharpFileService>();
|
||||
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
|
||||
var file = fileService.GetMessageFile(conversationId, messageId, source, index, fileName);
|
||||
if (string.IsNullOrEmpty(file))
|
||||
{
|
||||
return NotFound();
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ public class WebSocketsMiddleware
|
|||
{
|
||||
var regexes = new List<Regex>
|
||||
{
|
||||
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)
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue