diff --git a/core/src/main/java/com/dite/znpt/config/WebMvcConfig.java b/core/src/main/java/com/dite/znpt/config/WebMvcConfig.java index 609a88f..7f8d651 100644 --- a/core/src/main/java/com/dite/znpt/config/WebMvcConfig.java +++ b/core/src/main/java/com/dite/znpt/config/WebMvcConfig.java @@ -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 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 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; } } diff --git a/core/src/main/java/com/dite/znpt/constant/Constants.java b/core/src/main/java/com/dite/znpt/constant/Constants.java index 6e6d2b9..bb6b6d4 100644 --- a/core/src/main/java/com/dite/znpt/constant/Constants.java +++ b/core/src/main/java/com/dite/znpt/constant/Constants.java @@ -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/"; } diff --git a/core/src/main/java/com/dite/znpt/domain/AuditableEntity.java b/core/src/main/java/com/dite/znpt/domain/AuditableEntity.java index 7994845..0eb68a1 100644 --- a/core/src/main/java/com/dite/znpt/domain/AuditableEntity.java +++ b/core/src/main/java/com/dite/znpt/domain/AuditableEntity.java @@ -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 diff --git a/core/src/main/java/com/dite/znpt/enums/FilePathEnum.java b/core/src/main/java/com/dite/znpt/enums/FilePathEnum.java new file mode 100644 index 0000000..46ed9bb --- /dev/null +++ b/core/src/main/java/com/dite/znpt/enums/FilePathEnum.java @@ -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); + } + +} diff --git a/core/src/main/java/com/dite/znpt/service/impl/ImageCollectServiceImpl.java b/core/src/main/java/com/dite/znpt/service/impl/ImageCollectServiceImpl.java index 8188df1..3271a0f 100644 --- a/core/src/main/java/com/dite/znpt/service/impl/ImageCollectServiceImpl.java +++ b/core/src/main/java/com/dite/znpt/service/impl/ImageCollectServiceImpl.java @@ -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 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 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 impl @Resource private PartService partService; - @Value("${upload.temp-path.image}") - private String tempPath; - - @Value("${upload.perm-path.image}") - private String permPath; @Override public List list(ImageListReq req) { List partList= this.baseMapper.queryImageList(req); @@ -100,8 +92,8 @@ public class ImageServiceImpl extends ServiceImpl impl Map 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 impl throw new ServiceException(Message.IMAGE_IS_EMPTY); } List 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 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 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 impl List 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 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 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 listAppUploadImages() { List 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 split = StrUtil.split(imageDownPath, StrUtil.SLASH, true, true); // /static/image/source/date try { @@ -426,7 +391,7 @@ public class ImageServiceImpl extends ServiceImpl impl List 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); diff --git a/core/src/main/java/com/dite/znpt/service/impl/VideoFileInfoServiceImpl.java b/core/src/main/java/com/dite/znpt/service/impl/VideoFileInfoServiceImpl.java index dafbca4..a93ee3c 100644 --- a/core/src/main/java/com/dite/znpt/service/impl/VideoFileInfoServiceImpl.java +++ b/core/src/main/java/com/dite/znpt/service/impl/VideoFileInfoServiceImpl.java @@ -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 implements VideoFileInfoService { private final PartService partService; - @Value(value = "${upload.perm-path.video}") - private String uploadPermVideoPath; /** * 功能描述:查询视频文件信息列表 @@ -121,7 +120,7 @@ public class VideoFileInfoServiceImpl extends ServiceImpl