Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get file bytes #580

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string s

#region Common
string GetDirectory(string conversationId);
byte[] GetFileBytes(string fileStorageUrl);
byte[] GetFileBytes(string filePath);
bool SaveFileStreamToPath(string filePath, Stream stream);
bool SaveFileBytesToPath(string filePath, byte[] bytes);
string GetParentDir(string dir, int level = 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,4 @@ public static string GetFileContentType(string filePath)

return contentType;
}

public static async Task<byte[]> GetFileBytes(IServiceProvider services, FileBase file)
{
var bytes = new byte[0];
var settings = services.GetRequiredService<FileStorageSettings>();

if (settings.Default == FileStorageEnum.LocalFileStorage)
{
using var fs = File.OpenRead(file.FileStorageUrl);
var binary = BinaryData.FromStream(fs);
bytes = binary.ToArray();
fs.Close();
}
else
{
var http = services.GetRequiredService<IHttpClientFactory>();
using var client = http.CreateClient();
bytes = await client.GetByteArrayAsync(file.FileUrl);
}
return bytes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ public partial class LocalFileStorageService
{
public string GetDirectory(string conversationId)
{
var dir = Path.Combine(_dbSettings.FileRepository, CONVERSATION_FOLDER, conversationId, "attachments");
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, "attachments");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
return dir;
}

public byte[] GetFileBytes(string fileStorageUrl)
public byte[] GetFileBytes(string filePath)
{
using var stream = File.OpenRead(fileStorageUrl);
using var stream = File.OpenRead(filePath);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnu
}
}
}

return files;
}

Expand Down Expand Up @@ -202,8 +201,8 @@ public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messag
var dir = GetConversationFileDirectory(conversationId, messageId);
if (!ExistDirectory(dir)) continue;

Thread.Sleep(100);
DeleteDirectory(dir);
Thread.Sleep(100);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BotSharp.Abstraction.Files.Converters;
using System.IO;

namespace BotSharp.Core.Files.Services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<bool> Execute(RoleDialogModel message)
if (isNeedAttachments)
{
var files = await GetConversationFiles();
await BuildEmailAttachments(bodyBuilder, files);
BuildEmailAttachments(bodyBuilder, files);
}

mailMessage.Body = bodyBuilder.ToMessageBody();
Expand Down Expand Up @@ -82,15 +82,16 @@ private async Task<IEnumerable<MessageFileModel>> GetConversationFiles()
return selecteds;
}

private async Task BuildEmailAttachments(BodyBuilder builder, IEnumerable<MessageFileModel> files)
private void BuildEmailAttachments(BodyBuilder builder, IEnumerable<MessageFileModel> files)
{
if (files.IsNullOrEmpty()) return;

foreach (var file in files)
{
if (string.IsNullOrEmpty(file.FileStorageUrl)) continue;

var fileBytes = await FileUtility.GetFileBytes(_services, file);
var fileStorage = _services.GetRequiredService<IFileStorageService>();
var fileBytes = fileStorage.GetFileBytes(file.FileStorageUrl);
builder.Attachments.Add($"{file.FileName}.{file.FileType}", fileBytes, ContentType.Parse(file.ContentType));
Thread.Sleep(100);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ private async Task<string> GetImageEditGeneration(RoleDialogModel message, strin
Name = "Utility Assistant"
};

var fileBytes = await FileUtility.GetFileBytes(_services, image);
var fileStorage = _services.GetRequiredService<IFileStorageService>();
var fileBytes = fileStorage.GetFileBytes(image.FileStorageUrl);
using var stream = new MemoryStream();
stream.Write(fileBytes);
stream.Position = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.IO;

namespace BotSharp.Plugin.TencentCos.Services;

public partial class TencentCosService
Expand All @@ -9,11 +7,11 @@ public string GetDirectory(string conversationId)
return $"{CONVERSATION_FOLDER}/{conversationId}/attachments/";
}

public byte[] GetFileBytes(string fileStorageUrl)
public byte[] GetFileBytes(string filePath)
{
try
{
var fileData = _cosClient.BucketClient.DownloadFileBytes(fileStorageUrl);
var fileData = _cosClient.BucketClient.DownloadFileBytes(filePath);
return fileData;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,23 @@ private string GetConversationFileDirectory(string? conversationId, string? mess
return dir;
}

private IEnumerable<string> GetMessageIds(IEnumerable<RoleDialogModel> conversations, int? offset = null)
private IEnumerable<string> GetMessageIds(IEnumerable<RoleDialogModel> dialogs, int? offset = null)
{
if (conversations.IsNullOrEmpty()) return Enumerable.Empty<string>();
if (dialogs.IsNullOrEmpty()) return Enumerable.Empty<string>();

if (offset <= 1)
if (offset.HasValue && offset < 1)
{
offset = 1;
}

var messageIds = new List<string>();
if (offset.HasValue)
{
messageIds = conversations.Select(x => x.MessageId).Distinct().TakeLast(offset.Value).ToList();
messageIds = dialogs.Select(x => x.MessageId).Distinct().TakeLast(offset.Value).ToList();
}
else
{
messageIds = conversations.Select(x => x.MessageId).Distinct().ToList();
messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
}

return messageIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private string GetUserAvatarDir(string? userId, bool createNewDir = false)
}

var dir = $"{USERS_FOLDER}/{userId}/{USER_AVATAR_FOLDER}/";

return dir;
}
#endregion
Expand Down
1 change: 0 additions & 1 deletion src/Plugins/BotSharp.Plugin.TencentCos/TencentCosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public TencentCosClient(TencentCosSettings settings)
settings.SecretId, settings.SecretKey, settings.KeyDurationSecond);

var cosXml = new CosXmlServer(cosXmlConfig, cosCredentialProvider);

BucketClient = new BucketClient(cosXml, $"{settings.BucketName}-{settings.AppId}", settings.AppId, settings.Region);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Plugins/BotSharp.Plugin.TencentCos/TencentCosPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
});

services.AddScoped<TencentCosClient>();

services.AddScoped<IFileStorageService, TencentCosService>();
}
}
Expand Down