修复小程序后台路由误判

This commit is contained in:
2026-10-27 11:50:18 +08:00
parent d24a6ac5b8
commit d10161a583
15 changed files with 201 additions and 61 deletions
@@ -0,0 +1,39 @@
package com.xiaowang.jayucloud.salon.core.modules.file.controller.front;
import com.xiaowang.jayucloud.salon.core.annotation.OperationGuard;
import com.xiaowang.jayucloud.salon.core.common.Result;
import com.xiaowang.jayucloud.salon.core.modules.file.service.ObjectStorageDirectUploadService;
import com.xiaowang.jayucloud.salon.core.modules.file.vo.ObjectStorageFileVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/object-storage-file")
@RequiredArgsConstructor
public class MiniProgramObjectStorageUploadController {
private final ObjectStorageDirectUploadService objectStorageDirectUploadService;
@PostMapping("/upload")
@OperationGuard(action = "mini-program-upload-direct", ttlSeconds = 30, message = "上传处理中,请勿重复操作")
public Result<ObjectStorageFileVO> uploadFile(HttpServletRequest request,
@RequestParam("file") MultipartFile file) {
Object userTypeValue = request.getAttribute("userType");
if (!(userTypeValue instanceof Integer)
|| (!Integer.valueOf(2).equals(userTypeValue) && !Integer.valueOf(3).equals(userTypeValue))) {
return Result.error(403, "当前账号无权上传门店业务文件");
}
try {
return Result.success(objectStorageDirectUploadService.upload(file));
} catch (Exception e) {
e.printStackTrace();
return Result.error("文件上传失败: " + e.getMessage());
}
}
}
@@ -10,6 +10,7 @@ import com.xiaowang.jayucloud.salon.core.modules.file.dto.ObjectStorageUploadOut
import com.xiaowang.jayucloud.salon.core.modules.file.service.ChunkUploadService;
import com.xiaowang.jayucloud.salon.core.modules.file.service.ObjectStorageFileService;
import com.xiaowang.jayucloud.salon.core.modules.file.service.ObjectStorageOrchestrationService;
import com.xiaowang.jayucloud.salon.core.modules.file.service.ObjectStorageDirectUploadService;
import com.xiaowang.jayucloud.salon.core.modules.file.support.ObjectStorageUrlSupport;
import com.xiaowang.jayucloud.salon.core.support.id.TimeOrderedSnowflakeIdGenerator;
import com.xiaowang.jayucloud.salon.core.support.security.OperationGuardStrategy;
@@ -46,6 +47,7 @@ public class ObjectStorageFileController {
* Variable `objectStorageFileService`: Spring-injected collaborator used by this class to finish database access, validation, or downstream calls.
*/
private final ObjectStorageFileService objectStorageFileService;
private final ObjectStorageDirectUploadService objectStorageDirectUploadService;
/**
* Variable `objectStorageOrchestrationService`: Spring-injected collaborator used by this class to finish database access, validation, or downstream calls.
*/
@@ -156,35 +158,7 @@ public class ObjectStorageFileController {
@OperationGuard(action = "admin-upload-direct", ttlSeconds = 30, message = "上传处理中,请勿重复操作")
public Result<ObjectStorageFileVO> uploadFile(@RequestParam("file") MultipartFile file) {
try {
uploadSecurityValidator.validate(file);
String originalFilename = file.getOriginalFilename();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = snowflakeIdGenerator.nextIdString() + fileExtension;
ObjectStorageUploadOutcome uploadOutcome = objectStorageOrchestrationService.uploadPrimary(
file.getInputStream(),
file.getSize(),
file.getContentType(),
objectName
);
ObjectStorageFileDTO dto = new ObjectStorageFileDTO();
dto.setFileName(originalFilename);
dto.setFileUrl(uploadOutcome.getPrimaryUrl());
dto.setObjectName(objectName);
dto.setPrimaryProvider(uploadOutcome.getPrimaryProvider());
dto.setPrimaryUrl(uploadOutcome.getPrimaryUrl());
dto.setBackupProvider(String.join(",", objectStorageProperties.getReplicaRequiredProviders()));
dto.setContentType(file.getContentType());
dto.setFileSize(file.getSize());
dto.setPrimaryUploadStatus(1);
dto.setBackupUploadStatus(0);
dto.setBackupRetryCount(0);
dto.setUploadAttempts(uploadOutcome.getAttempts());
dto.setStatus(1);
Long fileId = objectStorageFileService.createObjectStorageFile(dto);
return Result.success(objectStorageFileService.getObjectStorageFileById(fileId));
return Result.success(objectStorageDirectUploadService.upload(file));
} catch (Exception e) {
e.printStackTrace();
return Result.error("文件上传失败: " + e.getMessage());
@@ -0,0 +1,54 @@
package com.xiaowang.jayucloud.salon.core.modules.file.service;
import com.xiaowang.jayucloud.salon.core.config.ObjectStorageProperties;
import com.xiaowang.jayucloud.salon.core.modules.file.dto.ObjectStorageFileDTO;
import com.xiaowang.jayucloud.salon.core.modules.file.dto.ObjectStorageUploadOutcome;
import com.xiaowang.jayucloud.salon.core.modules.file.vo.ObjectStorageFileVO;
import com.xiaowang.jayucloud.salon.core.support.id.TimeOrderedSnowflakeIdGenerator;
import com.xiaowang.jayucloud.salon.core.support.security.UploadSecurityValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
@RequiredArgsConstructor
public class ObjectStorageDirectUploadService {
private final ObjectStorageFileService objectStorageFileService;
private final ObjectStorageOrchestrationService objectStorageOrchestrationService;
private final TimeOrderedSnowflakeIdGenerator snowflakeIdGenerator;
private final UploadSecurityValidator uploadSecurityValidator;
private final ObjectStorageProperties objectStorageProperties;
public ObjectStorageFileVO upload(MultipartFile file) throws Exception {
uploadSecurityValidator.validate(file);
String originalFilename = file.getOriginalFilename();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = snowflakeIdGenerator.nextIdString() + fileExtension;
ObjectStorageUploadOutcome uploadOutcome = objectStorageOrchestrationService.uploadPrimary(
file.getInputStream(),
file.getSize(),
file.getContentType(),
objectName
);
ObjectStorageFileDTO dto = new ObjectStorageFileDTO();
dto.setFileName(originalFilename);
dto.setFileUrl(uploadOutcome.getPrimaryUrl());
dto.setObjectName(objectName);
dto.setPrimaryProvider(uploadOutcome.getPrimaryProvider());
dto.setPrimaryUrl(uploadOutcome.getPrimaryUrl());
dto.setBackupProvider(String.join(",", objectStorageProperties.getReplicaRequiredProviders()));
dto.setContentType(file.getContentType());
dto.setFileSize(file.getSize());
dto.setPrimaryUploadStatus(1);
dto.setBackupUploadStatus(0);
dto.setBackupRetryCount(0);
dto.setUploadAttempts(uploadOutcome.getAttempts());
dto.setStatus(1);
Long fileId = objectStorageFileService.createObjectStorageFile(dto);
return objectStorageFileService.getObjectStorageFileById(fileId);
}
}
@@ -50,7 +50,7 @@ import java.time.LocalDate;
import java.util.List;
@RestController
@RequestMapping("/admin/manager")
@RequestMapping({"/admin/manager", "/manager"})
@RequiredArgsConstructor
public class AdminManagerController {
@@ -18,7 +18,7 @@ import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@RequestMapping("/admin/user/workbench/registration")
@RequestMapping({"/admin/user/workbench/registration", "/user/workbench/registration"})
public class AdminWorkbenchRegistrationController {
private final WorkbenchRegistrationService registrationService;
private final UserMapper userMapper;
@@ -0,0 +1,27 @@
package com.xiaowang.jayucloud.salon.core.modules.file.controller.front;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class MiniProgramObjectStorageUploadControllerMappingTest {
@Test
void miniProgramRouteExposesOnlyDirectUpload() throws Exception {
RequestMapping mapping = MiniProgramObjectStorageUploadController.class.getAnnotation(RequestMapping.class);
assertNotNull(mapping);
assertArrayEquals(new String[]{"/object-storage-file"}, mapping.value());
Method uploadMethod = MiniProgramObjectStorageUploadController.class.getMethod(
"uploadFile", javax.servlet.http.HttpServletRequest.class,
org.springframework.web.multipart.MultipartFile.class);
PostMapping uploadMapping = uploadMethod.getAnnotation(PostMapping.class);
assertNotNull(uploadMapping);
assertArrayEquals(new String[]{"/upload"}, uploadMapping.value());
}
}
@@ -0,0 +1,18 @@
package com.xiaowang.jayucloud.salon.core.modules.file.controller.front;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class ObjectStorageFileControllerMappingTest {
@Test
void keepsFileManagementRoutesAdminOnly() {
RequestMapping mapping = ObjectStorageFileController.class.getAnnotation(RequestMapping.class);
assertNotNull(mapping);
assertArrayEquals(new String[]{"/admin/object-storage-file"}, mapping.value());
}
}
@@ -0,0 +1,18 @@
package com.xiaowang.jayucloud.salon.core.modules.manager.controller.admin;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class AdminManagerControllerMappingTest {
@Test
void exposesAdminCompatibilityAndMiniProgramBusinessRoutes() {
RequestMapping mapping = AdminManagerController.class.getAnnotation(RequestMapping.class);
assertNotNull(mapping);
assertArrayEquals(new String[]{"/admin/manager", "/manager"}, mapping.value());
}
}