From 49b69fa464ea0deed5136391a59c0cb769e30ab6 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Sun, 31 May 2020 17:49:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AA=92=E4=BD=93=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=95=BF=E5=BA=A6=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/apis/file/FileController.java | 9 +++ .../app/apis/file/FileAppController.java | 9 +++ .../wechat/file/FileWechatController.java | 9 +++ .../plugin/service/file/IFileService.java | 19 +++++++ .../service/file/impl/FileServiceImpl.java | 37 ++++--------- .../FileMediaAppMaxDurationProperties.java | 14 +++++ ...FileMediaBackendMaxDurationProperties.java | 14 +++++ .../FileMediaBaseMaxDurationProperties.java | 44 +++++++++++++++ .../FileMediaMaxDurationProperties.java | 55 +++++++++++++++++++ .../FileMediaWechatMaxDurationProperties.java | 14 +++++ .../config/properties/FileProperties.java | 25 +++------ 11 files changed, 205 insertions(+), 44 deletions(-) create mode 100644 cloud-common/src/main/java/com/cm/common/config/properties/FileMediaAppMaxDurationProperties.java create mode 100644 cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBackendMaxDurationProperties.java create mode 100644 cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBaseMaxDurationProperties.java create mode 100644 cloud-common/src/main/java/com/cm/common/config/properties/FileMediaMaxDurationProperties.java create mode 100644 cloud-common/src/main/java/com/cm/common/config/properties/FileMediaWechatMaxDurationProperties.java diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/file/FileController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/file/FileController.java index 86c3f41..d7cb136 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/file/FileController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/file/FileController.java @@ -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 uploadVideo(@RequestParam("video") MultipartFile video) throws SystemException { + if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getBackend() != null) { + fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getBackend().getVideo()); + } Map 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 uploadAudio(@RequestParam("audio") MultipartFile audio) throws SystemException { + if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getBackend() != null) { + fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getBackend().getAudio()); + } Map params = requestParams(); return uploadSingle(audio, UploadTypeEnum.AUDIO, params); } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/app/apis/file/FileAppController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/app/apis/file/FileAppController.java index 8108a57..6713394 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/app/apis/file/FileAppController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/app/apis/file/FileAppController.java @@ -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 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 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 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 params = requestParams(); return uploadSingle(token, audio, UploadTypeEnum.AUDIO, params); } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/wechat/file/FileWechatController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/wechat/file/FileWechatController.java index ae05b9a..325ef91 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/wechat/file/FileWechatController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/wechat/file/FileWechatController.java @@ -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 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 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 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 params = requestParams(); return uploadSingle(token, audio, UploadTypeEnum.AUDIO, params); } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java index c90283d..01df96a 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java @@ -186,4 +186,23 @@ public interface IFileService { * @throws SearchException */ List listFileByFileId(List 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; + } 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 a32050a..c4824ff 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 @@ -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 + " 秒"); } } diff --git a/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaAppMaxDurationProperties.java b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaAppMaxDurationProperties.java new file mode 100644 index 0000000..7616585 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaAppMaxDurationProperties.java @@ -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 { +} diff --git a/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBackendMaxDurationProperties.java b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBackendMaxDurationProperties.java new file mode 100644 index 0000000..735d0c7 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBackendMaxDurationProperties.java @@ -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 { +} diff --git a/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBaseMaxDurationProperties.java b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBaseMaxDurationProperties.java new file mode 100644 index 0000000..514287a --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaBaseMaxDurationProperties.java @@ -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(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaMaxDurationProperties.java b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaMaxDurationProperties.java new file mode 100644 index 0000000..9e3288f --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaMaxDurationProperties.java @@ -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(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaWechatMaxDurationProperties.java b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaWechatMaxDurationProperties.java new file mode 100644 index 0000000..3abdb33 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/config/properties/FileMediaWechatMaxDurationProperties.java @@ -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 { +} 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 8b47acd..5d0ca26 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,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(); }