二期内容提交
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
@@ -17,6 +18,10 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
/** Firstleg 保存第三方尺寸测量证据图片的内部入口。 */
|
||||
@RestController
|
||||
@@ -62,6 +67,26 @@ public class InternalMeasurementImageController {
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@GetMapping("/{tenantId}/{inboundId}/{fileId}")
|
||||
public ResponseEntity<byte[]> download(
|
||||
@RequestHeader(value = "X-Internal-Service-Token", required = false) String token,
|
||||
@PathVariable long tenantId,
|
||||
@PathVariable long inboundId,
|
||||
@PathVariable long fileId) {
|
||||
requireToken(token);
|
||||
if (tenantId <= 0 || inboundId <= 0 || fileId <= 0) {
|
||||
throw new BizException(ResultCode.BAD_REQUEST, "租户、入库单和文件ID必须大于0");
|
||||
}
|
||||
var image = images.download(tenantId, inboundId, fileId);
|
||||
return ResponseEntity.ok()
|
||||
.cacheControl(CacheControl.noStore())
|
||||
.contentType(MediaType.parseMediaType(image.contentType()))
|
||||
.contentLength(image.content().length)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.inline()
|
||||
.filename(image.fileName(), StandardCharsets.UTF_8).build().toString())
|
||||
.body(image.content());
|
||||
}
|
||||
|
||||
private void requireToken(String token) {
|
||||
if (token == null || !MessageDigest.isEqual(expectedToken, token.getBytes(StandardCharsets.UTF_8))) {
|
||||
throw new BizException(ResultCode.FORBIDDEN, "内部服务凭证无效");
|
||||
|
||||
+12
@@ -40,4 +40,16 @@ class InternalMeasurementImageControllerTest {
|
||||
.isInstanceOf(BizException.class)
|
||||
.hasMessageContaining("凭证无效");
|
||||
}
|
||||
@Test
|
||||
void 内部下载同时校验租户入库单和文件ID() {
|
||||
when(images.download(11L, 21L, 41L)).thenReturn(
|
||||
new MeasurementImageService.Download("measure.png", MediaType.IMAGE_PNG_VALUE,
|
||||
new byte[]{1, 2, 3}));
|
||||
|
||||
var response = controller.download("internal-token", 11L, 21L, 41L);
|
||||
|
||||
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG);
|
||||
assertThat(response.getBody()).containsExactly(1, 2, 3);
|
||||
verify(images).download(11L, 21L, 41L);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user