mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-10-08 21:26:40 +08:00
update file service
This commit is contained in:
parent
7a28835f1e
commit
31894f40d9
@ -19,7 +19,7 @@ import java.util.List;
|
|||||||
* @author 罗伊
|
* @author 罗伊
|
||||||
* @date 2020/8/15 15:06
|
* @date 2020/8/15 15:06
|
||||||
*/
|
*/
|
||||||
public class FileKeyVOSerializer extends JsonSerializer<String> {
|
public class FileKeyVoSerializer extends JsonSerializer<String> {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileService fileService;
|
private FileService fileService;
|
||||||
|
@ -9,6 +9,7 @@ import net.lab1024.smartadmin.service.common.constant.StringConst;
|
|||||||
import net.lab1024.smartadmin.service.common.constant.RedisKeyConst;
|
import net.lab1024.smartadmin.service.common.constant.RedisKeyConst;
|
||||||
import net.lab1024.smartadmin.service.common.domain.PageResultDTO;
|
import net.lab1024.smartadmin.service.common.domain.PageResultDTO;
|
||||||
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
|
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
|
||||||
|
import net.lab1024.smartadmin.service.common.util.SmartStringUtil;
|
||||||
import net.lab1024.smartadmin.service.module.support.file.FileDao;
|
import net.lab1024.smartadmin.service.module.support.file.FileDao;
|
||||||
import net.lab1024.smartadmin.service.module.support.file.domain.FileEntity;
|
import net.lab1024.smartadmin.service.module.support.file.domain.FileEntity;
|
||||||
import net.lab1024.smartadmin.service.module.support.file.domain.FileFolderTypeEnum;
|
import net.lab1024.smartadmin.service.module.support.file.domain.FileFolderTypeEnum;
|
||||||
@ -145,14 +146,7 @@ public class FileService {
|
|||||||
if (CollectionUtils.isEmpty(fileKeyList)) {
|
if (CollectionUtils.isEmpty(fileKeyList)) {
|
||||||
return Lists.newArrayList();
|
return Lists.newArrayList();
|
||||||
}
|
}
|
||||||
List<FileVO> fileVOList = Lists.newArrayList();
|
return fileKeyList.stream().map(this::getCacheFileVO).collect(Collectors.toList());
|
||||||
fileKeyList.forEach(e -> {
|
|
||||||
FileVO fileVO = this.getCacheFileVO(e);
|
|
||||||
if (fileVO != null) {
|
|
||||||
fileVOList.add(fileVO);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return fileVOList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileVO getCacheFileVO(String fileKey) {
|
private FileVO getCacheFileVO(String fileKey) {
|
||||||
@ -160,11 +154,11 @@ public class FileService {
|
|||||||
FileVO fileVO = redisService.getObject(redisKey, FileVO.class);
|
FileVO fileVO = redisService.getObject(redisKey, FileVO.class);
|
||||||
if (fileVO == null) {
|
if (fileVO == null) {
|
||||||
fileVO = fileDao.getByFileKey(fileKey);
|
fileVO = fileDao.getByFileKey(fileKey);
|
||||||
if (fileVO != null) {
|
if (fileVO == null) {
|
||||||
redisService.set(redisKey, fileVO, fileStorageService.cacheExpireSecond());
|
return null;
|
||||||
}
|
}
|
||||||
|
redisService.set(redisKey, fileVO, fileStorageService.cacheExpireSecond());
|
||||||
}
|
}
|
||||||
|
|
||||||
fileVO.setFileUrl(this.getCacheUrl(fileKey));
|
fileVO.setFileUrl(this.getCacheUrl(fileKey));
|
||||||
return fileVO;
|
return fileVO;
|
||||||
}
|
}
|
||||||
@ -181,10 +175,10 @@ public class FileService {
|
|||||||
return ResponseDTO.error(UserErrorCode.PARAM_ERROR);
|
return ResponseDTO.error(UserErrorCode.PARAM_ERROR);
|
||||||
}
|
}
|
||||||
// 处理逗号分隔的字符串
|
// 处理逗号分隔的字符串
|
||||||
List<String> stringList = Arrays.asList(fileKey.split(StringConst.SEPARATOR));
|
String keyList = SmartStringUtil.splitConvertToList(fileKey, StringConst.SEPARATOR)
|
||||||
stringList = stringList.stream().map(e -> this.getCacheUrl(e)).collect(Collectors.toList());
|
.stream().map(this::getCacheUrl)
|
||||||
String result = StringUtils.join(stringList, StringConst.SEPARATOR_CHAR);
|
.collect(Collectors.joining(StringConst.SEPARATOR));
|
||||||
return ResponseDTO.ok(result);
|
return ResponseDTO.ok(keyList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ import net.lab1024.smartadmin.service.module.support.file.domain.dto.FileDownloa
|
|||||||
import net.lab1024.smartadmin.service.module.support.file.domain.dto.FileMetadataDTO;
|
import net.lab1024.smartadmin.service.module.support.file.domain.dto.FileMetadataDTO;
|
||||||
import net.lab1024.smartadmin.service.module.support.file.domain.vo.FileUploadVO;
|
import net.lab1024.smartadmin.service.module.support.file.domain.vo.FileUploadVO;
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
@ -65,7 +66,7 @@ public class FileStorageCloudServiceImpl implements IFileStorageService {
|
|||||||
public ResponseDTO<FileUploadVO> fileUpload(MultipartFile file, String path) {
|
public ResponseDTO<FileUploadVO> fileUpload(MultipartFile file, String path) {
|
||||||
// 设置文件 key
|
// 设置文件 key
|
||||||
String originalFilename = file.getOriginalFilename();
|
String originalFilename = file.getOriginalFilename();
|
||||||
String fileType = this.getFileType(originalFilename);
|
String fileType = FileUtils.getExtension(originalFilename);
|
||||||
String fileKey = path + this.generateFileName(originalFilename);
|
String fileKey = path + this.generateFileName(originalFilename);
|
||||||
// 文件名称 URL 编码
|
// 文件名称 URL 编码
|
||||||
String urlEncoderFilename;
|
String urlEncoderFilename;
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
package net.lab1024.smartadmin.service.module.support.file.service;
|
package net.lab1024.smartadmin.service.module.support.file.service;
|
||||||
|
|
||||||
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
|
import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
|
||||||
import net.lab1024.smartadmin.service.module.support.file.domain.dto.FileDownloadDTO;
|
|
||||||
import net.lab1024.smartadmin.service.module.support.file.domain.vo.FileUploadVO;
|
|
||||||
import net.lab1024.smartadmin.service.common.util.date.SmartDateFormatterEnum;
|
import net.lab1024.smartadmin.service.common.util.date.SmartDateFormatterEnum;
|
||||||
import net.lab1024.smartadmin.service.common.util.date.SmartLocalDateUtil;
|
import net.lab1024.smartadmin.service.common.util.date.SmartLocalDateUtil;
|
||||||
|
import net.lab1024.smartadmin.service.module.support.file.domain.dto.FileDownloadDTO;
|
||||||
|
import net.lab1024.smartadmin.service.module.support.file.domain.vo.FileUploadVO;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,6 +24,7 @@ public interface IFileStorageService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
|
*
|
||||||
* @param file
|
* @param file
|
||||||
* @param path
|
* @param path
|
||||||
* @return
|
* @return
|
||||||
@ -42,106 +41,29 @@ public interface IFileStorageService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 流式下载(名称为原文件)
|
* 流式下载(名称为原文件)
|
||||||
|
*
|
||||||
* @param key
|
* @param key
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
ResponseDTO<FileDownloadDTO> fileDownload(String key);
|
ResponseDTO<FileDownloadDTO> fileDownload(String key);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单个删除文件
|
* 单个删除文件
|
||||||
|
* 根据文件key删除
|
||||||
|
*
|
||||||
* @param fileKey
|
* @param fileKey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
ResponseDTO<String> delete(String fileKey);
|
ResponseDTO<String> delete(String fileKey);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存过期秒数
|
* 缓存过期秒数
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
default Long cacheExpireSecond(){
|
|
||||||
return 3600L;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 不带后缀名的文件名
|
|
||||||
*
|
*
|
||||||
* @param file
|
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
default String getNameWithoutExtension(String file) {
|
default Long cacheExpireSecond() {
|
||||||
String fileName = new File(file).getName();
|
return 3600L;
|
||||||
int dotIndex = fileName.lastIndexOf('.');
|
|
||||||
return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断指定目录的文件是否存在
|
|
||||||
* @param filePath
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
default boolean isFileExist(String filePath) {
|
|
||||||
File file = new File(filePath);
|
|
||||||
return file.exists();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证文件是否存在,如果不存在则抛出异常
|
|
||||||
*
|
|
||||||
* @param filePath
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
default void isFileExistThrowException(String filePath) throws IOException {
|
|
||||||
File file = new File(filePath);
|
|
||||||
if (!file.exists()) {
|
|
||||||
throw new FileNotFoundException(filePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件流读取
|
|
||||||
* @param file
|
|
||||||
* @param charset
|
|
||||||
* @return
|
|
||||||
* @throws FileNotFoundException
|
|
||||||
*/
|
|
||||||
default BufferedReader newBufferedReader(File file, Charset charset) throws FileNotFoundException {
|
|
||||||
return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件写
|
|
||||||
* @param file
|
|
||||||
* @param charset
|
|
||||||
* @return
|
|
||||||
* @throws FileNotFoundException
|
|
||||||
*/
|
|
||||||
default BufferedWriter newBufferedWriter(File file, Charset charset) throws FileNotFoundException {
|
|
||||||
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建文件所在目录
|
|
||||||
* @param file
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
default boolean createParentDirs(File file) throws IOException {
|
|
||||||
File parent = file.getCanonicalFile().getParentFile();
|
|
||||||
if (parent == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return parent.mkdirs();
|
|
||||||
}
|
|
||||||
|
|
||||||
default boolean createNotExistParentDirFile(File file) throws IOException {
|
|
||||||
boolean createParentDirsRes = createParentDirs(file);
|
|
||||||
if (!createParentDirsRes) {
|
|
||||||
throw new IOException("cannot create parent Directory of " + file.getName());
|
|
||||||
}
|
|
||||||
return file.createNewFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -152,50 +74,22 @@ public interface IFileStorageService {
|
|||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
default String generateFileName(String originalFileName) {
|
default String generateFileName(String originalFileName) {
|
||||||
String time = SmartLocalDateUtil.format(LocalDateTime.now(), SmartDateFormatterEnum.YMDHMS);
|
return generateFileNameByType(FileUtils.getExtension(originalFileName));
|
||||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
||||||
String fileType = FileUtils.getExtension(originalFileName);
|
|
||||||
return time + uuid + "." + fileType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据文件类型 生成文件名
|
* 根据文件类型 生成文件名
|
||||||
|
* 当前年月日时分秒 +32位 uuid + 文件格式后缀
|
||||||
|
*
|
||||||
* @param fileType
|
* @param fileType
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
default String generateFileNameByType(String fileType) {
|
default String generateFileNameByType(String fileType) {
|
||||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHH"));
|
String time = SmartLocalDateUtil.format(LocalDateTime.now(), SmartDateFormatterEnum.YMDHMS);
|
||||||
return uuid + time + "." + fileType;
|
return uuid + time + "." + fileType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件类型
|
|
||||||
*
|
|
||||||
* @param originalFileName
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
default String getFileType(String originalFileName) {
|
|
||||||
return originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件格式
|
|
||||||
*
|
|
||||||
* @param fileName
|
|
||||||
* @return 返回内容:png、mp4 等
|
|
||||||
*/
|
|
||||||
default String getFormat(String fileName) {
|
|
||||||
if (StringUtils.isBlank(fileName)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
int index = fileName.lastIndexOf(".");
|
|
||||||
if (index == -1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return fileName.substring(index + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件类型
|
* 获取文件类型
|
||||||
*
|
*
|
||||||
@ -293,27 +187,20 @@ public interface IFileStorageService {
|
|||||||
*/
|
*/
|
||||||
default String getDownloadFileNameByUA(String fileName, String userAgent) {
|
default String getDownloadFileNameByUA(String fileName, String userAgent) {
|
||||||
try {
|
try {
|
||||||
if (userAgent.toLowerCase().indexOf("firefox") > 0) {
|
userAgent = userAgent.toUpperCase();
|
||||||
// firefox浏览器
|
if (userAgent.indexOf("MSIE") > 0) {
|
||||||
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
|
|
||||||
} else if (userAgent.toUpperCase().indexOf("MSIE") > 0) {
|
|
||||||
// IE浏览器
|
// IE浏览器
|
||||||
fileName = URLEncoder.encode(fileName, "UTF-8");
|
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
|
||||||
} else if (userAgent.toUpperCase().indexOf("EDGE") > 0) {
|
} else if (userAgent.indexOf("EDGE") > 0) {
|
||||||
// WIN10浏览器
|
// WIN10浏览器
|
||||||
fileName = URLEncoder.encode(fileName, "UTF-8");
|
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
|
||||||
} else if (userAgent.toUpperCase().indexOf("CHROME") > 0) {
|
|
||||||
// 谷歌
|
|
||||||
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
|
|
||||||
} else {
|
} else {
|
||||||
//万能乱码问题解决
|
// 其他
|
||||||
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
|
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
|
||||||
}
|
}
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user