diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/app/file/FileAppController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/app/file/FileAppController.java new file mode 100644 index 0000000..f6ee050 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/app/file/FileAppController.java @@ -0,0 +1,92 @@ +package com.cm.common.plugin.controller.app.file; + +import com.cm.common.base.AbstractController; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.enums.UploadTypeEnum; +import com.cm.common.exception.base.SystemException; +import com.cm.common.plugin.service.file.IFileService; +import com.cm.common.result.SuccessResultData; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: FileAppController + * @Description: APP文件上传 + * @Author: WangGeng + * @Date: 2019-08-02 15:38 + * @Version: 1.0 + **/ +@RestController +@RequestMapping(ISystemConstant.APP_PREFIX + "/file") +public class FileAppController extends AbstractController { + + @Autowired + private IFileService fileService; + + /** + * 上传文件 + * + * @param file + * @return + */ + @PostMapping("uploadfile") + public SuccessResultData uploadFile(@RequestHeader("token") String token, @RequestParam("file") MultipartFile file) throws SystemException { + Map params = requestParams(); + return uploadSingle(token, file, UploadTypeEnum.FILE, params); + } + + /** + * 上传图片 + * + * @param image + * @return + */ + @PostMapping("uploadimage") + public SuccessResultData uploadImage(@RequestHeader("token") String token, @RequestParam("image") MultipartFile image) throws SystemException { + Map params = requestParams(); + return uploadSingle(token, image, UploadTypeEnum.IMAGE, params); + } + + /** + * 上传视频 + * + * @param video + * @return + */ + @PostMapping("uploadvideo") + public SuccessResultData uploadVideo(@RequestHeader("token") String token, @RequestParam("video") MultipartFile video) throws SystemException { + Map params = requestParams(); + return uploadSingle(token, video, UploadTypeEnum.VIDEO, params); + } + + /** + * 上传音频 + * + * @param audio + * @return + * @throws SystemException + */ + @PostMapping("uploadaudio") + public SuccessResultData uploadAudio(@RequestHeader("token") String token, @RequestParam("audio") MultipartFile audio) throws SystemException { + Map params = requestParams(); + return uploadSingle(token, audio, UploadTypeEnum.AUDIO, params); + } + + /** + * 上传文件 + * + * @param uploadFile + * @param params + * @return + */ + private SuccessResultData uploadSingle(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException { + return fileService.uploadSingle(token, uploadFile, uploadTypeEnum, 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 e88aa21..8a11141 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 @@ -87,13 +87,26 @@ public interface IFileService { /** * 文件上传 * + * @param token + * @param uploadFile + * @param uploadTypeEnum + * @param params + * @return + * @throws SystemException + * @throws IOException + */ + SuccessResultData uploadSingle(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException; + + /** + * 文件上传 + * @param token * @param uploadFile * @param uploadTypeEnum * @param params * @return * @throws SystemException */ - FileDTO uploadSingleForFileDTO(MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException; + FileDTO uploadSingleForFileDTO(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException; /** * 文件下载 @@ -124,4 +137,6 @@ public interface IFileService { * @throws SearchException */ FilePO getFile(Map params) throws SearchException; + + } 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 a4ab2aa..0a4089a 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 @@ -58,13 +58,19 @@ public class FileServiceImpl extends AbstractService implements IFileService { @Override public SuccessResultData uploadSingle(MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException { - uploadFile(uploadFile, uploadTypeEnum, params); + uploadFile(null, uploadFile, uploadTypeEnum, params); return new SuccessResultData<>(params.get("fileId").toString()); } @Override - public FileDTO uploadSingleForFileDTO(MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException { - uploadFile(uploadFile, uploadTypeEnum, params); + public SuccessResultData uploadSingle(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException { + uploadFile(token, uploadFile, uploadTypeEnum, params); + return new SuccessResultData<>(params.get("fileId").toString()); + } + + @Override + public FileDTO uploadSingleForFileDTO(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException { + uploadFile(token, uploadFile, uploadTypeEnum, params); FileDTO fileDTO = new FileDTO(); fileDTO.setFileId(params.get("fileId").toString()); fileDTO.setFileName(params.get("fileName").toString()); @@ -75,12 +81,13 @@ public class FileServiceImpl extends AbstractService implements IFileService { /** * 上传文件 * + * @param token * @param uploadFile * @param uploadTypeEnum * @param params * @throws SystemException */ - private void uploadFile(MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException { + private void uploadFile(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params) throws SystemException { String baseUploadPath = fileProperties.getUploadPath(); if (StringUtils.isBlank(baseUploadPath)) { throw new SystemException("上传路径未配置"); @@ -104,7 +111,11 @@ public class FileServiceImpl extends AbstractService implements IFileService { params.put("fileType", fileType); params.put("fileSize", fileSize); params.put("isBack", 0); - setSaveInfo(params); + if (StringUtils.isBlank(token)) { + setSaveInfo(params); + } else { + setSaveInfo(token, params); + } fileDao.saveFile(params); } @@ -168,6 +179,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { return fileDao.getFile(params); } + /** * uEditor文件列表 * @@ -216,11 +228,11 @@ public class FileServiceImpl extends AbstractService implements IFileService { LOG.debug("UEditor上传文件"); FileDTO fileDTO = null; if (UEDITOR_UPLOAD_IMAGE.equals(action)) { - fileDTO = uploadSingleForFileDTO(file, UploadTypeEnum.IMAGE, params); + fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.IMAGE, params); } else if (UEDITOR_UPLOAD_VIDEO.equals(action)) { - fileDTO = uploadSingleForFileDTO(file, UploadTypeEnum.VIDEO, params); + fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.VIDEO, params); } else if (UEDITOR_UPLOAD_FILE.equals(action)) { - fileDTO = uploadSingleForFileDTO(file, UploadTypeEnum.FILE, params); + fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.FILE, params); } if (fileDTO == null) { throw new SystemException("文件上传失败");