转pdf minimal api 完成
continuous-integration/drone/push Build is passing Details

IRC_NewDev
hang 2024-10-19 18:22:58 +08:00
parent 41444bb292
commit 0ac17ac27f
7 changed files with 223 additions and 62 deletions

View File

@ -144,14 +144,24 @@ builder.Services.AddMasaMinimalAPIs(options =>
options.RouteHandlerBuilder= t=> {
t.RequireAuthorization()
.AddEndpointFilter<LimitUserRequestAuthorizationEndpointFilter>()
.AddEndpointFilter<ModelValidationEndpointFilter>()
//.AddEndpointFilter<ModelValidationEndpointFilter>()
.AddEndpointFilter<UnifiedApiResultEndpointFilter>()
.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<EncryptionRequestMiddleware>();
#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>("/UploadHub");
@ -244,8 +260,7 @@ app.MapHealthChecks("/health");
#endregion
//Map MinimalAPI routes
app.MapMasaMinimalAPIs();
// Serilog
SerilogExtension.AddSerilogSetup(enviromentName, app.Services);

View File

@ -2273,6 +2273,20 @@
<param name="password"></param>
<returns></returns>
</member>
<member name="T:IRaCIS.Core.Application.Service.MinimalApiService.TestMinimalApiService">
<summary>
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
</summary>
</member>
<member name="M:IRaCIS.Core.Application.Service.MinimalApiService.TestMinimalApiService.#ctor(IRaCIS.Core.Domain.Share.IUserInfo)">
<summary>
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
</summary>
</member>
<member name="M:IRaCIS.Core.Application.Service.QCCommon.VerifyIsCRCSubmmitAsync(IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.SubjectVisit},IRaCIS.Core.Domain.Share.IUserInfo,System.Nullable{System.Guid})">
<summary>
验证CRC 是否已提交 已提交 就不允许进行任何操作如果是IQC 那么还验证是否是当前任务领取人
@ -12683,20 +12697,6 @@
<param name="outEnrollTime"></param>
<returns></returns>
</member>
<member name="T:IRaCIS.Core.Application.Service.TestMinimalApiService">
<summary>
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
</summary>
</member>
<member name="M:IRaCIS.Core.Application.Service.TestMinimalApiService.#ctor(IRaCIS.Core.Domain.Share.IUserInfo)">
<summary>
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
</summary>
</member>
<member name="M:IRaCIS.Core.Application.Service.TestService.DeleteConsistentDate(System.Guid,IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.TaskConsistentRule},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.ReadingConsistentClinicalDataPDF},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.ReadingConsistentClinicalData})">
<summary>
清理一致性分析任务

View File

@ -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<IResult> 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<IResult> 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);
}
}
}
}

View File

@ -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
{
/// <summary>
/// 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
/// </summary>
[ApiExplorerSettings(GroupName = "Institution")]
public class TestMinimalApiService(IUserInfo _userInfo) : ServiceBase
{
public Task<List<string>> GetProjectList1Async()
{
var list = new List<string>()
{
"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"));
}
}
}

View File

@ -33,49 +33,6 @@ using System.Text;
namespace IRaCIS.Core.Application.Service
{
/// <summary>
/// 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
/// </summary>
[ApiExplorerSettings(GroupName = "Institution")]
public class TestMinimalApiService(IUserInfo _userInfo) : ServiceBase
{
public Task<List<string>> GetProjectList1Async()
{
var list = new List<string>()
{
"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"));
}
}