|
@@ -1,179 +0,0 @@
|
|
|
-package com.qs.mp.quartz.task;
|
|
|
-
|
|
|
-import com.qcloud.cos.COSClient;
|
|
|
-import com.qcloud.cos.exception.CosClientException;
|
|
|
-import com.qcloud.cos.exception.CosServiceException;
|
|
|
-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 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 org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-
|
|
|
-/**
|
|
|
- * 腾讯 COS 操作任务
|
|
|
- *
|
|
|
- * @author Cup
|
|
|
- * @date 2022/4/15
|
|
|
- */
|
|
|
-@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);
|
|
|
- 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 > 500) {
|
|
|
- float scale = 500f / pWidth;
|
|
|
- if ("image/png".equals(contentType)) {
|
|
|
- Thumbnails.of(bufferedImage).scale(scale > 0.5 ? 0.5 : scale).outputQuality(0.8)
|
|
|
- .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
|
|
|
- .toFile(outFilePath);
|
|
|
- } else {
|
|
|
- Thumbnails.of(bufferedImage).scale(scale).outputQuality(0.8).toFile(outFilePath);
|
|
|
- }
|
|
|
- }else {
|
|
|
- if ("image/png".equals(contentType)) {
|
|
|
- Thumbnails.of(bufferedImage).scale(0.5).outputQuality(0.8)
|
|
|
- .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
|
|
|
- .toFile(outFilePath);
|
|
|
- } else {
|
|
|
- Thumbnails.of(bufferedImage).scale(1f).outputQuality(0.8).toFile(outFilePath);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- File outFile = new File(outFilePath);
|
|
|
- LogUtil.info(logger, "压缩后文件大小:{0}", outFile.length()/ 1024);
|
|
|
- if (outFile.length() > downObjectMeta.getContentLength()) {
|
|
|
- // 没压缩,则使用原文件作为缩略图
|
|
|
- QcloudFileUtils.putFile(downFile, thumbName, publicBucketName, contentType);
|
|
|
- } else {
|
|
|
- QcloudFileUtils.putFile(outFile, thumbName, publicBucketName, contentType);
|
|
|
- }
|
|
|
-
|
|
|
- downFile.delete();
|
|
|
- outFile.delete();
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-
|