新增 页码定位 美化前端 其他功能调整等

This commit is contained in:
高雄
2026-01-19 17:46:32 +08:00
parent 904af89190
commit 7dc0469b30
48 changed files with 10922 additions and 1241 deletions

View File

@@ -56,44 +56,44 @@ public class CadToPdfService {
* @param fileAttribute 文件属性
* @return 转换结果的CompletableFuture
*/
public CompletableFuture<Boolean> cadToPdfAsync(String inputFilePath, String outputFilePath,
public CompletableFuture<Boolean> cadToPdfAsync(String inputFilePath, String outputFilePath,String cacheName,
String cadPreviewType, FileAttribute fileAttribute) {
String fileName = new File(inputFilePath).getName();
// 立即创建初始状态,防止重复执行
FileConvertStatusManager.startConvert(fileName);
FileConvertStatusManager.startConvert(cacheName);
// 创建可取消的任务
CompletableFuture<Boolean> taskFuture = new CompletableFuture<>();
taskCompletionStatus.put(fileName, new AtomicBoolean(false));
taskCompletionStatus.put(cacheName, new AtomicBoolean(false));
// 提交任务到线程池
Future<?> future = virtualThreadExecutor.submit(() -> {
try {
// 添加初始状态更新
FileConvertStatusManager.updateProgress(fileName, "正在启动转换任务", 5);
boolean result = convertCadWithConcurrencyControl(inputFilePath, outputFilePath,
FileConvertStatusManager.updateProgress(cacheName, "正在启动转换任务", 5);
boolean result = convertCadWithConcurrencyControl(inputFilePath, outputFilePath,cacheName,
cadPreviewType, fileAttribute);
if (result) {
taskFuture.complete(true);
taskCompletionStatus.get(fileName).set(true);
taskCompletionStatus.get(cacheName).set(true);
} else {
taskFuture.complete(false);
}
} catch (Exception e) {
logger.error("CAD转换任务执行失败: {}", fileName, e);
FileConvertStatusManager.markError(fileName, "转换过程异常: " + e.getMessage());
logger.error("CAD转换任务执行失败: {}", cacheName, e);
FileConvertStatusManager.markError(cacheName, "转换过程异常: " + e.getMessage());
taskFuture.completeExceptionally(e);
} finally {
// 移除任务记录
runningTasks.remove(fileName);
taskCompletionStatus.remove(fileName);
runningTasks.remove(cacheName);
taskCompletionStatus.remove(cacheName);
}
});
// 记录正在运行的任务
runningTasks.put(fileName, future);
runningTasks.put(cacheName, future);
// 设置超时取消
scheduleTimeoutCheck(fileName, taskFuture, future, outputFilePath);
scheduleTimeoutCheck(cacheName, taskFuture, future, outputFilePath);
return taskFuture;
}
@@ -160,34 +160,34 @@ public class CadToPdfService {
/**
* 带并发控制的CAD转换
*/
private boolean convertCadWithConcurrencyControl(String inputFilePath, String outputFilePath,
private boolean convertCadWithConcurrencyControl(String inputFilePath, String outputFilePath,String cacheName,
String cadPreviewType, FileAttribute fileAttribute)
throws Exception {
String fileName = new File(inputFilePath).getName();
long acquireStartTime = System.currentTimeMillis();
// 获取并发许可
if (!concurrentLimit.tryAcquire(30, TimeUnit.SECONDS)) {
long acquireTime = System.currentTimeMillis() - acquireStartTime;
logger.warn("获取并发许可超时,文件: {}, 等待时间: {}ms", fileName, acquireTime);
FileConvertStatusManager.updateProgress(fileName, "系统繁忙,等待资源中...", 15);
logger.warn("获取并发许可超时,文件: {}, 等待时间: {}ms", cacheName, acquireTime);
FileConvertStatusManager.updateProgress(cacheName, "系统繁忙,等待资源中...", 15);
throw new TimeoutException("系统繁忙,请稍后重试");
}
long acquireTime = System.currentTimeMillis() - acquireStartTime;
logger.debug("获取并发许可成功: {}, 等待时间: {}ms", fileName, acquireTime);
logger.debug("获取并发许可成功: {}, 等待时间: {}ms", cacheName, acquireTime);
// 更新状态
FileConvertStatusManager.updateProgress(fileName, "已获取转换资源,开始转换", 20);
FileConvertStatusManager.updateProgress(cacheName, "已获取转换资源,开始转换", 20);
long conversionStartTime = System.currentTimeMillis();
try {
boolean result = performCadConversion(inputFilePath, outputFilePath, cadPreviewType, fileAttribute);
boolean result = performCadConversion(inputFilePath, outputFilePath,cacheName, cadPreviewType, fileAttribute);
long conversionTime = System.currentTimeMillis() - conversionStartTime;
logger.debug("CAD转换核心完成: {}, 转换耗时: {}ms, 总耗时(含等待): {}ms",
fileName, conversionTime, conversionTime + acquireTime);
cacheName, conversionTime, conversionTime + acquireTime);
return result;
@@ -199,49 +199,48 @@ public class CadToPdfService {
/**
* 执行实际的CAD转换逻辑
*/
private boolean performCadConversion(String inputFilePath, String outputFilePath,
private boolean performCadConversion(String inputFilePath, String outputFilePath,String cacheName,
String cadPreviewType, FileAttribute fileAttribute) {
final InterruptionTokenSource source = new InterruptionTokenSource();
String fileName = new File(inputFilePath).getName();
long totalStartTime = System.currentTimeMillis();
try {
// 1. 验证输入参数
long validationStartTime = System.currentTimeMillis();
FileConvertStatusManager.updateProgress(fileName, "正在验证文件参数", 25);
FileConvertStatusManager.updateProgress(cacheName, "正在验证文件参数", 25);
if (!validateInputParameters(inputFilePath, outputFilePath, cadPreviewType)) {
long validationTime = System.currentTimeMillis() - validationStartTime;
logger.error("CAD转换参数验证失败: {}, 验证耗时: {}ms", fileName, validationTime);
FileConvertStatusManager.markError(fileName, "文件参数验证失败");
logger.error("CAD转换参数验证失败: {}, 验证耗时: {}ms", cacheName, validationTime);
FileConvertStatusManager.markError(cacheName, "文件参数验证失败");
return false;
}
long validationTime = System.currentTimeMillis() - validationStartTime;
// 2. 创建输出目录
long directoryStartTime = System.currentTimeMillis();
FileConvertStatusManager.updateProgress(fileName, "正在准备输出目录", 30);
FileConvertStatusManager.updateProgress(cacheName, "正在准备输出目录", 30);
createOutputDirectoryIfNeeded(outputFilePath, fileAttribute.isCompressFile());
long directoryTime = System.currentTimeMillis() - directoryStartTime;
// 3. 加载并转换CAD文件
long loadStartTime = System.currentTimeMillis();
FileConvertStatusManager.updateProgress(fileName, "正在加载CAD文件", 40);
FileConvertStatusManager.updateProgress(cacheName, "正在加载CAD文件", 40);
LoadOptions loadOptions = createLoadOptions();
try (Image cadImage = Image.load(inputFilePath, loadOptions)) {
long loadTime = System.currentTimeMillis() - loadStartTime;
logger.debug("CAD文件加载完成: {}, 加载耗时: {}ms", fileName, loadTime);
logger.debug("CAD文件加载完成: {}, 加载耗时: {}ms", cacheName, loadTime);
FileConvertStatusManager.updateProgress(fileName, "CAD文件加载完成开始渲染", 50);
FileConvertStatusManager.updateProgress(cacheName, "CAD文件加载完成开始渲染", 50);
// 4. 创建光栅化选项
long rasterizationStartTime = System.currentTimeMillis();
FileConvertStatusManager.updateProgress(fileName, "正在设置渲染参数", 60);
FileConvertStatusManager.updateProgress(cacheName, "正在设置渲染参数", 60);
CadRasterizationOptions rasterizationOptions = createRasterizationOptions(cadImage);
long rasterizationTime = System.currentTimeMillis() - rasterizationStartTime;
// 5. 根据预览类型创建选项
long optionsStartTime = System.currentTimeMillis();
FileConvertStatusManager.updateProgress(fileName, "正在配置输出格式", 70);
FileConvertStatusManager.updateProgress(cacheName, "正在配置输出格式", 70);
var options = switch (cadPreviewType.toLowerCase()) {
case "svg" -> createSvgOptions(rasterizationOptions, source);
case "pdf" -> createPdfOptions(rasterizationOptions, source);
@@ -251,15 +250,15 @@ public class CadToPdfService {
long optionsTime = System.currentTimeMillis() - optionsStartTime;
// 6. 保存转换结果
long saveStartTime = System.currentTimeMillis();
FileConvertStatusManager.updateProgress(fileName, "正在生成输出文件", 80);
FileConvertStatusManager.updateProgress(cacheName, "正在生成输出文件", 80);
saveConvertedFile(outputFilePath, cadImage, options);
long saveTime = System.currentTimeMillis() - saveStartTime;
FileConvertStatusManager.updateProgress(fileName, "文件转换完成", 90);
FileConvertStatusManager.updateProgress(cacheName, "文件转换完成", 90);
// 计算总时间
long totalTime = System.currentTimeMillis() - totalStartTime;
// 记录详细的性能信息
logger.debug("CAD转换详细耗时 - 文件: {}, 验证={}ms, 目录={}ms, 加载={}ms, 光栅化={}ms, 选项={}ms, 保存={}ms, 总耗时={}ms",
fileName, validationTime, directoryTime, loadTime,
cacheName, validationTime, directoryTime, loadTime,
rasterizationTime, optionsTime, saveTime, totalTime);
logger.info("CAD转换完成: 总耗时: {}ms", totalTime);
@@ -272,9 +271,9 @@ public class CadToPdfService {
}
// 转换成功,标记为完成
FileConvertStatusManager.updateProgress(fileName, "转换成功", 100);
FileConvertStatusManager.updateProgress(cacheName, "转换成功", 100);
// 短暂延迟后清理状态给前端一个显示100%的机会
FileConvertStatusManager.convertSuccess(fileName);
FileConvertStatusManager.convertSuccess(cacheName);
return true;
@@ -282,12 +281,12 @@ public class CadToPdfService {
} catch (Exception e) {
long totalTime = System.currentTimeMillis() - totalStartTime;
logger.error("CAD转换执行失败: {}, 耗时: {}ms", fileName, totalTime, e);
logger.error("CAD转换执行失败: {}, 耗时: {}ms", cacheName, totalTime, e);
// 检查是否已经标记为超时
FileConvertStatusManager.ConvertStatus status = FileConvertStatusManager.getConvertStatus(fileName);
FileConvertStatusManager.ConvertStatus status = FileConvertStatusManager.getConvertStatus(cacheName);
if (status == null || status.getStatus() != FileConvertStatusManager.Status.TIMEOUT) {
FileConvertStatusManager.markError(fileName, "转换失败: " + e.getMessage());
FileConvertStatusManager.markError(cacheName, "转换失败: " + e.getMessage());
}
// 删除可能已创建的不完整文件
@@ -302,7 +301,7 @@ public class CadToPdfService {
long cleanupTime = System.currentTimeMillis() - cleanupStartTime;
long totalTime = System.currentTimeMillis() - totalStartTime;
logger.debug("CAD转换资源清理完成: {}, 清理耗时: {}ms, 总耗时: {}ms",
fileName, cleanupTime, totalTime);
cacheName, cleanupTime, totalTime);
}
}
}