2024-08-09 21:03:29 +00:00
|
|
|
using BotSharp.Abstraction.Files;
|
2024-07-30 17:08:40 +00:00
|
|
|
using BotSharp.Abstraction.Files.Converters;
|
|
|
|
|
using BotSharp.Abstraction.Files.Enums;
|
2024-08-07 21:56:57 +00:00
|
|
|
using BotSharp.Abstraction.Files.Utilities;
|
2024-07-30 17:08:40 +00:00
|
|
|
using System.Net.Mime;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.TencentCos.Services;
|
|
|
|
|
|
|
|
|
|
public partial class TencentCosService
|
|
|
|
|
{
|
2024-08-28 20:34:41 +00:00
|
|
|
public async Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds)
|
2024-07-30 17:08:40 +00:00
|
|
|
{
|
|
|
|
|
var files = new List<MessageFileModel>();
|
2024-08-08 18:34:50 +00:00
|
|
|
if (string.IsNullOrEmpty(conversationId) || messageIds.IsNullOrEmpty())
|
2024-07-30 17:08:40 +00:00
|
|
|
{
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 18:34:50 +00:00
|
|
|
var source = FileSourceType.User;
|
2024-07-30 17:08:40 +00:00
|
|
|
var pathPrefix = $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}";
|
|
|
|
|
foreach (var messageId in messageIds)
|
|
|
|
|
{
|
|
|
|
|
var dir = $"{pathPrefix}/{messageId}/{source}";
|
|
|
|
|
foreach (var subDir in _cosClient.BucketClient.GetDirectories(dir))
|
|
|
|
|
{
|
|
|
|
|
var file = _cosClient.BucketClient.GetDirFiles(subDir).FirstOrDefault();
|
|
|
|
|
if (file == null) continue;
|
|
|
|
|
|
2024-08-08 18:50:45 +00:00
|
|
|
var screenshots = await GetScreenshots(file, subDir, messageId, source);
|
|
|
|
|
if (screenshots.IsNullOrEmpty()) continue;
|
|
|
|
|
|
|
|
|
|
files.AddRange(screenshots);
|
2024-07-30 17:08:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds,
|
2024-08-07 21:56:57 +00:00
|
|
|
string source, IEnumerable<string>? contentTypes = null)
|
2024-07-30 17:08:40 +00:00
|
|
|
{
|
|
|
|
|
var files = new List<MessageFileModel>();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(conversationId) || messageIds.IsNullOrEmpty()) return files;
|
|
|
|
|
|
|
|
|
|
foreach (var messageId in messageIds)
|
|
|
|
|
{
|
|
|
|
|
var dir = $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}/{messageId}/{source}";
|
|
|
|
|
if (!ExistDirectory(dir))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var subDir in _cosClient.BucketClient.GetDirectories(dir))
|
|
|
|
|
{
|
|
|
|
|
foreach (var file in _cosClient.BucketClient.GetDirFiles(subDir))
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var contentType = FileUtility.GetFileContentType(file);
|
2024-08-07 22:40:39 +00:00
|
|
|
if (!contentTypes.IsNullOrEmpty() && !contentTypes.Contains(contentType))
|
2024-07-30 17:08:40 +00:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fileName = Path.GetFileNameWithoutExtension(file);
|
2024-08-08 18:34:50 +00:00
|
|
|
var fileExtension = Path.GetExtension(file).Substring(1);
|
2024-07-30 17:08:40 +00:00
|
|
|
var model = new MessageFileModel()
|
|
|
|
|
{
|
|
|
|
|
MessageId = messageId,
|
2024-08-08 01:56:29 +00:00
|
|
|
FileUrl = BuilFileUrl(file),
|
2024-11-04 18:12:40 +00:00
|
|
|
FileDownloadUrl = BuilFileUrl(file),
|
2024-07-30 17:08:40 +00:00
|
|
|
FileStorageUrl = file,
|
|
|
|
|
FileName = fileName,
|
2024-08-08 18:34:50 +00:00
|
|
|
FileExtension = fileExtension,
|
2024-07-30 17:08:40 +00:00
|
|
|
ContentType = contentType,
|
|
|
|
|
FileSource = source
|
|
|
|
|
};
|
|
|
|
|
files.Add(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 18:50:45 +00:00
|
|
|
|
|
|
|
|
|
2024-07-30 17:08:40 +00:00
|
|
|
public string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName)
|
|
|
|
|
{
|
|
|
|
|
var dir = $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}/{source}/{index}/";
|
|
|
|
|
|
|
|
|
|
var fileList = _cosClient.BucketClient.GetDirFiles(dir);
|
|
|
|
|
var found = fileList.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).IsEqualTo(fileName));
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MessageFileModel> GetMessagesWithFile(string conversationId, IEnumerable<string> messageIds)
|
|
|
|
|
{
|
|
|
|
|
var foundMsgs = new List<MessageFileModel>();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(conversationId) || messageIds.IsNullOrEmpty()) return foundMsgs;
|
|
|
|
|
|
|
|
|
|
foreach (var messageId in messageIds)
|
|
|
|
|
{
|
|
|
|
|
var prefix = $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}/{messageId}";
|
|
|
|
|
var userDir = $"{prefix}/{FileSourceType.User}/";
|
|
|
|
|
if (ExistDirectory(userDir))
|
|
|
|
|
{
|
|
|
|
|
foundMsgs.Add(new MessageFileModel { MessageId = messageId, FileSource = FileSourceType.User });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var botDir = $"{prefix}/{FileSourceType.Bot}";
|
|
|
|
|
if (ExistDirectory(botDir))
|
|
|
|
|
{
|
|
|
|
|
foundMsgs.Add(new MessageFileModel { MessageId = messageId, FileSource = FileSourceType.Bot });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return foundMsgs;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 20:11:40 +00:00
|
|
|
public bool SaveMessageFiles(string conversationId, string messageId, string source, List<FileDataModel> files)
|
2024-07-30 17:08:40 +00:00
|
|
|
{
|
|
|
|
|
if (files.IsNullOrEmpty()) return false;
|
|
|
|
|
|
|
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < files.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var file = files[i];
|
|
|
|
|
if (string.IsNullOrEmpty(file.FileData))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
var (_, bytes) = FileUtility.GetFileInfoFromData(file.FileData);
|
2024-07-30 17:08:40 +00:00
|
|
|
var subDir = $"{dir}/{source}/{i + 1}";
|
|
|
|
|
_cosClient.BucketClient.UploadBytes($"{subDir}/{file.FileName}", bytes);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Error when saving message file {file.FileName}: {ex.Message}\r\n{ex.InnerException}");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messageIds, string targetMessageId, string? newMessageId = null)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(conversationId) || messageIds == null) return false;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(targetMessageId) && !string.IsNullOrEmpty(newMessageId))
|
|
|
|
|
{
|
|
|
|
|
var prevDir = GetConversationFileDirectory(conversationId, targetMessageId);
|
|
|
|
|
var newDir = $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}/{newMessageId}/";
|
|
|
|
|
|
|
|
|
|
if (ExistDirectory(prevDir))
|
|
|
|
|
{
|
|
|
|
|
if (ExistDirectory(newDir))
|
|
|
|
|
{
|
|
|
|
|
_cosClient.BucketClient.DeleteDir(newDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_cosClient.BucketClient.MoveDir(prevDir, newDir);
|
|
|
|
|
|
|
|
|
|
var botDir = $"{newDir}/{BOT_FILE_FOLDER}";
|
|
|
|
|
if (ExistDirectory(botDir))
|
|
|
|
|
{
|
|
|
|
|
_cosClient.BucketClient.DeleteDir(newDir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var messageId in messageIds)
|
|
|
|
|
{
|
|
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId);
|
|
|
|
|
if (!ExistDirectory(dir)) continue;
|
|
|
|
|
_cosClient.BucketClient.DeleteDir(dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeleteConversationFiles(IEnumerable<string> conversationIds)
|
|
|
|
|
{
|
|
|
|
|
if (conversationIds.IsNullOrEmpty()) return false;
|
|
|
|
|
|
|
|
|
|
foreach (var conversationId in conversationIds)
|
|
|
|
|
{
|
|
|
|
|
var convDir = GetConversationDirectory(conversationId);
|
|
|
|
|
if (!ExistDirectory(convDir)) continue;
|
|
|
|
|
|
|
|
|
|
_cosClient.BucketClient.DeleteDir(convDir);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Private methods
|
|
|
|
|
private string GetConversationFileDirectory(string? conversationId, string? messageId, bool createNewDir = false)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId))
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $"{CONVERSATION_FOLDER}/{conversationId}/{FILE_FOLDER}/{messageId}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string? GetConversationDirectory(string conversationId)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(conversationId)) return null;
|
|
|
|
|
|
|
|
|
|
var dir = $"{CONVERSATION_FOLDER}/{conversationId}";
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 05:51:15 +00:00
|
|
|
private IEnumerable<string> GetMessageIds(IEnumerable<RoleDialogModel> dialogs, int? offset = null)
|
2024-07-30 17:08:40 +00:00
|
|
|
{
|
2024-08-08 05:51:15 +00:00
|
|
|
if (dialogs.IsNullOrEmpty()) return Enumerable.Empty<string>();
|
2024-07-30 17:08:40 +00:00
|
|
|
|
2024-08-08 05:51:15 +00:00
|
|
|
if (offset.HasValue && offset < 1)
|
2024-07-30 17:08:40 +00:00
|
|
|
{
|
2024-08-07 21:56:57 +00:00
|
|
|
offset = 1;
|
2024-07-30 17:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var messageIds = new List<string>();
|
|
|
|
|
if (offset.HasValue)
|
|
|
|
|
{
|
2024-08-08 05:51:15 +00:00
|
|
|
messageIds = dialogs.Select(x => x.MessageId).Distinct().TakeLast(offset.Value).ToList();
|
2024-07-30 17:08:40 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-08-08 05:51:15 +00:00
|
|
|
messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
2024-07-30 17:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messageIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<string>> ConvertPdfToImages(string pdfLoc, string imageLoc)
|
|
|
|
|
{
|
|
|
|
|
var converters = _services.GetServices<IPdf2ImageConverter>();
|
|
|
|
|
if (converters.IsNullOrEmpty()) return Enumerable.Empty<string>();
|
|
|
|
|
|
|
|
|
|
var converter = GetPdf2ImageConverter();
|
|
|
|
|
if (converter == null)
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<string>();
|
|
|
|
|
}
|
|
|
|
|
return await converter.ConvertPdfToImages(pdfLoc, imageLoc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IPdf2ImageConverter? GetPdf2ImageConverter()
|
|
|
|
|
{
|
2024-08-09 21:03:29 +00:00
|
|
|
var settings = _services.GetRequiredService<FileCoreSettings>();
|
2024-08-31 00:31:18 +00:00
|
|
|
var converter = _services.GetServices<IPdf2ImageConverter>().FirstOrDefault(x => x.Provider == settings.Pdf2ImageConverter.Provider);
|
2024-08-09 21:03:29 +00:00
|
|
|
return converter;
|
2024-07-30 17:08:40 +00:00
|
|
|
}
|
2024-08-08 01:56:29 +00:00
|
|
|
|
|
|
|
|
private string BuilFileUrl(string file)
|
|
|
|
|
{
|
|
|
|
|
return $"https://{_fullBuketName}.cos.{_settings.Region}.myqcloud.com/{file}";
|
|
|
|
|
}
|
2024-08-08 18:50:45 +00:00
|
|
|
|
|
|
|
|
private async Task<IEnumerable<MessageFileModel>> GetScreenshots(string file, string parentDir, string messageId, string source)
|
|
|
|
|
{
|
|
|
|
|
var files = new List<MessageFileModel>();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var contentType = FileUtility.GetFileContentType(file);
|
|
|
|
|
var screenshotDir = $"{parentDir}/{SCREENSHOT_FILE_FOLDER}/";
|
|
|
|
|
var screenshots = _cosClient.BucketClient.GetDirFiles(screenshotDir);
|
|
|
|
|
if (!screenshots.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
foreach (var screenshot in screenshots)
|
|
|
|
|
{
|
|
|
|
|
var screenshotContentType = FileUtility.GetFileContentType(screenshot);
|
|
|
|
|
var fileName = Path.GetFileNameWithoutExtension(screenshot);
|
|
|
|
|
var fileExtension = Path.GetExtension(screenshot).Substring(1);
|
|
|
|
|
var model = new MessageFileModel
|
|
|
|
|
{
|
|
|
|
|
MessageId = messageId,
|
|
|
|
|
FileName = fileName,
|
|
|
|
|
FileExtension = fileExtension,
|
|
|
|
|
FileUrl = BuilFileUrl(screenshot),
|
|
|
|
|
FileStorageUrl = screenshot,
|
|
|
|
|
ContentType = contentType,
|
|
|
|
|
FileSource = source
|
|
|
|
|
};
|
|
|
|
|
files.Add(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (contentType == MediaTypeNames.Application.Pdf)
|
|
|
|
|
{
|
|
|
|
|
var images = await ConvertPdfToImages(file, screenshotDir);
|
|
|
|
|
foreach (var image in images)
|
|
|
|
|
{
|
|
|
|
|
var fileName = Path.GetFileNameWithoutExtension(image);
|
|
|
|
|
var fileExtension = Path.GetExtension(image).Substring(1);
|
|
|
|
|
var screenshotContentType = FileUtility.GetFileContentType(image);
|
|
|
|
|
var model = new MessageFileModel
|
|
|
|
|
{
|
|
|
|
|
MessageId = messageId,
|
|
|
|
|
FileName = fileName,
|
|
|
|
|
FileExtension = fileExtension,
|
|
|
|
|
FileUrl = BuilFileUrl(image),
|
|
|
|
|
FileStorageUrl = image,
|
|
|
|
|
ContentType = contentType,
|
|
|
|
|
FileSource = source
|
|
|
|
|
};
|
|
|
|
|
files.Add(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Error when getting message file screenshots {file} (messageId: {messageId}), Error: {ex.Message}\r\n{ex.InnerException}");
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-30 17:08:40 +00:00
|
|
|
#endregion
|
|
|
|
|
}
|