xiongzhu 4 gadi atpakaļ
vecāks
revīzija
cf09ba2b90

+ 6 - 0
pom.xml

@@ -374,6 +374,12 @@
             <groupId>com.fasterxml.jackson.datatype</groupId>
             <artifactId>jackson-datatype-jsr310</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>net.coobird</groupId>
+            <artifactId>thumbnailator</artifactId>
+            <version>0.4.14</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 29 - 1
src/main/java/com/izouma/nineth/web/FileUploadController.java

@@ -29,6 +29,7 @@ import java.util.Base64;
 import java.util.Date;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.regex.Pattern;
 
 
 @RestController
@@ -41,7 +42,34 @@ public class FileUploadController {
 
     @PostMapping("/file")
     public String uploadFile(@RequestParam("file") MultipartFile file,
-                             @RequestParam(value = "path", required = false) String path) {
+                             @RequestParam(value = "path", required = false) String path,
+                             @RequestParam(value = "compress", defaultValue = "false") boolean compress,
+                             @RequestParam(value = "width", required = false) Integer width,
+                             @RequestParam(value = "height", required = false) Integer height) throws IOException {
+
+        if (width != null && height == null) {
+            height = Integer.MAX_VALUE;
+        }
+        if (height != null && width == null) {
+            width = Integer.MAX_VALUE;
+        }
+
+        String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
+                .toLowerCase().replace("jpeg", "jpg");
+        if (Pattern.matches("(jpg|png)", ext)) {
+            File tmpFile = null;
+            try {
+                tmpFile = File.createTempFile("upload", "." + ext);
+                FileUtils.copyToFile(file.getInputStream(), tmpFile);
+            } catch (IOException e) {
+                e.printStackTrace();
+                throw new BusinessException("上传失败");
+            }
+            PngQuant pngQuant = new PngQuant();
+            ImageIO.write(pngQuant.getRemapped(ImageIO.read(file.getInputStream())),
+                    "png", new File("/Users/drew/Downloads/111.png"));
+        }
+
         if (path == null) {
             String basePath = "application";
             try {

+ 5 - 5
src/main/resources/application.yaml

@@ -78,11 +78,11 @@ storage:
   provider: aliyun
   local_path: /var/www/upload/
 aliyun:
-  access-key-id: PXzJyah5rZfWHIIH
-  access-key-secret: e1MS6j0wypXJrw8CM0hObZu8qKbfah
-  oss-end-point: oss-cn-hangzhou.aliyuncs.com
-  oss-bucket-name: 9space
-  oss-domain: https://9space.oss-cn-hangzhou.aliyuncs.com
+  access-key-id: LTAI5tCtRs8DXBLBsn49qRZX
+  access-key-secret: 86uC18LZsfB9JU04BK7ImVXfOytEkG
+  oss-end-point: oss-cn-shenzhen.aliyuncs.com
+  oss-bucket-name: 9space-2021
+  oss-domain: https://9space-2021.oss-cn-shenzhen.aliyuncs.com
 general:
   host: https://nft.9space.vip
 mychain:

+ 23 - 2
src/test/java/com/izouma/nineth/CommonTest.java

@@ -14,6 +14,8 @@ import io.ipfs.api.NamedStreamable;
 import io.ipfs.cid.Cid;
 import io.ipfs.multihash.Multihash;
 import lombok.SneakyThrows;
+import net.coobird.thumbnailator.Thumbnails;
+import net.coobird.thumbnailator.name.Rename;
 import org.apache.commons.codec.EncoderException;
 import org.apache.commons.codec.net.URLCodec;
 import org.apache.commons.lang3.RandomStringUtils;
@@ -26,6 +28,7 @@ import org.bouncycastle.util.encoders.Base64;
 import org.bouncycastle.util.encoders.UrlBase64Encoder;
 import org.junit.Assert;
 import org.junit.Test;
+import org.libjpegturbo.turbojpeg.processor.api.ImageProcessException;
 import org.libjpegturbo.turbojpeg.processor.api.ImageProcessInfo;
 import org.libjpegturbo.turbojpeg.processor.api.ImageProcessor;
 import org.libjpegturbo.turbojpeg.processor.impl.ImageProcessorImpl;
@@ -172,8 +175,8 @@ public class CommonTest {
     public void pngquant() {
         PngQuant pngQuant = new PngQuant();
         ImageIO.write(pngQuant
-                        .getRemapped(ImageIO.read(new File("/Users/drew/Downloads/国内申请-案件新增(客户经理).png"))),
-                "png", new File("/Users/drew/Downloads/111.png"));
+                        .getRemapped(ImageIO.read(new File("/Users/drew/Downloads/2021-11-01-18-07-04JCDfFxKb.HEIC"))),
+                "jpg", new File("/Users/drew/Downloads/111.jpg"));
     }
 
     @SneakyThrows
@@ -188,6 +191,24 @@ public class CommonTest {
         System.out.println(out);
     }
 
+    @Test
+    public void thumbnailator() throws IOException, ImageProcessException {
+        Thumbnails.of(new File("/Users/drew/Downloads/2021-11-01-17-47-55zMwbSlgJ.jpeg"))
+                .size(1000, 1000)
+                .outputFormat("jpg")
+                .toFile("/Users/drew/Desktop/1.jpg");
+
+        PngQuant pngQuant = new PngQuant();
+        ImageIO.write(pngQuant
+                        .getRemapped(ImageIO.read(new File("/Users/drew/Desktop/1.jpg"))),
+                "jpg", new File("/Users/drew/Desktop/2.jpg"));
+
+        ImageProcessor processor = new ImageProcessorImpl();
+        ImageProcessInfo processInfo = ImageProcessInfo.fromMap(ImageProcessorUtils.compressImage(processor,
+                new File("/Users/drew/Desktop/1.jpg"),
+                new File("/Users/drew/Desktop/3.jpg"), 75));
+    }
+
     @Test
     public void base64() {
         System.out.println(Base64.decode("e6e6vQJYhGmIkcA1pfnsipTovp10wJ"));