diff --git a/basic-interface/src/main/java/ink/wgink/interfaces/file/IMongoFileService.java b/basic-interface/src/main/java/ink/wgink/interfaces/file/IMongoFileService.java new file mode 100644 index 00000000..4db02de5 --- /dev/null +++ b/basic-interface/src/main/java/ink/wgink/interfaces/file/IMongoFileService.java @@ -0,0 +1,65 @@ +package ink.wgink.interfaces.file; + +import ink.wgink.pojo.ListPage; +import ink.wgink.pojo.pos.FilePO; +import ink.wgink.pojo.result.SuccessResultList; + +import java.util.List; + +/** + * @ClassName: IMongoFileService + * @Description: Mongo文件业务 + * @Author: wanggeng + * @Date: 2021/12/12 6:16 PM + * @Version: 1.0 + */ +public interface IMongoFileService { + + String UPLOAD_FILE_COLLECTION_NAME = "uploadFiles"; + + /** + * 保存文件 + * + * @param filePO + */ + void saveByUserId(String userId, FilePO filePO); + + /** + * 删除文件 + * + * @param fileIds 文件ID列表 + */ + void delete(List fileIds); + + /** + * 文件详情 + * + * @param fileId + * @return + */ + FilePO getPO(String fileId); + + /** + * 文件分页列表 + * + * @param page + * @return + */ + SuccessResultList> listPage(ListPage page); + + /** + * 文件列表 + * + * @param fileIds + * @return + */ + List listByIds(List fileIds); + + /** + * 文件列表 + * + * @param fileUrl 文件url + * @return + */ + List listByUrl(String fileUrl); +} diff --git a/basic-pojo/src/main/java/ink/wgink/pojo/pos/FilePO.java b/basic-pojo/src/main/java/ink/wgink/pojo/pos/FilePO.java index 25243228..80f42b24 100644 --- a/basic-pojo/src/main/java/ink/wgink/pojo/pos/FilePO.java +++ b/basic-pojo/src/main/java/ink/wgink/pojo/pos/FilePO.java @@ -17,7 +17,7 @@ public class FilePO implements Serializable { private String filePath; private String fileUrl; private String fileType; - private String fileSize; + private Long fileSize; private String fileSummary; private Integer isBack; private String creator; @@ -66,11 +66,11 @@ public class FilePO implements Serializable { this.fileType = fileType; } - public String getFileSize() { - return fileSize == null ? "" : fileSize.trim(); + public Long getFileSize() { + return fileSize == null ? 0 : fileSize; } - public void setFileSize(String fileSize) { + public void setFileSize(Long fileSize) { this.fileSize = fileSize; } @@ -83,7 +83,7 @@ public class FilePO implements Serializable { } public Integer getIsBack() { - return isBack; + return isBack == null ? 0 : isBack; } public void setIsBack(Integer isBack) { @@ -123,7 +123,7 @@ public class FilePO implements Serializable { } public Integer getIsDelete() { - return isDelete; + return isDelete == null ? 0 : isDelete; } public void setIsDelete(Integer isDelete) { diff --git a/module-file/pom.xml b/module-file/pom.xml index 6f6fc221..25ff492e 100644 --- a/module-file/pom.xml +++ b/module-file/pom.xml @@ -37,7 +37,6 @@ org.springframework.data spring-data-mongodb - provided diff --git a/module-file/src/main/java/ink/wgink/module/file/pojo/vos/FileVO.java b/module-file/src/main/java/ink/wgink/module/file/pojo/vos/FileVO.java index 94fe1baa..900a9101 100644 --- a/module-file/src/main/java/ink/wgink/module/file/pojo/vos/FileVO.java +++ b/module-file/src/main/java/ink/wgink/module/file/pojo/vos/FileVO.java @@ -1,5 +1,7 @@ package ink.wgink.module.file.pojo.vos; +import ink.wgink.pojo.pos.FilePO; + /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 @@ -10,79 +12,6 @@ package ink.wgink.module.file.pojo.vos; * @Date: 2021/3/15 11:39 上午 * @Version: 1.0 */ -public class FileVO { +public class FileVO extends FilePO { - String fileName; - String filePath; - String fileFullPath; - String fileUrl; - String fileType; - Long fileSize; - - public String getFileName() { - return fileName == null ? "" : fileName; - } - - public void setFileName(String fileName) { - this.fileName = fileName; - } - - public String getFilePath() { - return filePath == null ? "" : filePath; - } - - public void setFilePath(String filePath) { - this.filePath = filePath; - } - - public String getFileFullPath() { - return fileFullPath == null ? "" : fileFullPath; - } - - public void setFileFullPath(String fileFullPath) { - this.fileFullPath = fileFullPath; - } - - public String getFileUrl() { - return fileUrl == null ? "" : fileUrl; - } - - public void setFileUrl(String fileUrl) { - this.fileUrl = fileUrl; - } - - public String getFileType() { - return fileType == null ? "" : fileType; - } - - public void setFileType(String fileType) { - this.fileType = fileType; - } - - public Long getFileSize() { - return fileSize == null ? 0 : fileSize; - } - - public void setFileSize(Long fileSize) { - this.fileSize = fileSize; - } - - @Override - public String toString() { - final StringBuilder sb = new StringBuilder("{"); - sb.append("\"fileName\":\"") - .append(fileName).append('\"'); - sb.append(",\"filePath\":\"") - .append(filePath).append('\"'); - sb.append(",\"fileFullPath\":\"") - .append(fileFullPath).append('\"'); - sb.append(",\"fileUrl\":\"") - .append(fileUrl).append('\"'); - sb.append(",\"fileType\":\"") - .append(fileType).append('\"'); - sb.append(",\"fileSize\":") - .append(fileSize); - sb.append('}'); - return sb.toString(); - } } diff --git a/module-file/src/main/java/ink/wgink/module/file/service/IDefaultFileService.java b/module-file/src/main/java/ink/wgink/module/file/service/IDefaultFileService.java index ed4d168f..f52dcd6d 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/IDefaultFileService.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/IDefaultFileService.java @@ -25,6 +25,20 @@ import java.util.Map; */ public interface IDefaultFileService { + /** + * 保存文件 + * + * @param fileVO + */ + void saveFile(FileVO fileVO); + + /** + * 保存文件 + * + * @param userId + * @param fileVO + * @return + */ String saveFileByUserId(String userId, FileVO fileVO); /** @@ -48,6 +62,11 @@ public interface IDefaultFileService { */ void uploadFileByUserId(String userId, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params); + /** + * 删除文件 + * + * @param ids + */ void remove(List ids); /** @@ -89,17 +108,6 @@ public interface IDefaultFileService { */ void saveUploadFileInfoByUserId(String userId, String fileName, String uploadPath, String uploadFileName, String fileType, long fileSize, Map params); - /** - * 获取上传文件路径 - * - * @param baseUploadPath - * @param uploadTypeEnum - * @param fileType - * @return - * @throws FileException - */ - String getUploadPath(String baseUploadPath, UploadTypeEnum uploadTypeEnum, String fileType) throws FileException; - /** * 下载文件 * @@ -122,14 +130,55 @@ public interface IDefaultFileService { */ boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, FilePO filePO, boolean isOpen, boolean canRange); + /** + * 获取上传文件路径 + * + * @param baseUploadPath + * @param uploadTypeEnum + * @param fileType + * @return + * @throws FileException + */ + String getUploadPath(String baseUploadPath, UploadTypeEnum uploadTypeEnum, String fileType) throws FileException; + + /** + * 文件分页列表 + * + * @param page + * @return + */ SuccessResultList> listPageInfo(ListPage page); + /** + * 文件列表 + * + * @param idList + * @return + */ List list(List idList); + /** + * 文件列表 + * + * @param idList + * @return + */ List listPO(List idList); + /** + * 文件详情 + * + * @param fileId + * @return + */ FilePO getPO(String fileId); + /** + * 文件列表 + * + * @param url 文件url + * @return + */ List listByUrl(String url); } diff --git a/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java b/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java index b53a4588..cc66620e 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java @@ -92,14 +92,6 @@ public interface IFileService { */ String saveFileByUserId(String userId, FileVO fileVO); - /** - * 文件分页列表 - * - * @param page - * @return - */ - SuccessResultList> listPageInfo(ListPage page); - /** * 删除记录 * @@ -174,6 +166,16 @@ public interface IFileService { */ FileDTO uploadSingleForFileDTO(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map params); + /** + * 保存导入Excel异常文件信息 + * + * @param fileName + * @param uploadPath + * @param fileSize + * @param params + */ + void uploadErrorExcelFileInfo(String fileName, String uploadPath, long fileSize, Map params); + /** * 文件下载 * @@ -231,16 +233,6 @@ public interface IFileService { @Deprecated FilePO getPO(Map params); - /** - * 保存导入Excel异常文件信息 - * - * @param fileName - * @param uploadPath - * @param fileSize - * @param params - */ - void uploadErrorExcelFileInfo(String fileName, String uploadPath, long fileSize, Map params); - /** * 获取导入Excel异常文件路径 * @@ -264,4 +256,12 @@ public interface IFileService { */ List listPO(List idList); + /** + * 文件分页列表 + * + * @param page + * @return + */ + SuccessResultList> listPageInfo(ListPage page); + } diff --git a/module-file/src/main/java/ink/wgink/module/file/service/IMinIoFileService.java b/module-file/src/main/java/ink/wgink/module/file/service/IMinIoFileService.java index 4ba7f43c..763a59dc 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/IMinIoFileService.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/IMinIoFileService.java @@ -23,8 +23,20 @@ import java.util.Map; */ public interface IMinIoFileService { + /** + * 保存 + * + * @param userId + * @param fileVO + * @return + */ String saveFileByUserId(String userId, FileVO fileVO); + /** + * 删除 + * + * @param ids + */ void remove(List ids); /** @@ -76,13 +88,43 @@ public interface IMinIoFileService { */ boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, FilePO filePO, boolean isOpen, boolean canRange); + /** + * 文件分页列表 + * + * @param page + * @return + */ SuccessResultList> listPageInfo(ListPage page); + /** + * 文件列表 + * + * @param idList + * @return + */ List list(List idList); + /** + * 文件列表 + * + * @param idList + * @return + */ List listPO(List idList); + /** + * 详情 + * + * @param fileId + * @return + */ FilePO getPO(String fileId); + /** + * 文件列表 + * + * @param fileUrl + * @return + */ List listByUrl(String fileUrl); } diff --git a/module-file/src/main/java/ink/wgink/module/file/service/impl/DefaultFileServiceImpl.java b/module-file/src/main/java/ink/wgink/module/file/service/impl/DefaultFileServiceImpl.java index 0d1cc040..dd8cd0a0 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/impl/DefaultFileServiceImpl.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/impl/DefaultFileServiceImpl.java @@ -64,13 +64,30 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau private String[] audioTypes; private String[] fileTypes; + @Override + public void saveFile(FileVO fileVO) { + fileDao.save(HashMapUtil.beanToMap(fileVO)); + } + @Override public String saveFileByUserId(String userId, FileVO fileVO) { - String fileId = UUIDUtil.getUUID(); + String fileId; + if (StringUtils.isBlank(fileVO.getFileId())) { + fileId = UUIDUtil.getUUID(); + fileVO.setFileId(fileId); + } else { + fileId = fileVO.getFileId(); + } + if (StringUtils.isBlank(fileVO.getGmtCreate())) { + String dateTime = DateUtil.getTime(); + fileVO.setGmtCreate(dateTime); + fileVO.setGmtModified(dateTime); + } + if (StringUtils.isBlank(fileVO.getCreator())) { + fileVO.setCreator(userId); + fileVO.setModifier(userId); + } Map params = HashMapUtil.beanToMap(fileVO); - params.put("fileId", fileId); - params.put("isBack", 0); - setSaveInfoByUserId(params, userId); fileDao.save(params); return fileId; } @@ -327,7 +344,7 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau OutputStream outputStream = response.getOutputStream(); WritableByteChannel writableByteChannel = Channels.newChannel(outputStream); ) { - response.setHeader("Content-Length", filePO.getFileSize()); + response.setHeader("Content-Length", filePO.getFileSize().toString()); response.setContentType(StaticResourceRequestUtil.getContentType(filePO.getFileType())); if (!isOpen) { // 下载 @@ -388,6 +405,57 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau return isDownloadComplete; } + + @Override + public String getUploadPath(String baseUploadPath, UploadTypeEnum uploadTypeEnum, String fileType) throws FileException { + StringBuilder filePath = new StringBuilder(); + if (!baseUploadPath.endsWith(File.separator)) { + filePath.append(baseUploadPath).append(File.separator); + } else { + filePath.append(baseUploadPath); + } + boolean hasFileType = !StringUtils.isBlank(fileType); + if (uploadTypeEnum.getValue() == UploadTypeEnum.IMAGE.getValue()) { + if (hasFileType && !isTypeCorrect(getImageTypes(), fileType)) { + throw new FileException("图片格式不支持上传"); + } + filePath.append("images"); + } else if (uploadTypeEnum.getValue() == UploadTypeEnum.VIDEO.getValue()) { + if (hasFileType && !isTypeCorrect(getVideoTypes(), fileType)) { + throw new FileException("视频格式不支持上传"); + } + filePath.append("videos"); + } else if (uploadTypeEnum.getValue() == UploadTypeEnum.AUDIO.getValue()) { + if (hasFileType && !isTypeCorrect(getAudioTypes(), fileType)) { + throw new FileException("音频格式不支持上传"); + } + filePath.append("audios"); + } else if (uploadTypeEnum.getValue() == UploadTypeEnum.ERROR_EXCEL.getValue()) { + filePath.append("errorexcels"); + } else { + if (hasFileType && !isTypeCorrect(getFileTypes(), fileType)) { + throw new FileException("文件格式不支持上传"); + } + filePath.append("files"); + } + filePath.append(File.separator).append(DateUtil.getDays()); + return filePath.toString(); + } + + /** + * 获取上传文件名称 + * + * @param fileType + * @return + */ + private String getUploadFileName(String fileType) { + String uploadFileName = UUIDUtil.get32UUID(); + if (!StringUtils.isEmpty(fileType)) { + uploadFileName += "." + fileType; + } + return uploadFileName; + } + @Override public SuccessResultList> listPageInfo(ListPage page) { PageHelper.startPage(page.getPage(), page.getRows()); @@ -540,56 +608,6 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau return false; } - @Override - public String getUploadPath(String baseUploadPath, UploadTypeEnum uploadTypeEnum, String fileType) throws FileException { - StringBuilder filePath = new StringBuilder(); - if (!baseUploadPath.endsWith(File.separator)) { - filePath.append(baseUploadPath).append(File.separator); - } else { - filePath.append(baseUploadPath); - } - boolean hasFileType = !StringUtils.isBlank(fileType); - if (uploadTypeEnum.getValue() == UploadTypeEnum.IMAGE.getValue()) { - if (hasFileType && !isTypeCorrect(getImageTypes(), fileType)) { - throw new FileException("图片格式不支持上传"); - } - filePath.append("images"); - } else if (uploadTypeEnum.getValue() == UploadTypeEnum.VIDEO.getValue()) { - if (hasFileType && !isTypeCorrect(getVideoTypes(), fileType)) { - throw new FileException("视频格式不支持上传"); - } - filePath.append("videos"); - } else if (uploadTypeEnum.getValue() == UploadTypeEnum.AUDIO.getValue()) { - if (hasFileType && !isTypeCorrect(getAudioTypes(), fileType)) { - throw new FileException("音频格式不支持上传"); - } - filePath.append("audios"); - } else if (uploadTypeEnum.getValue() == UploadTypeEnum.ERROR_EXCEL.getValue()) { - filePath.append("errorexcels"); - } else { - if (hasFileType && !isTypeCorrect(getFileTypes(), fileType)) { - throw new FileException("文件格式不支持上传"); - } - filePath.append("files"); - } - filePath.append(File.separator).append(DateUtil.getDays()); - return filePath.toString(); - } - - /** - * 获取上传文件名称 - * - * @param fileType - * @return - */ - private String getUploadFileName(String fileType) { - String uploadFileName = UUIDUtil.get32UUID(); - if (!StringUtils.isEmpty(fileType)) { - uploadFileName += "." + fileType; - } - return uploadFileName; - } - /** * 保存文件到本地 * diff --git a/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java b/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java index 6bcf0c24..ed0cfaa7 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java @@ -51,15 +51,6 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService @Autowired private IMinIoFileService minIoFileService; - @Override - public SuccessResultList> listPageInfo(ListPage page) { - if (fileProperties.getUseMinIo()) { - return minIoFileService.listPageInfo(page); - } else { - return defaultFileService.listPageInfo(page); - } - } - @Override public void remove(String ids) { remove(Arrays.asList(ids.split("_"))); @@ -161,33 +152,6 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService defaultFileService.saveUploadFileInfo(null, fileName, uploadPath, fileName, "xls", fileSize, params); } - @Override - public String getUploadExcelErrorPath() { - String baseUploadPath = fileProperties.getUploadPath(); - if (StringUtils.isBlank(baseUploadPath)) { - throw new SystemException("上传路径未配置"); - } - return defaultFileService.getUploadPath(baseUploadPath, UploadTypeEnum.ERROR_EXCEL, null); - } - - @Override - public List list(List idList) throws SearchException { - if (fileProperties.getUseMinIo()) { - return minIoFileService.list(idList); - } else { - return defaultFileService.list(idList); - } - } - - @Override - public List listPO(List idList) { - if (fileProperties.getUseMinIo()) { - return minIoFileService.listPO(idList); - } else { - return defaultFileService.listPO(idList); - } - } - @Override public String saveFileByUserId(String userId, FileVO fileVO) { if (fileProperties.getUseMinIo()) { @@ -265,6 +229,42 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService } } + @Override + public String getUploadExcelErrorPath() { + String baseUploadPath = fileProperties.getUploadPath(); + if (StringUtils.isBlank(baseUploadPath)) { + throw new SystemException("上传路径未配置"); + } + return defaultFileService.getUploadPath(baseUploadPath, UploadTypeEnum.ERROR_EXCEL, null); + } + + @Override + public List list(List idList) throws SearchException { + if (fileProperties.getUseMinIo()) { + return minIoFileService.list(idList); + } else { + return defaultFileService.list(idList); + } + } + + @Override + public List listPO(List idList) { + if (fileProperties.getUseMinIo()) { + return minIoFileService.listPO(idList); + } else { + return defaultFileService.listPO(idList); + } + } + + @Override + public SuccessResultList> listPageInfo(ListPage page) { + if (fileProperties.getUseMinIo()) { + return minIoFileService.listPageInfo(page); + } else { + return defaultFileService.listPageInfo(page); + } + } + /** * uEditor文件列表 * diff --git a/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java b/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java index 74c44f45..5516d79f 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java @@ -3,7 +3,7 @@ package ink.wgink.module.file.service.impl; import ink.wgink.common.base.DefaultBaseService; import ink.wgink.exceptions.FileException; import ink.wgink.exceptions.base.SystemException; -import ink.wgink.module.file.dao.IFileDao; +import ink.wgink.interfaces.file.IMongoFileService; import ink.wgink.module.file.enums.UploadTypeEnum; import ink.wgink.module.file.pojo.dtos.FileDTO; import ink.wgink.module.file.pojo.dtos.FileInfoDTO; @@ -18,18 +18,15 @@ import ink.wgink.properties.FileMinIoProperties; import ink.wgink.properties.FileProperties; import ink.wgink.util.ArrayListUtil; import ink.wgink.util.UUIDUtil; -import ink.wgink.util.map.HashMapUtil; +import ink.wgink.util.date.DateUtil; import ink.wgink.util.request.StaticResourceRequestUtil; import io.minio.*; import io.minio.errors.*; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Pageable; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -45,7 +42,6 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; /** * @ClassName: MinIOServiceImpl @@ -57,15 +53,12 @@ import java.util.regex.Pattern; @Service public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFileService { - public static final String UPLOAD_FILE_COLLECTION_NAME = "uploadFiles"; @Autowired private FileProperties fileProperties; @Autowired - private IFileDao fileDao; - @Autowired private IDefaultFileService defaultFileService; @Autowired(required = false) - private MongoTemplate mongoTemplate; + private IMongoFileService mongoFileService; private FileMinIoProperties minIoProperties; private MinioClient minioClient; @@ -84,12 +77,8 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi @Override public String saveFileByUserId(String userId, FileVO fileVO) { String fileId = defaultFileService.saveFileByUserId(userId, fileVO); - if (mongoTemplate != null) { - Map params = HashMapUtil.beanToMap(fileVO); - params.put("fileId", fileId); - params.put("isBack", 0); - setSaveInfoByUserId(params, userId); - mongoTemplate.insert(params); + if (mongoFileService != null) { + mongoFileService.saveByUserId(userId, fileVO); } return fileId; } @@ -100,9 +89,8 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi return; } defaultFileService.remove(ids); - // 删除缓存 - if (mongoTemplate != null) { - mongoTemplate.remove(Query.query(Criteria.where("fileId").in(ids)), UPLOAD_FILE_COLLECTION_NAME); + if (mongoFileService != null) { + mongoFileService.delete(ids); } } @@ -114,8 +102,8 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi } List fileIds = ArrayListUtil.listBeanStringIdValue(filePOs, "fileId", FilePO.class); defaultFileService.delete(fileIds); - if (mongoTemplate != null) { - mongoTemplate.remove(Query.query(Criteria.where("fileId").in(fileIds))); + if (mongoFileService != null) { + mongoFileService.delete(fileIds); } } @@ -131,12 +119,13 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi @Override public boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, Map params, boolean canRange) { + String fileId = params.get("fileId").toString(); FilePO filePO = null; - if (mongoTemplate != null) { - filePO = mongoTemplate.findOne(Query.query(Criteria.where("fileId").is(params.get("fileId").toString())), FilePO.class, UPLOAD_FILE_COLLECTION_NAME); + if (mongoFileService != null) { + filePO = mongoFileService.getPO(fileId); } if (filePO == null) { - filePO = fileDao.getPO(params); + filePO = defaultFileService.getPO(fileId); } if (filePO == null) { throw new FileException("文件不存在"); @@ -181,7 +170,7 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi String objectName = filePO.getFileId() + "." + filePO.getFileType(); InputStream inputStream = null; try (OutputStream outputStream = response.getOutputStream();) { - response.setHeader("Content-Length", filePO.getFileSize()); + response.setHeader("Content-Length", filePO.getFileSize().toString()); response.setContentType(StaticResourceRequestUtil.getContentType(filePO.getFileType())); if (!isOpen) { // 下载 @@ -251,44 +240,36 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi @Override public SuccessResultList> listPageInfo(ListPage page) { - SuccessResultList> successResultList = null; - if (mongoTemplate != null) { - Query query = new Query(); - String keywords = getKeywords(page.getParams()); - if (!StringUtils.isBlank(keywords)) { - Pattern pattern = Pattern.compile("^.*" + keywords + ".*$", Pattern.CASE_INSENSITIVE); - query.addCriteria(Criteria.where("fileName").regex(pattern)); - } - long total = mongoTemplate.count(query, UPLOAD_FILE_COLLECTION_NAME); - query.with(Pageable.ofSize(page.getRows()).withPage(page.getPage() - 1)); - List dataDTOs = mongoTemplate.find(query, FileInfoDTO.class, UPLOAD_FILE_COLLECTION_NAME); - successResultList = new SuccessResultList<>(dataDTOs, page.getPage(), total); + SuccessResultList> filePOSuccessResultList = null; + if (mongoFileService != null) { + filePOSuccessResultList = mongoFileService.listPage(page); } - if (successResultList != null && !successResultList.getRows().isEmpty()) { - return successResultList; + if (filePOSuccessResultList != null && !filePOSuccessResultList.getRows().isEmpty()) { + List mongoFileInfos = filePO2FileInfo(filePOSuccessResultList.getRows()); + return new SuccessResultList<>(mongoFileInfos, filePOSuccessResultList.getPage(), filePOSuccessResultList.getTotal()); } return defaultFileService.listPageInfo(page); } @Override public List list(List idList) { - List fileDTOs = new ArrayList<>(); - if (mongoTemplate != null) { - return mongoTemplate.find(Query.query(Criteria.where("fileId").in(idList)), FileDTO.class, UPLOAD_FILE_COLLECTION_NAME); + List filePOs = null; + if (mongoFileService != null) { + filePOs = mongoFileService.listByIds(idList); } - if (!fileDTOs.isEmpty()) { - return fileDTOs; + if (filePOs != null && !filePOs.isEmpty()) { + return filePO2FileDTO(filePOs); } return defaultFileService.list(idList); } @Override public List listPO(List idList) { - List filePOs = new ArrayList<>(); - if (mongoTemplate != null) { - filePOs = mongoTemplate.find(Query.query(Criteria.where("fileId").in(idList)), FilePO.class, UPLOAD_FILE_COLLECTION_NAME); + List filePOs = null; + if (mongoFileService != null) { + filePOs = mongoFileService.listByIds(idList); } - if (!filePOs.isEmpty()) { + if (filePOs != null && !filePOs.isEmpty()) { return filePOs; } return defaultFileService.listPO(idList); @@ -297,8 +278,8 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi @Override public FilePO getPO(String fileId) { FilePO filePO = null; - if (mongoTemplate != null) { - filePO = mongoTemplate.findOne(Query.query(Criteria.where("fileId").is(fileId)), FilePO.class, UPLOAD_FILE_COLLECTION_NAME); + if (mongoFileService != null) { + filePO = mongoFileService.getPO(fileId); } if (filePO != null) { return filePO; @@ -308,17 +289,48 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi @Override public List listByUrl(String fileUrl) { - List fileDTOs = new ArrayList<>(); - if (mongoTemplate != null) { - Pattern pattern = Pattern.compile("^.*" + fileUrl + ".*$", Pattern.CASE_INSENSITIVE); - fileDTOs = mongoTemplate.find(Query.query(Criteria.where("fileUrl").regex(pattern)), FileDTO.class, UPLOAD_FILE_COLLECTION_NAME); + List filePOs = null; + if (mongoFileService != null) { + filePOs = mongoFileService.listByUrl(fileUrl); } - if (!fileDTOs.isEmpty()) { - return fileDTOs; + if (filePOs != null && !filePOs.isEmpty()) { + return filePO2FileDTO(filePOs); } return defaultFileService.listByUrl(fileUrl); } + /** + * mongoFile转fileInfo + * + * @param filePOs + * @return + */ + private List filePO2FileInfo(List filePOs) { + List fileInfoDTOs = new ArrayList<>(); + filePOs.forEach(mongoFile -> { + FileInfoDTO fileInfoDTO = new FileInfoDTO(); + BeanUtils.copyProperties(mongoFile, fileInfoDTO); + fileInfoDTOs.add(fileInfoDTO); + }); + return fileInfoDTOs; + } + + /** + * mongoFile转fileDTO + * + * @param filePOs + * @return + */ + private List filePO2FileDTO(List filePOs) { + List fileDTOs = new ArrayList<>(); + filePOs.forEach(mongoFile -> { + FileDTO fileDTO = new FileDTO(); + BeanUtils.copyProperties(mongoFile, fileDTO); + fileDTOs.add(fileDTO); + }); + return fileDTOs; + } + /** * 上传文件到minIo @@ -334,7 +346,7 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } - minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(uploadFileName).stream(inputStream, fileSize, -1).contentType(uploadFile.getContentType()).build()); + minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(uploadFileName).stream(inputStream, fileSize, -1).contentType(StaticResourceRequestUtil.getContentType(uploadFileName)).build()); } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) { throw new SystemException(e); } @@ -351,16 +363,14 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi * @param bucketName 桶名称 */ private void saveFile(String token, String fileId, String fileName, String fileType, long fileSize, String bucketName) { - Map params = getFileParams(fileId, fileName, fileType, fileSize, bucketName); - if (StringUtils.isBlank(token)) { - setSaveInfo(params); + String userId; + if(StringUtils.isBlank(token)) { + userId = securityComponent.getCurrentUser().getUserId(); } else { - setAppSaveInfo(token, params); - } - fileDao.save(params); - if (mongoTemplate != null) { - mongoTemplate.insert(params, UPLOAD_FILE_COLLECTION_NAME); + userId = getAppTokenUser(token).getId(); } + saveFileByUserId(userId, fileId, fileName, fileType, fileSize, bucketName); + } /** @@ -374,25 +384,37 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi * @param bucketName */ private void saveFileByUserId(String userId, String fileId, String fileName, String fileType, long fileSize, String bucketName) { - Map params = getFileParams(fileId, fileName, fileType, fileSize, bucketName); - setSaveInfoByUserId(params, userId); - fileDao.save(params); - if (mongoTemplate != null) { - mongoTemplate.insert(params, UPLOAD_FILE_COLLECTION_NAME); + FileVO fileVO = getFileVO(fileId, fileName, fileType, fileSize, bucketName); + defaultFileService.saveFile(fileVO); + if (mongoFileService != null) { + mongoFileService.saveByUserId(userId, fileVO); } } - private Map getFileParams(String fileId, String fileName, String fileType, long fileSize, String bucketName) { - Map params = getHashMap(2); - params.put("fileId", fileId); - params.put("fileName", fileName); - params.put("filePath", null); - params.put("fileUrl", bucketName); - params.put("fileType", fileType); - params.put("fileSize", fileSize); - params.put("fileSummary", "minio"); - params.put("isBack", 1); - return params; + /** + * 获取文件VO + * + * @param fileId + * @param fileName + * @param fileType + * @param fileSize + * @param bucketName + * @return + */ + private FileVO getFileVO(String fileId, String fileName, String fileType, long fileSize, String bucketName) { + String dateTime = DateUtil.getTime(); + FileVO fileVO = new FileVO(); + fileVO.setFileId(fileId); + fileVO.setFileName(fileName); + fileVO.setFilePath(null); + fileVO.setFileUrl(bucketName); + fileVO.setFileType(fileType); + fileVO.setFileSize(fileSize); + fileVO.setFileSummary("minio"); + fileVO.setIsBack(1); + fileVO.setGmtCreate(dateTime); + fileVO.setGmtModified(dateTime); + return fileVO; } /** diff --git a/mongo-module-file/pom.xml b/mongo-module-file/pom.xml new file mode 100644 index 00000000..c21494a1 --- /dev/null +++ b/mongo-module-file/pom.xml @@ -0,0 +1,28 @@ + + + + wg-basic + ink.wgink + 1.0-SNAPSHOT + + 4.0.0 + + mongo-module-file + + + + org.springframework.data + spring-data-mongodb + provided + + + ink.wgink + module-file + 1.0-SNAPSHOT + provided + + + + \ No newline at end of file diff --git a/mongo-module-file/src/main/java/ink/wgink/mongo/module/file/service/MongoFileService.java b/mongo-module-file/src/main/java/ink/wgink/mongo/module/file/service/MongoFileService.java new file mode 100644 index 00000000..3d1f0cb6 --- /dev/null +++ b/mongo-module-file/src/main/java/ink/wgink/mongo/module/file/service/MongoFileService.java @@ -0,0 +1,75 @@ +package ink.wgink.mongo.module.file.service; + +import ink.wgink.common.base.DefaultBaseService; +import ink.wgink.interfaces.file.IMongoFileService; +import ink.wgink.pojo.ListPage; +import ink.wgink.pojo.pos.FilePO; +import ink.wgink.pojo.result.SuccessResultList; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.regex.Pattern; + +/** + * @ClassName: MongoFileService + * @Description: + * @Author: wanggeng + * @Date: 2021/12/12 6:18 PM + * @Version: 1.0 + */ +@Service +public class MongoFileService extends DefaultBaseService implements IMongoFileService { + + @Autowired(required = false) + private MongoTemplate mongoTemplate; + + @Override + public void saveByUserId(String userId, FilePO filePO) { + filePO.setCreator(userId); + filePO.setModifier(userId); + mongoTemplate.insert(filePO, UPLOAD_FILE_COLLECTION_NAME); + } + + @Override + public void delete(List fileIds) { + mongoTemplate.remove(Query.query(Criteria.where("fileId").in(fileIds)), UPLOAD_FILE_COLLECTION_NAME); + } + + @Override + public FilePO getPO(String fileId) { + return mongoTemplate.findOne(Query.query(Criteria.where("fileId").is(fileId)), FilePO.class, UPLOAD_FILE_COLLECTION_NAME); + } + + @Override + public SuccessResultList> listPage(ListPage page) { + Query query = new Query(); + String keywords = getKeywords(page.getParams()); + if (!StringUtils.isBlank(keywords)) { + Pattern pattern = Pattern.compile("^.*" + keywords + ".*$", Pattern.CASE_INSENSITIVE); + query.addCriteria(Criteria.where("fileName").regex(pattern)); + } + long total = mongoTemplate.count(query, UPLOAD_FILE_COLLECTION_NAME); + query.with(Pageable.ofSize(page.getRows()).withPage(page.getPage() - 1)); + List dataDTOs = mongoTemplate.find(query, FilePO.class, UPLOAD_FILE_COLLECTION_NAME); + return new SuccessResultList<>(dataDTOs, page.getPage(), total); + } + + @Override + public List listByIds(List fileIds) { + return mongoTemplate.find(Query.query(Criteria.where("fileId").in(fileIds)), FilePO.class, UPLOAD_FILE_COLLECTION_NAME); + } + + @Override + public List listByUrl(String fileUrl) { + Pattern pattern = Pattern.compile("^.*" + fileUrl + ".*$", Pattern.CASE_INSENSITIVE); + return mongoTemplate.find(Query.query(Criteria.where("fileUrl").regex(pattern)), FilePO.class, UPLOAD_FILE_COLLECTION_NAME); + } + + +} diff --git a/pom.xml b/pom.xml index 3a108736..772b5144 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,7 @@ module-oauth2-client mongo-module-dictionary redis-cache + mongo-module-file pom