Преглед изворни кода

feat: 后端语音上传基础设施 - 文件配置+存储服务

- FileUploadConfig: 上传目录配置(app.upload.dir)

- UploadService: 音频存储服务(格式校验/10MB限制/用户隔离)

- WebMvcConfig: /uploads/**静态资源映射

- application.yml: multipart配置(10MB)
liaoxg пре 5 месеци
родитељ
комит
95ebfb77c8

+ 20 - 0
num-server/src/main/java/com/etotem/num/config/FileUploadConfig.java

@@ -0,0 +1,20 @@
+package com.etotem.num.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+@Configuration
+public class FileUploadConfig {
+
+    @Value("${app.upload.dir:./uploads}")
+    private String uploadDir;
+
+    @Bean
+    public Path uploadPath() {
+        return Paths.get(uploadDir).toAbsolutePath().normalize();
+    }
+}

+ 15 - 0
num-server/src/main/java/com/etotem/num/config/WebMvcConfig.java

@@ -2,16 +2,24 @@ package com.etotem.num.config;
 
 
 import com.etotem.num.interceptor.AdminInterceptor;
 import com.etotem.num.interceptor.AdminInterceptor;
 import com.etotem.num.interceptor.AuthInterceptor;
 import com.etotem.num.interceptor.AuthInterceptor;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
 @Configuration
 @Configuration
 public class WebMvcConfig implements WebMvcConfigurer {
 public class WebMvcConfig implements WebMvcConfigurer {
 
 
     private final AuthInterceptor authInterceptor;
     private final AuthInterceptor authInterceptor;
     private final AdminInterceptor adminInterceptor;
     private final AdminInterceptor adminInterceptor;
 
 
+    @Value("${app.upload.dir:./uploads}")
+    private String uploadDir;
+
     public WebMvcConfig(AuthInterceptor authInterceptor, AdminInterceptor adminInterceptor) {
     public WebMvcConfig(AuthInterceptor authInterceptor, AdminInterceptor adminInterceptor) {
         this.authInterceptor = authInterceptor;
         this.authInterceptor = authInterceptor;
         this.adminInterceptor = adminInterceptor;
         this.adminInterceptor = adminInterceptor;
@@ -26,4 +34,11 @@ public class WebMvcConfig implements WebMvcConfigurer {
                 .addPathPatterns("/api/admin/**")
                 .addPathPatterns("/api/admin/**")
                 .excludePathPatterns("/api/admin/login");
                 .excludePathPatterns("/api/admin/login");
     }
     }
+
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        Path absolutePath = Paths.get(uploadDir).toAbsolutePath().normalize();
+        registry.addResourceHandler("/uploads/**")
+                .addResourceLocations("file:" + absolutePath.toString() + "/");
+    }
 }
 }

+ 56 - 0
num-server/src/main/java/com/etotem/num/service/UploadService.java

@@ -0,0 +1,56 @@
+package com.etotem.num.service;
+
+import com.etotem.num.common.BizException;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.List;
+import java.util.UUID;
+
+@Service
+public class UploadService {
+
+    private final Path uploadPath;
+
+    private static final long MAX_FILE_SIZE = 10 * 1024 * 1024;
+
+    private static final List<String> SUPPORTED_EXTENSIONS = Arrays.asList("amr", "mp3", "wav", "sil");
+
+    public UploadService(Path uploadPath) {
+        this.uploadPath = uploadPath;
+    }
+
+    public String storeAudio(MultipartFile file, Long userId) {
+        if (file.isEmpty()) {
+            throw new BizException(400, "文件为空");
+        }
+        if (file.getSize() > MAX_FILE_SIZE) {
+            throw new BizException(400, "文件大小不能超过10MB");
+        }
+
+        String originalName = file.getOriginalFilename();
+        String ext = "";
+        if (originalName != null && originalName.contains(".")) {
+            ext = originalName.substring(originalName.lastIndexOf(".") + 1).toLowerCase();
+        }
+        if (!SUPPORTED_EXTENSIONS.contains(ext)) {
+            throw new BizException(400, "不支持的文件格式,仅支持: amr, mp3, wav, sil");
+        }
+
+        String filename = System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replace("-", "") + "." + ext;
+        Path userDir = uploadPath.resolve("audio").resolve(String.valueOf(userId));
+
+        try {
+            Files.createDirectories(userDir);
+            Files.copy(file.getInputStream(), userDir.resolve(filename));
+        } catch (IOException e) {
+            throw new BizException(500, "文件上传失败");
+        }
+
+        return "/uploads/audio/" + userId + "/" + filename;
+    }
+}

+ 4 - 0
num-server/src/main/resources/application.yml

@@ -2,6 +2,10 @@ server:
   port: 8186
   port: 8186
 
 
 spring:
 spring:
+  servlet:
+    multipart:
+      max-file-size: 10MB
+      max-request-size: 10MB
   datasource:
   datasource:
     url: jdbc:mysql://192.168.16.251:3306/num?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
     url: jdbc:mysql://192.168.16.251:3306/num?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
     username: num
     username: num