From c6e8d63bc5f70ea5adee7015576d8820869320a8 Mon Sep 17 00:00:00 2001
From: wenc000 <450292408@qq.com>
Date: Sun, 31 May 2020 15:04:04 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=9F=B3=E3=80=81=E8=A7=86?=
=?UTF-8?q?=E9=A2=91=E9=95=BF=E5=BA=A6=E6=A0=A1=E9=AA=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
cloud-common-plugin/pom.xml | 6 ++
.../cm/common/plugin/config/PluginConfig.java | 28 +++++++++
.../service/file/impl/FileServiceImpl.java | 58 +++++++++++++++++++
cloud-common/pom.xml | 1 -
.../config/properties/FileProperties.java | 44 +++++++++++++-
pom.xml | 9 +++
6 files changed, 144 insertions(+), 2 deletions(-)
create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/config/PluginConfig.java
diff --git a/cloud-common-plugin/pom.xml b/cloud-common-plugin/pom.xml
index 0000ee1..a06d6a2 100644
--- a/cloud-common-plugin/pom.xml
+++ b/cloud-common-plugin/pom.xml
@@ -18,6 +18,12 @@
cloud-common
1.0.1-SNAPSHOT
+
+
+ it.sauronsoftware
+ jave
+
+
\ No newline at end of file
diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/config/PluginConfig.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/config/PluginConfig.java
new file mode 100644
index 0000000..7f74d71
--- /dev/null
+++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/config/PluginConfig.java
@@ -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();
+ }
+
+}
diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java
index 80ddc3f..a32050a 100644
--- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java
+++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java
@@ -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文件列表
diff --git a/cloud-common/pom.xml b/cloud-common/pom.xml
index 07ebdb1..dab292b 100644
--- a/cloud-common/pom.xml
+++ b/cloud-common/pom.xml
@@ -242,7 +242,6 @@
pinyin4j
-
org.springframework.boot
spring-boot-starter-security
diff --git a/cloud-common/src/main/java/com/cm/common/config/properties/FileProperties.java b/cloud-common/src/main/java/com/cm/common/config/properties/FileProperties.java
index 54e6edd..8b47acd 100644
--- a/cloud-common/src/main/java/com/cm/common/config/properties/FileProperties.java
+++ b/cloud-common/src/main/java/com/cm/common/config/properties/FileProperties.java
@@ -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();
+ }
}
diff --git a/pom.xml b/pom.xml
index 54a8b7f..22292b6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -61,6 +61,7 @@
2.5.0
3.3.3
2.5.1
+ 1.0.2
@@ -368,6 +369,14 @@
spring-security-jwt
1.0.9.RELEASE
+
+
+
+ it.sauronsoftware
+ jave
+ ${sauronsoftware.version}
+
+