diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index 99903e9f7..1df9a24b5 100644 --- a/IRaCIS.Core.API/Progranm.cs +++ b/IRaCIS.Core.API/Progranm.cs @@ -144,14 +144,24 @@ builder.Services.AddMasaMinimalAPIs(options => options.RouteHandlerBuilder= t=> { t.RequireAuthorization() .AddEndpointFilter() - .AddEndpointFilter() + //.AddEndpointFilter() .AddEndpointFilter() - .WithGroupName("Institution"); + .WithGroupName("Institution").DisableAntiforgery(); }; + options.MapHttpMethodsForUnmatched = new string[] { "Post" }; options.DisableTrimMethodPrefix = true; //禁用去除方法前缀 options.DisableAutoMapRoute = false;//可通过配置true禁用全局自动路由映射或者删除此配置以启用全局自动路由映射 }); +//// 添加反伪造服务 +//builder.Services.AddAntiforgery(options => +//{ +// // 可选:设置自定义的头部名称以支持 AJAX 请求等 +// options.HeaderName = "X-XSRF-TOKEN"; +//}); + +//builder.Services.AddAntiforgery(); + #endregion var app = builder.Build(); @@ -160,7 +170,6 @@ var env = app.Environment; #region 配置中间件 - app.UseMiddleware(); #region 异常处理 全局业务异常已统一处理了,非业务错误会来到这里 400 -500状态码 @@ -235,6 +244,13 @@ app.UseRouting(); app.UseCors(t => t.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); app.UseAuthentication(); app.UseAuthorization(); + + +//Map MinimalAPI routes +app.MapMasaMinimalAPIs(); + +//// 这里添加反伪造中间件 +//app.UseAntiforgery(); app.MapControllers(); app.MapHub("/UploadHub"); @@ -244,8 +260,7 @@ app.MapHealthChecks("/health"); #endregion -//Map MinimalAPI routes -app.MapMasaMinimalAPIs(); + // Serilog SerilogExtension.AddSerilogSetup(enviromentName, app.Services); diff --git a/IRaCIS.Core.Application/BusinessFilter/MinimalAPI/ModelValidationEndpointFilter.cs b/IRaCIS.Core.Application/BusinessFilter/MinimalAPI/Todo/ModelValidationEndpointFilter.cs similarity index 100% rename from IRaCIS.Core.Application/BusinessFilter/MinimalAPI/ModelValidationEndpointFilter.cs rename to IRaCIS.Core.Application/BusinessFilter/MinimalAPI/Todo/ModelValidationEndpointFilter.cs diff --git a/IRaCIS.Core.Application/BusinessFilter/MinimalAPI/TrialGlobalLimitEndpointFilter.cs b/IRaCIS.Core.Application/BusinessFilter/MinimalAPI/Todo/TrialGlobalLimitEndpointFilter.cs similarity index 100% rename from IRaCIS.Core.Application/BusinessFilter/MinimalAPI/TrialGlobalLimitEndpointFilter.cs rename to IRaCIS.Core.Application/BusinessFilter/MinimalAPI/Todo/TrialGlobalLimitEndpointFilter.cs diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index ce70222db..9082c2c79 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -2273,6 +2273,20 @@ + + + minimal api 测试 + 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html + 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 + + + + + minimal api 测试 + 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html + 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 + + 验证CRC 是否已提交 已提交 就不允许进行任何操作,如果是IQC 那么还验证是否是当前任务领取人 @@ -12683,20 +12697,6 @@ - - - minimal api 测试 - 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html - 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 - - - - - minimal api 测试 - 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html - 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 - - 清理一致性分析任务 diff --git a/IRaCIS.Core.Application/Service/MinimalApiService/FileToPDFService.cs b/IRaCIS.Core.Application/Service/MinimalApiService/FileToPDFService.cs new file mode 100644 index 000000000..9e4570b79 --- /dev/null +++ b/IRaCIS.Core.Application/Service/MinimalApiService/FileToPDFService.cs @@ -0,0 +1,132 @@ +using IRaCIS.Core.Application.Helper; +using MassTransit; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.StaticFiles; +using RestSharp; +using SharpCompress.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Service.MinimalApiService +{ + [ApiExplorerSettings(GroupName = "Institution")] + public class FileToPDFService(IWebHostEnvironment _hostEnvironment) : ServiceBase + { + + [AllowAnonymous] + [RoutePattern(HttpMethod = "Post")] + public async Task UploadFileAsync([FromForm] IFormFile file) + { + var tempFileName = NewId.NextGuid() + file.FileName; + var tempPDFName = Path.GetFileNameWithoutExtension(tempFileName) + ".pdf"; + + + // 获取wwwroot目录 + var wwwRootPath = Path.Combine(FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment), "temp"); + + // 检查wwwroot/temp目录是否存在,不存在则创建 + if (!Directory.Exists(wwwRootPath)) + { + Directory.CreateDirectory(wwwRootPath); + } + + // 文件保存路径 + var pdfFilePath = Path.Combine(wwwRootPath, tempPDFName); + var tempFilePath = Path.Combine(wwwRootPath, tempFileName); + + + using var stream = File.OpenWrite(tempFilePath); + await file.CopyToAsync(stream); + + FileConvertHelper.ConvertWordToPdf(tempFilePath, Path.GetDirectoryName(pdfFilePath)); + + var fileBytes = await File.ReadAllBytesAsync(pdfFilePath); + + // 清理临时上传的文件和pdf + if (File.Exists(pdfFilePath)) + { + File.Delete(pdfFilePath); + } + if (File.Exists(tempFilePath)) + { + File.Delete(tempFilePath); + } + + new FileExtensionContentTypeProvider().Mappings.TryGetValue(Path.GetExtension(tempPDFName), out var contentType); + + return Results.File(fileBytes, contentType); + + } + + + public async Task GetPDFFileAsync(string fileUrl) + { + var tempFileName = NewId.NextGuid() + Path.GetFileName(fileUrl); + var tempPDFName = Path.GetFileNameWithoutExtension(tempFileName) + ".pdf"; + + + // 获取wwwroot目录 + var wwwRootPath = Path.Combine(FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment), "temp"); + + // 检查wwwroot/temp目录是否存在,不存在则创建 + if (!Directory.Exists(wwwRootPath)) + { + Directory.CreateDirectory(wwwRootPath); + } + + // 文件保存路径 + var pdfFilePath = Path.Combine(wwwRootPath, tempPDFName); + var tempFilePath = Path.Combine(wwwRootPath, tempFileName); + + + //请求url获取文件 + var client = new RestClient(fileUrl); + + var request = new RestRequest(fileUrl, Method.Get); + + var response = await client.ExecuteAsync(request); + + // 检查响应是否成功 + if (response.IsSuccessful) + { + // 将响应内容写入到本地文件 + await File.WriteAllBytesAsync(tempFilePath, response.RawBytes); + + FileConvertHelper.ConvertWordToPdf(tempFilePath, Path.GetDirectoryName(pdfFilePath)); + + var fileBytes = await File.ReadAllBytesAsync(pdfFilePath); + + // 清理临时上传的文件和pdf + if (File.Exists(pdfFilePath)) + { + File.Delete(pdfFilePath); + } + if (File.Exists(tempFilePath)) + { + File.Delete(tempFilePath); + } + + new FileExtensionContentTypeProvider().Mappings.TryGetValue(Path.GetExtension(tempPDFName), out var contentType); + + return Results.File(fileBytes, contentType); + } + else + { + Console.WriteLine($"下载文件失败: {response.ErrorMessage}"); + + return Results.Problem("下载文件失败", statusCode: StatusCodes.Status500InternalServerError); + + } + + + } + + } +} diff --git a/IRaCIS.Core.Application/Service/MinimalApiService/TestMinimalApiService.cs b/IRaCIS.Core.Application/Service/MinimalApiService/TestMinimalApiService.cs new file mode 100644 index 000000000..5b0678790 --- /dev/null +++ b/IRaCIS.Core.Application/Service/MinimalApiService/TestMinimalApiService.cs @@ -0,0 +1,57 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Service.MinimalApiService +{ + /// + /// minimal api 测试 + /// 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html + /// 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 + /// + [ApiExplorerSettings(GroupName = "Institution")] + + public class TestMinimalApiService(IUserInfo _userInfo) : ServiceBase + { + + + + public Task> GetProjectList1Async() + { + var list = new List() + { + "Auth", + "DCC", + "PM" + }; + return Task.FromResult(list); + } + + [AllowAnonymous] + public IResponseOutput GetTest() + { + + //throw new BusinessValidationFailedException("手动抛出的异常"); + + return ResponseOutput.Ok(_userInfo.IP); + } + + public IResponseOutput GetTestI18n() + { + var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US; + + //CultureInfo.CurrentCulture = new CultureInfo(StaticData.CultureInfo.en_US); + //CultureInfo.CurrentUICulture = new CultureInfo(StaticData.CultureInfo.en_US); + + return ResponseOutput.Ok(I18n.T("TaskAllocation_DoctorConfigExists")); + } + + } + +} diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index f3955893b..c764d0838 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -33,49 +33,6 @@ using System.Text; namespace IRaCIS.Core.Application.Service { - /// - /// minimal api 测试 - /// 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html - /// 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0 - /// - [ApiExplorerSettings(GroupName = "Institution")] - - public class TestMinimalApiService(IUserInfo _userInfo) : ServiceBase - { - - - - public Task> GetProjectList1Async() - { - var list = new List() - { - "Auth", - "DCC", - "PM" - }; - return Task.FromResult(list); - } - - [AllowAnonymous] - public IResponseOutput GetTest() - { - - //throw new BusinessValidationFailedException("手动抛出的异常"); - - return ResponseOutput.Ok(_userInfo.IP); - } - - public IResponseOutput GetTestI18n() - { - var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US; - - //CultureInfo.CurrentCulture = new CultureInfo(StaticData.CultureInfo.en_US); - //CultureInfo.CurrentUICulture = new CultureInfo(StaticData.CultureInfo.en_US); - - return ResponseOutput.Ok(I18n.T("TaskAllocation_DoctorConfigExists")); - } - - }