refactor(image): 重构水印处理逻辑以提高可维护性

- 移除 PhotoProcessContext 中的水印相关字段
- 新增 WatermarkConfig 类封装水印配置
- 修改 WatermarkStage 通过构造函数注入配置
- 调整 PrinterServiceImpl 中水印配置的传递方式
- 更新单元测试以适应新的配置注入方式
- 统一从配置对象读取水印参数而非上下文
- 优化日志记录与偏移量计算逻辑
This commit is contained in:
2025-11-26 14:56:37 +08:00
parent 90efc908c5
commit 333c4d3ca7
6 changed files with 113 additions and 47 deletions

View File

@@ -101,12 +101,6 @@ public class PhotoProcessContext {
private String resultUrl;
private IStorageAdapter storageAdapter;
private WatermarkInfo watermarkInfo;
private ImageWatermarkOperatorEnum watermarkType;
private String scenicText;
private String dateFormat;
private File qrcodeFile;
private Integer offsetLeft;
// ==================== 回调 ====================

View File

@@ -23,8 +23,6 @@ import java.io.File;
)
public class ConditionalRotateStage extends AbstractPipelineStage<PhotoProcessContext> {
private static final int OFFSET_LEFT_FOR_PORTRAIT = 40;
@Override
public String getName() {
return "ConditionalRotateStage";
@@ -62,9 +60,8 @@ public class ConditionalRotateStage extends AbstractPipelineStage<PhotoProcessCo
context.updateProcessedFile(rotatedFile);
context.setNeedRotation(true);
context.setOffsetLeft(OFFSET_LEFT_FOR_PORTRAIT);
log.info("图片已旋转{}度, offsetLeft={}", rotation, OFFSET_LEFT_FOR_PORTRAIT);
log.info("图片已旋转{}度", rotation);
return StageResult.success("已旋转" + rotation + "");
} catch (Exception e) {

View File

@@ -0,0 +1,37 @@
package com.ycwl.basic.image.pipeline.stages;
import com.ycwl.basic.image.watermark.enums.ImageWatermarkOperatorEnum;
import lombok.Builder;
import lombok.Getter;
import java.io.File;
/**
* 水印Stage配置
* 封装水印处理所需的所有配置参数
*/
@Getter
@Builder
public class WatermarkConfig {
/**
* 水印类型
*/
private final ImageWatermarkOperatorEnum watermarkType;
/**
* 景区文字
*/
private final String scenicText;
/**
* 日期格式
*/
@Builder.Default
private final String dateFormat = "yyyy.MM.dd";
/**
* 二维码文件
*/
private final File qrcodeFile;
}

View File

@@ -31,6 +31,19 @@ import java.util.List;
)
public class WatermarkStage extends AbstractPipelineStage<PhotoProcessContext> {
private static final int OFFSET_LEFT_FOR_PORTRAIT = 40;
private final WatermarkConfig config;
/**
* 构造函数
*
* @param config 水印配置
*/
public WatermarkStage(WatermarkConfig config) {
this.config = config;
}
@Override
public String getName() {
return "WatermarkStage";
@@ -43,12 +56,13 @@ public class WatermarkStage extends AbstractPipelineStage<PhotoProcessContext> {
@Override
protected StageResult doExecute(PhotoProcessContext context) {
ImageWatermarkOperatorEnum watermarkType = context.getWatermarkType();
if (watermarkType == null) {
if (config == null || config.getWatermarkType() == null) {
log.info("未配置水印类型,跳过水印处理");
return StageResult.skipped("未配置水印");
}
ImageWatermarkOperatorEnum watermarkType = config.getWatermarkType();
List<ImageWatermarkOperatorEnum> fallbackChain = buildFallbackChain(watermarkType);
log.debug("水印降级链: {}", fallbackChain);
@@ -138,30 +152,29 @@ public class WatermarkStage extends AbstractPipelineStage<PhotoProcessContext> {
info.setOriginalFile(originalFile);
info.setWatermarkedFile(watermarkedFile);
String scenicText = context.getScenicText();
// 从 config 读取景区文字
String scenicText = config.getScenicText();
if (StringUtils.isNotBlank(scenicText)) {
info.setScenicLine(scenicText);
}
// 从 config 读取日期格式
Date now = new Date();
String dateFormat = context.getDateFormat();
if (StringUtils.isBlank(dateFormat)) {
dateFormat = "yyyy.MM.dd";
}
String dateFormat = config.getDateFormat();
info.setDatetime(now);
info.setDtFormat(dateFormat);
File qrcodeFile = context.getQrcodeFile();
// 从 config 读取二维码文件
File qrcodeFile = config.getQrcodeFile();
if (qrcodeFile != null && qrcodeFile.exists()) {
info.setQrcodeFile(qrcodeFile);
}
Integer offsetLeft = context.getOffsetLeft();
if (offsetLeft != null) {
info.setOffsetLeft(offsetLeft);
// 根据旋转状态自己处理 offsetLeft
if (context.isNeedRotation()) {
info.setOffsetLeft(OFFSET_LEFT_FOR_PORTRAIT);
}
context.setWatermarkInfo(info);
return info;
}
}