Explorar o código

新增测试用例

qinchaoyue hai 1 mes
pai
achega
85dd64d006

+ 14 - 0
src/Hotline.Share/Tools/StringExtensions.cs

@@ -83,4 +83,18 @@ public static class StringExtensions
             return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(value))).Replace("-", string.Empty);
         }
     }
+
+    /// <summary>
+    /// 将 Stream 读取为 byte[]
+    /// </summary>
+    /// <param name="stream">要读取的 Stream</param>
+    /// <returns>byte[]</returns>
+    public static async Task<byte[]> ToByteArrayAsync(this Stream stream)
+    {
+        using (var memoryStream = new MemoryStream())
+        {
+            await stream.CopyToAsync(memoryStream);
+            return memoryStream.ToArray();
+        }
+    }
 }

+ 10 - 2
src/Hotline/File/FileDomainService.cs

@@ -2,6 +2,7 @@
 using Hotline.Caching.Interfaces;
 using Hotline.Settings;
 using Hotline.Share.Dtos.File;
+using Hotline.Share.Enums.Article;
 using Hotline.Share.Tools;
 using Microsoft.Extensions.Logging;
 using Renci.SshNet;
@@ -113,12 +114,12 @@ public class FileDomainService : IFileDomainService, IScopeDependency
         return null;
     }
 
-    public async Task<string> UploadFileFromMemoryAsync(byte[] fileBytes, string fileName, string uploadUrl)
+    public async Task<string> UploadFileFromMemoryAsync(byte[] fileBytes, string fileName, string uploadUrl, string contentType = "image/jpeg")
     {
         using HttpClient client = new HttpClient();
         using var multipartContent = new MultipartFormDataContent();
         var fileContent = new ByteArrayContent(fileBytes);
-        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
+        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
 
         multipartContent.Add(fileContent, "fileData", fileName);
 
@@ -128,5 +129,12 @@ public class FileDomainService : IFileDomainService, IScopeDependency
         var result = await response.Content.ReadAsStringAsync();
         return result;
     }
+
+    public async Task<FileJson> UploadFileFromMemoryAsync(Stream fileStream, string fileName, EFileType fileType)
+    {
+        var fileByte =  await fileStream.ToByteArrayAsync();
+        var uploadUrl = _setting.FileServerUrl + "/file/upload?source=hotline";
+        return (await UploadFileFromMemoryAsync(fileByte, fileName, uploadUrl, fileType.GetContentType())).FromJson<ApiResponse<FileJson>>().Result;
+    }
 }
 

+ 5 - 1
src/Hotline/File/IFileDomainService.cs

@@ -1,4 +1,6 @@
-using System;
+using Hotline.Share.Dtos.File;
+using Hotline.Share.Enums.Article;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -15,5 +17,7 @@ public interface IFileDomainService
     /// <param name="cancellationToken"></param>
     /// <returns></returns>
     Task<string> GetNetworkFileAsync(string file, string key, CancellationToken cancellationToken = default);
+
+    Task<FileJson> UploadFileFromMemoryAsync(Stream fileStream, string fileName, EFileType fileType);
 }
 

+ 16 - 1
test/Hotline.Tests/Application/KnowApplicationTest.cs

@@ -1,11 +1,15 @@
 using Hotline.Api.Controllers;
 using Hotline.Application.Knowledge;
+using Hotline.Application.Tools;
+using Hotline.File;
 using Hotline.Identity.Accounts;
 using Hotline.Identity.Roles;
 using Hotline.KnowledgeBase;
 using Hotline.KnowledgeBase.Notifies;
 using Hotline.Settings;
+using Hotline.Share.Dtos.File;
 using Hotline.Share.Dtos.Knowledge;
+using Hotline.Share.Enums.Article;
 using Hotline.Share.Tools;
 using Hotline.ThirdAccountDomainServices;
 using Hotline.ThirdAccountDomainServices.Interfaces;
@@ -14,6 +18,7 @@ using Mapster;
 using MediatR;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.DependencyInjection;
+using Senparc.Weixin.WxOpen.AdvancedAPIs.Tcb;
 using Shouldly;
 using System;
 using System.Collections.Generic;
@@ -33,8 +38,9 @@ public class KnowApplicationTest : TestBase
     private readonly IKnowledgeDomainService _knowledgeDomainService;
     private readonly IRepository<KnowledgeWord> _knowledgeWordRepository;
     private readonly IRepository<KnowledgeHotWord> _knowledgeHotWordRepository;
+    private readonly IFileDomainService _fileDomainService;
 
-    public KnowApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IKnowApplication knowApplication, IMediator mediator, IRepository<KnowledgeBase.Knowledge> knowledgeRepository, IKnowledgeDomainService knowledgeDomainService, IRepository<KnowledgeWord> knowledgeWordRepository, IRepository<KnowledgeHotWord> knowledgeHotWordRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount, cacheSettingData, thirdAccountDomainFactory)
+    public KnowApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IKnowApplication knowApplication, IMediator mediator, IRepository<KnowledgeBase.Knowledge> knowledgeRepository, IKnowledgeDomainService knowledgeDomainService, IRepository<KnowledgeWord> knowledgeWordRepository, IRepository<KnowledgeHotWord> knowledgeHotWordRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory, IFileDomainService fileDomainService) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount, cacheSettingData, thirdAccountDomainFactory)
     {
         _knowApplication = knowApplication;
         _mediator = mediator;
@@ -42,6 +48,7 @@ public class KnowApplicationTest : TestBase
         _knowledgeDomainService = knowledgeDomainService;
         _knowledgeWordRepository = knowledgeWordRepository;
         _knowledgeHotWordRepository = knowledgeHotWordRepository;
+        _fileDomainService = fileDomainService;
     }
 
     [Fact]
@@ -127,4 +134,12 @@ public class KnowApplicationTest : TestBase
         a.IsNullOrEmpty().ShouldBeTrue();
         a.NotNullOrEmpty().ShouldBeFalse();
     }
+
+    [Fact]
+    public async Task Word_Upload_Test()
+    {
+
+        var fileJson = await _fileDomainService.UploadFileFromMemoryAsync("<div>123</div>".HtmlToWord(), "测试文件" + EFileType.word.GetFileExtension(), EFileType.word);
+        fileJson.Id.ShouldNotBeNull();
+    }
 }