记得上下班打卡 | git大法好,push需谨慎

Commit 17c04adc authored by 胡佳晨's avatar 胡佳晨

Merge remote-tracking branch 'origin/jxl_0303_upload_appraisal' into dev_performance_paytxt

parents d1f58e82 59a3d15c
...@@ -27,14 +27,11 @@ public class FilesUtils { ...@@ -27,14 +27,11 @@ public class FilesUtils {
*/ */
public static File multipartFileToFile(MultipartFile file) { public static File multipartFileToFile(MultipartFile file) {
try { try {
File toFile = null; InputStream ins = null;
if (!file.equals("") && file.getSize() > 0) { ins = file.getInputStream();
InputStream ins = null; File toFile = new File(file.getOriginalFilename());
ins = file.getInputStream(); inputStreamToFile(ins, toFile);
toFile = new File(file.getOriginalFilename()); ins.close();
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile; return toFile;
} catch (Exception e) { } catch (Exception e) {
log.error("multipartFileToFileError", e); log.error("multipartFileToFileError", e);
......
...@@ -115,6 +115,11 @@ ...@@ -115,6 +115,11 @@
<artifactId>liquidnet-api-feign-stone</artifactId> <artifactId>liquidnet-api-feign-stone</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>com.liquidnet</groupId>
<artifactId>liquidnet-common-third-shumei</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.liquidnet.service.platform.controller.basicServices; package com.liquidnet.service.platform.controller.basicServices;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS; import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.BinaryUtil; import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.GenericResult; import com.aliyun.oss.model.GenericResult;
import com.aliyun.oss.model.ProcessObjectRequest; import com.aliyun.oss.model.ProcessObjectRequest;
import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.common.third.shumei.util.ShumeiUtil;
import com.liquidnet.commons.lang.util.DateUtil; import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.commons.lang.util.FilesUtils; import com.liquidnet.commons.lang.util.FilesUtils;
import com.liquidnet.commons.lang.util.IDGenerator; import com.liquidnet.commons.lang.util.IDGenerator;
...@@ -19,6 +22,7 @@ import io.swagger.annotations.Api; ...@@ -19,6 +22,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -29,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -29,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -45,6 +50,7 @@ import java.util.UUID; ...@@ -45,6 +50,7 @@ import java.util.UUID;
* @since 2021-07-09 * @since 2021-07-09
*/ */
@Api(tags = "basicServices") @Api(tags = "basicServices")
@Slf4j
@RestController @RestController
@RequestMapping("basicServices/alOss") @RequestMapping("basicServices/alOss")
public class AlOssController { public class AlOssController {
...@@ -61,6 +67,9 @@ public class AlOssController { ...@@ -61,6 +67,9 @@ public class AlOssController {
@Autowired @Autowired
private PlatformOssFilesMapper platformOssFilesMapper; private PlatformOssFilesMapper platformOssFilesMapper;
@Autowired
private ShumeiUtil shumeiUtil;
@PostMapping("/upload") @PostMapping("/upload")
@ApiOperation("阿里云上传") @ApiOperation("阿里云上传")
@ApiImplicitParams({ @ApiImplicitParams({
...@@ -77,24 +86,34 @@ public class AlOssController { ...@@ -77,24 +86,34 @@ public class AlOssController {
@RequestParam(defaultValue = "0", required = false) int resize, @RequestParam(defaultValue = "0", required = false) int resize,
@RequestParam(defaultValue = "0", required = false) int isCutFrame @RequestParam(defaultValue = "0", required = false) int isCutFrame
) { ) {
if (file.equals("") || file.getSize() <= 0) {
return ResponseDto.failure("不能上传大小为0的文件");
}
File fileNew = FilesUtils.multipartFileToFile(file); File fileNew = FilesUtils.multipartFileToFile(file);
if (null == fileNew) { if (null == fileNew) {
return ResponseDto.failure("上传失败"); return ResponseDto.failure("multipartFileToFile Error");
} }
// 是否上传过 有直接返回 // 是否上传过 有直接返回
PlatformOssFiles ossFile = this.getOldOssFile(fileNew); PlatformOssFiles ossFile = this.getOldOssFile(fileNew);
UploadVo uploadVo = new UploadVo(); UploadVo uploadVo = new UploadVo();
if (null == ossFile) { try {
// 上传 if (null == ossFile || (isCutFrame > 0 && ossFile.getVideoImg().isEmpty())) { //保证上传过的文件也能截帧
ossFile = this.uploadOssFile(file, pathName, buckType, resize, fileNew, isCutFrame); // 上传
ossFile = this.uploadOssFile(file, pathName, buckType, resize, fileNew, isCutFrame);
}
} finally {
// 删除临时文件 因为老文件也生成了所以也要删除
FilesUtils.deleteTempFile(fileNew);
} }
BeanUtils.copyProperties(ossFile, uploadVo);
// 删除临时文件 因为老文件也生成了所以也要删除
FilesUtils.deleteTempFile(fileNew);
return ResponseDto.success(uploadVo); if (null == ossFile) {
return ResponseDto.failure("上传失败,该文件不合规,请勿上传带有联系方式、色情、涉政、广告等敏感文件");
} else {
BeanUtils.copyProperties(ossFile, uploadVo);
return ResponseDto.success(uploadVo);
}
} }
/** /**
...@@ -108,6 +127,7 @@ public class AlOssController { ...@@ -108,6 +127,7 @@ public class AlOssController {
List<PlatformOssFiles> platformOssFiles = platformOssFilesMapper.selectList( List<PlatformOssFiles> platformOssFiles = platformOssFilesMapper.selectList(
Wrappers.lambdaQuery(PlatformOssFiles.class) Wrappers.lambdaQuery(PlatformOssFiles.class)
.eq(PlatformOssFiles::getMd5str, fileMD5) .eq(PlatformOssFiles::getMd5str, fileMD5)
.orderByDesc(PlatformOssFiles::getMid)
); );
if (CollectionUtils.isEmpty(platformOssFiles)) { if (CollectionUtils.isEmpty(platformOssFiles)) {
return null; return null;
...@@ -128,38 +148,49 @@ public class AlOssController { ...@@ -128,38 +148,49 @@ public class AlOssController {
*/ */
private PlatformOssFiles uploadOssFile(MultipartFile file, String pathName, int buckType, int resize, File fileNew, int isCutFrame) { private PlatformOssFiles uploadOssFile(MultipartFile file, String pathName, int buckType, int resize, File fileNew, int isCutFrame) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String filename = "";
String contentType = "";
int size = 0; int size = 0;
if (null == file) { String filename, contentType, uploadPath = "";
filename = fileNew.getName(); PlatformOssFiles ossFileVideoImg = null;
Path fileNewPath = new File(fileNew.getPath()).toPath(); try {
try { if (null == file) {
filename = fileNew.getName();
Path fileNewPath = new File(fileNew.getPath()).toPath();
contentType = Files.probeContentType(fileNewPath); contentType = Files.probeContentType(fileNewPath);
} catch (Exception e) { size = (int) fileNew.length();
} else {
filename = file.getResource().getFilename();
contentType = file.getContentType();
size = (int) file.getSize();
}
uploadPath = FilesUtils.getUploadPath(pathName, filename);
String bucketName = FilesUtils.getBucketName(buckType);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, uploadPath, fileNew);
ossClient.putObject(putObjectRequest);
if (contentType.contains("image") && !contentType.equals("image/vnd.adobe.photoshop")) {
if (!shumeiUtil.checkImage(IDGenerator.nextSnowId(), imgUrl.concat(uploadPath))) {
ossClient.deleteObject(bucketName, uploadPath);
return null;
}
} }
size = (int) fileNew.length();
} else {
filename = file.getResource().getFilename();
contentType = file.getContentType();
size = (int) file.getSize();
}
String uploadPath = FilesUtils.getUploadPath(pathName, filename);
String bucketName = FilesUtils.getBucketName(buckType);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, uploadPath, fileNew);
ossClient.putObject(putObjectRequest);
if (resize > 0) { // 裁切 if (resize > 0) { // 裁切
this.ossFileResize(resize, uploadPath, bucketName, ossClient); this.ossFileResize(resize, uploadPath, bucketName, ossClient);
} }
PlatformOssFiles ossFileVideoImg = null; if (isCutFrame > 0 && contentType.contains("video")) { // 截取帧数
if (isCutFrame > 0) { // 截取帧数 ossFileVideoImg = this.ossFileCutFrame(uploadPath, pathName, buckType);
ossFileVideoImg = this.ossFileCutFrame(uploadPath, pathName, buckType); }
}
ossClient.shutdown(); ossClient.shutdown();
} catch (OSSException | ClientException | IOException e) {
log.error("alOssUploadEx [e:{}]", e);
return null;
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
// 入库 // 入库
PlatformOssFiles platformOssFilesDate = new PlatformOssFiles(); PlatformOssFiles platformOssFilesDate = new PlatformOssFiles();
......
...@@ -16,4 +16,4 @@ liquidnet: ...@@ -16,4 +16,4 @@ liquidnet:
spring: spring:
profiles: profiles:
include: service-platform include: service-platform
\ No newline at end of file
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