|
@@ -6,9 +6,13 @@ import com.poteviohealth.cgp.common.model.VaultsResponse;
|
|
|
import com.poteviohealth.cgp.common.utils.CgpTool;
|
|
import com.poteviohealth.cgp.common.utils.CgpTool;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
|
|
@@ -37,6 +41,8 @@ public class OssUtils {
|
|
|
|
|
|
|
|
public VaultsResponse<String> uploadOneFile(MultipartFile dto,Long orderId) {
|
|
public VaultsResponse<String> uploadOneFile(MultipartFile dto,Long orderId) {
|
|
|
// 创建OSSClient实例。
|
|
// 创建OSSClient实例。
|
|
|
|
|
+
|
|
|
|
|
+ log.info("开始上传===");
|
|
|
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
|
|
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, secretAccessKey);
|
|
|
try {
|
|
try {
|
|
|
log.info("fileName==="+dto.getOriginalFilename());
|
|
log.info("fileName==="+dto.getOriginalFilename());
|
|
@@ -46,14 +52,26 @@ public class OssUtils {
|
|
|
if (pos != -1) {
|
|
if (pos != -1) {
|
|
|
suffix = dto.getOriginalFilename().substring(pos);
|
|
suffix = dto.getOriginalFilename().substring(pos);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyMM");
|
|
SimpleDateFormat format = new SimpleDateFormat("yyMM");
|
|
|
String dirStr = dir+format.format(new Date());
|
|
String dirStr = dir+format.format(new Date());
|
|
|
String fileName = dirStr+"/S"+orderId+"N-"+ CgpTool.generateUUID() +suffix;
|
|
String fileName = dirStr+"/S"+orderId+"N-"+ CgpTool.generateUUID() +suffix;
|
|
|
// 创建PutObject请求。
|
|
// 创建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();
|
|
|
|
|
+ outFile.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
ossClient.putObject(bucketName, fileName, dto.getInputStream());
|
|
ossClient.putObject(bucketName, fileName, dto.getInputStream());
|
|
|
|
|
|
|
|
String url = "https://" + bucketName + "." + endPoint + "/" + fileName;
|
|
String url = "https://" + bucketName + "." + endPoint + "/" + fileName;
|
|
|
// System.out.println(url);
|
|
// System.out.println(url);
|
|
|
|
|
+ log.info("结束上传===");
|
|
|
return VaultsResponse.success(url);
|
|
return VaultsResponse.success(url);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -90,4 +108,31 @@ public class OssUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public File compressVideo(File inputFile) {
|
|
|
|
|
+ log.info("视频压缩开始==="+inputFile.getName());
|
|
|
|
|
+ try {
|
|
|
|
|
+
|
|
|
|
|
+ // 创建ProcessBuilder对象
|
|
|
|
|
+ log.info("输入路径==="+inputFile.getAbsolutePath());
|
|
|
|
|
+ // 等待命令执行完成
|
|
|
|
|
+ String outName = "out"+CgpTool.generateUUID()+".mp4";
|
|
|
|
|
+ String command = "ffmpeg -i " + inputFile.getAbsolutePath() + " -r 15 -b:v 600k -s 720x1280 -ss 00:00:00 -t 00:00:15 "+ outName;
|
|
|
|
|
+ log.info("压缩方式==="+command);
|
|
|
|
|
+ ProcessBuilder pb = new ProcessBuilder("ffmpeg","-i",inputFile.getAbsolutePath(),"-r","15","-b:v","600k","-s","720x1280","-ss","00:00:00","-t","00:00:15",outName);
|
|
|
|
|
+ pb.redirectErrorStream(true);
|
|
|
|
|
+ pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
|
|
|
|
+ Process process = pb.start();
|
|
|
|
|
+ process.waitFor();
|
|
|
|
|
+ log.info("视频压缩完成===");
|
|
|
|
|
+ return new File(outName);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } catch (InterruptedException e){
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|