2023-11-09 21:11:36 +00:00
|
|
|
using System.IO;
|
2024-05-03 19:57:44 +00:00
|
|
|
using System.Threading;
|
2023-11-09 21:11:36 +00:00
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
namespace BotSharp.Core.Files;
|
2023-11-09 21:11:36 +00:00
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
public class BotSharpFileService : IBotSharpFileService
|
2023-11-09 21:11:36 +00:00
|
|
|
{
|
|
|
|
|
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";
|
2023-11-09 21:11:36 +00:00
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
public BotSharpFileService(
|
2023-11-09 21:11:36 +00:00
|
|
|
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>();
|
2024-05-06 06:51:59 +00:00
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId);
|
|
|
|
|
if (string.IsNullOrEmpty(dir))
|
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
|
|
|
|
|
|
|
|
foreach (var file in Directory.GetFiles(dir))
|
|
|
|
|
{
|
2024-05-06 06:51:59 +00:00
|
|
|
var fileName = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
var extension = Path.GetExtension(file);
|
|
|
|
|
var fileType = extension.Substring(1);
|
2024-05-03 22:24:18 +00:00
|
|
|
var model = new OutputFileModel()
|
|
|
|
|
{
|
2024-05-06 22:31:52 +00:00
|
|
|
FileUrl = $"/conversation/{conversationId}/message/{messageId}/file/{fileName}",
|
2024-05-03 22:24:18 +00:00
|
|
|
FileName = fileName,
|
2024-05-06 22:31:52 +00:00
|
|
|
FileType = fileType
|
2024-05-03 22:24:18 +00:00
|
|
|
};
|
|
|
|
|
outputFiles.Add(model);
|
|
|
|
|
}
|
|
|
|
|
return outputFiles;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 22:31:52 +00:00
|
|
|
public string? GetMessageFile(string conversationId, string messageId, string fileName)
|
2024-05-03 22:24:18 +00:00
|
|
|
{
|
2024-05-06 06:51:59 +00:00
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId);
|
|
|
|
|
if (string.IsNullOrEmpty(dir))
|
2024-05-03 22:24:18 +00:00
|
|
|
{
|
2024-05-06 06:51:59 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-03 22:24:18 +00:00
|
|
|
|
2024-05-06 22:31:52 +00:00
|
|
|
var found = Directory.GetFiles(dir).FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).IsEqualTo(fileName));
|
2024-05-03 22:24:18 +00:00
|
|
|
return found;
|
2023-11-09 21:11:36 +00:00
|
|
|
}
|
2024-05-03 19:57:44 +00:00
|
|
|
|
2024-05-06 22:31:52 +00:00
|
|
|
public void SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
|
|
|
|
if (files.IsNullOrEmpty()) return;
|
|
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true);
|
|
|
|
|
if (string.IsNullOrEmpty(dir)) return;
|
2024-05-03 19:57:44 +00:00
|
|
|
|
|
|
|
|
for (int i = 0; i < files.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var file = files[i];
|
2024-05-06 22:31:52 +00:00
|
|
|
if (string.IsNullOrEmpty(file.FileData))
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bytes = GetFileBytes(file.FileData);
|
2024-05-06 06:51:59 +00:00
|
|
|
var fileType = Path.GetExtension(file.FileName);
|
|
|
|
|
var fileName = $"{i + 1}{fileType}";
|
2024-05-03 19:57:44 +00:00
|
|
|
Thread.Sleep(100);
|
|
|
|
|
File.WriteAllBytes(Path.Combine(dir, fileName), bytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 22:31:52 +00:00
|
|
|
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 = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, newMessageId);
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(prevDir))
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(newDir))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(newDir, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Directory.Move(prevDir, newDir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( var messageId in messageIds)
|
|
|
|
|
{
|
|
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId);
|
|
|
|
|
if (string.IsNullOrEmpty(dir)) continue;
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
Directory.Delete(dir, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeleteConversationFiles(IEnumerable<string> conversationIds)
|
|
|
|
|
{
|
|
|
|
|
if (conversationIds.IsNullOrEmpty()) return false;
|
|
|
|
|
|
|
|
|
|
foreach (var conversationId in conversationIds)
|
|
|
|
|
{
|
|
|
|
|
var convDir = FindConversationDirectory(conversationId);
|
|
|
|
|
if (string.IsNullOrEmpty(convDir)) continue;
|
|
|
|
|
|
|
|
|
|
Directory.Delete(convDir, true);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 22:24:18 +00:00
|
|
|
#region Private methods
|
2024-05-06 06:51:59 +00:00
|
|
|
private string GetConversationFileDirectory(string? conversationId, string? messageId, bool createNewDir = false)
|
2024-05-03 22:24:18 +00:00
|
|
|
{
|
2024-05-06 06:51:59 +00:00
|
|
|
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId))
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId);
|
2024-05-03 22:24:18 +00:00
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
{
|
2024-05-06 06:51:59 +00:00
|
|
|
if (createNewDir)
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2024-05-03 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 22:31:52 +00:00
|
|
|
private string? FindConversationDirectory(string conversationId)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(conversationId)) return null;
|
|
|
|
|
|
|
|
|
|
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId);
|
|
|
|
|
if (!Directory.Exists(dir)) return null;
|
|
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
private byte[] GetFileBytes(string data)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
2024-05-06 06:51:59 +00:00
|
|
|
return new byte[0];
|
2024-05-03 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
var startIdx = data.IndexOf(',');
|
|
|
|
|
var base64Str = data.Substring(startIdx + 1);
|
|
|
|
|
return Convert.FromBase64String(base64Str);
|
2024-05-03 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
private string GetFileType(string data)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
2024-05-06 06:51:59 +00:00
|
|
|
return string.Empty;
|
2024-05-03 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
var startIdx = data.IndexOf(':');
|
|
|
|
|
var endIdx = data.IndexOf(';');
|
|
|
|
|
var fileType = data.Substring(startIdx + 1, endIdx - startIdx - 1);
|
|
|
|
|
return fileType;
|
2024-05-03 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|