优化
This commit is contained in:
@@ -81,6 +81,24 @@ public class PlatformMeasurementImageController {
|
||||
.body(image.content());
|
||||
}
|
||||
|
||||
@GetMapping("/{tenantId}/{inboundId}/{fileId}/thumbnail")
|
||||
@PreAuthorize("hasAuthority('platform:firstleg:inbound:measure:view')")
|
||||
@Operation(summary = "查看入库单称重图片缩略图")
|
||||
public ResponseEntity<byte[]> thumbnail(
|
||||
@AuthenticationPrincipal AuthenticatedUser user,
|
||||
@PathVariable long tenantId,
|
||||
@PathVariable long inboundId,
|
||||
@PathVariable long fileId) {
|
||||
requirePlatform(user);
|
||||
requirePositive(tenantId, inboundId, fileId);
|
||||
var image = images.thumbnail(tenantId, inboundId, fileId);
|
||||
return ResponseEntity.ok()
|
||||
.cacheControl(CacheControl.noStore())
|
||||
.contentType(MediaType.IMAGE_JPEG)
|
||||
.contentLength(image.content().length)
|
||||
.body(image.content());
|
||||
}
|
||||
|
||||
private long requirePlatform(AuthenticatedUser user) {
|
||||
if (user == null || !"PLATFORM".equalsIgnoreCase(user.appCode())
|
||||
|| user.tenantId() == null || user.tenantId() != 0L
|
||||
|
||||
@@ -15,11 +15,19 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
@Service
|
||||
public class MeasurementImageService {
|
||||
@@ -81,11 +89,45 @@ public class MeasurementImageService {
|
||||
return new Download(object.getOriginalName(), object.getContentType(), storage.read(object.getObjectKey()));
|
||||
}
|
||||
|
||||
public Download thumbnail(long tenantId, long inboundId, long fileId) {
|
||||
Download original = download(tenantId, inboundId, fileId);
|
||||
try {
|
||||
BufferedImage source = ImageIO.read(new ByteArrayInputStream(original.content()));
|
||||
if (source == null) {
|
||||
throw new BizException(ResultCode.VALIDATION_FAILED, "称重图片无法生成缩略图");
|
||||
}
|
||||
double scale = Math.min(1D, Math.min(480D / source.getWidth(), 320D / source.getHeight()));
|
||||
int width = Math.max(1, (int) Math.round(source.getWidth() * scale));
|
||||
int height = Math.max(1, (int) Math.round(source.getHeight() * scale));
|
||||
BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D graphics = target.createGraphics();
|
||||
try {
|
||||
graphics.setColor(Color.WHITE);
|
||||
graphics.fillRect(0, 0, width, height);
|
||||
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
|
||||
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
|
||||
RenderingHints.VALUE_RENDER_QUALITY);
|
||||
graphics.drawImage(source, 0, 0, width, height, null);
|
||||
} finally {
|
||||
graphics.dispose();
|
||||
}
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
if (!ImageIO.write(target, "jpg", output)) {
|
||||
throw new BizException(ResultCode.INTERNAL_ERROR, "称重图片缩略图编码失败");
|
||||
}
|
||||
return new Download("thumbnail.jpg", "image/jpeg", output.toByteArray());
|
||||
} catch (IOException exception) {
|
||||
throw new BizException(ResultCode.INTERNAL_ERROR, "称重图片缩略图生成失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteMeasurementEvidence(long tenantId, long inboundId, long fileId, long operatorId) {
|
||||
FileObjectEntity object = requireObject(tenantId, inboundId, fileId);
|
||||
if (!"INBOUND_MEASUREMENT_IMAGE".equals(object.getBusinessType())) {
|
||||
throw new BizException(ResultCode.CONFLICT, "仅允许清理第三方尺寸测量证据图片");
|
||||
if (!Set.of("INBOUND_WEIGHT_IMAGE", "INBOUND_MEASUREMENT_IMAGE")
|
||||
.contains(object.getBusinessType())) {
|
||||
throw new BizException(ResultCode.CONFLICT, "仅允许清理入库测量图片");
|
||||
}
|
||||
object.setStatus("DELETED");
|
||||
object.setUpdatedBy(operatorId);
|
||||
|
||||
Reference in New Issue
Block a user