Skip to content

Commit 25ecf41

Browse files
authored
Merge pull request #567 from GreenShadeZhang/tencent-cos
Add Tencent Cloud object storage support
2 parents e497233 + fdab693 commit 25ecf41

23 files changed

+1263
-11
lines changed

BotSharp.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.FileHandler
101101
EndProject
102102
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Planner", "src\Plugins\BotSharp.Plugin.Planner\BotSharp.Plugin.Planner.csproj", "{54E83C6F-54EE-4ADC-8D72-93C009CC4FB4}"
103103
EndProject
104+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FileStorages", "FileStorages", "{38B37C0D-1930-4D47-BCBF-E358EC1096B1}"
105+
EndProject
106+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.TencentCos", "src\Plugins\BotSharp.Plugin.TencentCos\BotSharp.Plugin.TencentCos.csproj", "{BF029B0A-768B-43A1-8D91-E70B95505716}"
107+
EndProject
104108
Global
105109
GlobalSection(SolutionConfigurationPlatforms) = preSolution
106110
Debug|Any CPU = Debug|Any CPU
@@ -413,6 +417,14 @@ Global
413417
{54E83C6F-54EE-4ADC-8D72-93C009CC4FB4}.Release|Any CPU.Build.0 = Release|Any CPU
414418
{54E83C6F-54EE-4ADC-8D72-93C009CC4FB4}.Release|x64.ActiveCfg = Release|Any CPU
415419
{54E83C6F-54EE-4ADC-8D72-93C009CC4FB4}.Release|x64.Build.0 = Release|Any CPU
420+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
421+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Debug|Any CPU.Build.0 = Debug|Any CPU
422+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Debug|x64.ActiveCfg = Debug|Any CPU
423+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Debug|x64.Build.0 = Debug|Any CPU
424+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Release|Any CPU.ActiveCfg = Release|Any CPU
425+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Release|Any CPU.Build.0 = Release|Any CPU
426+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Release|x64.ActiveCfg = Release|Any CPU
427+
{BF029B0A-768B-43A1-8D91-E70B95505716}.Release|x64.Build.0 = Release|Any CPU
416428
EndGlobalSection
417429
GlobalSection(SolutionProperties) = preSolution
418430
HideSolutionNode = FALSE
@@ -461,6 +473,8 @@ Global
461473
{A72B3BEB-E14B-4917-BE44-97EAE4E122D2} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
462474
{D6A99D4F-6248-419E-8A43-B38ADEBABA2C} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
463475
{54E83C6F-54EE-4ADC-8D72-93C009CC4FB4} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
476+
{38B37C0D-1930-4D47-BCBF-E358EC1096B1} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
477+
{BF029B0A-768B-43A1-8D91-E70B95505716} = {38B37C0D-1930-4D47-BCBF-E358EC1096B1}
464478
EndGlobalSection
465479
GlobalSection(ExtensibilityGlobals) = postSolution
466480
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Repositories.Enums;
2+
3+
public static class FileStorageEnum
4+
{
5+
public const string LocalFileStorage = nameof(LocalFileStorage);
6+
public const string AmazonS3Storage = nameof(AmazonS3Storage);
7+
public const string AzureBlobStorage = nameof(AzureBlobStorage);
8+
public const string TencentCosStorage = nameof(TencentCosStorage);
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.Files;
2+
3+
public class FileStorageSettings
4+
{
5+
public string Default { get; set; }
6+
}

src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.IO;
2+
13
namespace BotSharp.Abstraction.Files;
24

35
public interface IBotSharpFileService
@@ -74,5 +76,7 @@ Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string s
7476
(string, byte[]) GetFileInfoFromData(string data);
7577
string GetDirectory(string conversationId);
7678
string GetFileContentType(string filePath);
79+
byte[] GetFileBytes(string fileStorageUrl);
80+
bool SavefileToPath(string filePath, Stream stream);
7781
#endregion
7882
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
172172
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
173173
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.5.0" />
174-
<PackageReference Include="Fluid.Core" Version="2.8.0" />
174+
<PackageReference Include="Fluid.Core" Version="2.11.1" />
175175
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
176176
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
177177
<PackageReference Include="Nanoid" Version="3.1.0" />

src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Repositories.Enums;
12
using BotSharp.Core.Files.Services;
23
using Microsoft.Extensions.Configuration;
34

@@ -14,6 +15,12 @@ public class FilePlugin : IBotSharpPlugin
1415

1516
public void RegisterDI(IServiceCollection services, IConfiguration config)
1617
{
17-
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
18+
var myFileStorageSettings = new FileStorageSettings();
19+
config.Bind("FileStorage", myFileStorageSettings);
20+
21+
if (myFileStorageSettings.Default == FileStorageEnum.LocalFileStorage)
22+
{
23+
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
24+
}
1825
}
1926
}

src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Common.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,21 @@ public string GetFileContentType(string filePath)
4343

4444
return contentType;
4545
}
46+
47+
public byte[] GetFileBytes(string fileStorageUrl)
48+
{
49+
using var stream = File.OpenRead(fileStorageUrl);
50+
var bytes = new byte[stream.Length];
51+
stream.Read(bytes, 0, (int)stream.Length);
52+
return bytes;
53+
}
54+
55+
public bool SavefileToPath(string filePath, Stream stream)
56+
{
57+
using (var fileStream = new FileStream(filePath, FileMode.Create))
58+
{
59+
stream.CopyTo(fileStream);
60+
}
61+
return true;
62+
}
4663
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,7 @@ public IActionResult UploadAttachments([FromRoute] string conversationId,
356356
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
357357
var filePath = Path.Combine(dir, fileName);
358358

359-
using (var stream = new FileStream(filePath, FileMode.Create))
360-
{
361-
file.CopyTo(stream);
362-
}
359+
fileService.SavefileToPath(filePath, file.OpenReadStream());
363360
}
364361

365362
return Ok(new { message = "File uploaded successfully." });

src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,8 @@ public IActionResult GetUserAvatar()
158158
#region Private methods
159159
private FileContentResult BuildFileResult(string file)
160160
{
161-
using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
162-
var bytes = new byte[stream.Length];
163-
stream.Read(bytes, 0, (int)stream.Length);
161+
var fileService = _services.GetRequiredService<IBotSharpFileService>();
162+
var bytes = fileService.GetFileBytes(file);
164163
return File(bytes, "application/octet-stream", Path.GetFileName(file));
165164
}
166165
#endregion
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(TargetFramework)</TargetFramework>
5+
<LangVersion>$(LangVersion)</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
8+
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
10+
<OutputPath>$(SolutionDir)packages</OutputPath>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.39" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)