|
|
@@ -124,8 +124,12 @@ public class FileUploadController {
|
|
|
}
|
|
|
|
|
|
@PostMapping("/fileObject")
|
|
|
- public FileObject uploadFileObject(@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
- String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("");
|
|
|
+ public FileObject uploadFileObject(@RequestParam("file") MultipartFile file,
|
|
|
+ @RequestParam(value = "compress", defaultValue = "false") boolean compress,
|
|
|
+ @RequestParam(value = "width", required = false) Integer width,
|
|
|
+ @RequestParam(value = "height", required = false) Integer height) throws IOException {
|
|
|
+ String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
|
|
|
+ .toLowerCase().replace("jpeg", "jpg");
|
|
|
String[] extList = new String[]{"jpg", "jpeg", "png", "gif", "mp4"};
|
|
|
if (!ArrayUtils.contains(extList, ext.toLowerCase())) {
|
|
|
throw new BusinessException("仅支持" + StringUtils.join(extList, "、") + "格式");
|
|
|
@@ -135,8 +139,51 @@ public class FileUploadController {
|
|
|
String path = "nft/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
+ RandomStringUtils.randomAlphabetic(8)
|
|
|
+ "." + ext;
|
|
|
- String url = storageService.uploadFromInputStream(new FileInputStream(tmpFile), path);
|
|
|
+ String url;
|
|
|
+
|
|
|
+
|
|
|
+ if (Pattern.matches("(jpg|png)", ext) && compress) {
|
|
|
+ if (width == null && height == null) {
|
|
|
+ width = Integer.MAX_VALUE;
|
|
|
+ height = Integer.MAX_VALUE;
|
|
|
+ } else if (height == null) {
|
|
|
+ height = Integer.MAX_VALUE;
|
|
|
+ } else if (width == null) {
|
|
|
+ width = Integer.MAX_VALUE;
|
|
|
+ }
|
|
|
+ BufferedImage img = null;
|
|
|
+ if ("jpg".equals(ext)) {
|
|
|
+ img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
|
|
|
+ } else {
|
|
|
+ img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, true);
|
|
|
+ }
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(img, ext, os);
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ url = storageService.uploadFromInputStream(is, path);
|
|
|
+ } else if (width != null || height != null) {
|
|
|
+ if (height == null) {
|
|
|
+ height = Integer.MAX_VALUE;
|
|
|
+ } else if (width == null) {
|
|
|
+ width = Integer.MAX_VALUE;
|
|
|
+ }
|
|
|
+ BufferedImage img = null;
|
|
|
+ if ("jpg".equals(ext)) {
|
|
|
+ img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
|
|
|
+ } else {
|
|
|
+ img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, false);
|
|
|
+ }
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(img, ext, os);
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ url = storageService.uploadFromInputStream(is, path);
|
|
|
+ } else {
|
|
|
+ url = storageService.uploadFromInputStream(new FileInputStream(tmpFile), path);
|
|
|
+ }
|
|
|
+
|
|
|
String thumbUrl = null;
|
|
|
+
|
|
|
+
|
|
|
if ("mp4".equalsIgnoreCase(ext)) {
|
|
|
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
|
|
|
frameGrabber.start();
|