修改媒体文件长度控制

This commit is contained in:
wenc000 2020-05-31 17:49:50 +08:00
parent c6e8d63bc5
commit 49b69fa464
11 changed files with 205 additions and 44 deletions

View File

@ -2,6 +2,7 @@ package com.cm.common.plugin.controller.apis.file;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractController;
import com.cm.common.config.properties.FileProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.enums.UploadTypeEnum;
import com.cm.common.exception.base.SystemException;
@ -34,6 +35,8 @@ public class FileController extends AbstractController {
@Autowired
private IFileService fileService;
@Autowired
private FileProperties fileProperties;
@ApiOperation(value = "上传文件", notes = "上传文件接口")
@ApiImplicitParams({
@ -64,6 +67,9 @@ public class FileController extends AbstractController {
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("uploadvideo")
public SuccessResultData<String> uploadVideo(@RequestParam("video") MultipartFile video) throws SystemException {
if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getBackend() != null) {
fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getBackend().getVideo());
}
Map<String, Object> params = requestParams();
return uploadSingle(video, UploadTypeEnum.VIDEO, params);
}
@ -75,6 +81,9 @@ public class FileController extends AbstractController {
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("uploadaudio")
public SuccessResultData<String> uploadAudio(@RequestParam("audio") MultipartFile audio) throws SystemException {
if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getBackend() != null) {
fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getBackend().getAudio());
}
Map<String, Object> params = requestParams();
return uploadSingle(audio, UploadTypeEnum.AUDIO, params);
}

View File

@ -2,6 +2,7 @@ package com.cm.common.plugin.controller.app.apis.file;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractController;
import com.cm.common.config.properties.FileProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.enums.UploadTypeEnum;
import com.cm.common.exception.base.SystemException;
@ -33,6 +34,8 @@ public class FileAppController extends AbstractController {
@Autowired
private IFileService fileService;
@Autowired
private FileProperties fileProperties;
@ApiOperation(value = "上传文件", notes = "上传文件接口")
@ApiImplicitParams({
@ -66,6 +69,9 @@ public class FileAppController extends AbstractController {
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("uploadvideo")
public SuccessResultData<String> uploadVideo(@RequestHeader("token") String token, @RequestParam("video") MultipartFile video) throws SystemException {
if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) {
fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getApp().getVideo());
}
Map<String, Object> params = requestParams();
return uploadSingle(token, video, UploadTypeEnum.VIDEO, params);
}
@ -78,6 +84,9 @@ public class FileAppController extends AbstractController {
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("uploadaudio")
public SuccessResultData<String> uploadAudio(@RequestHeader("token") String token, @RequestParam("audio") MultipartFile audio) throws SystemException {
if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) {
fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getApp().getAudio());
}
Map<String, Object> params = requestParams();
return uploadSingle(token, audio, UploadTypeEnum.AUDIO, params);
}

View File

@ -1,6 +1,7 @@
package com.cm.common.plugin.controller.wechat.file;
import com.cm.common.base.AbstractController;
import com.cm.common.config.properties.FileProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.enums.UploadTypeEnum;
import com.cm.common.exception.base.SystemException;
@ -34,6 +35,8 @@ public class FileWechatController extends AbstractController {
@Autowired
private IFileService fileService;
@Autowired
private FileProperties fileProperties;
@ApiOperation(value = "上传文件", notes = "上传文件接口")
@ApiImplicitParams({
@ -67,6 +70,9 @@ public class FileWechatController extends AbstractController {
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("uploadvideo")
public SuccessResultData<String> uploadVideo(@RequestHeader("token") String token, @RequestParam("video") MultipartFile video) throws SystemException {
if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getWechat() != null) {
fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getWechat().getVideo());
}
Map<String, Object> params = requestParams();
return uploadSingle(token, video, UploadTypeEnum.VIDEO, params);
}
@ -78,6 +84,9 @@ public class FileWechatController extends AbstractController {
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("uploadaudio")
public SuccessResultData<String> uploadAudio(@RequestHeader("token") String token, @RequestParam("audio") MultipartFile audio) throws SystemException {
if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getWechat() != null) {
fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getWechat().getAudio());
}
Map<String, Object> params = requestParams();
return uploadSingle(token, audio, UploadTypeEnum.AUDIO, params);
}

View File

@ -186,4 +186,23 @@ public interface IFileService {
* @throws SearchException
*/
List<FileDTO> listFileByFileId(List<String> idList) throws SearchException;
/**
* 校验视频长度是否符合
*
* @param uploadFile
* @param maxDuration
* @throws SystemException
*/
void checkVideoDurationAllow(MultipartFile uploadFile, long maxDuration) throws SystemException;
/**
* 校验音频长度是否符合
*
* @param uploadFile
* @param maxDuration
* @throws SystemException
*/
void checkAudioDurationAllow(MultipartFile uploadFile, long maxDuration) throws SystemException;
}

View File

@ -107,13 +107,6 @@ 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);
// 文件保存名称
@ -291,28 +284,20 @@ public class FileServiceImpl extends AbstractService implements IFileService {
return fileDao.getFile(params);
}
/**
* 校验视频长度是否符合
*
* @param uploadFile
*/
private void checkVideoDurationAllow(MultipartFile uploadFile) {
if (fileProperties.getMaxVideoDuration() <= 0) {
@Override
public void checkVideoDurationAllow(MultipartFile uploadFile, long maxDuration) {
if (maxDuration <= 0) {
return;
}
checkDurationAllow(uploadFile, fileProperties.getMaxVideoDuration());
checkDurationAllow(uploadFile, maxDuration, "视频");
}
/**
* 校验音频长度是否符合
*
* @param uploadFile
*/
private void checkAudioDurationAllow(MultipartFile uploadFile) {
if (fileProperties.getMaxAudioDuration() <= 0) {
@Override
public void checkAudioDurationAllow(MultipartFile uploadFile, long maxDuration) {
if (maxDuration <= 0) {
return;
}
checkDurationAllow(uploadFile, fileProperties.getMaxAudioDuration());
checkDurationAllow(uploadFile, maxDuration, "音频");
}
/**
@ -321,7 +306,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
* @param uploadFile
* @param maxDuration
*/
private void checkDurationAllow(MultipartFile uploadFile, long maxDuration) {
private void checkDurationAllow(MultipartFile uploadFile, long maxDuration, String name) {
CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) uploadFile;
DiskFileItem diskFileItem = (DiskFileItem) commonsMultipartFile.getFileItem();
File source = diskFileItem.getStoreLocation();
@ -333,8 +318,8 @@ public class FileServiceImpl extends AbstractService implements IFileService {
throw new SystemException("文件解析错误");
}
long fileDuration = multimediaInfo.getDuration() / 1000;
if (fileDuration > fileProperties.getMaxVideoDuration()) {
throw new FileException("文件时间超过 " + maxDuration + "");
if (fileDuration > maxDuration) {
throw new FileException(name + "时间超过 " + maxDuration + "");
}
}

View File

@ -0,0 +1,14 @@
package com.cm.common.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: FileBackendMediaMaxDurationProperties
* @Description: APP媒体最大时长
* @Author: WangGeng
* @Date: 2020/5/31 16:50
* @Version: 1.0
**/
public class FileMediaAppMaxDurationProperties extends FileMediaBaseMaxDurationProperties {
}

View File

@ -0,0 +1,14 @@
package com.cm.common.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: FileBackendMediaMaxDurationProperties
* @Description: 后台媒体最大时长
* @Author: WangGeng
* @Date: 2020/5/31 16:50
* @Version: 1.0
**/
public class FileMediaBackendMaxDurationProperties extends FileMediaBaseMaxDurationProperties {
}

View File

@ -0,0 +1,44 @@
package com.cm.common.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: FileMaxDurationProperties
* @Description: 媒体最大长度
* @Author: WangGeng
* @Date: 2020/5/31 16:49
* @Version: 1.0
**/
public class FileMediaBaseMaxDurationProperties {
private Long video;
private Long audio;
public Long getVideo() {
return video == null ? 0 : video;
}
public void setVideo(Long video) {
this.video = video;
}
public Long getAudio() {
return audio == null ? 0 : audio;
}
public void setAudio(Long audio) {
this.audio = audio;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"video\":")
.append(video);
sb.append(",\"audio\":")
.append(audio);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,55 @@
package com.cm.common.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: MediaMaxDurationProperties
* @Description: 媒体最大长度
* @Author: WangGeng
* @Date: 2020/5/31 16:54
* @Version: 1.0
**/
public class FileMediaMaxDurationProperties {
private FileMediaBackendMaxDurationProperties backend;
private FileMediaAppMaxDurationProperties app;
private FileMediaWechatMaxDurationProperties wechat;
public FileMediaBackendMaxDurationProperties getBackend() {
return backend;
}
public void setBackend(FileMediaBackendMaxDurationProperties backend) {
this.backend = backend;
}
public FileMediaAppMaxDurationProperties getApp() {
return app;
}
public void setApp(FileMediaAppMaxDurationProperties app) {
this.app = app;
}
public FileMediaWechatMaxDurationProperties getWechat() {
return wechat;
}
public void setWechat(FileMediaWechatMaxDurationProperties wechat) {
this.wechat = wechat;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"backend\":")
.append(backend);
sb.append(",\"app\":")
.append(app);
sb.append(",\"wechat\":")
.append(wechat);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,14 @@
package com.cm.common.config.properties;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: FileBackendMediaMaxDurationProperties
* @Description: 微信媒体最大时长
* @Author: WangGeng
* @Date: 2020/5/31 16:50
* @Version: 1.0
**/
public class FileMediaWechatMaxDurationProperties extends FileMediaBaseMaxDurationProperties {
}

View File

@ -21,8 +21,7 @@ public class FileProperties {
private String fileTypes;
private Integer maxFileCount;
private Double imageOutputQuality;
private Long maxVideoDuration;
private Long maxAudioDuration;
private FileMediaMaxDurationProperties mediaMaxDuration;
public String getUploadPath() {
return uploadPath;
@ -80,20 +79,12 @@ public class FileProperties {
this.imageOutputQuality = imageOutputQuality;
}
public Long getMaxVideoDuration() {
return maxVideoDuration == null ? 0 : maxVideoDuration;
public FileMediaMaxDurationProperties getMediaMaxDuration() {
return mediaMaxDuration;
}
public void setMaxVideoDuration(Long maxVideoDuration) {
this.maxVideoDuration = maxVideoDuration;
}
public Long getMaxAudioDuration() {
return maxAudioDuration == null ? 0 : maxAudioDuration;
}
public void setMaxAudioDuration(Long maxAudioDuration) {
this.maxAudioDuration = maxAudioDuration;
public void setMediaMaxDuration(FileMediaMaxDurationProperties mediaMaxDuration) {
this.mediaMaxDuration = mediaMaxDuration;
}
@Override
@ -113,10 +104,8 @@ public class FileProperties {
.append(maxFileCount);
sb.append(",\"imageOutputQuality\":")
.append(imageOutputQuality);
sb.append(",\"maxVideoDuration\":")
.append(maxVideoDuration);
sb.append(",\"maxAudioDuration\":")
.append(maxAudioDuration);
sb.append(",\"mediaMaxDuration\":")
.append(mediaMaxDuration);
sb.append('}');
return sb.toString();
}