get file bytes
This commit is contained in:
parent
c2ed5e4030
commit
7a3dcc77a5
|
|
@ -52,7 +52,7 @@ public interface IFileStorageService
|
|||
|
||||
#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);
|
||||
|
|
|
|||
|
|
@ -43,25 +43,4 @@ public static class FileUtility
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ 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);
|
||||
|
|
@ -14,9 +14,9 @@ public partial class LocalFileStorageService
|
|||
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;
|
||||
|
|
@ -87,7 +87,6 @@ public partial class LocalFileStorageService
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
|
|
@ -202,8 +201,8 @@ public partial class LocalFileStorageService
|
|||
var dir = GetConversationFileDirectory(conversationId, messageId);
|
||||
if (!ExistDirectory(dir)) continue;
|
||||
|
||||
Thread.Sleep(100);
|
||||
DeleteDirectory(dir);
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
using BotSharp.Abstraction.Files.Converters;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Files.Services;
|
||||
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ public class HandleEmailSenderFn : IFunctionCallback
|
|||
if (isNeedAttachments)
|
||||
{
|
||||
var files = await GetConversationFiles();
|
||||
await BuildEmailAttachments(bodyBuilder, files);
|
||||
BuildEmailAttachments(bodyBuilder, files);
|
||||
}
|
||||
|
||||
mailMessage.Body = bodyBuilder.ToMessageBody();
|
||||
|
|
@ -82,7 +82,7 @@ public class HandleEmailSenderFn : IFunctionCallback
|
|||
return selecteds;
|
||||
}
|
||||
|
||||
private async Task BuildEmailAttachments(BodyBuilder builder, IEnumerable<MessageFileModel> files)
|
||||
private void BuildEmailAttachments(BodyBuilder builder, IEnumerable<MessageFileModel> files)
|
||||
{
|
||||
if (files.IsNullOrEmpty()) return;
|
||||
|
||||
|
|
@ -90,7 +90,8 @@ public class HandleEmailSenderFn : IFunctionCallback
|
|||
{
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,7 +78,8 @@ public class EditImageFn : IFunctionCallback
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
using System.IO;
|
||||
|
||||
namespace BotSharp.Plugin.TencentCos.Services;
|
||||
|
||||
public partial class TencentCosService
|
||||
|
|
@ -9,11 +7,11 @@ public partial class TencentCosService
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -223,11 +223,11 @@ public partial class TencentCosService
|
|||
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;
|
||||
}
|
||||
|
|
@ -235,11 +235,11 @@ public partial class TencentCosService
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -51,7 +51,6 @@ public partial class TencentCosService
|
|||
}
|
||||
|
||||
var dir = $"{USERS_FOLDER}/{userId}/{USER_AVATAR_FOLDER}/";
|
||||
|
||||
return dir;
|
||||
}
|
||||
#endregion
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ namespace BotSharp.Plugin.TencentCos
|
|||
settings.SecretId, settings.SecretKey, settings.KeyDurationSecond);
|
||||
|
||||
var cosXml = new CosXmlServer(cosXmlConfig, cosCredentialProvider);
|
||||
|
||||
BucketClient = new BucketClient(cosXml, $"{settings.BucketName}-{settings.AppId}", settings.AppId, settings.Region);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ public class TencentCosPlugin : IBotSharpPlugin
|
|||
});
|
||||
|
||||
services.AddScoped<TencentCosClient>();
|
||||
|
||||
services.AddScoped<IFileStorageService, TencentCosService>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue