单文件下载改为流式传输

This commit is contained in:
lxp
2026-11-05 15:40:02 +08:00
parent 3141169a35
commit 366426d98a
22 changed files with 1101 additions and 51 deletions
@@ -5,9 +5,11 @@ import com.linkerbot.vibemake.bean.Contracts.*;
import com.linkerbot.vibemake.common.*;
import com.linkerbot.vibemake.service.*;
import com.linkerbot.vibemake.service.admin.AdminManagementService;
import com.linkerbot.vibemake.service.gitea.GiteaClient;
import com.linkerbot.vibemake.service.download.FileDownloadService;
import com.linkerbot.vibemake.service.download.FileDownloadResponse;
import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
@@ -15,7 +17,6 @@ import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.nio.charset.StandardCharsets;
import java.util.List;
@RestController
@@ -25,6 +26,7 @@ public class AdminProjectController {
private final AdminManagementService management;
private final ProjectService projects;
private final FileService files;
private final FileDownloadService downloads;
private final UrdfService urdf;
@GetMapping
@@ -91,22 +93,13 @@ public class AdminProjectController {
@PathVariable String projectCode,
@RequestParam String path,
@RequestParam(required = false) String commitSha,
@RequestParam(defaultValue = "false") boolean download) {
@RequestParam(defaultValue = "false") boolean download,
HttpServletResponse response) {
if (!download)
return ResponseEntity.ok(
ApiResponse.success(files.ownerFileView(0, projectCode, path, commitSha)));
GiteaClient.StoredFile file = files.ownerFile(0, projectCode, path, commitSha);
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(
HttpHeaders.CONTENT_DISPOSITION,
ContentDisposition.attachment()
.filename(
file.path().substring(file.path().lastIndexOf('/') + 1),
StandardCharsets.UTF_8)
.build()
.toString())
.body(file.bytes());
FileDownloadResponse.write(response, destination -> downloads.owner(0, projectCode, path, commitSha, destination));
return null;
}
@GetMapping("/{projectCode}/commits")
@@ -108,6 +108,9 @@ vibemake:
max-text-bytes: 1048576
max-file-bytes: 52428800
max-request-bytes: 209715200
file-download:
max-bytes: ${FILE_DOWNLOAD_MAX_BYTES:1073741824}
timeout: ${FILE_DOWNLOAD_TIMEOUT:5m}
gitea:
connect-timeout: 5s
request-timeout: 30s
@@ -108,6 +108,7 @@ class AdminWorkflowTest {
@Autowired FakeGiteaClient gitea;
@Autowired ProjectService projectService;
@Autowired FileService fileService;
@Autowired com.linkerbot.vibemake.service.config.FileDownloadProperties fileDownloadProperties;
@Autowired UserService userService;
@Autowired ProjectLocks locks;
@Autowired com.linkerbot.vibemake.service.write.RepositoryWrites writes;
@@ -189,6 +190,44 @@ class AdminWorkflowTest {
.asText();
}
@Test
void fileDownloadsUseStreamingWithCurrentAndHistoricalVersionsAndOwnLimit() throws Exception {
String code = createProject();
String endpoint = "/admin-api/projects/" + code;
String first = initialSha(code);
request(signed(body(post(endpoint + "/commits"), Map.of(
"expectedCommitSha", first, "message", "更新下载内容", "changes", List.of(
Map.of("action", "UPDATE_FILE", "path", "README.md", "content", "后台版本"))))), 200);
for (boolean history : List.of(false, true)) {
String content = history ? "initial" : "后台版本";
MockHttpServletRequestBuilder download = signed(get(endpoint + "/files")
.param("path", "README.md").param("download", "true"));
if (history) download.param("commitSha", first);
MvcResult result = mvc.perform(download).andReturn();
assertThat(result.getResponse().getStatus()).isEqualTo(200);
assertThat(result.getResponse().getContentType()).isEqualTo("application/octet-stream");
assertThat(result.getResponse().getContentAsByteArray()).isEqualTo(content.getBytes(java.nio.charset.StandardCharsets.UTF_8));
assertThat(result.getResponse().getHeader("Content-Disposition")).contains("attachment");
assertThat(result.getResponse().getHeader("Content-Length"))
.isEqualTo(Integer.toString(content.getBytes(java.nio.charset.StandardCharsets.UTF_8).length));
assertThat(result.getResponse().getHeader("Cache-Control")).isEqualTo("no-store");
assertThat(result.getResponse().getHeader("X-Request-Id")).isNotBlank();
}
assertThat(request(signed(get(endpoint + "/files").param("path", "README.md")), 200)
.path("data").path("content").asText()).isEqualTo("后台版本");
request(get(endpoint + "/files").param("path", "README.md").param("download", "true"), 401);
long maximum = fileDownloadProperties.getMaxBytes();
try {
fileDownloadProperties.setMaxBytes(1);
MvcResult result = mvc.perform(signed(get(endpoint + "/files")
.param("path", "README.md").param("download", "true"))).andReturn();
assertThat(result.getResponse().getStatus()).isEqualTo(413);
assertThat(json.readTree(result.getResponse().getContentAsByteArray()).path("code").asText()).isEqualTo("FILE_TOO_LARGE");
assertThat(result.getResponse().getHeader("Content-Disposition")).isNull();
assertThat(result.getResponse().getHeader("Content-Length")).isNull();
} finally { fileDownloadProperties.setMaxBytes(maximum); }
}
@Test
void publicationTimeCanBeSetOmittedClearedAndIsProtectedFromOrdinaryUsers() throws Exception {
String code = createProject();
@@ -5,6 +5,8 @@ import com.linkerbot.vibemake.common.PageResponse;
import com.linkerbot.vibemake.service.gitea.*;
import java.security.MessageDigest;
import java.io.ByteArrayInputStream;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
@@ -164,6 +166,16 @@ final class FakeGiteaClient implements GiteaClient {
return new StoredFile(path, blob(bytes), bytes.clone());
}
@Override
public synchronized FileDownload openFile(Repository repository, String sha, String path,
Duration timeout, long maxBytes) {
Map<String, byte[]> snapshot = repo(repository).snapshots.get(sha);
byte[] bytes = snapshot == null ? null : snapshot.get(path);
if (bytes == null) throw new GiteaFailure(404, "NOT_FOUND", "文件不存在", false);
if (bytes.length > maxBytes) throw new GiteaFailure(413, "FILE_TOO_LARGE", "文件超过下载限制", false);
return new FileDownload(new ByteArrayInputStream(bytes), bytes.length);
}
@Override
public synchronized CommitView commit(
Repository repository,