refine file storage
This commit is contained in:
parent
e3c9729622
commit
c93511065a
|
|
@ -5,8 +5,11 @@ public interface IBotSharpFileService
|
|||
string GetDirectory(string conversationId);
|
||||
IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int offset = 2);
|
||||
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, bool imageOnly = false);
|
||||
string? GetMessageFile(string conversationId, string messageId, string fileName);
|
||||
void SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files);
|
||||
string GetMessageFile(string conversationId, string messageId, string fileName);
|
||||
bool SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files);
|
||||
|
||||
string GetUserAvatar();
|
||||
bool SaveUserAvatar(BotSharpFile file);
|
||||
|
||||
/// <summary>
|
||||
/// Delete files under messages
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
|
|
@ -8,21 +9,27 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
{
|
||||
private readonly BotSharpDatabaseSettings _dbSettings;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IUserIdentity _user;
|
||||
private readonly ILogger<BotSharpFileService> _logger;
|
||||
private readonly string _baseDir;
|
||||
private readonly IEnumerable<string> _allowedTypes = new List<string> { "image/png", "image/jpeg" };
|
||||
|
||||
private const string CONVERSATION_FOLDER = "conversations";
|
||||
private const string FILE_FOLDER = "files";
|
||||
private const string USERS_FOLDER = "users";
|
||||
private const string USER_AVATAR_FOLDER = "avatar";
|
||||
|
||||
private const int MIN_OFFSET = 1;
|
||||
private const int MAX_OFFSET = 5;
|
||||
|
||||
public BotSharpFileService(
|
||||
BotSharpDatabaseSettings dbSettings,
|
||||
IUserIdentity user,
|
||||
ILogger<BotSharpFileService> logger,
|
||||
IServiceProvider services)
|
||||
{
|
||||
_dbSettings = dbSettings;
|
||||
_user = user;
|
||||
_logger = logger;
|
||||
_services = services;
|
||||
_baseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dbSettings.FileRepository);
|
||||
|
|
@ -38,7 +45,7 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
return dir;
|
||||
}
|
||||
|
||||
public IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int offset = 2)
|
||||
public IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int offset = 1)
|
||||
{
|
||||
var files = new List<MessageFileModel>();
|
||||
if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty())
|
||||
|
|
@ -68,7 +75,7 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
foreach (var messageId in messageIds)
|
||||
{
|
||||
var dir = GetConversationFileDirectory(conversationId, messageId);
|
||||
if (string.IsNullOrEmpty(dir))
|
||||
if (!ExistDirectory(dir))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
@ -101,24 +108,24 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
return files;
|
||||
}
|
||||
|
||||
public string? GetMessageFile(string conversationId, string messageId, string fileName)
|
||||
public string GetMessageFile(string conversationId, string messageId, string fileName)
|
||||
{
|
||||
var dir = GetConversationFileDirectory(conversationId, messageId);
|
||||
if (string.IsNullOrEmpty(dir))
|
||||
if (!ExistDirectory(dir))
|
||||
{
|
||||
return null;
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var found = Directory.GetFiles(dir).FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).IsEqualTo(fileName));
|
||||
return found;
|
||||
}
|
||||
|
||||
public void SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files)
|
||||
public bool SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files)
|
||||
{
|
||||
if (files.IsNullOrEmpty()) return;
|
||||
if (files.IsNullOrEmpty()) return false;
|
||||
|
||||
var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true);
|
||||
if (string.IsNullOrEmpty(dir)) return;
|
||||
if (!ExistDirectory(dir)) return false;
|
||||
|
||||
try
|
||||
{
|
||||
|
|
@ -136,10 +143,53 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
Thread.Sleep(100);
|
||||
File.WriteAllBytes(Path.Combine(dir, fileName), bytes);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Error when saving conversation files: {ex.Message}");
|
||||
_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;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when saving user avatar: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -152,9 +202,9 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
var prevDir = GetConversationFileDirectory(conversationId, targetMessageId);
|
||||
var newDir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, newMessageId);
|
||||
|
||||
if (Directory.Exists(prevDir))
|
||||
if (ExistDirectory(prevDir))
|
||||
{
|
||||
if (Directory.Exists(newDir))
|
||||
if (ExistDirectory(newDir))
|
||||
{
|
||||
Directory.Delete(newDir, true);
|
||||
}
|
||||
|
|
@ -182,7 +232,7 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
foreach (var conversationId in conversationIds)
|
||||
{
|
||||
var convDir = FindConversationDirectory(conversationId);
|
||||
if (string.IsNullOrEmpty(convDir)) continue;
|
||||
if (!ExistDirectory(convDir)) continue;
|
||||
|
||||
Directory.Delete(convDir, true);
|
||||
}
|
||||
|
|
@ -215,16 +265,9 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
}
|
||||
|
||||
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId);
|
||||
if (!Directory.Exists(dir))
|
||||
if (!Directory.Exists(dir) && createNewDir)
|
||||
{
|
||||
if (createNewDir)
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
|
@ -234,8 +277,21 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
if (string.IsNullOrEmpty(conversationId)) return null;
|
||||
|
||||
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId);
|
||||
if (!Directory.Exists(dir)) return null;
|
||||
return dir;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
|
|
@ -250,5 +306,10 @@ public class BotSharpFileService : IBotSharpFileService
|
|||
|
||||
return contentType;
|
||||
}
|
||||
|
||||
private bool ExistDirectory(string? dir)
|
||||
{
|
||||
return !string.IsNullOrEmpty(dir) && Directory.Exists(dir);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ public class FileController : ControllerBase
|
|||
}
|
||||
|
||||
[HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")]
|
||||
public async Task<IActionResult> GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
|
||||
public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName)
|
||||
{
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
var file = fileService.GetMessageFile(conversationId, messageId, fileName);
|
||||
|
|
@ -54,7 +54,30 @@ public class FileController : ControllerBase
|
|||
{
|
||||
return NotFound();
|
||||
}
|
||||
return BuildFileResult(file);
|
||||
}
|
||||
|
||||
[HttpPost("/user/avatar")]
|
||||
public bool UploadUserAvatar([FromBody] BotSharpFile file)
|
||||
{
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
return fileService.SaveUserAvatar(file);
|
||||
}
|
||||
|
||||
[HttpGet("/user/avatar")]
|
||||
public IActionResult GetUserAvatar()
|
||||
{
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
var file = fileService.GetUserAvatar();
|
||||
if (string.IsNullOrEmpty(file))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return BuildFileResult(file);
|
||||
}
|
||||
|
||||
private FileContentResult BuildFileResult(string file)
|
||||
{
|
||||
using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var bytes = new byte[stream.Length];
|
||||
stream.Read(bytes, 0, (int)stream.Length);
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ public class UserViewModel
|
|||
public string Source { get; set; }
|
||||
[JsonPropertyName("external_id")]
|
||||
public string? ExternalId { get; set; }
|
||||
public string Avatar { get; set; } = "/user/avatar";
|
||||
[JsonPropertyName("create_date")]
|
||||
public DateTime CreateDate { get; set; }
|
||||
[JsonPropertyName("update_date")]
|
||||
|
|
@ -47,7 +48,8 @@ public class UserViewModel
|
|||
Source = user.Source,
|
||||
ExternalId = user.ExternalId,
|
||||
CreateDate = user.CreatedTime,
|
||||
UpdateDate = user.UpdatedTime
|
||||
UpdateDate = user.UpdatedTime,
|
||||
Avatar = "/user/avatar"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,13 +14,11 @@ public class WebSocketsMiddleware
|
|||
|
||||
public async Task Invoke(HttpContext httpContext)
|
||||
{
|
||||
var request = httpContext.Request;;
|
||||
var messageFileRegex = new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/file/[a-z0-9-]+", RegexOptions.IgnoreCase);
|
||||
var request = httpContext.Request;
|
||||
|
||||
// web sockets cannot pass headers so we must take the access token from query param and
|
||||
// add it to the header before authentication middleware runs
|
||||
if ((request.Path.StartsWithSegments("/chatHub", StringComparison.OrdinalIgnoreCase)
|
||||
|| messageFileRegex.IsMatch(request.Path.Value ?? string.Empty)) &&
|
||||
if ((VerifyChatHubRequest(request) || VerifyGetRequest(request)) &&
|
||||
request.Query.TryGetValue("access_token", out var accessToken))
|
||||
{
|
||||
request.Headers["Authorization"] = $"Bearer {accessToken}";
|
||||
|
|
@ -28,4 +26,20 @@ public class WebSocketsMiddleware
|
|||
|
||||
await _next(httpContext);
|
||||
}
|
||||
|
||||
private bool VerifyChatHubRequest(HttpRequest request)
|
||||
{
|
||||
return request.Path.StartsWithSegments("/chatHub", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private bool VerifyGetRequest(HttpRequest request)
|
||||
{
|
||||
var regexes = new List<Regex>
|
||||
{
|
||||
new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/file/[a-z0-9-]+", RegexOptions.IgnoreCase),
|
||||
new Regex(@"/user/avatar", RegexOptions.IgnoreCase)
|
||||
};
|
||||
|
||||
return request.Method.IsEqualTo("GET") && regexes.Any(x => x.IsMatch(request.Path.Value ?? string.Empty));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue