Browse Source

Merge branch 'dev' into 'mp-server-test'

Dev

See merge request quanshu/mp-server!381
zhong chunping 3 years ago
parent
commit
aedc5bbfc2

+ 25 - 7
mp-admin/src/main/java/com/qs/mp/web/controller/common/FileUploadController.java

@@ -1,10 +1,10 @@
 package com.qs.mp.web.controller.common;
 
 
-import com.alibaba.druid.sql.visitor.functions.If;
 import com.qs.mp.common.core.domain.AjaxResult;
 import com.qs.mp.common.enums.PicHandlerTypeEnum;
-import com.qs.mp.common.filter.ThumbnailsImgFilter;
+import com.qs.mp.common.image.CompressUtil;
+import com.qs.mp.common.image.ThumbnailsImgFilter;
 import com.qs.mp.common.qcloud.QcloudFileUtils;
 import com.qs.mp.common.utils.LogUtil;
 import com.qs.mp.common.utils.StringUtils;
@@ -13,7 +13,10 @@ import com.qs.mp.core.domain.LoginUser;
 import com.qs.mp.core.domain.UploadAttachment;
 import com.qs.mp.framework.security.handle.HostHolder;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
 import javax.imageio.ImageIO;
 import net.coobird.thumbnailator.Thumbnails;
 import net.coobird.thumbnailator.Thumbnails.Builder;
@@ -55,7 +58,6 @@ public class FileUploadController extends BaseApiController {
     @Value("${cloud.private-bucket-name}")
     private String privateBucketName;
 
-
     @Autowired
     protected HostHolder hostHolder;
 
@@ -247,6 +249,8 @@ public class FileUploadController extends BaseApiController {
             int pWidth = bufferedImage.getWidth();
             int pHeight = bufferedImage.getHeight();
             builder.size(pWidth, pHeight);
+            double accuracy = CompressUtil.getAccuracy(file.getSize()/1024);
+            LogUtil.info(logger, "原始文件长:{0},大小:{1},压缩比:{2}", pWidth, file.getSize()/1024, accuracy);
 
             String outFileDir = filePath+"/thumb";
             File tempFile = new File(outFileDir);
@@ -258,15 +262,28 @@ public class FileUploadController extends BaseApiController {
             builder.toFile(outFilePath);
 
             // 压缩图片
-            if (pWidth > 500) {
-                float scale = 500f / pWidth;
-                Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8f).toFile(outFilePath);
+            if (pWidth > 750) {
+                float scale = 750f / pWidth;
+                if ("image/png".equals(file.getContentType())) {
+                    Thumbnails.of(outFilePath).scale(scale).outputQuality(accuracy)
+                        .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
+                        .toFile(outFilePath);
+                } else {
+                    Thumbnails.of(outFilePath).scale(scale).outputQuality(accuracy).toFile(outFilePath);
+                }
             }else {
-                Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8f).toFile(outFilePath);
+                if ("image/png".equals(file.getContentType())) {
+                    Thumbnails.of(outFilePath).scale(1f).outputQuality(accuracy)
+                        .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
+                        .toFile(outFilePath);
+                } else {
+                    Thumbnails.of(outFilePath).scale(1f).outputQuality(accuracy).toFile(outFilePath);
+                }
             }
 
 
             File outFile = new File(outFilePath);
+            LogUtil.info(logger, "压缩后文件大小:{0}", outFile.length()/ 1024);
             QcloudFileUtils.putFile(outFile, thumbName, bucketName, mimeType);
             if(outFile.exists()) {
                 LogUtil.info(log, "delete file..."+outFilePath);
@@ -278,4 +295,5 @@ public class FileUploadController extends BaseApiController {
         }
         return name;
     }
+
 }

+ 89 - 0
mp-common/src/main/java/com/qs/mp/common/image/CompressUtil.java

@@ -0,0 +1,89 @@
+package com.qs.mp.common.image;
+
+import com.qs.mp.common.utils.LogUtil;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import net.coobird.thumbnailator.Thumbnails;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 压缩工具类
+ *
+ * @author zhongcp
+ * @Date 2022/4/16
+ */
+public class CompressUtil {
+
+  protected static final Logger logger = LoggerFactory.getLogger(CompressUtil.class);
+
+  private static final Integer ZERO = 0;
+  private static final Integer ONE_ZERO_TWO_FOUR = 1024;
+  private static final Integer NINE_ZERO_ZERO = 400;
+  private static final Integer THREE_TWO_SEVEN_FIVE = 2047;
+  private static final Integer TWO_ZERO_FOUR_SEVEN = 900;
+  private static final Double ZERO_EIGHT_FIVE = 0.85;
+  private static final Double ZERO_SIX = 0.6;
+  private static final Double ZERO_FOUR_FOUR = 0.44;
+  private static final Double ZERO_FOUR = 0.4;
+
+  /**
+   * 根据指定大小压缩图片
+   *
+   * @param imageBytes  源图片字节数组
+   * @param desFileSize 指定图片大小,单位kb
+   * @return 压缩质量后的图片字节数组
+   */
+  public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
+    if (imageBytes == null || imageBytes.length <= ZERO
+        || imageBytes.length < desFileSize * ONE_ZERO_TWO_FOUR) {
+      return imageBytes;
+    }
+    long srcSize = imageBytes.length;
+    double accuracy = getAccuracy(srcSize / ONE_ZERO_TWO_FOUR);
+    try {
+
+      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
+      while (imageBytes.length > desFileSize * ONE_ZERO_TWO_FOUR) {
+        ByteArrayInputStream inputStream = null;
+        try {
+          inputStream = new ByteArrayInputStream(imageBytes);
+          Thumbnails.of(inputStream)
+              .scale(accuracy)
+              .outputQuality(accuracy)
+              .toOutputStream(outputStream);
+          imageBytes = outputStream.toByteArray();
+        } finally {
+          if (null != inputStream) {
+            inputStream.close();
+          }
+        }
+      }
+      LogUtil.info(logger, "图片原大小={}kb | 压缩后大小={}kb",
+          srcSize / ONE_ZERO_TWO_FOUR, imageBytes.length / ONE_ZERO_TWO_FOUR);
+    } catch (Exception e) {
+      LogUtil.error(logger, "【图片压缩】msg=图片压缩失败!", e);
+    }
+    return imageBytes;
+  }
+
+  /**
+   * 自动调节精度(经验数值)
+   *
+   * @param size 源图片大小
+   * @return 图片压缩质量比
+   */
+  public static double getAccuracy(long size) {
+    double accuracy;
+    if (size < NINE_ZERO_ZERO) {
+      accuracy = ZERO_EIGHT_FIVE;
+    } else if (size < TWO_ZERO_FOUR_SEVEN) {
+      accuracy = ZERO_SIX;
+    } else if (size < THREE_TWO_SEVEN_FIVE) {
+      accuracy = ZERO_FOUR_FOUR;
+    } else {
+      accuracy = ZERO_FOUR;
+    }
+    return accuracy;
+  }
+}

+ 1 - 1
mp-common/src/main/java/com/qs/mp/common/filter/ThumbnailsImgFilter.java → mp-common/src/main/java/com/qs/mp/common/image/ThumbnailsImgFilter.java

@@ -1,4 +1,4 @@
-package com.qs.mp.common.filter;
+package com.qs.mp.common.image;
 
 import java.awt.Color;
 import java.awt.Graphics2D;

+ 5 - 10
mp-common/src/main/java/com/qs/mp/common/jsms/JSMSUtils.java

@@ -66,11 +66,9 @@ public class JSMSUtils {
 
             }
         } catch (APIConnectionException e) {
-            logger.error("Connection error. Should retry later. ", e);
+            logger.error("Connection error. Should retry later. " + e.getMessage());
         } catch (APIRequestException e) {
-            logger.error("Error response from JPush server. Should review and fix it. ", e);
-            logger.info("HTTP Status: " + e.getStatus());
-            logger.info("Error Message: " + e.getMessage());
+            logger.error("Error response from JPush server. Should review and fix it. " + e.getMessage());
         }
         return null;
     }
@@ -95,9 +93,9 @@ public class JSMSUtils {
 
             }
         } catch (APIConnectionException e) {
-            LogUtil.error(logger, e, "短信发送失败,mobile:{0}", mobile);
+            LogUtil.error(logger, "短信发送失败,mobile:{0},msg:{1}", new Object[]{mobile, e.getMessage()});
         } catch (APIRequestException e) {
-            LogUtil.error(logger, e, "短信发送失败,mobile:{0}", mobile);
+            LogUtil.error(logger, "短信发送失败,mobile:{0},msg:{1}", new Object[]{mobile, e.getMessage()});
         }
         return null;
     }
@@ -140,12 +138,9 @@ public class JSMSUtils {
                 return (new GsonBuilder()).excludeFieldsWithoutExposeAnnotation().create().fromJson("{\"is_valid\":TRUE}", ValidSMSResult.class);
             }
         } catch (APIConnectionException e) {
-            logger.error("Connection error. Should retry later. ", e);
+            logger.error("Connection error. Should retry later. " +  e.getMessage());
         } catch (APIRequestException e) {
             logger.error(e.getStatus() + " errorCode: " + e.getErrorCode() + " " + e.getErrorMessage());
-            logger.error("Error response from JPush server. Should review and fix it. ", e);
-            logger.info("HTTP Status: " + e.getStatus());
-            logger.info("Error Message: " + e.getMessage());
         }
         return null;
     }

+ 138 - 133
mp-quartz/src/main/java/com/qs/mp/quartz/task/CosTask.java

@@ -1,35 +1,26 @@
 package com.qs.mp.quartz.task;
 
-import cn.hutool.core.img.Img;
 import com.qcloud.cos.COSClient;
 import com.qcloud.cos.exception.CosClientException;
 import com.qcloud.cos.exception.CosServiceException;
-import com.qcloud.cos.model.*;
-import com.qs.mp.common.core.domain.AjaxResult;
-import com.qs.mp.common.enums.PicHandlerTypeEnum;
-import com.qs.mp.common.filter.ThumbnailsImgFilter;
+import com.qcloud.cos.model.COSObjectSummary;
+import com.qcloud.cos.model.GetObjectRequest;
+import com.qcloud.cos.model.ListObjectsRequest;
+import com.qcloud.cos.model.ObjectListing;
+import com.qcloud.cos.model.ObjectMetadata;
+import com.qs.mp.common.image.CompressUtil;
 import com.qs.mp.common.qcloud.QcloudFileUtils;
 import com.qs.mp.common.utils.LogUtil;
-import com.qs.mp.common.utils.StringUtils;
-import com.qs.mp.common.utils.UUIDUtils;
-import com.qs.mp.core.domain.LoginUser;
-import com.qs.mp.core.domain.UploadAttachment;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import javax.imageio.ImageIO;
 import net.coobird.thumbnailator.Thumbnails;
-import net.coobird.thumbnailator.geometry.Positions;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.multipart.MultipartFile;
-
-import javax.imageio.ImageIO;
-import java.awt.image.BufferedImage;
-import java.io.*;
-import java.util.ArrayList;
-import java.util.List;
 
 
 /**
@@ -41,127 +32,141 @@ import java.util.List;
 @Component("cosTask")
 public class CosTask {
 
-    protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
-
-
-    /**
-     * 文件上传路径
-     */
-    @Value("${mp.profile}")
-    public String filePath;
-
-    /**
-     * 公开
-     */
-    @Value("${cloud.public-bucket-name}")
-    private String publicBucketName;
-
-
-    /**
-     * 腾讯COS图片压缩替换任务
-     */
-    public void compressPicture() throws IOException, InterruptedException {
-        LogUtil.info(logger, "...图片压缩替换任务开始...");
-
-        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
-        // 设置bucket名称
-        listObjectsRequest.setBucketName(publicBucketName);
-        // prefix表示列出的object的key以prefix开始
-        listObjectsRequest.setPrefix("/");
-        // deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的object
-        listObjectsRequest.setDelimiter("/");
-        // 设置最大遍历出多少个对象, 一次listobject最大支持1000
-        listObjectsRequest.setMaxKeys(500);
-        ObjectListing objectListing = null;
-        do {
-            try {
-                COSClient cosClient = QcloudFileUtils.getCosClient();
-                objectListing = cosClient.listObjects(listObjectsRequest);
-            } catch (CosServiceException e) {
-                e.printStackTrace();
-                return;
-            } catch (CosClientException e) {
-                e.printStackTrace();
-                return;
-            }
-            // common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
-            List<String> commonPrefixs = objectListing.getCommonPrefixes();
-
-            // object summary表示所有列出的object列表
-            List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
-
-            for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
-                // 文件的路径key
-                String key = cosObjectSummary.getKey();
-
-                if (key.length() < 2) {
-                    continue;
-                }
-
-                if ("_s".equals(key.substring(key.length() - 2))) {
-              //  if("EJU6PLW0GUARR4EWIHZA.jpg_s".equals(key)) {
-                    // 获取原图文件名
-                    String picName = key.substring(0, key.length() - 2);
-                    System.err.println("key = " + picName);
-                    try {
-                        uploadAndCommpressImg(picName);
-                    } catch (Exception e) {
-                        System.err.println(e);
-                    }
-                }
-            }
-            String nextMarker = objectListing.getNextMarker();
-            listObjectsRequest.setMarker(nextMarker);
-        } while (objectListing.isTruncated());
-
-        LogUtil.info(logger, "...图片压缩替换完成!!...");
-    }
-
-    private void uploadAndCommpressImg(String key) throws IOException, InterruptedException {
-        String path = filePath + "/thumb/" + key;
-        File downFile = new File(path);
-        GetObjectRequest getObjectRequest = new GetObjectRequest(publicBucketName, key);
+  protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
+
+
+  /**
+   * 文件上传路径
+   */
+  @Value("${mp.profile}")
+  public String filePath;
+
+  /**
+   * 公开
+   */
+  @Value("${cloud.public-bucket-name}")
+  private String publicBucketName;
+
+
+  /**
+   * 腾讯COS图片压缩替换任务
+   */
+  public void compressPicture() throws IOException, InterruptedException {
+    LogUtil.info(logger, "...图片压缩替换任务开始...");
+
+    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
+    // 设置bucket名称
+    listObjectsRequest.setBucketName(publicBucketName);
+    // prefix表示列出的object的key以prefix开始
+    listObjectsRequest.setPrefix("/");
+    // deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的object
+    listObjectsRequest.setDelimiter("/");
+    // 设置最大遍历出多少个对象, 一次listobject最大支持1000
+    listObjectsRequest.setMaxKeys(500);
+    ObjectListing objectListing = null;
+    do {
+      try {
         COSClient cosClient = QcloudFileUtils.getCosClient();
-        ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
-//        FileInputStream fileInputStream = new FileInputStream(path);
-        String mimeType = "image/jpg";
-//        QcloudFileUtils.putStream(fileInputStream, key, publicBucketName, mimeType);
-
-        // 压缩图片
-        BufferedImage bufferedImage = ImageIO.read(downFile);
-        Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
-        int pWidth = bufferedImage.getWidth();
-        int pHeight = bufferedImage.getHeight();
-        builder.size(pWidth, pHeight);
-
-        String outFileDir = filePath + "/thumb";
-        File tempFile = new File(outFileDir);
-        if (!tempFile.exists()) {
-            tempFile.mkdirs();
+        objectListing = cosClient.listObjects(listObjectsRequest);
+      } catch (CosServiceException e) {
+        e.printStackTrace();
+        return;
+      } catch (CosClientException e) {
+        e.printStackTrace();
+        return;
+      }
+      // common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
+      List<String> commonPrefixs = objectListing.getCommonPrefixes();
+
+      // object summary表示所有列出的object列表
+      List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
+
+      for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
+        // 文件的路径key
+        String key = cosObjectSummary.getKey();
+
+        if (key.length() < 2) {
+          continue;
         }
-        String thumbName = key + "_s";
-        String outFilePath = filePath + "/thumb/" + thumbName + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1);
-        builder.toFile(outFilePath);
-
-        // 压缩图片
-        if (pWidth > 500) {
-
-            float scale = 500f / pWidth;
 
-            Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8f).toFile(outFilePath);
-        }else {
-            Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8f).toFile(outFilePath);
+        if ("_s".equals(key.substring(key.length() - 2))) {
+          //  if("EJU6PLW0GUARR4EWIHZA.jpg_s".equals(key)) {
+          // 获取原图文件名
+          String picName = key.substring(0, key.length() - 2);
+          System.err.println("key = " + picName);
+          try {
+            uploadAndCommpressImg(picName);
+          } catch (Exception e) {
+            System.err.println(e);
+          }
         }
+      }
+      String nextMarker = objectListing.getNextMarker();
+      listObjectsRequest.setMarker(nextMarker);
+    } while (objectListing.isTruncated());
+
+    LogUtil.info(logger, "...图片压缩替换完成!!...");
+  }
+
+  private void uploadAndCommpressImg(String key) throws IOException, InterruptedException {
+    String path = filePath + "/thumb/" + key;
+    File downFile = new File(path);
+    GetObjectRequest getObjectRequest = new GetObjectRequest(publicBucketName, key);
+    COSClient cosClient = QcloudFileUtils.getCosClient();
+    ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
+//        FileInputStream fileInputStream = new FileInputStream(path);
+    String contentType = downObjectMeta.getContentType();
+//        QcloudFileUtils.putStream(fileInputStream, key, publicBucketName, mimeType);
 
+    // 压缩图片
+    BufferedImage bufferedImage = ImageIO.read(downFile);
+    Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
+    int pWidth = bufferedImage.getWidth();
+    int pHeight = bufferedImage.getHeight();
+    builder.size(pWidth, pHeight);
+    double accuracy = CompressUtil.getAccuracy(downObjectMeta.getContentLength()/1024);
+    LogUtil.info(logger, "原始文件长:{0},大小:{1},类型:{2},压缩比:{3}", pWidth, downObjectMeta.getContentLength()/1024, contentType, 0.8);
+
+    String outFileDir = filePath + "/thumb";
+    File tempFile = new File(outFileDir);
+    if (!tempFile.exists()) {
+      tempFile.mkdirs();
+    }
+    String thumbName = key + "_s";
+    String outFilePath =
+        filePath + "/thumb/" + thumbName + "." + contentType.substring(contentType.lastIndexOf("/") + 1);
+    builder.toFile(outFilePath);
+
+    // 压缩图片
+    // 压缩图片
+    if (pWidth > 750) {
+      float scale = 750f / pWidth;
+      if ("image/png".equals(contentType)) {
+        Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8)
+            .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
+            .toFile(outFilePath);
+      } else {
+        Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8).toFile(outFilePath);
+      }
+    }else {
+      if ("image/png".equals(contentType)) {
+        Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8)
+            .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
+            .toFile(outFilePath);
+      } else {
+        Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8).toFile(outFilePath);
+      }
+    }
 
-        File outFile = new File(outFilePath);
-        QcloudFileUtils.putFile(outFile, thumbName, publicBucketName, mimeType);
-
-        downFile.delete();
-        outFile.delete();
+    File outFile = new File(outFilePath);
+    QcloudFileUtils.putFile(outFile, thumbName, publicBucketName, contentType);
+    LogUtil.info(logger, "压缩后文件大小:{0}", outFile.length()/ 1024);
+    downFile.delete();
+    outFile.delete();
 
 
-    }
+  }
 
 
 }
+