增加mongo + minio的文件统一管理
This commit is contained in:
parent
27d586a9bb
commit
34312ff619
@ -33,6 +33,14 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<!-- minio end -->
|
<!-- minio end -->
|
||||||
|
|
||||||
|
<!-- mongodb start -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-mongodb</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- mongodb end -->
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -3,7 +3,12 @@ package ink.wgink.module.file.service;
|
|||||||
import ink.wgink.exceptions.FileException;
|
import ink.wgink.exceptions.FileException;
|
||||||
import ink.wgink.exceptions.base.SystemException;
|
import ink.wgink.exceptions.base.SystemException;
|
||||||
import ink.wgink.module.file.enums.UploadTypeEnum;
|
import ink.wgink.module.file.enums.UploadTypeEnum;
|
||||||
|
import ink.wgink.module.file.pojo.dtos.FileDTO;
|
||||||
|
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
|
||||||
|
import ink.wgink.module.file.pojo.vos.FileVO;
|
||||||
|
import ink.wgink.pojo.ListPage;
|
||||||
import ink.wgink.pojo.pos.FilePO;
|
import ink.wgink.pojo.pos.FilePO;
|
||||||
|
import ink.wgink.pojo.result.SuccessResultList;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -20,6 +25,8 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public interface IDefaultFileService {
|
public interface IDefaultFileService {
|
||||||
|
|
||||||
|
String saveFileByUserId(String userId, FileVO fileVO);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件
|
* 上传文件
|
||||||
*
|
*
|
||||||
@ -41,6 +48,8 @@ public interface IDefaultFileService {
|
|||||||
*/
|
*/
|
||||||
void uploadFileByUserId(String userId, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params);
|
void uploadFileByUserId(String userId, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params);
|
||||||
|
|
||||||
|
void remove(List<String> ids);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 物理删除文件
|
* 物理删除文件
|
||||||
*
|
*
|
||||||
@ -48,6 +57,13 @@ public interface IDefaultFileService {
|
|||||||
*/
|
*/
|
||||||
void deleteFile(List<FilePO> filePOs);
|
void deleteFile(List<FilePO> filePOs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 物理删除
|
||||||
|
*
|
||||||
|
* @param params
|
||||||
|
*/
|
||||||
|
void delete(List<String> ids);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存文件信息
|
* 保存文件信息
|
||||||
*
|
*
|
||||||
@ -92,7 +108,7 @@ public interface IDefaultFileService {
|
|||||||
* @param params
|
* @param params
|
||||||
* @param canRange
|
* @param canRange
|
||||||
*/
|
*/
|
||||||
void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange);
|
boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
@ -106,4 +122,14 @@ public interface IDefaultFileService {
|
|||||||
*/
|
*/
|
||||||
boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, FilePO filePO, boolean isOpen, boolean canRange);
|
boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, FilePO filePO, boolean isOpen, boolean canRange);
|
||||||
|
|
||||||
|
SuccessResultList<List<FileInfoDTO>> listPageInfo(ListPage page);
|
||||||
|
|
||||||
|
List<FileDTO> list(List<String> idList);
|
||||||
|
|
||||||
|
List<FilePO> listPO(List<String> idList);
|
||||||
|
|
||||||
|
FilePO getPO(String fileId);
|
||||||
|
|
||||||
|
List<FileDTO> listByUrl(String url);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -228,6 +228,7 @@ public interface IFileService {
|
|||||||
* @param params
|
* @param params
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
FilePO getPO(Map<String, Object> params);
|
FilePO getPO(Map<String, Object> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package ink.wgink.module.file.service;
|
package ink.wgink.module.file.service;
|
||||||
|
|
||||||
import ink.wgink.module.file.enums.UploadTypeEnum;
|
import ink.wgink.module.file.enums.UploadTypeEnum;
|
||||||
|
import ink.wgink.module.file.pojo.dtos.FileDTO;
|
||||||
|
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
|
||||||
|
import ink.wgink.module.file.pojo.vos.FileVO;
|
||||||
|
import ink.wgink.pojo.ListPage;
|
||||||
import ink.wgink.pojo.pos.FilePO;
|
import ink.wgink.pojo.pos.FilePO;
|
||||||
|
import ink.wgink.pojo.result.SuccessResultList;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -18,6 +23,10 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public interface IMinIoFileService {
|
public interface IMinIoFileService {
|
||||||
|
|
||||||
|
String saveFileByUserId(String userId, FileVO fileVO);
|
||||||
|
|
||||||
|
void remove(List<String> ids);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 物理删除文件
|
* 物理删除文件
|
||||||
*
|
*
|
||||||
@ -45,6 +54,17 @@ public interface IMinIoFileService {
|
|||||||
*/
|
*/
|
||||||
void uploadFileByUserId(String userId, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params);
|
void uploadFileByUserId(String userId, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
* @param params
|
||||||
|
* @param canRange
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
*
|
*
|
||||||
@ -56,4 +76,13 @@ public interface IMinIoFileService {
|
|||||||
*/
|
*/
|
||||||
boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, FilePO filePO, boolean isOpen, boolean canRange);
|
boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, FilePO filePO, boolean isOpen, boolean canRange);
|
||||||
|
|
||||||
|
SuccessResultList<List<FileInfoDTO>> listPageInfo(ListPage page);
|
||||||
|
|
||||||
|
List<FileDTO> list(List<String> idList);
|
||||||
|
|
||||||
|
List<FilePO> listPO(List<String> idList);
|
||||||
|
|
||||||
|
FilePO getPO(String fileId);
|
||||||
|
|
||||||
|
List<FileDTO> listByUrl(String fileUrl);
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,25 @@
|
|||||||
package ink.wgink.module.file.service.impl;
|
package ink.wgink.module.file.service.impl;
|
||||||
|
|
||||||
|
import com.github.pagehelper.PageHelper;
|
||||||
|
import com.github.pagehelper.PageInfo;
|
||||||
import ink.wgink.common.base.DefaultBaseService;
|
import ink.wgink.common.base.DefaultBaseService;
|
||||||
import ink.wgink.exceptions.FileException;
|
import ink.wgink.exceptions.FileException;
|
||||||
import ink.wgink.exceptions.SaveException;
|
import ink.wgink.exceptions.SaveException;
|
||||||
import ink.wgink.exceptions.base.SystemException;
|
import ink.wgink.exceptions.base.SystemException;
|
||||||
import ink.wgink.module.file.dao.IFileDao;
|
import ink.wgink.module.file.dao.IFileDao;
|
||||||
import ink.wgink.module.file.enums.UploadTypeEnum;
|
import ink.wgink.module.file.enums.UploadTypeEnum;
|
||||||
|
import ink.wgink.module.file.pojo.dtos.FileDTO;
|
||||||
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
|
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
|
||||||
|
import ink.wgink.module.file.pojo.vos.FileVO;
|
||||||
import ink.wgink.module.file.service.IDefaultFileService;
|
import ink.wgink.module.file.service.IDefaultFileService;
|
||||||
import ink.wgink.module.file.service.IFileService;
|
import ink.wgink.module.file.service.IFileService;
|
||||||
|
import ink.wgink.pojo.ListPage;
|
||||||
import ink.wgink.pojo.pos.FilePO;
|
import ink.wgink.pojo.pos.FilePO;
|
||||||
|
import ink.wgink.pojo.result.SuccessResultList;
|
||||||
import ink.wgink.properties.FileProperties;
|
import ink.wgink.properties.FileProperties;
|
||||||
import ink.wgink.util.UUIDUtil;
|
import ink.wgink.util.UUIDUtil;
|
||||||
import ink.wgink.util.date.DateUtil;
|
import ink.wgink.util.date.DateUtil;
|
||||||
|
import ink.wgink.util.map.HashMapUtil;
|
||||||
import ink.wgink.util.request.StaticResourceRequestUtil;
|
import ink.wgink.util.request.StaticResourceRequestUtil;
|
||||||
import net.coobird.thumbnailator.Thumbnails;
|
import net.coobird.thumbnailator.Thumbnails;
|
||||||
import org.apache.catalina.connector.ClientAbortException;
|
import org.apache.catalina.connector.ClientAbortException;
|
||||||
@ -32,6 +39,7 @@ import java.nio.channels.FileChannel;
|
|||||||
import java.nio.channels.WritableByteChannel;
|
import java.nio.channels.WritableByteChannel;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -56,6 +64,17 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau
|
|||||||
private String[] audioTypes;
|
private String[] audioTypes;
|
||||||
private String[] fileTypes;
|
private String[] fileTypes;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveFileByUserId(String userId, FileVO fileVO) {
|
||||||
|
String fileId = UUIDUtil.getUUID();
|
||||||
|
Map<String, Object> params = HashMapUtil.beanToMap(fileVO);
|
||||||
|
params.put("fileId", fileId);
|
||||||
|
params.put("isBack", 0);
|
||||||
|
setSaveInfoByUserId(params, userId);
|
||||||
|
fileDao.save(params);
|
||||||
|
return fileId;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件
|
* 上传文件
|
||||||
*
|
*
|
||||||
@ -129,6 +148,14 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(List<String> ids) {
|
||||||
|
Map<String, Object> params = getHashMap(2);
|
||||||
|
params.put("fileIds", ids);
|
||||||
|
setUpdateInfo(params);
|
||||||
|
fileDao.remove(params);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteFile(List<FilePO> filePOs) {
|
public void deleteFile(List<FilePO> filePOs) {
|
||||||
Map<String, Object> fileParams = getHashMap(4);
|
Map<String, Object> fileParams = getHashMap(4);
|
||||||
@ -177,6 +204,13 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(List<String> ids) {
|
||||||
|
Map<String, Object> params = getHashMap(2);
|
||||||
|
params.put("fileIds", ids);
|
||||||
|
fileDao.delete(params);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除源文件
|
* 删除源文件
|
||||||
*
|
*
|
||||||
@ -275,10 +309,13 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange) {
|
public boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange) {
|
||||||
FilePO filePO = fileDao.getPO(params);
|
FilePO filePO = fileDao.getPO(params);
|
||||||
|
if (null == filePO) {
|
||||||
|
throw new FileException("文件不存在");
|
||||||
|
}
|
||||||
boolean isOpen = Boolean.valueOf(params.get("isOpen").toString());
|
boolean isOpen = Boolean.valueOf(params.get("isOpen").toString());
|
||||||
downLoadFile(request, response, filePO, isOpen, canRange);
|
return downLoadFile(request, response, filePO, isOpen, canRange);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -351,6 +388,42 @@ public class DefaultFileServiceImpl extends DefaultBaseService implements IDefau
|
|||||||
return isDownloadComplete;
|
return isDownloadComplete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SuccessResultList<List<FileInfoDTO>> listPageInfo(ListPage page) {
|
||||||
|
PageHelper.startPage(page.getPage(), page.getRows());
|
||||||
|
List<FileInfoDTO> fileInfoDTOs = fileDao.listInfo(page.getParams());
|
||||||
|
PageInfo<FileInfoDTO> pageInfo = new PageInfo<>(fileInfoDTOs);
|
||||||
|
return new SuccessResultList<>(fileInfoDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FileDTO> list(List<String> idList) {
|
||||||
|
Map<String, Object> params = getHashMap(1);
|
||||||
|
params.put("ids", idList);
|
||||||
|
return fileDao.list(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FilePO> listPO(List<String> idList) {
|
||||||
|
Map<String, Object> params = getHashMap(2);
|
||||||
|
params.put("fileIds", idList);
|
||||||
|
return fileDao.listPO(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FilePO getPO(String fileId) {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("fileId", fileId);
|
||||||
|
return fileDao.getPO(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FileDTO> listByUrl(String fileUrl) {
|
||||||
|
Map<String, Object> params = getHashMap(2);
|
||||||
|
params.put("fileUrl", fileUrl);
|
||||||
|
return fileDao.list(params);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理视频流问题
|
* 处理视频流问题
|
||||||
*
|
*
|
||||||
|
@ -5,11 +5,9 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import com.github.pagehelper.PageInfo;
|
import com.github.pagehelper.PageInfo;
|
||||||
import ink.wgink.common.base.DefaultBaseService;
|
import ink.wgink.common.base.DefaultBaseService;
|
||||||
import ink.wgink.exceptions.FileException;
|
|
||||||
import ink.wgink.exceptions.ParamsException;
|
import ink.wgink.exceptions.ParamsException;
|
||||||
import ink.wgink.exceptions.SearchException;
|
import ink.wgink.exceptions.SearchException;
|
||||||
import ink.wgink.exceptions.base.SystemException;
|
import ink.wgink.exceptions.base.SystemException;
|
||||||
import ink.wgink.module.file.dao.IFileDao;
|
|
||||||
import ink.wgink.module.file.enums.UploadTypeEnum;
|
import ink.wgink.module.file.enums.UploadTypeEnum;
|
||||||
import ink.wgink.module.file.pojo.dtos.FileDTO;
|
import ink.wgink.module.file.pojo.dtos.FileDTO;
|
||||||
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
|
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
|
||||||
@ -23,8 +21,6 @@ import ink.wgink.pojo.result.ErrorResult;
|
|||||||
import ink.wgink.pojo.result.SuccessResultList;
|
import ink.wgink.pojo.result.SuccessResultList;
|
||||||
import ink.wgink.properties.FileProperties;
|
import ink.wgink.properties.FileProperties;
|
||||||
import ink.wgink.util.ResourceUtil;
|
import ink.wgink.util.ResourceUtil;
|
||||||
import ink.wgink.util.UUIDUtil;
|
|
||||||
import ink.wgink.util.map.HashMapUtil;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -48,26 +44,20 @@ import java.util.*;
|
|||||||
public class FileServiceImpl extends DefaultBaseService implements IFileService {
|
public class FileServiceImpl extends DefaultBaseService implements IFileService {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(FileServiceImpl.class);
|
private static final Logger LOG = LoggerFactory.getLogger(FileServiceImpl.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileProperties fileProperties;
|
private FileProperties fileProperties;
|
||||||
@Autowired
|
@Autowired
|
||||||
private IFileDao fileDao;
|
|
||||||
@Autowired
|
|
||||||
private IDefaultFileService defaultFileService;
|
private IDefaultFileService defaultFileService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private IMinIoFileService minIoFileService;
|
private IMinIoFileService minIoFileService;
|
||||||
|
|
||||||
public List<FileInfoDTO> listFileInfo(Map<String, Object> params) {
|
|
||||||
return fileDao.listInfo(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SuccessResultList<List<FileInfoDTO>> listPageInfo(ListPage page) {
|
public SuccessResultList<List<FileInfoDTO>> listPageInfo(ListPage page) {
|
||||||
PageHelper.startPage(page.getPage(), page.getRows());
|
if (fileProperties.getUseMinIo()) {
|
||||||
List<FileInfoDTO> fileInfoDTOs = fileDao.listInfo(page.getParams());
|
return minIoFileService.listPageInfo(page);
|
||||||
PageInfo<FileInfoDTO> pageInfo = new PageInfo<>(fileInfoDTOs);
|
} else {
|
||||||
return new SuccessResultList<>(fileInfoDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
|
return defaultFileService.listPageInfo(page);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -77,10 +67,11 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(List<String> ids) {
|
public void remove(List<String> ids) {
|
||||||
Map<String, Object> params = getHashMap(2);
|
if (fileProperties.getUseMinIo()) {
|
||||||
params.put("fileIds", ids);
|
minIoFileService.remove(ids);
|
||||||
setUpdateInfo(params);
|
} else {
|
||||||
fileDao.remove(params);
|
defaultFileService.remove(ids);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -98,9 +89,8 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService
|
|||||||
minIoFileService.deleteFile(filePOs);
|
minIoFileService.deleteFile(filePOs);
|
||||||
} else {
|
} else {
|
||||||
defaultFileService.deleteFile(filePOs);
|
defaultFileService.deleteFile(filePOs);
|
||||||
|
defaultFileService.delete(ids);
|
||||||
}
|
}
|
||||||
// 删除记录
|
|
||||||
fileDao.delete(params);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -182,27 +172,29 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FileDTO> list(List<String> idList) throws SearchException {
|
public List<FileDTO> list(List<String> idList) throws SearchException {
|
||||||
Map<String, Object> params = getHashMap(1);
|
if (fileProperties.getUseMinIo()) {
|
||||||
params.put("ids", idList);
|
return minIoFileService.list(idList);
|
||||||
return fileDao.list(params);
|
} else {
|
||||||
|
return defaultFileService.list(idList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FilePO> listPO(List<String> idList) {
|
public List<FilePO> listPO(List<String> idList) {
|
||||||
Map<String, Object> params = getHashMap(2);
|
if (fileProperties.getUseMinIo()) {
|
||||||
params.put("fileIds", idList);
|
return minIoFileService.listPO(idList);
|
||||||
return fileDao.listPO(params);
|
} else {
|
||||||
|
return defaultFileService.listPO(idList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String saveFileByUserId(String userId, FileVO fileVO) {
|
public String saveFileByUserId(String userId, FileVO fileVO) {
|
||||||
String fileId = UUIDUtil.getUUID();
|
if (fileProperties.getUseMinIo()) {
|
||||||
Map<String, Object> params = HashMapUtil.beanToMap(fileVO);
|
return minIoFileService.saveFileByUserId(userId, fileVO);
|
||||||
params.put("fileId", fileId);
|
} else {
|
||||||
params.put("isBack", 0);
|
return defaultFileService.saveFileByUserId(userId, fileVO);
|
||||||
setSaveInfoByUserId(params, userId);
|
}
|
||||||
fileDao.save(params);
|
|
||||||
return fileId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -218,19 +210,11 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange) {
|
public boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange) {
|
||||||
FilePO filePO = fileDao.getPO(params);
|
|
||||||
if (null == filePO) {
|
|
||||||
throw new FileException("文件不存在");
|
|
||||||
}
|
|
||||||
boolean isOpen = Boolean.valueOf(params.get("isOpen").toString());
|
|
||||||
// 没有备份的数据从本地下载
|
|
||||||
if (filePO.getIsBack() == 0) {
|
|
||||||
return defaultFileService.downLoadFile(request, response, filePO, isOpen, canRange);
|
|
||||||
}
|
|
||||||
if (fileProperties.getUseMinIo()) {
|
if (fileProperties.getUseMinIo()) {
|
||||||
return minIoFileService.downLoadFile(request, response, filePO, isOpen, canRange);
|
return minIoFileService.downLoadFile(request, response, params, canRange);
|
||||||
|
} else {
|
||||||
|
return defaultFileService.downLoadFile(request, response, params, canRange);
|
||||||
}
|
}
|
||||||
throw new FileException("文件下载异常");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -270,7 +254,15 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FilePO getPO(Map<String, Object> params) throws SearchException {
|
public FilePO getPO(Map<String, Object> params) throws SearchException {
|
||||||
return fileDao.getPO(params);
|
if (params.get("fileId") == null) {
|
||||||
|
throw new ParamsException("fileId不能为空");
|
||||||
|
}
|
||||||
|
String fileId = params.get("fileId").toString();
|
||||||
|
if (fileProperties.getUseMinIo()) {
|
||||||
|
return minIoFileService.getPO(fileId);
|
||||||
|
} else {
|
||||||
|
return defaultFileService.getPO(fileId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -286,14 +278,22 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService
|
|||||||
int pageSize = Integer.parseInt(params.get("size").toString());
|
int pageSize = Integer.parseInt(params.get("size").toString());
|
||||||
int pageNum = start / pageSize + 1;
|
int pageNum = start / pageSize + 1;
|
||||||
String downloadFile = "true";
|
String downloadFile = "true";
|
||||||
|
String fileUrl = null;
|
||||||
if (UEDITOR_LIST_IMAGE.equals(action)) {
|
if (UEDITOR_LIST_IMAGE.equals(action)) {
|
||||||
params.put("fileUrl", "images");
|
fileUrl = "images";
|
||||||
} else if (UEDITOR_LIST_FILE.equals(action)) {
|
} else {
|
||||||
params.put("fileUrl", "files");
|
// UEDITOR_LIST_FILE.equals(action)
|
||||||
|
fileUrl = "files";
|
||||||
downloadFile = "false";
|
downloadFile = "false";
|
||||||
}
|
}
|
||||||
PageHelper.startPage(pageNum, pageSize);
|
PageHelper.startPage(pageNum, pageSize);
|
||||||
List<FileDTO> listFileDTOs = fileDao.list(params);
|
List<FileDTO> listFileDTOs;
|
||||||
|
if (fileProperties.getUseMinIo()) {
|
||||||
|
listFileDTOs = minIoFileService.listByUrl(fileUrl);
|
||||||
|
} else {
|
||||||
|
listFileDTOs = defaultFileService.listByUrl(fileUrl);
|
||||||
|
}
|
||||||
|
|
||||||
PageInfo<FileDTO> pageInfo = new PageInfo<>(listFileDTOs);
|
PageInfo<FileDTO> pageInfo = new PageInfo<>(listFileDTOs);
|
||||||
List<Map<String, String>> fileList = new ArrayList<>();
|
List<Map<String, String>> fileList = new ArrayList<>();
|
||||||
for (FileDTO fileDTO : listFileDTOs) {
|
for (FileDTO fileDTO : listFileDTOs) {
|
||||||
|
@ -5,12 +5,20 @@ import ink.wgink.exceptions.FileException;
|
|||||||
import ink.wgink.exceptions.base.SystemException;
|
import ink.wgink.exceptions.base.SystemException;
|
||||||
import ink.wgink.module.file.dao.IFileDao;
|
import ink.wgink.module.file.dao.IFileDao;
|
||||||
import ink.wgink.module.file.enums.UploadTypeEnum;
|
import ink.wgink.module.file.enums.UploadTypeEnum;
|
||||||
|
import ink.wgink.module.file.pojo.dtos.FileDTO;
|
||||||
|
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
|
||||||
|
import ink.wgink.module.file.pojo.vos.FileVO;
|
||||||
|
import ink.wgink.module.file.service.IDefaultFileService;
|
||||||
import ink.wgink.module.file.service.IFileService;
|
import ink.wgink.module.file.service.IFileService;
|
||||||
import ink.wgink.module.file.service.IMinIoFileService;
|
import ink.wgink.module.file.service.IMinIoFileService;
|
||||||
|
import ink.wgink.pojo.ListPage;
|
||||||
import ink.wgink.pojo.pos.FilePO;
|
import ink.wgink.pojo.pos.FilePO;
|
||||||
|
import ink.wgink.pojo.result.SuccessResultList;
|
||||||
import ink.wgink.properties.FileMinIoProperties;
|
import ink.wgink.properties.FileMinIoProperties;
|
||||||
import ink.wgink.properties.FileProperties;
|
import ink.wgink.properties.FileProperties;
|
||||||
|
import ink.wgink.util.ArrayListUtil;
|
||||||
import ink.wgink.util.UUIDUtil;
|
import ink.wgink.util.UUIDUtil;
|
||||||
|
import ink.wgink.util.map.HashMapUtil;
|
||||||
import ink.wgink.util.request.StaticResourceRequestUtil;
|
import ink.wgink.util.request.StaticResourceRequestUtil;
|
||||||
import io.minio.*;
|
import io.minio.*;
|
||||||
import io.minio.errors.*;
|
import io.minio.errors.*;
|
||||||
@ -18,6 +26,10 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.joda.time.DateTimeZone;
|
import org.joda.time.DateTimeZone;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -30,8 +42,10 @@ import java.io.OutputStream;
|
|||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ClassName: MinIOServiceImpl
|
* @ClassName: MinIOServiceImpl
|
||||||
@ -43,10 +57,15 @@ import java.util.Map;
|
|||||||
@Service
|
@Service
|
||||||
public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFileService {
|
public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFileService {
|
||||||
|
|
||||||
|
public static final String UPLOAD_FILE_COLLECTION_NAME = "uploadFiles";
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileProperties fileProperties;
|
private FileProperties fileProperties;
|
||||||
@Autowired
|
@Autowired
|
||||||
private IFileDao fileDao;
|
private IFileDao fileDao;
|
||||||
|
@Autowired
|
||||||
|
private IDefaultFileService defaultFileService;
|
||||||
|
@Autowired(required = false)
|
||||||
|
private MongoTemplate mongoTemplate;
|
||||||
private FileMinIoProperties minIoProperties;
|
private FileMinIoProperties minIoProperties;
|
||||||
private MinioClient minioClient;
|
private MinioClient minioClient;
|
||||||
|
|
||||||
@ -63,8 +82,41 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteFile(List<FilePO> filePOs) {
|
public String saveFileByUserId(String userId, FileVO fileVO) {
|
||||||
|
String fileId = defaultFileService.saveFileByUserId(userId, fileVO);
|
||||||
|
if (mongoTemplate != null) {
|
||||||
|
Map<String, Object> params = HashMapUtil.beanToMap(fileVO);
|
||||||
|
params.put("fileId", fileId);
|
||||||
|
params.put("isBack", 0);
|
||||||
|
setSaveInfoByUserId(params, userId);
|
||||||
|
mongoTemplate.insert(params);
|
||||||
|
}
|
||||||
|
return fileId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(List<String> ids) {
|
||||||
|
if (ids.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
defaultFileService.remove(ids);
|
||||||
|
// 删除缓存
|
||||||
|
if (mongoTemplate != null) {
|
||||||
|
mongoTemplate.remove(Query.query(Criteria.where("fileId").in(ids)), UPLOAD_FILE_COLLECTION_NAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteFile(List<FilePO> filePOs) {
|
||||||
|
// 删除数据
|
||||||
|
if (filePOs.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<String> fileIds = ArrayListUtil.listBeanStringIdValue(filePOs, "fileId", FilePO.class);
|
||||||
|
defaultFileService.delete(fileIds);
|
||||||
|
if (mongoTemplate != null) {
|
||||||
|
mongoTemplate.remove(Query.query(Criteria.where("fileId").in(fileIds)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -77,6 +129,22 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi
|
|||||||
uploadFile(userId, uploadFile, uploadTypeEnum, params, true);
|
uploadFile(userId, uploadFile, uploadTypeEnum, params, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params, boolean canRange) {
|
||||||
|
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 (filePO == null) {
|
||||||
|
filePO = fileDao.getPO(params);
|
||||||
|
}
|
||||||
|
if (filePO == null) {
|
||||||
|
throw new FileException("文件不存在");
|
||||||
|
}
|
||||||
|
boolean isOpen = Boolean.valueOf(params.get("isOpen").toString());
|
||||||
|
return downLoadFile(request, response, filePO, isOpen, canRange);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件
|
* 上传文件
|
||||||
*
|
*
|
||||||
@ -146,9 +214,7 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi
|
|||||||
long startRange = 0;
|
long startRange = 0;
|
||||||
long endRange = contentLength - 1;
|
long endRange = contentLength - 1;
|
||||||
if (!StringUtils.isBlank(rangeString)) {
|
if (!StringUtils.isBlank(rangeString)) {
|
||||||
if (!isOpen) {
|
response.setContentType("multipart/byteranges");
|
||||||
response.setContentType("multipart/byteranges");
|
|
||||||
}
|
|
||||||
String[] rangeArray = rangeString.substring(rangeString.indexOf("=") + 1).split("-");
|
String[] rangeArray = rangeString.substring(rangeString.indexOf("=") + 1).split("-");
|
||||||
startRange = Long.valueOf(rangeArray[0]);
|
startRange = Long.valueOf(rangeArray[0]);
|
||||||
if (rangeArray.length > 1) {
|
if (rangeArray.length > 1) {
|
||||||
@ -183,6 +249,76 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi
|
|||||||
return isDownloadComplete;
|
return isDownloadComplete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SuccessResultList<List<FileInfoDTO>> listPageInfo(ListPage page) {
|
||||||
|
SuccessResultList<List<FileInfoDTO>> 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<FileInfoDTO> dataDTOs = mongoTemplate.find(query, FileInfoDTO.class, UPLOAD_FILE_COLLECTION_NAME);
|
||||||
|
successResultList = new SuccessResultList<>(dataDTOs, page.getPage(), total);
|
||||||
|
}
|
||||||
|
if (successResultList != null && !successResultList.getRows().isEmpty()) {
|
||||||
|
return successResultList;
|
||||||
|
}
|
||||||
|
return defaultFileService.listPageInfo(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FileDTO> list(List<String> idList) {
|
||||||
|
List<FileDTO> fileDTOs = new ArrayList<>();
|
||||||
|
if (mongoTemplate != null) {
|
||||||
|
return mongoTemplate.find(Query.query(Criteria.where("fileId").in(idList)), FileDTO.class, UPLOAD_FILE_COLLECTION_NAME);
|
||||||
|
}
|
||||||
|
if (!fileDTOs.isEmpty()) {
|
||||||
|
return fileDTOs;
|
||||||
|
}
|
||||||
|
return defaultFileService.list(idList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FilePO> listPO(List<String> idList) {
|
||||||
|
List<FilePO> filePOs = new ArrayList<>();
|
||||||
|
if (mongoTemplate != null) {
|
||||||
|
filePOs = mongoTemplate.find(Query.query(Criteria.where("fileId").in(idList)), FilePO.class, UPLOAD_FILE_COLLECTION_NAME);
|
||||||
|
}
|
||||||
|
if (!filePOs.isEmpty()) {
|
||||||
|
return filePOs;
|
||||||
|
}
|
||||||
|
return defaultFileService.listPO(idList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 (filePO != null) {
|
||||||
|
return filePO;
|
||||||
|
}
|
||||||
|
return defaultFileService.getPO(fileId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FileDTO> listByUrl(String fileUrl) {
|
||||||
|
List<FileDTO> 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);
|
||||||
|
}
|
||||||
|
if (!fileDTOs.isEmpty()) {
|
||||||
|
return fileDTOs;
|
||||||
|
}
|
||||||
|
return defaultFileService.listByUrl(fileUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件到minIo
|
* 上传文件到minIo
|
||||||
@ -222,6 +358,9 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi
|
|||||||
setAppSaveInfo(token, params);
|
setAppSaveInfo(token, params);
|
||||||
}
|
}
|
||||||
fileDao.save(params);
|
fileDao.save(params);
|
||||||
|
if (mongoTemplate != null) {
|
||||||
|
mongoTemplate.insert(params, UPLOAD_FILE_COLLECTION_NAME);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -238,6 +377,9 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi
|
|||||||
Map<String, Object> params = getFileParams(fileId, fileName, fileType, fileSize, bucketName);
|
Map<String, Object> params = getFileParams(fileId, fileName, fileType, fileSize, bucketName);
|
||||||
setSaveInfoByUserId(params, userId);
|
setSaveInfoByUserId(params, userId);
|
||||||
fileDao.save(params);
|
fileDao.save(params);
|
||||||
|
if (mongoTemplate != null) {
|
||||||
|
mongoTemplate.insert(params, UPLOAD_FILE_COLLECTION_NAME);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> getFileParams(String fileId, String fileName, String fileType, long fileSize, String bucketName) {
|
private Map<String, Object> getFileParams(String fileId, String fileName, String fileType, long fileSize, String bucketName) {
|
||||||
|
Loading…
Reference in New Issue
Block a user