浏览代码

图片查重

qin 1 年之前
父节点
当前提交
86b647d811
共有 1 个文件被更改,包括 18 次插入9 次删除
  1. 18 9
      src/main/java/com/poteviohealth/cgp/statistics/utils/OssUtils.java

+ 18 - 9
src/main/java/com/poteviohealth/cgp/statistics/utils/OssUtils.java

@@ -6,13 +6,13 @@ import com.poteviohealth.cgp.common.model.VaultsResponse;
 import com.poteviohealth.cgp.common.utils.CgpTool;
 import lombok.extern.log4j.Log4j2;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.mock.web.MockMultipartFile;
 import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 
@@ -38,8 +38,20 @@ public class OssUtils {
 
     @Value("${aliyun.oss.dir}")
     private String dir;
+    public VaultsResponse<String> uploadOneFile(MultipartFile dto, Long orderId) {
+        try {
+            return uploadOneFile(dto,orderId,null,dto.getInputStream());
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    public VaultsResponse<String> uploadOneFile(MultipartFile dto, Long orderId, InputStream inputStream) {
+            return uploadOneFile(dto,orderId,null,inputStream);
+    }
 
-    public VaultsResponse<String> uploadOneFile(MultipartFile dto,Long orderId) {
+    public VaultsResponse<String> uploadOneFile(MultipartFile dto, Long orderId,File file, InputStream inputStream) {
         // 创建OSSClient实例。
 
         log.info("开始上传===");
@@ -58,16 +70,13 @@ public class OssUtils {
                 String fileName = dirStr+"/S"+orderId+"N-"+ CgpTool.generateUUID() +suffix;
                 // 创建PutObject请求。
             log.info("Size=="+dto.getSize()/1024L/1024L);
-            if(suffix.indexOf("mp4") != -1 && (dto.getSize() /1024L/1024L >3L)){
-                File tempFile = File.createTempFile("temp_", ".mp4");
-                dto.transferTo(tempFile);
-                File outFile =  this.compressVideo(tempFile);
-                dto = new MockMultipartFile(outFile.getName(), new FileInputStream(outFile));
-                tempFile.delete();
+            if(suffix.indexOf("mp4") != -1 && (dto.getSize() /1024L/1024L >3L) && file != null){
+                File outFile =  this.compressVideo(file);
+                inputStream = new FileInputStream(outFile);
                 outFile.delete();
             }
 
-                ossClient.putObject(bucketName, fileName, dto.getInputStream());
+                ossClient.putObject(bucketName, fileName, inputStream);
 
                 String url = "https://" + bucketName + "." + endPoint + "/" + fileName;
                 // System.out.println(url);