2024-05-13 21:53:41 +00:00
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
2024-05-22 04:33:31 +00:00
|
|
|
using System;
|
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-22 04:33:31 +00:00
|
|
|
private readonly IUserIdentity _user;
|
2024-05-14 16:51:39 +00:00
|
|
|
private readonly ILogger<BotSharpFileService> _logger;
|
2024-05-03 19:57:44 +00:00
|
|
|
private readonly string _baseDir;
|
2024-05-13 21:53:41 +00:00
|
|
|
private readonly IEnumerable<string> _allowedTypes = new List<string> { "image/png", "image/jpeg" };
|
2024-05-03 19:57:44 +00:00
|
|
|
|
|
|
|
|
private const string CONVERSATION_FOLDER = "conversations";
|
|
|
|
|
private const string FILE_FOLDER = "files";
|
2024-05-22 04:33:31 +00:00
|
|
|
private const string USERS_FOLDER = "users";
|
|
|
|
|
private const string USER_AVATAR_FOLDER = "avatar";
|
|
|
|
|
|
2024-05-13 21:53:41 +00:00
|
|
|
private const int MIN_OFFSET = 1;
|
|
|
|
|
private const int MAX_OFFSET = 5;
|
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,
|
2024-05-22 04:33:31 +00:00
|
|
|
IUserIdentity user,
|
2024-05-14 16:51:39 +00:00
|
|
|
ILogger<BotSharpFileService> logger,
|
2023-11-09 21:11:36 +00:00
|
|
|
IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_dbSettings = dbSettings;
|
2024-05-22 04:33:31 +00:00
|
|
|
_user = user;
|
2024-05-14 16:51:39 +00:00
|
|
|
_logger = logger;
|
2023-11-09 21:11:36 +00:00
|
|
|
_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-22 04:33:31 +00:00
|
|
|
public IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int offset = 1)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
2024-05-13 21:53:41 +00:00
|
|
|
var files = new List<MessageFileModel>();
|
|
|
|
|
if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty())
|
2023-11-09 21:11:36 +00:00
|
|
|
{
|
2024-05-13 21:53:41 +00:00
|
|
|
return files;
|
2023-11-09 21:11:36 +00:00
|
|
|
}
|
2024-05-03 22:24:18 +00:00
|
|
|
|
2024-05-13 21:53:41 +00:00
|
|
|
if (offset <= 0)
|
|
|
|
|
{
|
|
|
|
|
offset = MIN_OFFSET;
|
|
|
|
|
}
|
|
|
|
|
else if (offset > MAX_OFFSET)
|
2024-05-03 22:24:18 +00:00
|
|
|
{
|
2024-05-13 21:53:41 +00:00
|
|
|
offset = MAX_OFFSET;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var messageIds = conversations.Select(x => x.MessageId).Distinct().TakeLast(offset).ToList();
|
|
|
|
|
files = GetMessageFiles(conversationId, messageIds, imageOnly: true).ToList();
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, bool imageOnly = false)
|
|
|
|
|
{
|
|
|
|
|
var files = new List<MessageFileModel>();
|
|
|
|
|
if (messageIds.IsNullOrEmpty()) return files;
|
|
|
|
|
|
|
|
|
|
foreach (var messageId in messageIds)
|
|
|
|
|
{
|
|
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId);
|
2024-05-22 04:33:31 +00:00
|
|
|
if (!ExistDirectory(dir))
|
2024-05-03 22:24:18 +00:00
|
|
|
{
|
2024-05-13 21:53:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var file in Directory.GetFiles(dir))
|
|
|
|
|
{
|
|
|
|
|
var contentType = GetFileContentType(file);
|
|
|
|
|
if (imageOnly && !_allowedTypes.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}/file/{fileName}",
|
|
|
|
|
FileStorageUrl = file,
|
|
|
|
|
FileName = fileName,
|
|
|
|
|
FileType = fileType,
|
|
|
|
|
ContentType = contentType
|
|
|
|
|
};
|
|
|
|
|
files.Add(model);
|
|
|
|
|
}
|
2024-05-03 22:24:18 +00:00
|
|
|
}
|
2024-05-13 21:53:41 +00:00
|
|
|
|
|
|
|
|
return files;
|
2024-05-03 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-22 04:33:31 +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);
|
2024-05-22 04:33:31 +00:00
|
|
|
if (!ExistDirectory(dir))
|
2024-05-03 22:24:18 +00:00
|
|
|
{
|
2024-05-22 04:33:31 +00:00
|
|
|
return string.Empty;
|
2024-05-06 06:51:59 +00:00
|
|
|
}
|
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-22 04:33:31 +00:00
|
|
|
public bool SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
2024-05-22 04:33:31 +00:00
|
|
|
if (files.IsNullOrEmpty()) return false;
|
2024-05-03 19:57:44 +00:00
|
|
|
|
2024-05-06 06:51:59 +00:00
|
|
|
var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true);
|
2024-05-22 04:33:31 +00:00
|
|
|
if (!ExistDirectory(dir)) return false;
|
2024-05-03 19:57:44 +00:00
|
|
|
|
2024-05-14 16:51:39 +00:00
|
|
|
try
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
2024-05-14 16:51:39 +00:00
|
|
|
for (int i = 0; i < files.Count; i++)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
2024-05-14 16:51:39 +00:00
|
|
|
var file = files[i];
|
|
|
|
|
if (string.IsNullOrEmpty(file.FileData))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-05-03 19:57:44 +00:00
|
|
|
|
2024-05-14 16:51:39 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2024-05-22 04:33:31 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Error when saving conversation files: {ex.Message}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetUserAvatar()
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var user = db.GetUserById(_user.Id);
|
|
|
|
|
var dir = GetUserAvatarDir(user?.Id);
|
|
|
|
|
|
|
|
|
|
if (!ExistDirectory(dir)) return string.Empty;
|
|
|
|
|
|
|
|
|
|
var found = Directory.GetFiles(dir).FirstOrDefault() ?? string.Empty;
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SaveUserAvatar(BotSharpFile file)
|
|
|
|
|
{
|
|
|
|
|
if (file == null || string.IsNullOrEmpty(file.FileData)) return false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var user = db.GetUserById(_user.Id);
|
|
|
|
|
var dir = GetUserAvatarDir(user?.Id);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(dir)) return false;
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(dir, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir = GetUserAvatarDir(user?.Id, createNewDir: true);
|
|
|
|
|
var (_, bytes) = GetFileInfoFromData(file.FileData);
|
|
|
|
|
File.WriteAllBytes(Path.Combine(dir, file.FileName), bytes);
|
|
|
|
|
return true;
|
2024-05-14 16:51:39 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-05-22 04:33:31 +00:00
|
|
|
_logger.LogWarning($"Error when saving user avatar: {ex.Message}");
|
|
|
|
|
return false;
|
2024-05-03 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2024-05-22 04:33:31 +00:00
|
|
|
if (ExistDirectory(prevDir))
|
2024-05-06 22:31:52 +00:00
|
|
|
{
|
2024-05-22 04:33:31 +00:00
|
|
|
if (ExistDirectory(newDir))
|
2024-05-06 22:31:52 +00:00
|
|
|
{
|
|
|
|
|
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);
|
2024-05-22 04:33:31 +00:00
|
|
|
if (!ExistDirectory(convDir)) continue;
|
2024-05-06 22:31:52 +00:00
|
|
|
|
|
|
|
|
Directory.Delete(convDir, true);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 16:51:39 +00:00
|
|
|
public (string, byte[]) GetFileInfoFromData(string data)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
return (string.Empty, new byte[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var typeStartIdx = data.IndexOf(':');
|
|
|
|
|
var typeEndIdx = data.IndexOf(';');
|
|
|
|
|
var contentType = data.Substring(typeStartIdx + 1, typeEndIdx - typeStartIdx - 1);
|
|
|
|
|
|
|
|
|
|
var base64startIdx = data.IndexOf(',');
|
|
|
|
|
var base64Str = data.Substring(base64startIdx + 1);
|
|
|
|
|
|
|
|
|
|
return (contentType, Convert.FromBase64String(base64Str));
|
|
|
|
|
}
|
|
|
|
|
|
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-22 04:33:31 +00:00
|
|
|
if (!Directory.Exists(dir) && createNewDir)
|
2024-05-03 22:24:18 +00:00
|
|
|
{
|
2024-05-22 04:33:31 +00:00
|
|
|
Directory.CreateDirectory(dir);
|
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);
|
2024-05-22 04:33:31 +00:00
|
|
|
return dir;
|
|
|
|
|
}
|
2024-05-06 22:31:52 +00:00
|
|
|
|
2024-05-22 04:33:31 +00:00
|
|
|
private string GetUserAvatarDir(string? userId, bool createNewDir = false)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(userId))
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dir = Path.Combine(_baseDir, USERS_FOLDER, userId, USER_AVATAR_FOLDER);
|
|
|
|
|
if (!Directory.Exists(dir) && createNewDir)
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
}
|
2024-05-06 22:31:52 +00:00
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 21:53:41 +00:00
|
|
|
private string GetFileContentType(string filePath)
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
2024-05-13 21:53:41 +00:00
|
|
|
string contentType;
|
|
|
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
|
|
|
if (!provider.TryGetContentType(filePath, out contentType))
|
2024-05-03 19:57:44 +00:00
|
|
|
{
|
2024-05-13 21:53:41 +00:00
|
|
|
contentType = string.Empty;
|
2024-05-03 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-13 21:53:41 +00:00
|
|
|
return contentType;
|
2024-05-03 19:57:44 +00:00
|
|
|
}
|
2024-05-22 04:33:31 +00:00
|
|
|
|
|
|
|
|
private bool ExistDirectory(string? dir)
|
|
|
|
|
{
|
|
|
|
|
return !string.IsNullOrEmpty(dir) && Directory.Exists(dir);
|
|
|
|
|
}
|
2024-05-03 22:24:18 +00:00
|
|
|
#endregion
|
2023-11-09 21:11:36 +00:00
|
|
|
}
|