2024-05-03 22:24:18 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-11-09 21:11:36 +00:00
|
|
|
using System.IO;
|
2024-05-03 19:57:44 +00:00
|
|
|
using System.IO.Enumeration;
|
|
|
|
|
using System.Threading;
|
2023-11-09 21:11:36 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Conversations.Services;
|
|
|
|
|
|
|
|
|
|
public class ConversationAttachmentService : IConversationAttachmentService
|
|
|
|
|
{
|
|
|
|
|
private readonly BotSharpDatabaseSettings _dbSettings;
|
|
|
|
|
private readonly IServiceProvider _services;
|
2024-05-03 19:57:44 +00:00
|
|
|
private readonly string _baseDir;
|
|
|
|
|
|
|
|
|
|
private const string CONVERSATION_FOLDER = "conversations";
|
|
|
|
|
private const string FILE_FOLDER = "files";
|
2024-05-03 22:24:18 +00:00
|
|
|
private const string SEPARATOR = ".";
|
2023-11-09 21:11:36 +00:00
|
|
|
|
|
|
|
|
public ConversationAttachmentService(
|
|
|
|
|
BotSharpDatabaseSettings dbSettings,
|
|
|
|
|
IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_dbSettings = dbSettings;
|
|
|
|
|
_services = services;
|
2024-05-03 19:57:44 +00:00
|
|
|
_baseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dbSettings.FileRepository);
|
2023-11-09 21:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetDirectory(string conversationId)
|
|
|
|
|
{
|
2024-05-03 19:57:44 +00:00
|
|
|
var dir = Path.Combine(_dbSettings.FileRepository, CONVERSATION_FOLDER, conversationId, "attachments");
|
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
}
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 22:24:18 +00:00
|
|
|
public IEnumerable<OutputFileModel> GetConversationFiles(string conversationId, string messageId)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
2024-05-03 22:24:18 +00:00
|
|
|
var outputFiles = new List<OutputFileModel>();
|
|
|
|
|
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId))
|
2023-11-09 21:11:36 +00:00
|
|
|
{
|
2024-05-03 22:24:18 +00:00
|
|
|
return outputFiles;
|
2023-11-09 21:11:36 +00:00
|
|
|
}
|
2024-05-03 22:24:18 +00:00
|
|
|
|
|
|
|
|
var context = _services.GetRequiredService<IHttpContextAccessor>();
|
|
|
|
|
var request = context.HttpContext.Request;
|
|
|
|
|
var host = $"{request.Scheme}{Uri.SchemeDelimiter}{request.Host.Value}";
|
|
|
|
|
var dir = GetConversationFileDirectory(conversationId);
|
|
|
|
|
|
|
|
|
|
foreach (var file in Directory.GetFiles(dir))
|
|
|
|
|
{
|
|
|
|
|
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
|
|
|
|
|
var splits = fileName.Split('.');
|
|
|
|
|
var fileMsgId = splits.First();
|
|
|
|
|
|
|
|
|
|
if (fileMsgId != messageId) continue;
|
|
|
|
|
|
|
|
|
|
var index = splits[1];
|
|
|
|
|
var fileType = splits.Last();
|
|
|
|
|
var model = new OutputFileModel()
|
|
|
|
|
{
|
|
|
|
|
FileUrl = $"{host}/conversation/{conversationId}/file/{messageId}/type/{fileType}/{index}",
|
|
|
|
|
FileName = fileName,
|
|
|
|
|
FileType = fileType
|
|
|
|
|
};
|
|
|
|
|
outputFiles.Add(model);
|
|
|
|
|
}
|
|
|
|
|
return outputFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string? GetMessageFile(string conversationId, string messageId, string fileType, int index)
|
|
|
|
|
{
|
|
|
|
|
var targetFile = $"{messageId}{SEPARATOR}{index}.{fileType}";
|
|
|
|
|
var dir = GetConversationFileDirectory(conversationId);
|
|
|
|
|
var files = Directory.GetFiles(dir);
|
|
|
|
|
var found = files.FirstOrDefault(f =>
|
|
|
|
|
{
|
|
|
|
|
var fileName = f.Split(Path.DirectorySeparatorChar).Last();
|
|
|
|
|
return fileName.IsEqualTo(targetFile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return found;
|
2023-11-09 21:11:36 +00:00
|
|
|
}
|
2024-05-03 19:57:44 +00:00
|
|
|
|
|
|
|
|
public void SaveConversationFiles(List<BotSharpFile> files)
|
|
|
|
|
{
|
|
|
|
|
if (files.IsNullOrEmpty()) return;
|
|
|
|
|
|
2024-05-03 22:24:18 +00:00
|
|
|
var conversationId = files.First().ConversationId;
|
|
|
|
|
var dir = GetConversationFileDirectory(conversationId);
|
2024-05-03 19:57:44 +00:00
|
|
|
|
|
|
|
|
for (int i = 0; i < files.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var file = files[i];
|
|
|
|
|
if (string.IsNullOrEmpty(file.ConversationId)
|
|
|
|
|
|| string.IsNullOrEmpty(file.MessageId)
|
|
|
|
|
|| string.IsNullOrEmpty(file.FileData))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fileType = GetFileType(file.FileData);
|
|
|
|
|
var bytes = GetFileBytes(file.FileData);
|
|
|
|
|
var parsedFormat = ParseFileFormat(fileType);
|
|
|
|
|
if (string.IsNullOrEmpty(parsedFormat))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 22:24:18 +00:00
|
|
|
var fileName = $"{file.MessageId}{SEPARATOR}{i+1}{parsedFormat}";
|
2024-05-03 19:57:44 +00:00
|
|
|
Thread.Sleep(100);
|
|
|
|
|
File.WriteAllBytes(Path.Combine(dir, fileName), bytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 22:24:18 +00:00
|
|
|
#region Private methods
|
|
|
|
|
private string GetConversationFileDirectory(string conversationId)
|
|
|
|
|
{
|
|
|
|
|
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER);
|
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
}
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 19:57:44 +00:00
|
|
|
private string GetFileType(string data)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var startIdx = data.IndexOf(':');
|
|
|
|
|
var endIdx = data.IndexOf(';');
|
|
|
|
|
var fileType = data.Substring(startIdx + 1, endIdx - startIdx - 1);
|
|
|
|
|
return fileType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] GetFileBytes(string data)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var startIdx = data.IndexOf(',');
|
|
|
|
|
var base64Str = data.Substring(startIdx + 1);
|
|
|
|
|
return Convert.FromBase64String(base64Str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ParseFileFormat(string type)
|
|
|
|
|
{
|
|
|
|
|
var parsed = string.Empty;
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case "image/png":
|
|
|
|
|
parsed = ".png";
|
|
|
|
|
break;
|
|
|
|
|
case "image/jpeg":
|
|
|
|
|
case "image/jpg":
|
|
|
|
|
parsed = ".jpeg";
|
|
|
|
|
break;
|
|
|
|
|
case "application/pdf":
|
|
|
|
|
parsed = ".pdf";
|
|
|
|
|
break;
|
|
|
|
|
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
|
|
|
|
parsed = ".xlsx";
|
|
|
|
|
break;
|
|
|
|
|
case "text/plain":
|
|
|
|
|
parsed = ".txt";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return parsed;
|
|
|
|
|
}
|
2024-05-03 22:24:18 +00:00
|
|
|
#endregion
|
2023-11-09 21:11:36 +00:00
|
|
|
}
|