Commit 05c61c6115616019fdf47ea03f0f26bf038d0d57

Authored by xp.Huang
1 parent 0c13553f

fix: 修复图片本地上传,不能访问本地资源,同时上传的文件名称加上(yyyy-MM-dd)格式

  1 +package org.thingsboard.server.config.yunteng;
  2 +
  3 +import org.springframework.beans.factory.annotation.Value;
  4 +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
  5 +import org.springframework.context.annotation.Configuration;
  6 +import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  7 +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  8 +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  9 +
  10 +@Configuration
  11 +@EnableWebMvc
  12 +@ConditionalOnExpression("'${file.storage.type}'=='local'")
  13 +public class LocalOssMvcConfig implements WebMvcConfigurer {
  14 +
  15 + @Value("${file.storage.local.uploadDir}")
  16 + private String uploadDir;
  17 +
  18 + @Value("${file.storage.local.staticUrl}")
  19 + private String ossFileUrl;
  20 +
  21 + @Override
  22 + public void addResourceHandlers(ResourceHandlerRegistry registry) {
  23 + registry.addResourceHandler(ossFileUrl).addResourceLocations("file:" + uploadDir);
  24 + }
  25 +}
@@ -1190,7 +1190,7 @@ file: @@ -1190,7 +1190,7 @@ file:
1190 uploadDir: /Users/thingskit/upload/ #文件上传地址 只有type = local需要 1190 uploadDir: /Users/thingskit/upload/ #文件上传地址 只有type = local需要
1191 downloadPath: /download_file/ #与controller里面下载文件GetMapping的path一致, 只有type = local需要 1191 downloadPath: /download_file/ #与controller里面下载文件GetMapping的path一致, 只有type = local需要
1192 uploadPath: /upload #与controller里面下载文件GetMapping的path一致, 只有type = local需要 1192 uploadPath: /upload #与controller里面下载文件GetMapping的path一致, 只有type = local需要
1193 - staticUrl: /oss/files/** #oss静态访问路径 只有type = local需要 1193 + staticUrl: /static/files/** #oss静态访问路径 只有type = local需要
1194 randomFileName: ${file.storage.randomFileName} 1194 randomFileName: ${file.storage.randomFileName}
1195 minio: 1195 minio:
1196 minioUrl: ${MINIO_URL:https://dev.thingskit.com:9000} #minio储存地址 1196 minioUrl: ${MINIO_URL:https://dev.thingskit.com:9000} #minio储存地址
@@ -28,6 +28,8 @@ import java.nio.file.Files; @@ -28,6 +28,8 @@ import java.nio.file.Files;
28 import java.nio.file.Path; 28 import java.nio.file.Path;
29 import java.nio.file.Paths; 29 import java.nio.file.Paths;
30 import java.nio.file.StandardCopyOption; 30 import java.nio.file.StandardCopyOption;
  31 +import java.time.LocalDateTime;
  32 +import java.time.format.DateTimeFormatter;
31 import java.util.Objects; 33 import java.util.Objects;
32 34
33 @Service 35 @Service
@@ -139,6 +141,8 @@ public class LocalFileStorageService implements FileStorageService { @@ -139,6 +141,8 @@ public class LocalFileStorageService implements FileStorageService {
139 } else { 141 } else {
140 fileName = RandomStringUtils.randomAlphabetic(15); 142 fileName = RandomStringUtils.randomAlphabetic(15);
141 } 143 }
  144 + String date = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE);
  145 + fileName = date + "-" + fileName;
142 } 146 }
143 // Check if the file's name contains invalid characters 147 // Check if the file's name contains invalid characters
144 if (fileName.contains("..")) { 148 if (fileName.contains("..")) {