|
|
@@ -4,6 +4,7 @@ import com.etotem.cfc.config.AiYunyiProperties;
|
|
|
import com.etotem.cfc.dto.AiyunyiTokenPair;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.core.io.ByteArrayResource;
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
@@ -11,8 +12,11 @@ import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.lang.reflect.Array;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
@@ -638,4 +642,90 @@ public class AicdsGatewayService {
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ // ==================== 照片上传能力(ai.aiyunyi.upload,S-cfc-496) ====================
|
|
|
+
|
|
|
+ /** B.7:平台 uploadFile 转发路径(注意 {@code /app-api/} 前缀,非 {@code /open-api/})。 */
|
|
|
+ private static final String PATH_UPLOAD_FILE = "/app-api/detection/package/enter/upload";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 能力键 {@code ai.aiyunyi.upload}(S-cfc-496):契约 B.7 —— 照片上传代理。
|
|
|
+ *
|
|
|
+ * <p>平台 {@code uploadFile} 为免登录 PermitAll 接口:multipart/form-data,
|
|
|
+ * {@code file} 必填 / {@code path} 选填。转发请求只保留网关签名头
|
|
|
+ * ({@code X-App-Key / X-Timestamp / X-Api-Sign}),刻意<b>不</b>附加业务鉴权头
|
|
|
+ * (免登录接口不接收业务令牌 / 用户加密头)。</p>
|
|
|
+ *
|
|
|
+ * <p>响应 {@code {"code":0,"data":"https://.../xxx.jpg","msg":""}},
|
|
|
+ * {@code data} 即图片 URL,直接填入 {@code enter/detection_data} 的
|
|
|
+ * {@code *UrlList[].url}(§3.3 / §4.1)。</p>
|
|
|
+ *
|
|
|
+ * @param file 待上传照片(必填)
|
|
|
+ * @param path 存储路径/文件名(选填,如 a.jpg)
|
|
|
+ * @return 平台返回的图片 URL
|
|
|
+ */
|
|
|
+ public String uploadFile(MultipartFile file, String path) {
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("爱云医照片上传缺少 file(B.7 必填)");
|
|
|
+ }
|
|
|
+ if (isCircuitOpen()) {
|
|
|
+ throw new IllegalStateException("爱云医网关熔断中,拒绝发起照片上传");
|
|
|
+ }
|
|
|
+ String appKey = resolveAppKey();
|
|
|
+ String timestamp = String.valueOf(System.currentTimeMillis());
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
+ headers.set(H_APP_KEY, appKey);
|
|
|
+ headers.set(H_TIMESTAMP, timestamp);
|
|
|
+ headers.set(H_API_SIGN, resolveSign(appKey, timestamp));
|
|
|
+
|
|
|
+ MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
|
|
|
+ form.add("file", toUploadResource(file));
|
|
|
+ if (!isBlank(path)) {
|
|
|
+ form.add("path", path);
|
|
|
+ }
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> entity =
|
|
|
+ new HttpEntity<MultiValueMap<String, Object>>(form, headers);
|
|
|
+ String url = resolveBaseUrl() + PATH_UPLOAD_FILE;
|
|
|
+ try {
|
|
|
+ ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, entity,
|
|
|
+ Map.class, new Object[0]);
|
|
|
+ consecutiveFailures.set(0);
|
|
|
+ return extractUploadImageUrl(response == null ? null : response.getBody());
|
|
|
+ } catch (RestClientException e) {
|
|
|
+ recordFailure();
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 把上传文件转为 multipart 资源,保留原始文件名(平台按后缀识别图片类型)。 */
|
|
|
+ private static ByteArrayResource toUploadResource(final MultipartFile file) {
|
|
|
+ try {
|
|
|
+ return new ByteArrayResource(file.getBytes()) {
|
|
|
+ @Override
|
|
|
+ public String getFilename() {
|
|
|
+ return file.getOriginalFilename();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ } catch (java.io.IOException e) {
|
|
|
+ throw new IllegalStateException("读取待上传照片失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 解析 uploadFile 响应 {@code {code,data,msg}},{@code data} 即图片 URL;非 0/缺失归一为异常。 */
|
|
|
+ private static String extractUploadImageUrl(Map<String, Object> body) {
|
|
|
+ if (body == null) {
|
|
|
+ throw new IllegalStateException("爱云医照片上传响应为空(B.7)");
|
|
|
+ }
|
|
|
+ Object code = body.get("code");
|
|
|
+ if (code instanceof Number && ((Number) code).intValue() != 0) {
|
|
|
+ throw new IllegalStateException("爱云医照片上传失败,code=" + code);
|
|
|
+ }
|
|
|
+ Object data = body.get("data");
|
|
|
+ String imageUrl = data == null ? null : data.toString();
|
|
|
+ if (imageUrl == null || imageUrl.trim().isEmpty()) {
|
|
|
+ throw new IllegalStateException("爱云医照片上传响应缺少 data 图片 URL(B.7)");
|
|
|
+ }
|
|
|
+ return imageUrl;
|
|
|
+ }
|
|
|
}
|