新增音、视频长度校验

This commit is contained in:
wenc000 2020-05-31 15:04:04 +08:00
parent 54565ed1e5
commit c6e8d63bc5
6 changed files with 144 additions and 2 deletions

View File

@ -18,6 +18,12 @@
<artifactId>cloud-common</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
<!-- sauronsoftware start -->
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>jave</artifactId>
</dependency>
<!-- sauronsoftware end -->
</dependencies>
</project>

View File

@ -0,0 +1,28 @@
package com.cm.common.plugin.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: PluginConfig
* @Description: 插件配置
* @Author: WangGeng
* @Date: 2020/5/31 14:15
* @Version: 1.0
**/
@Configuration
public class PluginConfig {
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getCommonsMultipartResolver() {
return new CommonsMultipartResolver();
}
}

View File

@ -21,13 +21,18 @@ import com.cm.common.utils.ResourceUtil;
import com.cm.common.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -102,6 +107,13 @@ public class FileServiceImpl extends AbstractService implements IFileService {
long fileSize = uploadFile.getSize();
// 文件类型
String fileType = getFileType(fileName);
// 判断视频的时长是否满足要求
if (uploadTypeEnum.equals(UploadTypeEnum.VIDEO)) {
checkVideoDurationAllow(uploadFile);
}
if (uploadTypeEnum.equals(UploadTypeEnum.AUDIO)) {
checkAudioDurationAllow(uploadFile);
}
// 文件保存路径
String uploadPath = getUploadPath(baseUploadPath, uploadTypeEnum, fileType);
// 文件保存名称
@ -279,6 +291,52 @@ public class FileServiceImpl extends AbstractService implements IFileService {
return fileDao.getFile(params);
}
/**
* 校验视频长度是否符合
*
* @param uploadFile
*/
private void checkVideoDurationAllow(MultipartFile uploadFile) {
if (fileProperties.getMaxVideoDuration() <= 0) {
return;
}
checkDurationAllow(uploadFile, fileProperties.getMaxVideoDuration());
}
/**
* 校验音频长度是否符合
*
* @param uploadFile
*/
private void checkAudioDurationAllow(MultipartFile uploadFile) {
if (fileProperties.getMaxAudioDuration() <= 0) {
return;
}
checkDurationAllow(uploadFile, fileProperties.getMaxAudioDuration());
}
/**
* 校验音视频长度是否符合
*
* @param uploadFile
* @param maxDuration
*/
private void checkDurationAllow(MultipartFile uploadFile, long maxDuration) {
CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) uploadFile;
DiskFileItem diskFileItem = (DiskFileItem) commonsMultipartFile.getFileItem();
File source = diskFileItem.getStoreLocation();
Encoder encoder = new Encoder();
MultimediaInfo multimediaInfo;
try {
multimediaInfo = encoder.getInfo(source);
} catch (EncoderException e) {
throw new SystemException("文件解析错误");
}
long fileDuration = multimediaInfo.getDuration() / 1000;
if (fileDuration > fileProperties.getMaxVideoDuration()) {
throw new FileException("文件时间超过 " + maxDuration + "");
}
}
/**
* uEditor文件列表

View File

@ -242,7 +242,6 @@
<artifactId>pinyin4j</artifactId>
</dependency>
<!-- pingyin end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>

View File

@ -21,7 +21,8 @@ public class FileProperties {
private String fileTypes;
private Integer maxFileCount;
private Double imageOutputQuality;
private Long maxVideoDuration;
private Long maxAudioDuration;
public String getUploadPath() {
return uploadPath;
@ -78,4 +79,45 @@ public class FileProperties {
public void setImageOutputQuality(Double imageOutputQuality) {
this.imageOutputQuality = imageOutputQuality;
}
public Long getMaxVideoDuration() {
return maxVideoDuration == null ? 0 : maxVideoDuration;
}
public void setMaxVideoDuration(Long maxVideoDuration) {
this.maxVideoDuration = maxVideoDuration;
}
public Long getMaxAudioDuration() {
return maxAudioDuration == null ? 0 : maxAudioDuration;
}
public void setMaxAudioDuration(Long maxAudioDuration) {
this.maxAudioDuration = maxAudioDuration;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"uploadPath\":\"")
.append(uploadPath).append('\"');
sb.append(",\"imageTypes\":\"")
.append(imageTypes).append('\"');
sb.append(",\"videoTypes\":\"")
.append(videoTypes).append('\"');
sb.append(",\"audioTypes\":\"")
.append(audioTypes).append('\"');
sb.append(",\"fileTypes\":\"")
.append(fileTypes).append('\"');
sb.append(",\"maxFileCount\":")
.append(maxFileCount);
sb.append(",\"imageOutputQuality\":")
.append(imageOutputQuality);
sb.append(",\"maxVideoDuration\":")
.append(maxVideoDuration);
sb.append(",\"maxAudioDuration\":")
.append(maxAudioDuration);
sb.append('}');
return sb.toString();
}
}

View File

@ -61,6 +61,7 @@
<swagger.version>2.5.0</swagger.version>
<zxing.version>3.3.3</zxing.version>
<pingyin4j.version>2.5.1</pingyin4j.version>
<sauronsoftware.version>1.0.2</sauronsoftware.version>
</properties>
<!-- 依赖管理 -->
@ -368,6 +369,14 @@
<artifactId>spring-security-jwt</artifactId>
<version>1.0.9.RELEASE</version>
</dependency>
<!-- sauronsoftware start -->
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>jave</artifactId>
<version>${sauronsoftware.version}</version>
</dependency>
<!-- sauronsoftware end -->
</dependencies>
</dependencyManagement>