|
@@ -1,10 +1,18 @@
|
|
|
package com.izouma.nineth.web;
|
|
package com.izouma.nineth.web;
|
|
|
|
|
|
|
|
|
|
+import com.izouma.nineth.domain.FileObject;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.service.storage.StorageService;
|
|
import com.izouma.nineth.service.storage.StorageService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.bytedeco.javacv.FFmpegFrameGrabber;
|
|
|
|
|
+import org.bytedeco.javacv.Frame;
|
|
|
|
|
+import org.bytedeco.javacv.Java2DFrameConverter;
|
|
|
|
|
+import org.pngquant.PngQuant;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
@@ -12,13 +20,15 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
|
+import java.io.*;
|
|
|
import java.net.URLConnection;
|
|
import java.net.URLConnection;
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Base64;
|
|
import java.util.Base64;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
@@ -39,8 +49,8 @@ public class FileUploadController {
|
|
|
} catch (Exception ignored) {
|
|
} catch (Exception ignored) {
|
|
|
}
|
|
}
|
|
|
path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
- + RandomStringUtils.randomAlphabetic(8)
|
|
|
|
|
- + "." + FilenameUtils.getExtension(file.getOriginalFilename());
|
|
|
|
|
|
|
+ + RandomStringUtils.randomAlphabetic(8)
|
|
|
|
|
+ + "." + FilenameUtils.getExtension(file.getOriginalFilename());
|
|
|
}
|
|
}
|
|
|
InputStream is;
|
|
InputStream is;
|
|
|
try {
|
|
try {
|
|
@@ -66,7 +76,7 @@ public class FileUploadController {
|
|
|
} catch (Exception ignored) {
|
|
} catch (Exception ignored) {
|
|
|
}
|
|
}
|
|
|
path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
- + RandomStringUtils.randomAlphabetic(8) + ext;
|
|
|
|
|
|
|
+ + RandomStringUtils.randomAlphabetic(8) + ext;
|
|
|
}
|
|
}
|
|
|
InputStream is;
|
|
InputStream is;
|
|
|
try {
|
|
try {
|
|
@@ -77,4 +87,50 @@ public class FileUploadController {
|
|
|
}
|
|
}
|
|
|
return storageService.uploadFromInputStream(is, path);
|
|
return storageService.uploadFromInputStream(is, path);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping("/fileObject")
|
|
|
|
|
+ public FileObject uploadFileObject(@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
|
|
+ String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("");
|
|
|
|
|
+ String[] extList = new String[]{"jpg", "jpeg", "png", "gif", "mp4"};
|
|
|
|
|
+ if (!ArrayUtils.contains(extList, ext.toLowerCase())) {
|
|
|
|
|
+ throw new BusinessException("仅支持" + StringUtils.join(extList, "、") + "格式");
|
|
|
|
|
+ }
|
|
|
|
|
+ File tmpFile = File.createTempFile("upload_", "." + ext);
|
|
|
|
|
+ FileUtils.copyToFile(file.getInputStream(), tmpFile);
|
|
|
|
|
+ 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 thumbUrl = null;
|
|
|
|
|
+ if ("mp4".equalsIgnoreCase(ext)) {
|
|
|
|
|
+ FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
|
|
|
|
|
+ frameGrabber.start();
|
|
|
|
|
+ Java2DFrameConverter aa = new Java2DFrameConverter();
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ BufferedImage bi;
|
|
|
|
|
+ Frame f = frameGrabber.grabKeyFrame();
|
|
|
|
|
+
|
|
|
|
|
+ bi = aa.convert(f);
|
|
|
|
|
+ File thumbFile = null;
|
|
|
|
|
+ while (bi != null) {
|
|
|
|
|
+ thumbFile = File.createTempFile("video_thumb_", ".png");
|
|
|
|
|
+ PngQuant pngQuant = new PngQuant();
|
|
|
|
|
+ ImageIO.write(pngQuant.getRemapped(bi), "png", thumbFile);
|
|
|
|
|
+ f = frameGrabber.grabKeyFrame();
|
|
|
|
|
+ bi = aa.convert(f);
|
|
|
|
|
+ }
|
|
|
|
|
+ Objects.requireNonNull(thumbFile);
|
|
|
|
|
+ String thumbPath = "thumb_image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
|
|
+ + RandomStringUtils.randomAlphabetic(8) + ".png";
|
|
|
|
|
+ thumbUrl = storageService.uploadFromInputStream(new FileInputStream(thumbFile), thumbPath);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ frameGrabber.stop();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new FileObject(file.getOriginalFilename(), url, thumbUrl, file.getContentType());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|