refactor(phase-4): 解散万能 util——similarasin/util 与 common/util 按域归位
similarasin/util 7 支: - 域内件(SimilarAsinImageEmbedder/PuzzleImageMerger/SimilarAsinLogSupport/ BoundedImageCache)并入既有 service/support 先例; - 通用 Excel 件(ExcelCellImageWriter/WpsCellImageWriter)归 common/excel; - 仅测试用的 SimilarAsinPerfFixture 移出生产源到 src/test。 common/util 13 支解散为四个关注点包:excel(读/写/单元格图片)、 text(BrandLabel/SecretMasking/UserMessages)、task(行过滤/分组传播/判死文案/ 删除台账/锁跳过留痕)、support(LRU/下载头/线程池)。老 util 目录不再存在。 纯粹的包搬迁与 import 重写(diff 219 增 / 217 删),零行为改动: 搬迁前后由 SimilarAsinCreateTaskSnapshotTest 的建单入口 golden 对撞相等; 新增 MovedComponentScanTest 钉住三枚 @Component 仍被根包扫描发现、老包零残留。
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.nanri.aiimage.common.text;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 品牌值的唯一清洗出口:剥掉 {@code Brand: } 这类"字段标签 + 冒号"前缀。全仓只此一份,
|
||||
* 读入(解析上传的 Excel、接收客户端回传的行)与写出(出件写 cell)两端都走它。
|
||||
*
|
||||
* <p>亚马逊详情页把卖家名渲染成 "Brand: XXX",用户从页面复制进 Excel 就会带上前缀。
|
||||
* 该值进 WIPO 品牌库查询时会被当 Solr 字段限定符({@code undefined field Brand} → 上游 500),
|
||||
* 10-08 单日被顶着重跑 30 轮、连带把品牌全局锁持到 1h45m,压死 11 个别的用户的任务。
|
||||
*
|
||||
* <p>清洗必须落在这一处而不是某个调用点:{@code biz_publish_item.brand} 有两个写入来源
|
||||
* (服务端 Excel 解析、客户端结果分片回传 {@code replaceRows}),历史上只堵了前者,
|
||||
* 后者在任务完成时整表删后重插,把脏值又写回去。
|
||||
*
|
||||
* <p>读写两端都要过一遍,是因为干净值不等于干净文件:出件是用户的下一份上传物,
|
||||
* 任何一个出件漏剥,用户下载再上传就把脏值送回链路里,清洗脚本的账也随之作废。
|
||||
*/
|
||||
public final class BrandLabel {
|
||||
|
||||
/**
|
||||
* 只剥行首的一个标签,且要求冒号紧跟其后:{@code Brands: NIKE} 与 {@code Brand: NIKE: X}
|
||||
* 都是真实品牌写法,不能顺手裁掉。
|
||||
*/
|
||||
private static final Pattern LABEL_PREFIX = Pattern.compile(
|
||||
"(?i)^\\s*(?:brand|品牌|marke|marca|marque|marka)\\s*[::]\\s*");
|
||||
|
||||
private BrandLabel() {
|
||||
}
|
||||
|
||||
public static String strip(String value) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return value;
|
||||
}
|
||||
return LABEL_PREFIX.matcher(value).replaceFirst("").trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.nanri.aiimage.common.text;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* 敏感串脱敏工具(2026-09 全维度审查后收口)。
|
||||
*
|
||||
* <p>背景:代理提取链接形态为 {@code http://user:pass@host:port},代码中曾有多处
|
||||
* 直接把整串打进日志,导致用户代理账号密码长期留在应用日志与 docker logs 里。
|
||||
*/
|
||||
public final class SecretMasking {
|
||||
|
||||
private SecretMasking() {
|
||||
}
|
||||
|
||||
/** 通用掩码:保留前 4 与后 4 字符;过短则整体掩掉。 */
|
||||
public static String mask(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return "";
|
||||
}
|
||||
String text = value.trim();
|
||||
if (text.length() <= 8) {
|
||||
return "****";
|
||||
}
|
||||
return text.substring(0, 4) + "****" + text.substring(text.length() - 4);
|
||||
}
|
||||
|
||||
/** 代理掩码:隐去账号密码,保留 scheme://host:port 便于运维核对。 */
|
||||
public static String maskProxy(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
URI uri = URI.create(value.trim());
|
||||
if (uri.getHost() == null || uri.getHost().isBlank()) {
|
||||
return mask(value);
|
||||
}
|
||||
StringBuilder masked = new StringBuilder();
|
||||
masked.append(uri.getScheme() == null ? "http" : uri.getScheme()).append("://");
|
||||
if (uri.getUserInfo() != null && !uri.getUserInfo().isBlank()) {
|
||||
masked.append("***@");
|
||||
}
|
||||
masked.append(uri.getHost());
|
||||
if (uri.getPort() > 0) {
|
||||
masked.append(':').append(uri.getPort());
|
||||
}
|
||||
return masked.toString();
|
||||
} catch (Exception ex) {
|
||||
return mask(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.nanri.aiimage.common.text;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 用户可见文案把关(2026-10-09 收口)。
|
||||
*
|
||||
* <p>背景:任务 30456 的结果文件其实已经生成、能下载,卡片上却跟着一条
|
||||
* {@code Similar ASIN result rows are empty; please retry result generation}。文案是英文不是
|
||||
* 最要命的——最要命的是它是**底层异常原文**:全仓有 61 处把 {@code ex.getMessage()} 直接
|
||||
* 拼进用户可见字段(异常响应、任务 error_message、结果作业 error_msg、看板 message),
|
||||
* 底层抛什么用户就看什么。逐条翻译译不完,因为新的 catch 天天在加。
|
||||
*
|
||||
* <p>本类把"只把中文给用户看"做成出口处的代码约束:中文照原样过,非中文换成中文句子,
|
||||
* 原文只进日志。诊断信息不丢——调用点本来就带着 {@code log.warn} 的堆栈。
|
||||
*/
|
||||
public final class UserMessages {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(UserMessages.class);
|
||||
|
||||
private UserMessages() {
|
||||
}
|
||||
|
||||
/** 中文主干 + 异常详情:详情是中文才拼接,英文详情丢弃(原文进 debug 日志)。 */
|
||||
public static String with(String headline, Throwable ex) {
|
||||
return with(headline, ex == null ? null : ex.getMessage());
|
||||
}
|
||||
|
||||
/** 中文主干 + 详情:详情是中文才拼接。 */
|
||||
public static String with(String headline, String detail) {
|
||||
String head = headline == null ? "" : headline.trim().replaceAll("[::]\\s*$", "");
|
||||
if (detail == null || detail.isBlank()) {
|
||||
return head;
|
||||
}
|
||||
if (hasChinese(detail)) {
|
||||
return head + ":" + detail.trim();
|
||||
}
|
||||
log.debug("[user-message] dropped non-chinese detail headline={} detail={}", head, detail);
|
||||
return head;
|
||||
}
|
||||
|
||||
/**
|
||||
* 整句把关:原文含中文则照原样返回,否则返回中文兜底句。
|
||||
*
|
||||
* <p>用于"整条消息都来自底层"的出口({@code ApiResponse.fail(ex.getMessage())}、
|
||||
* {@code setErrorMessage(ex.getMessage())})。被换掉时原文进 WARN,因为这类出口
|
||||
* 调用点未必另外记日志,静默替换会把根因从系统里彻底抹掉。
|
||||
*/
|
||||
public static String orFallback(String raw, String fallback) {
|
||||
if (raw != null && !raw.isBlank() && hasChinese(raw)) {
|
||||
return raw.trim();
|
||||
}
|
||||
log.warn("[user-message] replaced non-chinese message fallback={} raw={}",
|
||||
fallback, raw == null ? "" : raw);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
/** 是否含汉字:含则认为是给人看的中文文案,可以原样透出。 */
|
||||
public static boolean hasChinese(String text) {
|
||||
if (text == null) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c >= '一' && c <= '鿿') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user