已将业务时间生成入口统一为 Asia/Shanghai,不依赖服务器默认时区

This commit is contained in:
lxp
2026-10-29 16:42:10 +08:00
parent 0dd39eb23d
commit bec48bb3ea
22 changed files with 357 additions and 69 deletions
@@ -6,6 +6,7 @@ import com.linkerbot.vibemake.bean.entity.User;
import com.linkerbot.vibemake.common.*;
import com.linkerbot.vibemake.service.UserService;
import com.linkerbot.vibemake.service.mapper.UserMapper;
import com.linkerbot.vibemake.service.support.ProjectSupport;
import lombok.RequiredArgsConstructor;
@@ -14,8 +15,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.Locale;
@Service
@@ -46,7 +45,7 @@ public class UserServiceImpl implements UserService {
candidate.setEmail(normalizedEmail);
candidate.setNickname("用户_" + candidate.getUserCode().substring(0, 8));
candidate.setStatus("ACTIVE");
candidate.setCreatedAt(LocalDateTime.now(Clock.systemUTC()));
candidate.setCreatedAt(ProjectSupport.now());
candidate.setUpdatedAt(candidate.getCreatedAt());
try {
users.insert(candidate);
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.vibemake.bean.Contracts.CoverUploadSignature;
import com.linkerbot.vibemake.common.Codes;
import com.linkerbot.vibemake.service.config.OssProperties;
import com.linkerbot.vibemake.service.support.ProjectSupport;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.time.*;
@@ -24,6 +25,9 @@ public class OssPostSigner {
public static final long MAX_FILE_BYTES = 5L * 1024 * 1024;
private static final Duration VALIDITY = Duration.ofMinutes(5);
private static final String VERSION = "OSS4-HMAC-SHA256";
private static final DateTimeFormatter BUSINESS_DATE =
DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ProjectSupport.TIME_ZONE);
// OSS V4 协议中的凭据日期、请求时间和策略过期时间必须使用 UTC。
private static final DateTimeFormatter DATE =
DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneOffset.UTC);
private static final DateTimeFormatter TIMESTAMP =
@@ -49,7 +53,7 @@ public class OssPostSigner {
public CoverUploadSignature sign(String userCode, String contentType, String extension) {
String key = properties.getRootDirectory() + "/project-covers/" + userCode + "/"
+ DATE.format(clock.instant()) + "/" + Codes.next() + "." + extension;
+ BUSINESS_DATE.format(clock.instant()) + "/" + Codes.next() + "." + extension;
SignedPost post = post(key, contentType, 1, MAX_FILE_BYTES, VALIDITY, false);
return new CoverUploadSignature(properties.uploadUrl(), key,
resourcePaths.publicUrl(key), post.expiresAt(), post.formFields());
@@ -105,7 +109,7 @@ public class OssPostSigner {
fields.put("x-oss-forbid-overwrite", "true");
fields.put("x-oss-content-type", contentType);
}
return new SignedPost(LocalDateTime.ofInstant(expiration, ZoneOffset.UTC), Map.copyOf(fields));
return new SignedPost(ProjectSupport.fromInstant(expiration), Map.copyOf(fields));
} catch (JsonProcessingException | GeneralSecurityException error) {
log.error("OSS 签名生成失败 objectKey={} region={} errorType={}",
key, properties.getRegion(), error.getClass().getSimpleName());
@@ -0,0 +1,85 @@
package com.linkerbot.vibemake.service.oss;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.vibemake.bean.Contracts.ResourceFile;
import com.linkerbot.vibemake.bean.Contracts.ResourceFileView;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.common.FilePaths;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/** 仅处理资源路径和清单,不依赖 OSS 上传配置、签名或客户端。 */
@Component
@RequiredArgsConstructor
public class ProjectResourceFiles {
public static final long OSS_MAX_FILE_BYTES = 5L * 1024 * 1024 * 1024;
private static final Logger log = LoggerFactory.getLogger(ProjectResourceFiles.class);
private static final TypeReference<List<ResourceFile>> FILES_TYPE = new TypeReference<>() {};
private final ObjectMapper json;
private final OssResourcePaths paths;
public void validate(String prefix, ProjectResourceType type, List<ResourceFile> files) {
if (files == null || files.isEmpty()) throw BusinessException.badRequest("资源文件清单不能为空");
if (type.singleFile() && files.size() != 1)
throw BusinessException.badRequest("该资源类型必须且只能上传一个文件");
Set<String> seen = new HashSet<>();
for (ResourceFile file : files) {
if (file == null || file.sizeBytes() == null || file.sizeBytes() < 0)
throw BusinessException.badRequest("文件大小不合法");
if (file.sizeBytes() > OSS_MAX_FILE_BYTES)
throw new BusinessException(413, "RESOURCE_FILE_TOO_LARGE", "OSS 表单直传单文件不能超过 5 GB");
String path = FilePaths.file(file.relativePath());
if (type.singleFile() && path.contains("/"))
throw BusinessException.badRequest("单文件资源请直接提交文件名");
if (!path.toLowerCase(Locale.ROOT).endsWith(type.extension()))
throw BusinessException.badRequest("该资源类型只接受 " + type.extension() + " 文件");
if (!seen.add(path)) throw BusinessException.badRequest("上传路径不能重复");
String key = paths.objectKey(prefix + path);
if ((prefix + path).length() > 512 || key.getBytes(StandardCharsets.UTF_8).length > 1023)
throw BusinessException.badRequest("资源文件对象路径过长");
}
}
public String manifest(List<ResourceFile> files) {
try {
return json.writeValueAsString(files);
} catch (JsonProcessingException error) {
throw new IllegalStateException("资源文件清单序列化失败", error);
}
}
public List<ResourceFileView> fileViews(String prefix, String manifest, ProjectResourceType type) {
if (prefix == null || prefix.isEmpty()) return List.of();
// Fork 可以引用源项目批次;这里校验资源命名空间,不要求目录属于当前项目。
String storagePrefix = paths.storagePath(prefix);
if (!storagePrefix.matches("/project-resources/(?:[a-z0-9]{12}|[a-z0-9]{32})/"
+ type.code() + "/[a-f0-9]{32}/")) throw invalidManifest();
try {
List<ResourceFile> files = json.readValue(manifest, FILES_TYPE);
validate(storagePrefix, type, files);
List<ResourceFileView> result = new ArrayList<>();
for (ResourceFile file : files)
result.add(new ResourceFileView(file.relativePath(), file.sizeBytes(),
paths.publicUrl(storagePrefix + file.relativePath())));
return List.copyOf(result);
} catch (JsonProcessingException | IllegalArgumentException | BusinessException error) {
log.error("项目资源文件清单读取失败 resourceType={} prefix={} errorType={}",
type.code(), storagePrefix, error.getClass().getSimpleName());
throw invalidManifest();
}
}
private BusinessException invalidManifest() {
return new BusinessException(500, "RESOURCE_MANIFEST_INVALID", "项目资源文件清单异常");
}
}
@@ -2,15 +2,23 @@ package com.linkerbot.vibemake.service.support;
import com.linkerbot.vibemake.common.BusinessException;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
/** 项目、文件和互动服务共用的时间与分页约定。 */
public final class ProjectSupport {
public static final ZoneId TIME_ZONE = ZoneId.of("Asia/Shanghai");
private ProjectSupport() {}
/** 业务数据库和接口统一使用上海时间,不依赖 JVM 默认时区。 */
public static LocalDateTime now() {
return LocalDateTime.now(Clock.systemUTC());
return fromInstant(Instant.now());
}
public static LocalDateTime fromInstant(Instant instant) {
return LocalDateTime.ofInstant(instant, TIME_ZONE);
}
public static void pageCheck(long page, long size) {