feat(phase-4): 京东商品图片预签名直传与登记链——服务端零图片字节
This commit is contained in:
+47
@@ -227,6 +227,48 @@ public class OssStorageService {
|
||||
return presignPublicClientUpload(objectKey, SOFTWARE_VERSION_PREFIX, "software version", expirySeconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用按桶直传预签名(v1.4 京东商品图等独立桶用):签名 host 与
|
||||
* {@link #presignSoftwareVersionUpload} 同口径取 public-endpoint(oss.aishufu.top),
|
||||
* 调用方拿到后直接 PUT 落对象;桶名与 key 由调用方(服务端自己的 ingest 服务)生成,
|
||||
* 本方法不再做前缀守卫——前缀归属校验归生成 key 的那侧。
|
||||
* {@code presignSoftwareVersionUpload} 系列的既有语义一字不动。
|
||||
*/
|
||||
public String presignObjectUpload(String bucket, String objectKey, int expirySeconds) {
|
||||
String normalizedBucket = requireStorageName(bucket, "bucket");
|
||||
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
|
||||
try {
|
||||
return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.PUT)
|
||||
.bucket(normalizedBucket)
|
||||
.object(normalizedObjectKey)
|
||||
.expiry(Math.max(60, expirySeconds), TimeUnit.SECONDS)
|
||||
.build());
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("presign", normalizedBucket + "/" + normalizedObjectKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/** 指定桶对象的字节数;对象不存在返回 -1(京东商品图登记对撞用,语义同 publicClientObjectSize)。 */
|
||||
public long objectSize(String bucket, String objectKey) {
|
||||
String normalizedBucket = requireStorageName(bucket, "bucket");
|
||||
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
|
||||
try {
|
||||
var stat = buildClient().statObject(StatObjectArgs.builder()
|
||||
.bucket(normalizedBucket)
|
||||
.object(normalizedObjectKey)
|
||||
.build());
|
||||
return stat.size();
|
||||
} catch (ErrorResponseException ex) {
|
||||
if (isNotFound(ex)) {
|
||||
return -1L;
|
||||
}
|
||||
throw storageFailure("stat", normalizedBucket + "/" + normalizedObjectKey, ex);
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("stat", normalizedBucket + "/" + normalizedObjectKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/** 公开 client 桶对象大小共用实现:对象不存在返回 -1(直传确认时先卡大小)。 */
|
||||
private long publicClientObjectSize(String objectKey, String prefix, String what) {
|
||||
requirePrefixedObjectKey(objectKey, prefix, what);
|
||||
@@ -246,6 +288,11 @@ public class OssStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 指定桶对象的公开直链(京东商品图等独立桶的展示源;AC-4:url 100% 自有域名)。 */
|
||||
public String getBucketObjectUrl(String bucket, String objectKey) {
|
||||
return getPublicUrl(requireStorageName(objectKey, "objectKey"), requireStorageName(bucket, "bucket"));
|
||||
}
|
||||
|
||||
/** 返回软件版本对象的字节数;对象不存在返回 -1(直传完成后由服务端核对大小是否超限)。 */
|
||||
public long softwareVersionObjectSize(String objectKey) {
|
||||
return publicClientObjectSize(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.nanri.aiimage.modules.jd.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdImagePresignRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdImageRegisterRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdImagePresignVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdImageRegisterVo;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductImageIngestService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 内部接口:京东商品图片预签名与登记(v1.4 Phase 4,蓝图 §3.3)。
|
||||
*
|
||||
* <p>客户端先取预签名(服务端生成内容寻址 objectKey),按 URL 直传独立桶
|
||||
* {@code jd-products},再经 chunk 回传(主链路)或本控制器 {@code /register}
|
||||
* (补登记口)登记落 {@code biz_jd_product_image}——服务端零图片字节。</p>
|
||||
*
|
||||
* <p>/api/internal 虽在 AdminApiGuardFilter 兜底名单内、可信令牌会放行,controller
|
||||
* 仍自校验令牌——防止配置漂移时匿名可达(InternalTaskEvidenceController 同款)。
|
||||
* 身份以请求头 JWT 为准(user_token 钩子统一注入),body.uid 只作存量装机兜底;
|
||||
* 任务级 owner 对撞与 companyId 投影收口在 {@link JdProductImageIngestService}。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/internal/jd-images")
|
||||
@Tag(name = "内部接口", description = "京东商品图片预签名直传与登记(X-Internal-Token + JWT 鉴权)。")
|
||||
public class InternalJdImageController {
|
||||
|
||||
private final JdProductImageIngestService imageIngestService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@PostMapping("/presign")
|
||||
@Operation(summary = "取图片直传预签名", description = "服务端生成 objectKey 并按需签发 PUT URL;"
|
||||
+ "同公司同内容命中既有对象时只回 objectKey(uploadUrl 为空,客户端登记既有 key 不重传)。")
|
||||
public ApiResponse<JdImagePresignVo> presign(HttpServletRequest request,
|
||||
@RequestBody(required = false) JdImagePresignRequest body) {
|
||||
if (!adminAuthSupport.isTrustedInternalToken(request)) {
|
||||
log.warn("[jd-images] 拒绝未携带可信内部令牌的预签名请求 remoteAddr={}", request.getRemoteAddr());
|
||||
throw new BusinessException(401, "未授权");
|
||||
}
|
||||
if (body == null) {
|
||||
throw new BusinessException("预签名载荷为空");
|
||||
}
|
||||
Long uid = adminAuthSupport.resolveOwnershipUserId(request, body.getUid());
|
||||
return ApiResponse.success(imageIngestService.presign(uid, body));
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
@Operation(summary = "登记图片对象", description = "归属先判(uid 与任务 owner 对撞)→ 逐图 statObject"
|
||||
+ "对撞「对象存在 + 字节数一致」→ 写 biz_jd_product_image;对不上进 mismatched 并留日志。")
|
||||
public ApiResponse<JdImageRegisterVo> register(HttpServletRequest request,
|
||||
@RequestBody(required = false) JdImageRegisterRequest body) {
|
||||
if (!adminAuthSupport.isTrustedInternalToken(request)) {
|
||||
log.warn("[jd-images] 拒绝未携带可信内部令牌的登记请求 remoteAddr={}", request.getRemoteAddr());
|
||||
throw new BusinessException(401, "未授权");
|
||||
}
|
||||
if (body == null) {
|
||||
throw new BusinessException("登记载荷为空");
|
||||
}
|
||||
Long uid = adminAuthSupport.resolveOwnershipUserId(request, body.getUid());
|
||||
return ApiResponse.success(imageIngestService.register(uid, body));
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.nanri.aiimage.modules.jd.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东商品图片预签名请求(v1.4 Phase 4,内部面 {@code /api/internal/jd-images/presign})。
|
||||
*
|
||||
* <p>与客户端 {@code jd_image_collector._default_presign} 逐键对齐:顶层
|
||||
* {@code productCode / images}(taskId/uid 客户端不传,可空——服务端按内部令牌 +
|
||||
* JWT 会话身份解析 uid,归属与桶前缀由 uid 投影的 company_id 决定);图片项键集
|
||||
* {@code sha256 / bytes / contentType}。objectKey 由服务端生成(内容寻址:
|
||||
* {@code <key-prefix><companyId>/<sha256>.<ext>}),客户端不得自造(蓝图 §3.3)。</p>
|
||||
*/
|
||||
@Data
|
||||
public class JdImagePresignRequest {
|
||||
|
||||
/** 来源任务(可空:客户端采集器按商品取预签名,不绑任务;带了则做 owner 对撞)。 */
|
||||
private Long taskId;
|
||||
|
||||
/** 调用者 uid(可空:真身份以请求头 JWT 为准,body.uid 只作存量装机兜底)。 */
|
||||
private Long uid;
|
||||
|
||||
/** 图片所属京东商品编码(客户端逐商品成批取预签名;服务端只留痕用,不做归属判定)。 */
|
||||
private String productCode;
|
||||
|
||||
/** 本批待直传图片规格(≤ 20 个,服务端钳制)。 */
|
||||
private List<JdImageSpecDto> images;
|
||||
|
||||
/** 单张图片直传规格(键集与客户端 {@code _default_presign} 逐键对齐)。 */
|
||||
@Data
|
||||
public static class JdImageSpecDto {
|
||||
|
||||
/** 内容摘要(64 位小写 hex;幂等与对象 key 命名的一半)。 */
|
||||
private String sha256;
|
||||
|
||||
/** 声明字节数(直传完成后登记对撞的第一道尺)。 */
|
||||
private Long bytes;
|
||||
|
||||
/** 图片 MIME(objectKey 扩展名与 OSS PUT Content-Type 来源)。 */
|
||||
private String contentType;
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.nanri.aiimage.modules.jd.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东商品图片登记请求(v1.4 Phase 4,内部面 {@code /api/internal/jd-images/register})。
|
||||
*
|
||||
* <p>蓝图 §3.3 ③ 契约形状:归属先判(taskId + uid 对撞任务 owner)→ 逐图
|
||||
* {@code statObject} 对撞「对象存在 + 字节数一致」→ 写 {@code biz_jd_product_image}。
|
||||
* 当前客户端主链路把图片清单并进 chunk 回传({@code JdIngestChunkRequest} 的
|
||||
* {@code images}),本端点作为显式补登记口保留;两条路的落行出口都是
|
||||
* {@code JdProductImageIngestService} 同一份对撞逻辑。</p>
|
||||
*/
|
||||
@Data
|
||||
public class JdImageRegisterRequest {
|
||||
|
||||
/** 来源任务(必填:本端点按任务做归属与商品解析)。 */
|
||||
private Long taskId;
|
||||
|
||||
/** 调用者 uid(可空:真身份以请求头 JWT 为准;解析后必须与任务 owner 一致)。 */
|
||||
private Long uid;
|
||||
|
||||
/** 图片所属京东商品编码(必填:按公司+任务+编码解析商品行 id)。 */
|
||||
private String productCode;
|
||||
|
||||
/** 本批登记图片清单(直传完成后回传的 objectKey 明细)。 */
|
||||
private List<JdImageRegDto> images;
|
||||
|
||||
/** 单张登记明细(键集与 chunk 回传 {@code JdIngestImageDto} 同词表)。 */
|
||||
@Data
|
||||
public static class JdImageRegDto {
|
||||
|
||||
/** MAIN / ATTR / DETAIL。 */
|
||||
private String type;
|
||||
|
||||
/** 服务端预签名时生成的对象 key(客户端不得自造,前缀不符进 mismatched)。 */
|
||||
private String objectKey;
|
||||
|
||||
private String sha256;
|
||||
|
||||
/** 声明字节数(与 statObject 实测对撞,不符进 mismatched)。 */
|
||||
private Long bytes;
|
||||
|
||||
/** 销售属性名(ATTR 图专用)。 */
|
||||
private String attrName;
|
||||
|
||||
private Integer sortNo;
|
||||
|
||||
private Integer width;
|
||||
|
||||
private Integer height;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东商品图片预签名响应(v1.4 Phase 4)。
|
||||
*
|
||||
* <p>{@code items} 只含服务端愿意放行的图:新对象给 {@code uploadUrl},命中既有对象
|
||||
* (同公司同内容已上传过)只回 {@code objectKey}、{@code uploadUrl} 置空——客户端据此
|
||||
* 「不发 PUT、只登记既有 key」({@code jd_image_collector} 约定);无效/对不上的图
|
||||
* 不进 items,计入 {@code skipped} 并留日志。</p>
|
||||
*/
|
||||
@Data
|
||||
public class JdImagePresignVo {
|
||||
|
||||
/** 放行清单(按请求顺序;客户端按 sha256 对回)。 */
|
||||
private List<Item> items;
|
||||
|
||||
/** 未放行数(sha256/bytes 非法、同公司同内容命中、对象字节数与声明不符)。 */
|
||||
private int skipped;
|
||||
|
||||
/** 单张放行项。 */
|
||||
@Data
|
||||
public static class Item {
|
||||
|
||||
/** 请求侧内容摘要(客户端按它对回)。 */
|
||||
private String sha256;
|
||||
|
||||
/** 服务端生成的自有桶对象 key。 */
|
||||
private String objectKey;
|
||||
|
||||
/** 直传 PUT 预签名 URL;命中既有对象时为空(不发 PUT)。 */
|
||||
private String uploadUrl;
|
||||
|
||||
/** 预签名有效期(秒)。 */
|
||||
private Integer expiresIn;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 京东商品图片登记响应(v1.4 Phase 4,蓝图 §3.3 ③)。
|
||||
*
|
||||
* <p>{@code registered}=实际落行数(upsertBatch 批量);{@code skipped}=空明细/
|
||||
* 非法键(sha256 缺失等);{@code mismatched}=对象不存在或实测字节数与声明不符
|
||||
* ——对不上不落行、不静默吞,每条都留日志(蓝图 §3.3 ③ 必做项)。</p>
|
||||
*/
|
||||
@Data
|
||||
public class JdImageRegisterVo {
|
||||
|
||||
private int registered;
|
||||
|
||||
private int skipped;
|
||||
|
||||
private int mismatched;
|
||||
}
|
||||
+376
@@ -0,0 +1,376 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdImagePresignRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdImageRegisterRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdImagePresignVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdImageRegisterVo;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 京东商品图片入库链(v1.4 Phase 4,蓝图 §3.3):服务端发预签名、客户端直传 OSS、
|
||||
* 服务端登记对撞——<b>服务端零图片流量</b>(主机 B 公网出口约 5Mbps,2 万张图走
|
||||
* 中转会拖死发版与采集)。
|
||||
*
|
||||
* <p>两条登记入口共用同一份对撞与落行逻辑:
|
||||
* ① chunk 回传(主链路,{@code JdResultIngestService} 接 {@link #registerTaskImages})——
|
||||
* 客户端把直传完成的 objectKey 清单并进回传行;② 显式补登记口
|
||||
* {@code /api/internal/jd-images/register}({@link #register})。落行唯一出口
|
||||
* {@link JdProductImageMapper#upsertBatch}(uk_product_image 幂等,铁律 1:SQL 全落 XML)。</p>
|
||||
*
|
||||
* <p>三条红线:① <b>objectKey 由服务端生成</b>(内容寻址
|
||||
* {@code <key-prefix><companyId>/<sha256>.<ext>},蓝图 §3.3 防越权写别人前缀;
|
||||
* 与客户端实拍契约对齐——客户端采集器不传 taskId,故 key 不含任务号,同公司同内容
|
||||
* 天然同 key,存储层即幂等);② <b>归属先判</b>——uid 必须与任务 owner 一致
|
||||
* (非 owner 403),companyId 从任务 owner 投影、读不出拒绝不回填默认 1(铁律 30);
|
||||
* ③ <b>登记对撞</b>——{@code statObject}「对象存在 + 字节数一致」才落行,
|
||||
* 对不上进 mismatched 并留日志(不静默吞,防半截对象进后管裂图)。</p>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdProductImageIngestService {
|
||||
|
||||
private static final String LOG_TAG = "jd-images";
|
||||
|
||||
/** 单次预签名/登记批量上限(蓝图 §3.3:≤ 20,与客户端 PRESIGN_BATCH_SIZE 对齐)。 */
|
||||
public static final int MAX_BATCH_SIZE = 20;
|
||||
|
||||
/** 图片档词表(V194 image_type 同词表)。 */
|
||||
private static final Set<String> VALID_IMAGE_TYPES = Set.of("MAIN", "ATTR", "DETAIL");
|
||||
|
||||
/** MIME → 对象扩展名(objectKey 命名用;未知 MIME 落 .bin,登记时照样对撞字节数)。 */
|
||||
private static final Map<String, String> MIME_EXTENSIONS = Map.of(
|
||||
"image/jpeg", ".jpg",
|
||||
"image/png", ".png",
|
||||
"image/webp", ".webp",
|
||||
"image/gif", ".gif");
|
||||
|
||||
private static final int PRESIGN_EXPIRY_SECONDS = 600;
|
||||
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
/** 京东商品图独立桶(application.yml 写死 jd-products,不设 env 防 ILM 误配,铁律 17)。 */
|
||||
@Value("${aiimage.jd.oss.bucket:jd-products}")
|
||||
private String bucket;
|
||||
|
||||
/** 对象 key 前缀(与 application.yml aiimage.jd.oss.key-prefix 同源)。 */
|
||||
@Value("${aiimage.jd.oss.key-prefix:jd-products/}")
|
||||
private String keyPrefix;
|
||||
|
||||
/** 单商品图片上限(超限截断并记日志,防详情长图爆炸;aiimage.jd.collect 同段配置)。 */
|
||||
@Value("${aiimage.jd.collect.image-max-per-product:200}")
|
||||
private int imageMaxPerProduct;
|
||||
|
||||
// ---------- 预签名(客户端直传前取 URL) ----------
|
||||
|
||||
/**
|
||||
* 批量取直传预签名:归属解析(uid → company_id,读不出拒绝)→ 服务端生成
|
||||
* 内容寻址 objectKey → 同公司同内容命中既有对象则跳过(不发 URL,客户端登记既有 key)。
|
||||
*/
|
||||
public JdImagePresignVo presign(Long uid, JdImagePresignRequest request) {
|
||||
if (request == null || request.getImages() == null || request.getImages().isEmpty()) {
|
||||
throw new BusinessException("预签名请求缺 images 清单");
|
||||
}
|
||||
if (request.getImages().size() > MAX_BATCH_SIZE) {
|
||||
throw new BusinessException("单次预签名最多 " + MAX_BATCH_SIZE + " 张,请分批");
|
||||
}
|
||||
Long companyId = requireCompanyId(uid);
|
||||
if (request.getTaskId() != null) {
|
||||
assertTaskOwnedBy(request.getTaskId(), uid);
|
||||
}
|
||||
List<JdImagePresignVo.Item> items = new ArrayList<>();
|
||||
int skipped = 0;
|
||||
for (JdImagePresignRequest.JdImageSpecDto spec : request.getImages()) {
|
||||
if (spec == null || !isValidSha256(spec.getSha256()) || spec.getBytes() == null
|
||||
|| spec.getBytes() <= 0) {
|
||||
skipped++;
|
||||
log.warn("[{}] 预签名跳过非法规格 companyId={} sha256={} bytes={} productCode={}",
|
||||
LOG_TAG, companyId,
|
||||
spec == null ? null : spec.getSha256(),
|
||||
spec == null ? null : spec.getBytes(),
|
||||
request.getProductCode());
|
||||
continue;
|
||||
}
|
||||
String objectKey = buildObjectKey(companyId, spec.getSha256().toLowerCase(Locale.ROOT),
|
||||
spec.getContentType());
|
||||
long actualSize = ossStorageService.objectSize(bucket, objectKey);
|
||||
if (actualSize >= 0) {
|
||||
if (actualSize != spec.getBytes()) {
|
||||
// 同 key 已有对象但字节数对不上:不发 URL 也不放行登记,留日志人工查(防半截对象)。
|
||||
skipped++;
|
||||
log.warn("[{}] 既有对象字节数与声明不符,跳过 companyId={} objectKey={} actual={} declared={}",
|
||||
LOG_TAG, companyId, objectKey, actualSize, spec.getBytes());
|
||||
continue;
|
||||
}
|
||||
items.add(item(spec.getSha256(), objectKey, null));
|
||||
skipped++;
|
||||
log.info("[{}] 同公司同内容命中既有对象,跳过直传 companyId={} objectKey={}",
|
||||
LOG_TAG, companyId, objectKey);
|
||||
continue;
|
||||
}
|
||||
items.add(item(spec.getSha256(), objectKey,
|
||||
ossStorageService.presignObjectUpload(bucket, objectKey, PRESIGN_EXPIRY_SECONDS)));
|
||||
}
|
||||
JdImagePresignVo vo = new JdImagePresignVo();
|
||||
vo.setItems(items);
|
||||
vo.setSkipped(skipped);
|
||||
return vo;
|
||||
}
|
||||
|
||||
// ---------- 登记(显式补登记口) ----------
|
||||
|
||||
/**
|
||||
* 显式补登记(蓝图 §3.3 ③):归属先判 → 商品解析 → 逐图 statObject 对撞 → 落行。
|
||||
*/
|
||||
public JdImageRegisterVo register(Long uid, JdImageRegisterRequest request) {
|
||||
if (request == null || request.getTaskId() == null || request.getTaskId() <= 0) {
|
||||
throw new BusinessException("taskId 不合法");
|
||||
}
|
||||
if (request.getProductCode() == null || request.getProductCode().isBlank()) {
|
||||
throw new BusinessException("productCode 不合法");
|
||||
}
|
||||
FileTaskEntity task = assertTaskOwnedBy(request.getTaskId(), uid);
|
||||
Long companyId = requireCompanyId(task.getUserId());
|
||||
JdProductEntity product = requireProductRow(companyId, request.getTaskId(),
|
||||
request.getProductCode().trim());
|
||||
JdImageRegisterVo vo = new JdImageRegisterVo();
|
||||
RegistrationOutcome outcome = doRegisterImages(task.getId(), companyId, product.getId(),
|
||||
request.getProductCode().trim(), toPlainSpecs(request.getImages()));
|
||||
vo.setRegistered(outcome.registered());
|
||||
vo.setSkipped(outcome.skipped());
|
||||
vo.setMismatched(outcome.mismatched());
|
||||
return vo;
|
||||
}
|
||||
|
||||
// ---------- chunk 回传路径的登记 ----------
|
||||
|
||||
/**
|
||||
* chunk 回传行内的图片清单登记(主链路):直传完成后的 objectKey 明细逐图对撞落行。
|
||||
* 返回实际落行数;空清单/非法图不落行(幂等键保证重传不重复)。
|
||||
*/
|
||||
public int registerTaskImages(Long taskId, Long companyId, String productCode,
|
||||
List<JdIngestImageSpec> specs) {
|
||||
if (specs == null || specs.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
JdProductEntity product = jdProductMapper.selectOne(new LambdaQueryWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getCompanyId, companyId)
|
||||
.eq(JdProductEntity::getTaskId, taskId)
|
||||
.eq(JdProductEntity::getProductCode, productCode)
|
||||
.last("limit 1"));
|
||||
if (product == null) {
|
||||
// 商品行还没落(回传行缺标题被 deduped 丢弃等):图片无处挂靠,留日志不落孤儿行。
|
||||
log.warn("[{}] 商品行未落库,图片登记跳过 taskId={} productCode={} imageCount={}",
|
||||
LOG_TAG, taskId, productCode, specs.size());
|
||||
return 0;
|
||||
}
|
||||
return doRegisterImages(taskId, companyId, product.getId(), productCode, specs).registered();
|
||||
}
|
||||
|
||||
/** 登记结果三计数(registered 进返回值/VO,skipped/mismatched 必须透出,不静默吞)。 */
|
||||
private record RegistrationOutcome(int registered, int skipped, int mismatched) {
|
||||
}
|
||||
|
||||
private RegistrationOutcome doRegisterImages(Long taskId, Long companyId, Long productId,
|
||||
String productCode,
|
||||
List<JdIngestImageSpec> specs) {
|
||||
if (specs == null || specs.isEmpty()) {
|
||||
return new RegistrationOutcome(0, 0, 0);
|
||||
}
|
||||
int cap = Math.max(1, imageMaxPerProduct);
|
||||
List<JdIngestImageSpec> effective = specs;
|
||||
if (specs.size() > cap) {
|
||||
effective = specs.subList(0, cap);
|
||||
log.warn("[{}] 单商品图片超上限截断 taskId={} productCode={} received={} cap={}",
|
||||
LOG_TAG, taskId, productCode, specs.size(), cap);
|
||||
}
|
||||
List<JdProductImageEntity> rows = new ArrayList<>();
|
||||
int skipped = 0;
|
||||
int mismatched = 0;
|
||||
for (JdIngestImageSpec spec : effective) {
|
||||
if (spec == null || !isValidSha256(spec.sha256) || spec.bytes == null || spec.bytes <= 0) {
|
||||
skipped++;
|
||||
log.warn("[{}] 登记跳过非法规格 taskId={} productCode={} sha256={}",
|
||||
LOG_TAG, taskId, productCode, spec == null ? null : spec.sha256);
|
||||
continue;
|
||||
}
|
||||
String type = normalizeImageType(spec.type);
|
||||
if (spec.objectKey == null || !spec.objectKey.startsWith(keyPrefix + companyId + "/")) {
|
||||
// 客户端不得自造 key(蓝图 §3.3):前缀与公司段不符一律不落行(空 key=预签名被拒)。
|
||||
mismatched++;
|
||||
log.warn("[{}] objectKey 前缀与归属不符,拒绝登记 taskId={} companyId={} objectKey={}",
|
||||
LOG_TAG, companyId, spec.objectKey);
|
||||
continue;
|
||||
}
|
||||
long actualSize = ossStorageService.objectSize(bucket, spec.objectKey);
|
||||
if (actualSize < 0) {
|
||||
mismatched++;
|
||||
log.warn("[{}] 登记对撞失败:对象不存在 taskId={} objectKey={} sha256={}",
|
||||
LOG_TAG, spec.objectKey, spec.sha256);
|
||||
continue;
|
||||
}
|
||||
if (actualSize != spec.bytes) {
|
||||
mismatched++;
|
||||
log.warn("[{}] 登记对撞失败:字节数不符 taskId={} objectKey={} actual={} declared={}",
|
||||
LOG_TAG, spec.objectKey, actualSize, spec.bytes);
|
||||
continue;
|
||||
}
|
||||
JdProductImageEntity row = new JdProductImageEntity();
|
||||
row.setProductId(productId);
|
||||
row.setCompanyId(companyId);
|
||||
row.setImageType(type);
|
||||
row.setAttrName(spec.attrName);
|
||||
row.setSortNo(spec.sortNo);
|
||||
row.setObjectKey(spec.objectKey);
|
||||
row.setUrl(ossStorageService.getBucketObjectUrl(bucket, spec.objectKey));
|
||||
row.setByteSize(spec.bytes);
|
||||
row.setSha256(spec.sha256.toLowerCase(Locale.ROOT));
|
||||
row.setWidth(spec.width);
|
||||
row.setHeight(spec.height);
|
||||
rows.add(row);
|
||||
}
|
||||
if (!rows.isEmpty()) {
|
||||
jdProductImageMapper.upsertBatch(rows);
|
||||
refreshProductImageCount(productId);
|
||||
}
|
||||
log.info("[{}] 图片登记完成 taskId={} productId={} productCode={} received={} registered={} "
|
||||
+ "skipped={} mismatched={}",
|
||||
LOG_TAG, taskId, productId, productCode, specs.size(), rows.size(), skipped, mismatched);
|
||||
return new RegistrationOutcome(rows.size(), skipped, mismatched);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登记后把商品行 image_count 刷成图片表实际行数(V193 列 DEFAULT 0,不刷会长期停在 0
|
||||
* 冒充没图);按表实数取值天然幂等,重复登记不累计。
|
||||
*/
|
||||
private void refreshProductImageCount(Long productId) {
|
||||
Long total = jdProductImageMapper.selectCount(new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.eq(JdProductImageEntity::getProductId, productId));
|
||||
jdProductMapper.update(null, new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getId, productId)
|
||||
.set(JdProductEntity::getImageCount, total == null ? 0 : total.intValue()));
|
||||
}
|
||||
|
||||
// ---------- 公共小件 ----------
|
||||
|
||||
/** 归属先判:任务存在、属京东模块、uid 与 owner 一致(uid 缺失/不符均 403,fail-closed)。 */
|
||||
private FileTaskEntity assertTaskOwnedBy(Long taskId, Long uid) {
|
||||
if (uid == null) {
|
||||
throw new BusinessException(403, "无法确认调用者身份(缺 JWT 会话),拒绝图片登记");
|
||||
}
|
||||
FileTaskEntity task = fileTaskMapper.selectById(taskId);
|
||||
if (task == null || !JdCollectTaskService.MODULE_TYPE.equals(task.getModuleType())) {
|
||||
throw new BusinessException("任务不存在");
|
||||
}
|
||||
if (!uid.equals(task.getUserId())) {
|
||||
throw new BusinessException(403, "无权操作该任务的图片:调用者与任务归属不一致");
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
/** uid → company_id 投影;读不出拒绝(不回填默认 1,铁律 30)。 */
|
||||
private Long requireCompanyId(Long uid) {
|
||||
if (uid == null) {
|
||||
throw new BusinessException(403, "无法确认调用者身份,拒绝图片入库");
|
||||
}
|
||||
AdminUserEntity owner = adminUserMapper.selectById(uid);
|
||||
if (owner == null || owner.getCompanyId() == null) {
|
||||
throw new BusinessException("任务归属缺失(uid=" + uid + " 无 company_id),图片入库被拒");
|
||||
}
|
||||
return owner.getCompanyId();
|
||||
}
|
||||
|
||||
private JdProductEntity requireProductRow(Long companyId, Long taskId, String productCode) {
|
||||
JdProductEntity product = jdProductMapper.selectOne(new LambdaQueryWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getCompanyId, companyId)
|
||||
.eq(JdProductEntity::getTaskId, taskId)
|
||||
.eq(JdProductEntity::getProductCode, productCode)
|
||||
.last("limit 1"));
|
||||
if (product == null) {
|
||||
throw new BusinessException("商品行不存在(company=" + companyId + " task=" + taskId
|
||||
+ " code=" + productCode + "),请先回传商品再登记图片");
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
/** 内容寻址 key:<prefix><companyId>/<sha256><ext>(扩展名由 MIME 推,未知落 .bin)。 */
|
||||
private String buildObjectKey(Long companyId, String sha256, String contentType) {
|
||||
String ext = MIME_EXTENSIONS.getOrDefault(
|
||||
contentType == null ? "" : contentType.trim().toLowerCase(Locale.ROOT), ".bin");
|
||||
return keyPrefix + companyId + "/" + sha256 + ext;
|
||||
}
|
||||
|
||||
private static String normalizeImageType(String type) {
|
||||
if (type == null || type.isBlank()) {
|
||||
return "MAIN";
|
||||
}
|
||||
String normalized = type.trim().toUpperCase(Locale.ROOT);
|
||||
return VALID_IMAGE_TYPES.contains(normalized) ? normalized : "MAIN";
|
||||
}
|
||||
|
||||
private static boolean isValidSha256(String sha256) {
|
||||
if (sha256 == null || sha256.length() != 64) {
|
||||
return false;
|
||||
}
|
||||
for (char c : sha256.toCharArray()) {
|
||||
if ((c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static JdImagePresignVo.Item item(String sha256, String objectKey, String uploadUrl) {
|
||||
JdImagePresignVo.Item item = new JdImagePresignVo.Item();
|
||||
item.setSha256(sha256);
|
||||
item.setObjectKey(objectKey);
|
||||
item.setUploadUrl(uploadUrl);
|
||||
item.setExpiresIn(uploadUrl == null ? null : PRESIGN_EXPIRY_SECONDS);
|
||||
return item;
|
||||
}
|
||||
|
||||
private static List<JdIngestImageSpec> toPlainSpecs(List<JdImageRegisterRequest.JdImageRegDto> images) {
|
||||
List<JdIngestImageSpec> specs = new ArrayList<>();
|
||||
if (images == null) {
|
||||
return specs;
|
||||
}
|
||||
for (JdImageRegisterRequest.JdImageRegDto dto : images) {
|
||||
if (dto == null) {
|
||||
specs.add(null);
|
||||
continue;
|
||||
}
|
||||
specs.add(new JdIngestImageSpec(dto.getType(), dto.getObjectKey(), dto.getSha256(),
|
||||
dto.getBytes(), dto.getAttrName(), dto.getSortNo(), dto.getWidth(), dto.getHeight()));
|
||||
}
|
||||
return specs;
|
||||
}
|
||||
|
||||
/** 登记明细的中性形状:chunk 回传 DTO 与显式登记 DTO 都归一到它,共用一份对撞逻辑。 */
|
||||
public record JdIngestImageSpec(String type, String objectKey, String sha256, Long bytes,
|
||||
String attrName, Integer sortNo, Integer width, Integer height) {
|
||||
}
|
||||
}
|
||||
+26
-6
@@ -35,9 +35,11 @@ import java.util.Map;
|
||||
*
|
||||
* <p>链路:客户端回传 chunk → 归属解析({@code task.user_id -> users.company_id},读不出跳过
|
||||
* 不回填默认 1,铁律 30)→ 行 upsert(唯一出口 {@link JdProductMapper#upsertBatch},
|
||||
* upsert-only 不 delete,B94-01)→ scope 台账合并写(断点游标 + 批次号 + 累计行数)→
|
||||
* {@code isEnd=true} 时任务落 SUCCESS。图片只消费键名与数量(tempKey 待位),
|
||||
* 预签名直传与登记对撞在 Phase 4 接 {@code JdProductImageIngestService}。
|
||||
* upsert-only 不 delete,B94-01)→ 图片清单登记(Phase 4:直传完成的 objectKey 逐图
|
||||
* statObject 对撞落 {@code biz_jd_product_image},唯一出口
|
||||
* {@link JdProductImageIngestService#registerTaskImages})→
|
||||
* scope 台账合并写(断点游标 + 批次号 + 累计行数)→
|
||||
* {@code isEnd=true} 时任务落 SUCCESS。
|
||||
*
|
||||
* <p>回传 payload 键集契约({@code JdIngestPayloadContractTest} 绊线钉住):顶层
|
||||
* {@code batchId / cursor / items / isEnd}(+ {@code error} 失败上报),行内
|
||||
@@ -65,6 +67,7 @@ public class JdResultIngestService {
|
||||
private final JdCollectTaskService collectTaskService;
|
||||
private final JdStaleTaskService jdStaleTaskService;
|
||||
private final TaskProgressSnapshotService taskProgressSnapshotService;
|
||||
private final JdProductImageIngestService jdProductImageIngestService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JdIngestChunkVo submitChunk(Long taskId, JdIngestChunkRequest request) {
|
||||
@@ -111,8 +114,10 @@ public class JdResultIngestService {
|
||||
continue;
|
||||
}
|
||||
rows.add(buildRow(task, companyId, createRequest, item, summary, request.getBatchId(), scope));
|
||||
// 图片本 Phase 只计数占位(tempKey 待位):预签名直传与登记对撞在 Phase 4 落 biz_jd_product_image。
|
||||
imageCount += item.getImages() == null ? 0 : item.getImages().size();
|
||||
// Phase 4:直传完成的图片清单按 statObject 对撞落 biz_jd_product_image(登记链唯一出口
|
||||
// JdProductImageIngestService,uk_product_image 幂等;商品行未落库时跳过不落孤儿行)。
|
||||
imageCount += jdProductImageIngestService.registerTaskImages(taskId, companyId,
|
||||
item.getProductCode().trim(), toImageSpecs(item.getImages()));
|
||||
}
|
||||
}
|
||||
if (!rows.isEmpty()) {
|
||||
@@ -126,7 +131,7 @@ public class JdResultIngestService {
|
||||
vo.setAccept(true);
|
||||
vo.setDeduped(deduped);
|
||||
vo.setUpserted(rows.size());
|
||||
vo.setImagesRegistered(0);
|
||||
vo.setImagesRegistered(imageCount);
|
||||
return vo;
|
||||
}
|
||||
|
||||
@@ -281,6 +286,21 @@ public class JdResultIngestService {
|
||||
return row;
|
||||
}
|
||||
|
||||
/** chunk 图片明细 → 登记 spec(中性形状,登记链共用一份对撞逻辑)。 */
|
||||
private static List<JdProductImageIngestService.JdIngestImageSpec> toImageSpecs(
|
||||
List<JdIngestChunkRequest.JdIngestImageDto> images) {
|
||||
List<JdProductImageIngestService.JdIngestImageSpec> specs = new ArrayList<>();
|
||||
if (images == null) {
|
||||
return specs;
|
||||
}
|
||||
for (JdIngestChunkRequest.JdIngestImageDto dto : images) {
|
||||
specs.add(dto == null ? null : new JdProductImageIngestService.JdIngestImageSpec(
|
||||
dto.getType(), dto.getObjectKey(), dto.getSha256(), dto.getBytes(),
|
||||
dto.getAttrName(), dto.getSortNo(), null, null));
|
||||
}
|
||||
return specs;
|
||||
}
|
||||
|
||||
private JdIngestChunkRequest.JdProductSummaryDto toSummary(Map<String, Object> summary) {
|
||||
if (summary == null || summary.isEmpty()) {
|
||||
return null;
|
||||
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdImagePresignRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdImageRegisterRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdImagePresignVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdImageRegisterVo;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* v1.4 Phase 4 图片入库链行为测试(蓝图 §4 Phase 4 待激活清单:JdImageStorageTest)。
|
||||
*
|
||||
* <p>覆盖蓝图点名的五条判据:独立桶 objectKey 服务端生成(内容寻址
|
||||
* {@code jd-products/<companyId>/<sha256>.<ext>},客户端不得自造)、sha256 命中既有对象
|
||||
* 不发 URL 只回既有 key、非 owner 拒绝(uid 与任务 owner 对撞,fail-closed)、
|
||||
* 登记时 statObject 字节数不符进 mismatched 不落行、空 images 不落行。
|
||||
* 夹具走生产接线,不反射私有方法。</p>
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class JdImageStorageTest {
|
||||
|
||||
private static final Long TASK_ID = 33890L;
|
||||
private static final Long USER_ID = 7L;
|
||||
private static final Long OTHER_UID = 9L;
|
||||
private static final Long COMPANY_ID = 2L;
|
||||
private static final Long PRODUCT_ID = 5L;
|
||||
private static final String SHA =
|
||||
"3f2a1b4c5d6e7f8091a2b3c4d5e6f7081a2b3c4d5e6f7081a2b3c4d5e6f70812";
|
||||
private static final String PRODUCT_CODE = "10036442942137";
|
||||
|
||||
@Mock
|
||||
private FileTaskMapper fileTaskMapper;
|
||||
@Mock
|
||||
private JdProductMapper jdProductMapper;
|
||||
@Mock
|
||||
private JdProductImageMapper jdProductImageMapper;
|
||||
@Mock
|
||||
private AdminUserMapper adminUserMapper;
|
||||
@Mock
|
||||
private OssStorageService ossStorageService;
|
||||
|
||||
@InjectMocks
|
||||
private JdProductImageIngestService service;
|
||||
|
||||
@BeforeAll
|
||||
static void initTableInfo() {
|
||||
// MP 条件构造器在纯单测里必须先初始化实体 TableInfo(JdProductSyncServiceTest 同款)
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
FileTaskEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
JdProductEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
JdProductImageEntity.class);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void injectConfig() {
|
||||
// @Value 出厂值在纯单测里不生效,按 application.yml 默认值显式注入
|
||||
ReflectionTestUtils.setField(service, "bucket", "jd-products");
|
||||
ReflectionTestUtils.setField(service, "keyPrefix", "jd-products/");
|
||||
ReflectionTestUtils.setField(service, "imageMaxPerProduct", 200);
|
||||
}
|
||||
|
||||
// ---------- 夹具 ----------
|
||||
|
||||
private AdminUserEntity owner() {
|
||||
AdminUserEntity owner = new AdminUserEntity();
|
||||
owner.setId(USER_ID);
|
||||
owner.setCompanyId(COMPANY_ID);
|
||||
return owner;
|
||||
}
|
||||
|
||||
private FileTaskEntity runningTask() {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(TASK_ID);
|
||||
task.setModuleType("JD_SHOP_COLLECT");
|
||||
task.setStatus("RUNNING");
|
||||
task.setUserId(USER_ID);
|
||||
return task;
|
||||
}
|
||||
|
||||
private JdProductEntity productRow() {
|
||||
JdProductEntity product = new JdProductEntity();
|
||||
product.setId(PRODUCT_ID);
|
||||
product.setCompanyId(COMPANY_ID);
|
||||
product.setTaskId(TASK_ID);
|
||||
product.setProductCode(PRODUCT_CODE);
|
||||
return product;
|
||||
}
|
||||
|
||||
private JdImagePresignRequest.JdImageSpecDto spec(String sha256, Long bytes, String contentType) {
|
||||
JdImagePresignRequest.JdImageSpecDto spec = new JdImagePresignRequest.JdImageSpecDto();
|
||||
spec.setSha256(sha256);
|
||||
spec.setBytes(bytes);
|
||||
spec.setContentType(contentType);
|
||||
return spec;
|
||||
}
|
||||
|
||||
private JdImagePresignRequest presignRequest(String sha256, Long bytes, String contentType) {
|
||||
JdImagePresignRequest request = new JdImagePresignRequest();
|
||||
request.setProductCode(PRODUCT_CODE);
|
||||
request.setImages(List.of(spec(sha256, bytes, contentType)));
|
||||
return request;
|
||||
}
|
||||
|
||||
private JdImageRegisterRequest.JdImageRegDto regDto(String objectKey, Long bytes) {
|
||||
return regDto(objectKey, SHA, bytes);
|
||||
}
|
||||
|
||||
private JdImageRegisterRequest.JdImageRegDto regDto(String objectKey, String sha256, Long bytes) {
|
||||
JdImageRegisterRequest.JdImageRegDto dto = new JdImageRegisterRequest.JdImageRegDto();
|
||||
dto.setType("MAIN");
|
||||
dto.setObjectKey(objectKey);
|
||||
dto.setSha256(sha256);
|
||||
dto.setBytes(bytes);
|
||||
dto.setSortNo(1);
|
||||
return dto;
|
||||
}
|
||||
|
||||
private JdImageRegisterRequest registerRequest(JdImageRegisterRequest.JdImageRegDto... images) {
|
||||
JdImageRegisterRequest request = new JdImageRegisterRequest();
|
||||
request.setTaskId(TASK_ID);
|
||||
request.setProductCode(PRODUCT_CODE);
|
||||
request.setImages(List.of(images));
|
||||
return request;
|
||||
}
|
||||
|
||||
private void stubRegisterHappyPath() {
|
||||
when(fileTaskMapper.selectById(TASK_ID)).thenReturn(runningTask());
|
||||
when(adminUserMapper.selectById(USER_ID)).thenReturn(owner());
|
||||
when(jdProductMapper.selectOne(any(Wrapper.class))).thenReturn(productRow());
|
||||
}
|
||||
|
||||
// ---------- 判据 ----------
|
||||
|
||||
@Test
|
||||
void 预签名按独立桶生成内容寻址objectKey() {
|
||||
when(adminUserMapper.selectById(USER_ID)).thenReturn(owner());
|
||||
when(ossStorageService.objectSize(eq("jd-products"), anyString())).thenReturn(-1L);
|
||||
when(ossStorageService.presignObjectUpload(eq("jd-products"), anyString(), anyInt()))
|
||||
.thenReturn("https://oss.aishufu.top/jd-products/2/" + SHA + ".jpg?X-Amz-Signature=signed");
|
||||
|
||||
JdImagePresignVo vo = service.presign(USER_ID,
|
||||
presignRequest(SHA, 102400L, "image/jpeg"));
|
||||
|
||||
assertThat(vo.getItems()).hasSize(1);
|
||||
JdImagePresignVo.Item item = vo.getItems().get(0);
|
||||
// objectKey 由服务端生成:独立桶前缀 + 归属公司段 + 内容摘要 + MIME 推扩展名(蓝图 §3.3)
|
||||
assertThat(item.getObjectKey()).isEqualTo("jd-products/2/" + SHA + ".jpg");
|
||||
assertThat(item.getUploadUrl()).contains("X-Amz-Signature");
|
||||
assertThat(item.getExpiresIn()).isEqualTo(600);
|
||||
assertThat(vo.getSkipped()).isZero();
|
||||
verify(ossStorageService).presignObjectUpload(eq("jd-products"),
|
||||
eq("jd-products/2/" + SHA + ".jpg"), eq(600));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 同公司同内容命中既有对象不发URL只回既有key() {
|
||||
when(adminUserMapper.selectById(USER_ID)).thenReturn(owner());
|
||||
when(ossStorageService.objectSize(eq("jd-products"), anyString())).thenReturn(102400L);
|
||||
|
||||
JdImagePresignVo vo = service.presign(USER_ID,
|
||||
presignRequest(SHA, 102400L, "image/jpeg"));
|
||||
|
||||
assertThat(vo.getItems()).hasSize(1);
|
||||
assertThat(vo.getItems().get(0).getObjectKey()).isEqualTo("jd-products/2/" + SHA + ".jpg");
|
||||
assertThat(vo.getItems().get(0).getUploadUrl())
|
||||
.as("命中既有对象:不发 PUT URL,客户端登记既有 key 不重传(AC-5)")
|
||||
.isNull();
|
||||
assertThat(vo.getSkipped()).isEqualTo(1);
|
||||
verify(ossStorageService, never()).presignObjectUpload(anyString(), anyString(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 非owner与缺身份一律拒绝() {
|
||||
when(fileTaskMapper.selectById(TASK_ID)).thenReturn(runningTask());
|
||||
// uid=9 不是任务 owner(owner=7)
|
||||
assertThatThrownBy(() -> service.register(OTHER_UID, registerRequest()))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("无权操作该任务的图片");
|
||||
// 缺身份(JWT 解析失败且未带 uid)fail-closed
|
||||
assertThatThrownBy(() -> service.register(null, registerRequest()))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("无法确认调用者身份");
|
||||
assertThatThrownBy(() -> service.presign(null, presignRequest(SHA, 1L, "image/jpeg")))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("无法确认调用者身份");
|
||||
verify(jdProductImageMapper, never()).upsertBatch(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 登记时字节数不符进mismatched不落行() {
|
||||
stubRegisterHappyPath();
|
||||
when(ossStorageService.objectSize(eq("jd-products"), anyString())).thenReturn(50L);
|
||||
|
||||
JdImageRegisterVo vo = service.register(USER_ID,
|
||||
registerRequest(regDto("jd-products/2/" + SHA + ".jpg", 102400L)));
|
||||
|
||||
assertThat(vo.getRegistered()).isZero();
|
||||
assertThat(vo.getMismatched())
|
||||
.as("statObject 实测字节与声明不符:不落行、不静默吞(蓝图 §3.3 ③ 必做项)")
|
||||
.isEqualTo(1);
|
||||
verify(jdProductImageMapper, never()).upsertBatch(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 空images与非法规格不落行() {
|
||||
// 空 images:显式登记口直接拒绝空载荷(无 taskId 上下文);chunk 路径空清单返回 0 不查库
|
||||
assertThat(service.registerTaskImages(TASK_ID, COMPANY_ID, PRODUCT_CODE, List.of())).isZero();
|
||||
assertThat(service.registerTaskImages(TASK_ID, COMPANY_ID, PRODUCT_CODE, null)).isZero();
|
||||
verify(jdProductMapper, never()).selectOne(any(Wrapper.class));
|
||||
verify(jdProductImageMapper, never()).upsertBatch(any());
|
||||
|
||||
// 非法规格(sha256 非 64 位 hex)逐条跳过,不进对撞不进 upsert
|
||||
stubRegisterHappyPath();
|
||||
JdImageRegisterVo vo = service.register(USER_ID,
|
||||
registerRequest(regDto("jd-products/2/" + SHA + ".jpg", "短摘要", 100L)));
|
||||
assertThat(vo.getRegistered()).isZero();
|
||||
assertThat(vo.getSkipped()).isEqualTo(1);
|
||||
assertThat(vo.getMismatched()).isZero();
|
||||
verify(jdProductImageMapper, never()).upsertBatch(any());
|
||||
}
|
||||
}
|
||||
+2
@@ -76,6 +76,8 @@ class JdProductSyncServiceTest {
|
||||
private JdStaleTaskService jdStaleTaskService;
|
||||
@Mock
|
||||
private TaskProgressSnapshotService taskProgressSnapshotService;
|
||||
@Mock
|
||||
private JdProductImageIngestService jdProductImageIngestService;
|
||||
@Spy
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user