文件保存路径修改为:仅在配置文件配置总路径,子路径在FilePathEnum处理及转换

This commit is contained in:
cuizhibin 2025-06-23 14:56:15 +08:00
parent 4c7b7b4347
commit fe75768d05
8 changed files with 86 additions and 88 deletions

View File

@ -1,11 +1,14 @@
package com.dite.znpt.config;
import cn.hutool.core.collection.ListUtil;
import com.dite.znpt.constant.Constants;
import com.dite.znpt.enums.FilePathEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -18,23 +21,14 @@ import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Value(value = "${upload.temp-path.image}")
private String uploadTempPath;
@Value(value = "${upload.perm-path.image}")
private String uploadPermPath;
@Value(value = "${upload.perm-path.video}")
private String uploadPermVideoPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
registry.addResourceHandler(Constants.PERM_IMAGE_PATH + "**").addResourceLocations("file:" + uploadPermPath);
registry.addResourceHandler(Constants.PERM_VIDEO_PATH + "**").addResourceLocations("file:" + uploadPermVideoPath);
registry.addResourceHandler(Constants.TEMP_IMAGE_PATH + "**").addResourceLocations("file:" + uploadTempPath);
for (FilePathEnum pathEnum : FilePathEnum.values()) {
registry.addResourceHandler(pathEnum.getUrlPath() + "**").addResourceLocations("file:" + pathEnum.getFileAbsolutePath());
}
}
// @Override
@ -51,7 +45,11 @@ public class WebMvcConfig implements WebMvcConfigurer {
// 动态获取哪些 path 可以忽略鉴权
public List<String> excludePaths() {
// 此处仅为示例实际项目你可以写任意代码来查询这些path
return Arrays.asList("/auth/login", "/favicon.ico", "/favicon.ico", "/doc.html", "/swagger-ui/**", "/swagger-resources","/webjars/**", "/v3/api-docs/**", "/**/v3/api-docs", "/static/image/**","/static/image/temp/**");
ArrayList<String> list = ListUtil.toList("/auth/login", "/favicon.ico", "/favicon.ico", "/doc.html", "/swagger-ui/**", "/swagger-resources", "/webjars/**", "/v3/api-docs/**", "/**/v3/api-docs");
for (FilePathEnum pathEnum : FilePathEnum.values()) {
list.add(pathEnum.getUrlPath() + "**");
}
return list;
}
}

View File

@ -86,11 +86,4 @@ public class Constants {
* 1:代表隐藏
*/
public static final String VISIBLE_1 = "1";
// 持久化图片路径请求前缀
public static final String TEMP_IMAGE_PATH = "/static/image/temp/";
// 临时图片路径请求前缀
public static final String PERM_IMAGE_PATH = "/static/image/";
// 持久化视频文件路径请求前缀
public static final String PERM_VIDEO_PATH = "/static/video/";
}

View File

@ -1,6 +1,7 @@
package com.dite.znpt.domain;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
@ -10,6 +11,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
@ -22,20 +24,21 @@ import java.util.List;
@NoArgsConstructor
@Data
public class AuditableEntity implements Serializable {
@Serial
private static final long serialVersionUID = 141481953116476081L;
@ApiParam(hidden = true)
@ApiModelProperty(value = "创建人id", hidden = true)
private String createBy;
@ApiParam(hidden = true)
@ApiModelProperty(value = "更新时间", hidden = true)
private String updateBy;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "创建时间", example = "2022-01-22", notes = "创建时间", hidden = true)
@ApiModelProperty(value = "创建时间", hidden = true)
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(hidden = true)
@ApiModelProperty(value = "修改人id", hidden = true)
private LocalDateTime updateTime;
@ExcelIgnore

View File

@ -0,0 +1,47 @@
package com.dite.znpt.enums;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.core.env.Environment;
@Getter
@AllArgsConstructor
public enum FilePathEnum {
IMAGE("/static/image/", "/image/"),
IMAGE_TEMP("/static/image/temp/", "/image/temp/"),
VIDEO("/static/video/", "/video/"),
AUDIO("/static/audio/", "/audio/"),
;
private final String urlPath;
private final String fileRelativePath;
/**
* 功能描述获取文件绝对路径
*
* @return {@link String }
* @author cuizhibin
* @date 2025/06/23 14:46
**/
public String getFileAbsolutePath() {
return SpringUtil.getBean(Environment.class).getProperty("upload.save-path") + fileRelativePath;
}
/**
* 功能描述获取图像下载路径
*
* @param absolutePath
* @return {@link String }
* @author cuizhibin
* @date 2025/06/06 09:07
**/
public String getImageDownPath(String absolutePath) {
String relativePath = StrUtil.removePrefix(absolutePath, getFileAbsolutePath());
return StrUtil.replace(urlPath.concat(relativePath), FileUtil.FILE_SEPARATOR, StrUtil.SLASH);
}
}

View File

@ -10,6 +10,7 @@ import com.dite.znpt.converts.Converts;
import com.dite.znpt.domain.entity.ImageCollectEntity;
import com.dite.znpt.domain.entity.ImageEntity;
import com.dite.znpt.domain.vo.ImageCollectReq;
import com.dite.znpt.enums.FilePathEnum;
import com.dite.znpt.enums.ImageSourceEnum;
import com.dite.znpt.exception.ServiceException;
import com.dite.znpt.mapper.ImageCollectMapper;
@ -34,11 +35,6 @@ import java.util.List;
@Service
public class ImageCollectServiceImpl extends ServiceImpl<ImageCollectMapper, ImageCollectEntity> implements ImageCollectService {
@Value("${upload.perm-path.image}")
private String permPath;
@Value("${upload.temp-path.image}")
private String tempPath;
@Resource
private ImageService imageService;
@ -58,8 +54,8 @@ public class ImageCollectServiceImpl extends ServiceImpl<ImageCollectMapper, Ima
this.save(imageCollect);
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
List<ImageEntity> imageList = Converts.INSTANCE.toImageEntity(req.getImageList());
String permPathPrefix = permPath.concat(ImageSourceEnum.COLLECT.getCode()).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR).concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
String temPathPrefix = tempPath.concat(ImageSourceEnum.COLLECT.getCode()).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR);
String permPathPrefix = FilePathEnum.IMAGE.getFileAbsolutePath().concat(ImageSourceEnum.COLLECT.getCode()).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR).concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
String temPathPrefix = FilePathEnum.IMAGE_TEMP.getFileAbsolutePath().concat(ImageSourceEnum.COLLECT.getCode()).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR);
imageList.forEach(image -> {
image.setPartId(partId);
image.setCollectId(imageCollect.getCollectId());
@ -68,7 +64,7 @@ public class ImageCollectServiceImpl extends ServiceImpl<ImageCollectMapper, Ima
if (file.exists()) {
byte[] bytes = FileUtil.readBytes(file);
FileUtil.writeBytes(bytes, path);
String url = Constants.PERM_IMAGE_PATH.concat(StrUtil.removePrefix(path,permPath).replace(FileUtil.FILE_SEPARATOR, StrUtil.SLASH));
String url = FilePathEnum.IMAGE.getUrlPath().concat(StrUtil.removePrefix(path,FilePathEnum.IMAGE.getFileAbsolutePath()).replace(FileUtil.FILE_SEPARATOR, StrUtil.SLASH));
image.setImagePath(url);
FileUtil.del(file);
}else {

View File

@ -19,10 +19,7 @@ import com.dite.znpt.domain.entity.ImageEntity;
import com.dite.znpt.domain.entity.PartEntity;
import com.dite.znpt.domain.page.PageDomain;
import com.dite.znpt.domain.vo.*;
import com.dite.znpt.enums.ImageSourceEnum;
import com.dite.znpt.enums.ImageTypeEnum;
import com.dite.znpt.enums.ShootingMethodEnum;
import com.dite.znpt.enums.WeatherEnum;
import com.dite.znpt.enums.*;
import com.dite.znpt.exception.ServiceException;
import com.dite.znpt.mapper.ImageMapper;
import com.dite.znpt.service.ImageCollectService;
@ -71,11 +68,6 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
@Resource
private PartService partService;
@Value("${upload.temp-path.image}")
private String tempPath;
@Value("${upload.perm-path.image}")
private String permPath;
@Override
public List<ImageListResp> list(ImageListReq req) {
List<ImageListResp> partList= this.baseMapper.queryImageList(req);
@ -100,8 +92,8 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
Map<String, PartEntity> partIdMap= partService.listByIds(partIds).stream().collect(Collectors.toMap(PartEntity::getPartId, Function.identity()));
list.forEach(req -> {
if(partIdMap.containsKey(req.getPartId())){
String path_prefix = permPath.concat(StrUtil.BACKSLASH).concat(req.getImageSource()).concat(StrUtil.BACKSLASH).concat(req.getPartId()).concat(StrUtil.BACKSLASH);
String temp_path_prefix = tempPath.concat(StrUtil.BACKSLASH).concat(req.getImageSource()).concat(StrUtil.BACKSLASH).concat(req.getPartId()).concat(StrUtil.BACKSLASH);
String path_prefix = FilePathEnum.IMAGE.getFileAbsolutePath().concat(StrUtil.BACKSLASH).concat(req.getImageSource()).concat(StrUtil.BACKSLASH).concat(req.getPartId()).concat(StrUtil.BACKSLASH);
String temp_path_prefix = FilePathEnum.IMAGE_TEMP.getFileAbsolutePath().concat(StrUtil.BACKSLASH).concat(req.getImageSource()).concat(StrUtil.BACKSLASH).concat(req.getPartId()).concat(StrUtil.BACKSLASH);
File file = new File(req.getImagePath());
if(file.exists() && req.getImagePath().contains(temp_path_prefix)){
ImageEntity entity = new ImageEntity();
@ -140,12 +132,12 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
throw new ServiceException(Message.IMAGE_IS_EMPTY);
}
List<ImageReq> list = new ArrayList<>(files.length);
File temCategory = new File(tempPath);
File temCategory = new File(FilePathEnum.IMAGE_TEMP.getFileAbsolutePath());
if (!temCategory.exists()) {
// 创建完整的目录
temCategory.mkdirs();
}
String temPathPrefix = tempPath.concat(imageSource).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR);
String temPathPrefix = FilePathEnum.IMAGE_TEMP.getFileAbsolutePath().concat(imageSource).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR);
for (MultipartFile file : files) {
if (!file.isEmpty()) {
try {
@ -167,7 +159,7 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
throw new ServiceException(Message.IMAGE_IS_EMPTY);
}
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String path_prefix = permPath.concat(imageSource).concat(FileUtil.FILE_SEPARATOR).concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
String path_prefix = FilePathEnum.IMAGE.getFileAbsolutePath().concat(imageSource).concat(FileUtil.FILE_SEPARATOR).concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
if (Objects.nonNull(imageWorkReq)) {
path_prefix = path_prefix.concat(StrUtil.emptyToDefault(imageWorkReq.getUploadUser(), "默认用户")).concat(FileUtil.FILE_SEPARATOR)
.concat(StrUtil.emptyToDefault(imageWorkReq.getLongitude(), "0"))
@ -182,7 +174,7 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
for (MultipartFile multipartFile : files) {
String path = path_prefix + multipartFile.getOriginalFilename();
FileUtil.writeBytes(multipartFile.getBytes(),path);
result.add(getImageDownPath(StrUtil.removePrefix(path, permPath)));
result.add(FilePathEnum.IMAGE.getImageDownPath(path));
}
String partId = imageWorkReq.getPartId();
if (Objects.nonNull(partId)) {
@ -196,7 +188,7 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
List<ImageEntity> imageList = new ArrayList<>();
result.forEach(path -> {
ImageEntity imageEntity = new ImageEntity();
String absolutePath = permPath + StrUtil.removePrefix(path, Constants.PERM_IMAGE_PATH);
String absolutePath = FilePathEnum.IMAGE.getFileAbsolutePath() + StrUtil.removePrefix(path, FilePathEnum.IMAGE.getUrlPath());
try {
ImageReq imageReq = imageRespBuilder(absolutePath);
BeanUtil.copyProperties(imageReq, imageEntity);
@ -245,7 +237,7 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
req.setCameraManufacturer(obj.getStr("Make"));
req.setCameraModel(obj.getStr("Model"));
req.setImageName(obj.getStr("File Name"));
req.setImagePath(getTmpImageDownPath(StrUtil.removePrefix(path, tempPath)));
req.setImagePath(FilePathEnum.IMAGE_TEMP.getImageDownPath(path));
BigDecimal imageSize = new BigDecimal(extractDigit(obj.getStr("File Size"))).divide(new BigDecimal(1024*1024), 4, RoundingMode.HALF_UP);
req.setImageSize(imageSize.toString().concat("M"));
req.setImageWidth(extractDigit(obj.getStr("Image Width")));
@ -299,41 +291,14 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
}
}
/**
* 功能描述获取图像下载路径
*
* @param relativePath
* @return {@link String }
* @author cuizhibin
* @date 2025/06/06 09:07
**/
public static String getImageDownPath(String relativePath) {
String url = Constants.PERM_IMAGE_PATH.concat(relativePath);
return StrUtil.replace(url, FileUtil.FILE_SEPARATOR, StrUtil.SLASH);
}
/**
* 功能描述获取tmp图像下载路径
*
* @param relativePath
* @return {@link String }
* @author cuizhibin
* @date 2025/06/06 09:07
**/
public static String getTmpImageDownPath(String relativePath) {
String url = Constants.TEMP_IMAGE_PATH.concat(StrUtil.SLASH).concat(relativePath);
return StrUtil.replace(url, FileUtil.FILE_SEPARATOR, StrUtil.SLASH);
}
@Override
public List<AppImageResp> listAppUploadImages() {
List<String> filePaths = new ArrayList<>();
PathUtil.walkFiles(Path.of(permPath), new SimpleFileVisitor<>() {
PathUtil.walkFiles(Path.of(FilePathEnum.IMAGE.getFileAbsolutePath()), new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
if (path.toFile().isFile()) {
String relativePath = StrUtil.removePrefix(path.toFile().getAbsolutePath(), permPath);
String imageDownPath = getImageDownPath(relativePath);
String imageDownPath = FilePathEnum.IMAGE.getImageDownPath(path.toFile().getAbsolutePath());
List<String> split = StrUtil.split(imageDownPath, StrUtil.SLASH, true, true);
// /static/image/source/date
try {
@ -426,7 +391,7 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
List<ImageEntity> newImageList = new ArrayList<>();
partReq.getImagePaths().forEach(path -> {
ImageEntity imageEntity = new ImageEntity();
String absolutePath = permPath + StrUtil.removePrefix(path, Constants.PERM_IMAGE_PATH);
String absolutePath = FilePathEnum.IMAGE.getFileAbsolutePath() + StrUtil.removePrefix(path, FilePathEnum.IMAGE.getUrlPath());
try {
ImageReq imageReq = imageRespBuilder(absolutePath);
BeanUtil.copyProperties(imageReq, imageEntity);

View File

@ -10,6 +10,7 @@ import com.dite.znpt.domain.entity.VideoFileInfoEntity;
import com.dite.znpt.domain.vo.VideoFileInfoListReq;
import com.dite.znpt.domain.vo.VideoFileInfoReq;
import com.dite.znpt.domain.vo.VideoFileInfoResp;
import com.dite.znpt.enums.FilePathEnum;
import com.dite.znpt.exception.ServiceException;
import com.dite.znpt.service.PartService;
import com.dite.znpt.service.VideoFileInfoService;
@ -39,8 +40,6 @@ import java.util.stream.Collectors;
public class VideoFileInfoServiceImpl extends ServiceImpl<VideoFileInfoMapper, VideoFileInfoEntity> implements VideoFileInfoService {
private final PartService partService;
@Value(value = "${upload.perm-path.video}")
private String uploadPermVideoPath;
/**
* 功能描述查询视频文件信息列表
@ -121,7 +120,7 @@ public class VideoFileInfoServiceImpl extends ServiceImpl<VideoFileInfoMapper, V
throw new ServiceException(Message.IMAGE_IS_EMPTY);
}
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String path_prefix = uploadPermVideoPath.concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
String path_prefix = FilePathEnum.VIDEO.getFileAbsolutePath().concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
if (!FileUtil.exist(path_prefix)) {
FileUtil.mkdir(path_prefix);
}
@ -130,7 +129,7 @@ public class VideoFileInfoServiceImpl extends ServiceImpl<VideoFileInfoMapper, V
VideoFileInfoEntity info = BeanUtil.copyProperties(infoReq, VideoFileInfoEntity.class);
String path = path_prefix + multipartFile.getOriginalFilename();
FileUtil.writeBytes(multipartFile.getBytes(), path);
info.setFilePath(Constants.PERM_VIDEO_PATH + StrUtil.removePrefix(path, uploadPermVideoPath).replace(FileUtil.FILE_SEPARATOR, StrUtil.SLASH));
info.setFilePath(FilePathEnum.VIDEO.getUrlPath() + StrUtil.removePrefix(path, FilePathEnum.VIDEO.getFileAbsolutePath()).replace(FileUtil.FILE_SEPARATOR, StrUtil.SLASH));
result.add(info);
}
baseMapper.insert(result);

View File

@ -134,9 +134,6 @@ zlm-config:
dynamicPortEnd: 30185
upload:
temp-path:
image: D:\Upload\Image\Temp\
perm-path:
image: D:\Upload\Image\
video: D:\Upload\Video\
# 此处仅定义总的父路径,细节定义到 FilePathEnum
save-path: D:\Upload\