|
@@ -1,13 +1,18 @@
|
|
package com.fdkankan.fusion.service.impl;
|
|
package com.fdkankan.fusion.service.impl;
|
|
|
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
import com.fdkankan.fusion.common.FilePath;
|
|
import com.fdkankan.fusion.common.FilePath;
|
|
import com.fdkankan.fusion.common.ResultCode;
|
|
import com.fdkankan.fusion.common.ResultCode;
|
|
|
|
+import com.fdkankan.fusion.common.util.FileMd5Util;
|
|
import com.fdkankan.fusion.common.util.ShellUtil;
|
|
import com.fdkankan.fusion.common.util.ShellUtil;
|
|
import com.fdkankan.fusion.common.util.UploadToOssUtil;
|
|
import com.fdkankan.fusion.common.util.UploadToOssUtil;
|
|
import com.fdkankan.fusion.common.util.VideoUtil;
|
|
import com.fdkankan.fusion.common.util.VideoUtil;
|
|
|
|
+import com.fdkankan.fusion.entity.UploadFile;
|
|
import com.fdkankan.fusion.exception.BusinessException;
|
|
import com.fdkankan.fusion.exception.BusinessException;
|
|
|
|
+import com.fdkankan.fusion.service.IUploadFileService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -31,6 +36,8 @@ public class UploadService {
|
|
private String queryPath;
|
|
private String queryPath;
|
|
@Value("${spring.profiles.active}")
|
|
@Value("${spring.profiles.active}")
|
|
private String environment;
|
|
private String environment;
|
|
|
|
+ @Autowired
|
|
|
|
+ IUploadFileService uploadFileService;
|
|
|
|
|
|
public String uploadFile(MultipartFile file, Integer videoFolderId) {
|
|
public String uploadFile(MultipartFile file, Integer videoFolderId) {
|
|
if(file.isEmpty()){
|
|
if(file.isEmpty()){
|
|
@@ -72,14 +79,19 @@ public class UploadService {
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
}
|
|
}
|
|
if(file.getSize()>10 * 1024 * 1024 * 100){
|
|
if(file.getSize()>10 * 1024 * 1024 * 100){
|
|
- System.out.println(file.getSize());
|
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
|
|
}
|
|
}
|
|
//获取文件名
|
|
//获取文件名
|
|
String fileName = file.getOriginalFilename();
|
|
String fileName = file.getOriginalFilename();
|
|
|
|
+ String fileMD5 = FileMd5Util.getFileMD5(file);
|
|
|
|
+ UploadFile uploadFile = uploadFileService.getByMd5(fileMD5);
|
|
|
|
+ if(uploadFile != null){
|
|
|
|
+ return uploadFile.getFileUrl();
|
|
|
|
+ }
|
|
if(StringUtils.isEmpty(fileName)){
|
|
if(StringUtils.isEmpty(fileName)){
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
}
|
|
}
|
|
|
|
+ String oldName = fileName.substring(0,fileName.lastIndexOf("."));
|
|
File localFile = null;
|
|
File localFile = null;
|
|
try {
|
|
try {
|
|
//获取文件后缀名
|
|
//获取文件后缀名
|
|
@@ -95,12 +107,15 @@ public class UploadService {
|
|
}
|
|
}
|
|
localFile = File.createTempFile(fileName + suffixName,suffixName);
|
|
localFile = File.createTempFile(fileName + suffixName,suffixName);
|
|
file.transferTo(localFile);
|
|
file.transferTo(localFile);
|
|
|
|
+
|
|
String path = localFile.getPath();
|
|
String path = localFile.getPath();
|
|
uploadToOssUtil.uploadOss(path,filePathAdd+ fileName + suffixName);
|
|
uploadToOssUtil.uploadOss(path,filePathAdd+ fileName + suffixName);
|
|
if(!uploadToOssUtil.existKey(filePathAdd + fileName + suffixName)){
|
|
if(!uploadToOssUtil.existKey(filePathAdd + fileName + suffixName)){
|
|
throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
|
|
throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
|
|
}
|
|
}
|
|
- return queryPath +filePathAdd+ URLEncoder.encode(fileName,"utf-8") + suffixName;
|
|
|
|
|
|
+ String url = queryPath +filePathAdd+ URLEncoder.encode(fileName,"utf-8") + suffixName;
|
|
|
|
+ uploadFileService.addFile(oldName,url,localFile.length(), fileMD5);
|
|
|
|
+ return url;
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
log.info("upload-error:{}",e);
|
|
log.info("upload-error:{}",e);
|
|
throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
|
|
throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
|