项目新增封面图字段

This commit is contained in:
lxp
2026-10-15 11:09:21 +08:00
parent 92b2251f25
commit a1076d1098
21 changed files with 780 additions and 7 deletions
@@ -0,0 +1,30 @@
package com.linkerbot.community.api.controller;
import com.linkerbot.community.bean.Contracts.CoverUploadInput;
import com.linkerbot.community.bean.Contracts.CoverUploadSignature;
import com.linkerbot.community.common.ApiResponse;
import com.linkerbot.community.security.CurrentUser;
import com.linkerbot.community.service.CoverUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/uploads")
@Tag(name = "图片上传")
public class UploadController {
private final CoverUploadService uploads;
@PostMapping("/project-covers/signature")
@Operation(summary = "生成项目封面 OSS POST V4 直传签名")
public ResponseEntity<ApiResponse<CoverUploadSignature>> signature(
@Valid @RequestBody CoverUploadInput input) {
return ResponseEntity.ok().cacheControl(CacheControl.noStore())
.body(ApiResponse.success(uploads.signature(CurrentUser.required(), input)));
}
}
@@ -11,6 +11,8 @@ spring:
connection-timeout: 5000
community:
oss:
root-directory: vibemake
gitea:
base-url: ${GITEA_BASE_URL}
owner: ${GITEA_ORGANIZATION:community}
@@ -11,6 +11,8 @@ spring:
connection-timeout: 5000
community:
oss:
root-directory: vibemake_dev
gitea:
base-url: ${GITEA_BASE_URL:http://localhost:3000}
owner: ${GITEA_ORGANIZATION:community}
@@ -20,6 +20,13 @@ mybatis-plus:
configuration:
map-underscore-to-camel-case: true
community:
oss:
enabled: ${OSS_ENABLED:true}
region: ${OSS_REGION:cn-beijing}
bucket: ${OSS_BUCKET:linkerbot}
access-key-id: ${OSS_ACCESS_KEY_ID:}
access-key-secret: ${OSS_ACCESS_KEY_SECRET:}
public-base-url: ${OSS_PUBLIC_BASE_URL:https://linkerbot.oss-cn-beijing.aliyuncs.com}
ai:
enabled: ${AI_ENABLED:true}
api-key: ${ARK_API_KEY:}
@@ -169,6 +169,8 @@ class AiSearchTest {
@ValueSource(strings = {"GENERAL", "HUMANOID", "QUADRUPED", "WHEELED"})
void filtersConfiguredTypesAndKeepsProjectContract(String type) throws Exception {
Project expected = project("教学机器人", type, "支持 ROS2 教学");
expected.setCoverUrl("https://images.example.com/robot.png");
projects.updateById(expected);
project("其他机器人", "OTHER", "ROS2");
model.handler = (name, input) -> name.endsWith("intent") ? intent(type, List.of("ROS2", "ROS 2")) : rank(input);
MvcResult initial = start(Map.of("message", "找教学项目"));
@@ -178,6 +180,7 @@ class AiSearchTest {
.containsExactly("understanding", "searching", "ranking", "validating");
JsonNode card = events.getLast().path("data").path("projects").get(0);
assertThat(card.path("project").path("projectCode").asText()).isEqualTo(expected.getProjectCode());
assertThat(card.path("project").path("coverUrl").asText()).isEqualTo(expected.getCoverUrl());
assertThat(card.path("project").path("owner").has("id")).isFalse();
assertThat(card.path("project").has("id")).isFalse();
assertThat(card.path("project").has("giteaRepoId")).isFalse();
@@ -0,0 +1,19 @@
package com.linkerbot.community.api;
import static org.assertj.core.api.Assertions.*;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.io.ClassPathResource;
class OssProfileTest {
@Test
void environmentProfilesKeepCoverDirectoriesSeparate() throws IOException {
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
assertThat(loader.load("test", new ClassPathResource("application-test.yml")).getFirst()
.getProperty("community.oss.root-directory")).isEqualTo("vibemake_dev");
assertThat(loader.load("prod", new ClassPathResource("application-prod.yml")).getFirst()
.getProperty("community.oss.root-directory")).isEqualTo("vibemake");
}
}
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.community.security.CaptchaService;
import com.linkerbot.community.service.config.CommunityProperties;
import com.linkerbot.community.service.config.OssProperties;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
@@ -62,11 +63,13 @@ class WorkflowTest {
@Autowired ObjectMapper json;
@Autowired JdbcTemplate jdbc;
@Autowired FakeGiteaClient gitea;
@Autowired OssProperties oss;
@BeforeEach
void reset() throws Exception {
SchemaSupport.reset(jdbc);
gitea.reset();
oss.setEnabled(false);
}
private JsonNode perform(MockHttpServletRequestBuilder builder, int status) throws Exception {
@@ -168,6 +171,117 @@ class WorkflowTest {
200);
}
@Test
void coverRoundTripsThroughMetadataPublicationAndForkWithoutFileCommit() throws Exception {
String owner = login("cover-owner@example.com");
String other = login("cover-other@example.com");
String first = "https://images.example.com/first.png";
String next = "https://external.example.com/second.webp?version=2";
Map<String, Object> input = new LinkedHashMap<>(Map.of(
"name", "封面项目", "description", "项目说明", "projectType", "GENERAL", "coverUrl", " " + first + " "));
JsonNode created = perform(body(auth(post("/api/workspace/projects"), owner), input), 200).path("data");
String code = created.path("projectCode").asText();
String path = "/api/workspace/projects/" + code;
assertThat(created.path("coverUrl").asText()).isEqualTo(first);
assertThat(perform(auth(get(path), owner), 200).path("data").path("coverUrl").asText()).isEqualTo(first);
assertThat(perform(auth(get("/api/workspace/projects"), owner), 200)
.path("data").path("items").get(0).path("coverUrl").asText()).isEqualTo(first);
String sha = head(owner, code);
assertThat(perform(body(auth(put(path + "/publication"), owner), Map.of("commitSha", sha)), 200)
.path("data").path("coverUrl").asText()).isEqualTo(first);
assertThat(perform(get("/api/community/projects"), 200).path("data").path("items").get(0)
.path("coverUrl").asText()).isEqualTo(first);
input.put("coverUrl", next);
perform(body(auth(patch(path), other), input), 404);
perform(body(auth(patch(path), owner), input), 200);
assertThat(perform(get("/api/community/projects/" + code), 200).path("data").path("coverUrl").asText()).isEqualTo(next);
assertThat(head(owner, code)).isEqualTo(sha);
input.remove("coverUrl");
assertThat(perform(body(auth(patch(path), owner), input), 200).path("data").path("coverUrl").asText()).isEqualTo(next);
input.put("coverUrl", null);
perform(body(auth(patch(path), owner), input), 200);
assertThat(perform(auth(get(path), owner), 200).path("data").path("coverUrl").asText()).isEqualTo(next);
JsonNode fork = perform(body(auth(post("/api/community/projects/" + code + "/forks"), other),
Map.of("name", "封面副本", "publishedCommitSha", sha)), 200).path("data");
assertThat(fork.path("coverUrl").asText()).isEqualTo(next);
input.put("coverUrl", " ");
assertThat(perform(body(auth(patch(path), owner), input), 200).path("data").path("coverUrl").isNull()).isTrue();
assertThat(jdbc.queryForObject("SELECT cover_url FROM project WHERE project_code = ?", String.class, code)).isNull();
assertThat(perform(get("/api/community/projects/" + code), 200).path("data").path("coverUrl").isNull()).isTrue();
assertThat(perform(auth(get("/api/workspace/projects/" + fork.path("projectCode").asText()), other), 200)
.path("data").path("coverUrl").asText()).isEqualTo(next);
assertThat(head(owner, code)).isEqualTo(sha);
}
@Test
void optionalCoversRemainCompatibleAndInvalidUrlsAreRejectedBeforeCreatingRepositories() throws Exception {
String owner = login("cover-validation@example.com");
String code = create(owner, "旧请求");
assertThat(perform(auth(get("/api/workspace/projects/" + code), owner), 200).path("data").path("coverUrl").isNull()).isTrue();
Map<String, Object> input = new LinkedHashMap<>(Map.of("name", "封面", "projectType", "GENERAL"));
for (String empty : Arrays.asList(null, "", " ")) {
input.put("coverUrl", empty);
assertThat(perform(body(auth(post("/api/workspace/projects"), owner), input), 200)
.path("data").path("coverUrl").isNull()).isTrue();
}
long before = jdbc.queryForObject("SELECT COUNT(*) FROM project", Long.class);
for (String invalid : List.of("http://images.example.com/a.png", "/a.png", "data:image/png;base64,abc",
"https://user:password@images.example.com/a.png", "https:///missing-host.png",
"https://invalid_host/a.png", "https://images.example.com:70000/a.png",
"https://images.example.com/" + "a".repeat(2048))) {
input.put("coverUrl", invalid);
perform(body(auth(post("/api/workspace/projects"), owner), input), 400);
perform(body(auth(patch("/api/workspace/projects/" + code), owner), input), 400);
}
assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM project", Long.class)).isEqualTo(before);
String prefix = "https://images.example.com/";
String max = prefix + "a".repeat(2048 - prefix.length());
input.put("coverUrl", " " + max + " ");
assertThat(perform(body(auth(patch("/api/workspace/projects/" + code), owner), input), 200)
.path("data").path("coverUrl").asText()).isEqualTo(max);
}
@Test
void coverSignaturesAuthenticateValidateAndReturnDirectUploadForms() throws Exception {
String endpoint = "/api/uploads/project-covers/signature";
Map<String, Object> input = Map.of("contentType", "image/png", "fileSize", 123);
perform(body(post(endpoint), input), 401);
String token = login("upload@example.com");
assertThat(perform(body(auth(post(endpoint), token), input), 503).path("code").asText()).isEqualTo("OSS_UNAVAILABLE");
assertThat(oss.getRootDirectory()).isEqualTo("vibemake_dev");
oss.setRegion("cn-hangzhou");
oss.setEndpoint("");
oss.setBucket("examplebucket");
oss.setAccessKeyId("UnitTestAccessKey");
oss.setAccessKeySecret("unit-test-secret");
oss.setPublicBaseUrl("https://images.example.com");
oss.setEnabled(true);
String userCode = perform(auth(get("/api/users/me"), token), 200).path("data").path("userCode").asText();
for (String type : List.of("image/jpeg", "image/png", "image/webp")) {
JsonNode result = perform(body(auth(post(endpoint), token), Map.of("contentType", type, "fileSize", 5242880)), 200).path("data");
assertThat(result.path("objectKey").asText()).startsWith("vibemake_dev/project-covers/" + userCode + "/");
assertThat(result.path("uploadUrl").asText()).isEqualTo("https://examplebucket.oss-cn-hangzhou.aliyuncs.com");
assertThat(result.path("coverUrl").asText()).isEqualTo("https://images.example.com/" + result.path("objectKey").asText());
assertThat(result.path("formFields").path("content-type").asText()).isEqualTo(type);
assertThat(result.toString()).doesNotContain("unit-test-secret");
}
for (Object invalid : List.of(
Map.of("contentType", "image/svg+xml", "fileSize", 1),
Map.of("contentType", "image/png", "fileSize", 0),
Map.of("contentType", "image/png", "fileSize", -1),
Map.of("contentType", "image/png"), Map.of("fileSize", 1)))
perform(body(auth(post(endpoint), token), invalid), 400);
JsonNode tooLarge = perform(body(auth(post(endpoint), token), Map.of("contentType", "image/png", "fileSize", 5242881)), 413);
assertThat(tooLarge.path("code").asText()).isEqualTo("UPLOAD_TOO_LARGE");
mvc.perform(body(auth(post(endpoint), token), input)).andExpect(header().string("Cache-Control", "no-store"));
assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM project", Long.class)).isZero();
JsonNode spec = perform(get("/v3/api-docs"), 200);
assertThat(spec.path("paths").has(endpoint)).isTrue();
}
@Test
void loginNormalizesEmailAndConsumesCaptchaAndLogout() throws Exception {
JsonNode first = loginData("OWNER@example.com");