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-22 15:39:04 +00:00
|
|
|
public partial 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-06-06 01:16:48 +00:00
|
|
|
private readonly IEnumerable<string> _allowedImageTypes = new List<string> { "image/png", "image/jpeg" };
|
2024-06-07 04:02:10 +00:00
|
|
|
private readonly IEnumerable<string> _allowScreenShotTypes = new List<string> { "pdf" };
|
2024-05-03 19:57:44 +00:00
|
|
|
|
|
|
|
|
private const string CONVERSATION_FOLDER = "conversations";
|
|
|
|
|
private const string FILE_FOLDER = "files";
|
2024-06-06 19:57:41 +00:00
|
|
|
private const string USER_FILE_FOLDER = "user";
|
2024-06-07 04:02:10 +00:00
|
|
|
private const string SCREENSHOT_FILE_FOLDER = "screenshot";
|
2024-06-06 19:57:41 +00:00
|
|
|
private const string BOT_FILE_FOLDER = "bot";
|
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-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-06-26 03:38:01 +00:00
|
|
|
public 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
|
|
|
|
2024-06-26 03:38:01 +00:00
|
|
|
#region Private methods
|
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
|
|
|
}
|