Commit fe3fbb34 authored by yaobaizheng's avatar yaobaizheng

图片压缩异步删除

parent a1b1de7a
package com.lyy.admin.moudle.system.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.lyy.admin.common.utils.hwobs.ObsUtils;
......@@ -18,6 +20,7 @@ import java.io.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@Service
public class UploadServiceImpl implements UploadService {
......@@ -29,6 +32,8 @@ public class UploadServiceImpl implements UploadService {
@Value("${lyy.tempFilePath}")
String tempFilePath;
@Value("${lyy.imgSize}")
Integer imgSize;
@Resource
private ObsUtils obsUtils;
......@@ -46,31 +51,60 @@ public class UploadServiceImpl implements UploadService {
String fileOriginalName = files.getOriginalFilename();
String fileLastType = fileOriginalName.substring(files.getOriginalFilename().lastIndexOf("."));
String date = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd");
String filePath ="";
String newFile = IdUtil.simpleUUID();
String newFilename = newFile + fileLastType;
String filePath = "/" + date + "/" + newFile + ".jpg" ;
//将MultipartFile转为File,存储为本地临时文件
String localDir = tempFilePath + newFilename;
File file = new File(localDir);
InputStream inputStream = files.getInputStream();
FileUtils.copyInputStreamToFile(inputStream, file);
String newFilename1 = IdUtil.simpleUUID() + ".jpg";
String localDir1 = tempFilePath + newFilename1;
File file1 = FileUtil.file(localDir1);
Img.from(file).setQuality(1).write(file1);
InputStream inputStream1 = new FileInputStream(localDir1);
obsUtils.ObsUpload(bucketName, rootPath + filePath, inputStream1);
//删除临时文件
FileUtils.delete(file);
FileUtils.delete(file1);
if(files.getSize()/(1024*1024) <= imgSize ){
String newFilename = IdUtil.simpleUUID() + fileLastType;
filePath = "/" + date + "/" + newFilename;
InputStream inputStream = files.getInputStream();
obsUtils.ObsUpload(bucketName, rootPath + filePath, inputStream);
inputStream.close();
}else{
String newFile = IdUtil.simpleUUID();
String newFilename = newFile + fileLastType;
//将MultipartFile转为File,存储为本地临时文件
String localDir = tempFilePath + newFilename;
File file = new File(localDir);
InputStream inputStream = files.getInputStream();
FileUtils.copyInputStreamToFile(inputStream, file);
String newFilename1 = IdUtil.simpleUUID() + ".jpg";
String localDir1 = tempFilePath + newFilename1;
File file1 = FileUtil.file(localDir1);
TimeInterval timer = DateUtil.timer();
Img.from(file).setQuality(1).write(file1);
System.out.println(timer.interval());
InputStream inputStream1 = new FileInputStream(localDir1);
filePath = "/" + date + "/" + newFile + ".jpg" ;
obsUtils.ObsUpload(bucketName, rootPath + filePath, inputStream1);
//删除临时文件
CompletableFuture.runAsync(()-> {
try {
FileUtils.delete(file);
FileUtils.delete(file1);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
inputStream.close();
inputStream1.close();
}
return rootPath + filePath;
}
public static void main(String[] args) {
TimeInterval timer = DateUtil.timer();
ImgUtil.scale(
FileUtil.file("C:\\Users\\26996\\Pictures\\Screenshots\\1704418796(1).jpg"),
FileUtil.file("C:\\Users\\26996\\Pictures\\Screenshots\\170441879611111.jpg"),
1f//缩放比例
);
System.out.println(timer.interval());
}
@Override
public String uploadFile(MultipartFile files,String rootPath) throws Exception {
//华为obs
......
......@@ -60,6 +60,8 @@ lyy:
profile: /home/liyeyun/uploadPath
# 临时文件路径
tempFilePath: /home/liyeyun/uploadPath/temp/
# 文件压缩阈值 (单位 M)
imgSize: 1
# 华为OBS文件路径 示例( https://img-t.knowingindustry.com)
# obsPath: http://img-t.knowingindustry.com
obsPath: https://cfld-hw-obs-01.obs.cn-north-4.myhuaweicloud.com
......
......@@ -110,7 +110,7 @@ anti:
#是否启用反爬虫插件
enabled: false
#局部拦截时,需要反爬的接口列表,以','分隔,支持正则匹配。全局拦截模式下无需配置
include-urls: ^${server.servlet.context-path}/.*$
include-urls: ^${server.servlet.context-path}/.*$,^/api/liyeren/.*$
#是否启用 IP Rule
ip-rule:
enabled: true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment