利用第三方服务转pdf
continuous-integration/drone/push Build is passing Details

IRC_NewDev
hang 2024-10-19 16:31:27 +08:00
parent f97cfd58c0
commit 4ab4fbd599
10 changed files with 201 additions and 17 deletions

View File

@ -72,7 +72,9 @@
"ReadingRestTimeMin": 10,
"IsNeedChangePassWord": true,
"ChangePassWordDays": 90
"ChangePassWordDays": 90,
"ThirdPdfUrl": "http://106.14.89.110:30088/api/v1/convert/file/pdf"
},
"SystemEmailSendConfig": {

View File

@ -34,6 +34,8 @@ public class ServiceVerifyConfigOption
[Description("修改密码的天数")]
public int ChangePassWordDays { get; set; }
public string ThirdPdfUrl { get; set; }
}
public class SystemEmailSendConfig

View File

@ -1,11 +1,18 @@
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using RestSharp;
using SharpCompress.Common;
using System.Diagnostics;
namespace IRaCIS.Core.Application.Helper;
public class FileConvertHelper
{
/// <summary>
/// 镜像里面打入libreoffice 的方案
/// </summary>
/// <param name="inputWordFilePath"></param>
/// <param name="outputPdfDir"></param>
static public void ConvertWordToPdf(string inputWordFilePath, string outputPdfDir)
{
// 设置 libreoffice 命令行参数
@ -30,4 +37,88 @@ public class FileConvertHelper
}
/// <summary>
/// 返回文件字节数组
/// File.WriteAllBytes(outputFilePath, response.RawBytes); 直接将字节数组写入到本地某个路径
/// var stream = new MemoryStream(response.RawBytes); 直接变为内存流,给其他程序用也可以
/// </summary>
/// <returns></returns>
static public async Task<byte[]> ThirdStirling_PDFWordToPdfAsync(string filePath)
{
var apiUrl = GetApiUrl();
var client = new RestClient(apiUrl);
var request = new RestRequest(apiUrl, Method.Post);
request.AddHeader("accept", "*/*");
// 直接上传文件,无需生成字节数组
request.AddFile("fileInput", filePath);
return await GetPDFByteArrayAsync(client, request);
}
static public async Task<byte[]> ThirdStirling_PDFWordToPdfAsync(byte[] fileBytes, string fileName)
{
var apiUrl = GetApiUrl();
var client = new RestClient(apiUrl);
var request = new RestRequest(apiUrl, Method.Post);
request.AddHeader("accept", "*/*");
// 添加文件流到请求
request.AddFile("fileInput", fileBytes, fileName);
return await GetPDFByteArrayAsync(client, request);
}
static private async Task<byte[]> GetPDFByteArrayAsync(RestClient client, RestRequest request)
{
try
{
// 发送请求并获取响应
var response = await client.ExecuteAsync(request);
// 检查请求是否成功
if (response.IsSuccessful)
{
return response.RawBytes ?? new byte[] { };
}
else
{
Console.WriteLine($"上传失败,错误代码: {response.StatusCode}, 错误消息: {response.ErrorMessage}");
return Array.Empty<byte>();
}
}
catch (Exception ex)
{
Console.WriteLine("发生异常: " + ex.Message);
return Array.Empty<byte>();
}
}
static private string GetApiUrl()
{
var enviromentName = Environment.GetEnvironmentVariables()["ASPNETCORE_ENVIRONMENT"]?.ToString();
var configuration = new ConfigurationBuilder().AddJsonFile($"appsettings.{enviromentName}.json", false, false).Build();
// 手动绑定配置
var appSettings = new ServiceVerifyConfigOption();
configuration.GetSection("BasicSystemConfig").Bind(appSettings);
return appSettings.ThirdPdfUrl;
}
}

View File

@ -12762,6 +12762,21 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:IRaCIS.Core.Application.Helper.FileConvertHelper.ConvertWordToPdf(System.String,System.String)">
<summary>
镜像里面打入libreoffice 的方案
</summary>
<param name="inputWordFilePath"></param>
<param name="outputPdfDir"></param>
</member>
<member name="M:IRaCIS.Core.Application.Helper.FileConvertHelper.ThirdStirling_PDFWordToPdfAsync(System.String)">
<summary>
返回文件字节数组
File.WriteAllBytes(outputFilePath, response.RawBytes); 直接将字节数组写入到本地某个路径
var stream = new MemoryStream(response.RawBytes); 直接变为内存流,给其他程序用也可以
</summary>
<returns></returns>
</member>
<member name="M:IRaCIS.Core.Application.Helper.FileStoreHelper.WriteFileAsync(System.IO.Stream,System.String)">
<summary>
写文件导到磁盘
@ -12916,6 +12931,11 @@
加急阅片 IR 申请重阅 或者PM 申请重阅
</summary>
</member>
<member name="M:IRaCIS.Core.Application.MassTransit.Consumer.UrgentIRApplyedReReadingConsumer.#ctor(IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.User},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.TrialUser},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.TaskMedicalReview},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.Trial},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.SubjectVisit},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.ReadingQuestionCriterionTrial},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.VisitTask},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.Dictionary},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.EmailNoticeConfig},Microsoft.Extensions.Options.IOptionsMonitor{IRaCIS.Core.Domain.Share.SystemEmailSendConfig})">
<summary>
加急阅片 IR 申请重阅 或者PM 申请重阅
</summary>
</member>
<member name="T:IRaCIS.Core.Application.MassTransit.Consumer.UserSiteSurveySubmitedEventConsumer">
<summary>
用户提交 发送邮件 通知SPM 或者PM
@ -13042,17 +13062,17 @@
影像质控
</summary>
</member>
<member name="T:IRaCIS.Core.Application.MassTransit.Consumer.QCImageQuestionSchedule">
<member name="T:IRaCIS.Core.Application.MassTransit.Consumer.QCImageQuestionRecurringSchedule">
<summary>
QC 影像质疑待处理
</summary>
</member>
<member name="T:IRaCIS.Core.Application.MassTransit.Consumer.CRCImageQuestionSchedule">
<member name="T:IRaCIS.Core.Application.MassTransit.Consumer.CRCImageQuestionRecurringSchedule">
<summary>
CRC 影像质疑
</summary>
</member>
<member name="T:IRaCIS.Core.Application.MassTransit.Consumer.ImageQCSchedule">
<member name="T:IRaCIS.Core.Application.MassTransit.Consumer.ImageQCRecurringSchedule">
<summary>
影像质控
</summary>

View File

@ -1,14 +1,18 @@
using IRaCIS.Core.Domain;
using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Domain;
using IRaCIS.Core.Domain.Share;
using MassTransit;
using MassTransit.Mediator;
using MassTransit.Scheduling;
using Medallion.Threading;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Options;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -71,11 +75,76 @@ public class TestMasstransitService : BaseService
////发布后,不会立即进入消费者,消费者是另外的线程执行
//await _mediatorScoped.Publish(new MasstransiTestCommand { value = "message at " + DateTime.Now.ToString() });
await _recurringMessageScheduler.ScheduleRecurringPublish(new QCImageQuestionSchedule() { CronExpression = "0/3 * * * * ? " }, new MasstransiTestCommand { value = "message at " + DateTime.Now.ToString() });
await _recurringMessageScheduler.ScheduleRecurringPublish(new QCImageQuestionRecurringSchedule() { CronExpression = "0/3 * * * * ? " }, new MasstransiTestCommand { value = "message at " + DateTime.Now.ToString() });
//await _scheduler.SchedulePublish(DateTime.Now.AddSeconds(10), new MasstransiTestCommand() { value = "message at " + DateTime.Now.ToString() });
return ResponseOutput.Ok();
}
public async Task<IResponseOutput> TestWordToPdf()
{
var dd = await FileConvertHelper.ThirdStirling_PDFWordToPdfAsync(@"C:\Users\hang\Desktop\test.docx");
string outputFilePath = @"C:\Users\hang\Desktop\test-byte4-output.pdf"; // 保存路径
File.WriteAllBytes(outputFilePath, dd); // 将响应字节数组写入文件
//var envName = Environment.GetEnvironmentVariables()["ASPNETCORE_ENVIRONMENT"]?.ToString();
//// API 地址
//string apiUrl = "http://106.14.89.110:30088/api/v1/convert/file/pdf";
//// 模拟文件路径,假设你有一个内存流的文件,不指定实际文件
//string filePath = @"C:\Users\hang\Desktop\test.docx"; // 本地文件路径或者你可以直接使用流
//var fileBytes = File.ReadAllBytes(@"C:\Users\hang\Desktop\test.docx");
//try
//{
// // 创建 RestClient
// var client = new RestClient(apiUrl);
// // 创建 RestRequest使用 POST 方法
// var request = new RestRequest(apiUrl, Method.Post);
// request.AddHeader("accept", "*/*");
// //new FileExtensionContentTypeProvider().Mappings.TryGetValue(Path.GetExtension(filePath), out var contentType);
// // 直接上传文件,无需生成字节数组
// //request.AddFile("fileInput", filePath, contentType);
// // 添加文件流到请求
// request.AddFile("fileInput", fileBytes, "test-byte.docx");
// // 发送请求并获取响应
// var response = await client.ExecuteAsync(request);
// // 检查请求是否成功
// if (response.IsSuccessful)
// {
// // 保存接收到的文件到本地
// string outputFilePath = @"C:\Users\hang\Desktop\test-byte3-output.pdf"; // 保存路径
// File.WriteAllBytes(outputFilePath, response.RawBytes); // 将响应字节数组写入文件
// //如果要文件流,就这样
// //var stream = new MemoryStream(response.RawBytes);
// }
// else
// {
// Console.WriteLine($"上传失败,错误代码: {response.StatusCode}, 错误消息: {response.ErrorMessage}");
// }
//}
//catch (Exception ex)
//{
// Console.WriteLine("发生异常: " + ex.Message);
//}
return ResponseOutput.Ok();
}
}

View File

@ -11,9 +11,9 @@ namespace IRaCIS.Core.Application.MassTransit.Consumer;
public class QCImageQuestionScheduleConsumer : IConsumer<QCImageQuestionSchedule>
public class QCImageQuestionScheduleConsumer : IConsumer<QCImageQuestionRecurringSchedule>
{
public Task Consume(ConsumeContext<QCImageQuestionSchedule> context)
public Task Consume(ConsumeContext<QCImageQuestionRecurringSchedule> context)
{
Console.WriteLine(DateTime.Now);

View File

@ -32,7 +32,7 @@ public abstract class IRCRecurringSchedule :
/// <summary>
/// QC 影像质疑待处理
/// </summary>
public class QCImageQuestionSchedule : IRCRecurringSchedule
public class QCImageQuestionRecurringSchedule : IRCRecurringSchedule
{
}
@ -40,7 +40,7 @@ public class QCImageQuestionSchedule : IRCRecurringSchedule
/// <summary>
/// CRC 影像质疑
/// </summary>
public class CRCImageQuestionSchedule : IRCRecurringSchedule
public class CRCImageQuestionRecurringSchedule : IRCRecurringSchedule
{
}
@ -48,7 +48,7 @@ public class CRCImageQuestionSchedule : IRCRecurringSchedule
/// <summary>
/// 影像质控
/// </summary>
public class ImageQCSchedule : IRCRecurringSchedule
public class ImageQCRecurringSchedule : IRCRecurringSchedule
{
}

View File

@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="EntityFrameworkCore.Projectables.Abstractions" Version="3.0.4" />
<PackageReference Include="MassTransit" Version="8.2.5" />
<PackageReference Include="MassTransit" Version="8.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@ -25,9 +25,9 @@
<PackageReference Include="EntityFrameworkCore.Triggered" Version="3.2.2" />
<PackageReference Include="EntityFrameworkCore.Projectables" Version="3.0.4" />
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.3" />
<PackageReference Include="MassTransit" Version="8.2.5" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.2.5" />
<PackageReference Include="MassTransit.Hangfire" Version="8.2.5" />
<PackageReference Include="MassTransit" Version="8.3.0" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.3.0" />
<PackageReference Include="MassTransit.Hangfire" Version="8.3.0" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />