项目下载ZIP、TAR.GZ

This commit is contained in:
lxp
2026-11-02 16:53:17 +08:00
parent 556dab4377
commit 674f0400db
13 changed files with 923 additions and 3 deletions
@@ -35,6 +35,7 @@ public class LimitProperties {
values.put("admin-login", 30);
values.put("vibemake-read", 120);
values.put("file-read", 60);
values.put("archive-read", 10);
values.put("create", 5);
values.put("fork", 10);
values.put("file-write", 10);
@@ -2,7 +2,10 @@ package com.linkerbot.vibemake.service.gitea;
import com.linkerbot.vibemake.bean.Contracts.*;
import com.linkerbot.vibemake.common.PageResponse;
import com.linkerbot.vibemake.service.archive.ArchiveFormat;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.time.Duration;
@@ -13,6 +16,16 @@ public interface GiteaClient {
record StoredFile(String path, String blobSha, byte[] bytes) {}
record Archive(InputStream input, long contentLength) implements AutoCloseable {
@Override
public void close() throws IOException { input.close(); }
}
default Archive archive(Repository repository, String sha, ArchiveFormat format,
Duration timeout, long maxBytes) {
throw new UnsupportedOperationException("未实现仓库归档下载");
}
record Change(String operation, String path, String blobSha, byte[] bytes) {}
/** 只从 main 可达的提交中查找,并校验完整尾注及唯一父提交;空结果不是失败证明。 */
@@ -62,6 +75,8 @@ public interface GiteaClient {
String head(Repository repository);
default String head(Repository repository, Duration timeout) { return head(repository); }
List<TreeEntry> tree(Repository repository, String sha, String path);
StoredFile file(Repository repository, String sha, String path);
@@ -9,15 +9,18 @@ import com.linkerbot.vibemake.common.LimitedHttpBody;
import com.linkerbot.vibemake.common.PageResponse;
import com.linkerbot.vibemake.service.config.VibemakeProperties;
import com.linkerbot.vibemake.service.fork.ForkDeadline;
import com.linkerbot.vibemake.service.archive.ArchiveFormat;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpTimeoutException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.*;
@@ -53,6 +56,47 @@ public class RestGiteaClient implements GiteaClient {
.orElse("");
}
@Override
public Archive archive(Repository repository, String sha, ArchiveFormat format,
Duration timeout, long maxBytes) {
if (properties.getGitea().getToken().isBlank())
throw new GiteaFailure(503, "GITEA_NOT_CONFIGURED", "请配置 Gitea Token", false);
long deadline = System.nanoTime() + timeout.toNanos();
String base = properties.getGitea().getBaseUrl().replaceAll("/+$", "");
HttpRequest request = HttpRequest.newBuilder(URI.create(base + "/api/v1"
+ repoPath(repository) + "/archive/" + FilePaths.sha(sha) + "." + format.extension()))
.timeout(timeout)
.header("Accept", format.contentType())
.header("Authorization", "token " + properties.getGitea().getToken())
.GET().build();
try {
HttpResponse<InputStream> response = http.send(request, HttpResponse.BodyHandlers.ofInputStream());
InputStream input = response.body();
try {
if (response.statusCode() == 404)
throw new GiteaFailure(404, "NOT_FOUND", "仓库或提交版本不存在", false);
if (response.statusCode() != 200) throw ArchiveInputStream.failed();
long length = response.headers().firstValueAsLong("Content-Length").orElse(-1);
if (length < -1 || length == 0) throw ArchiveInputStream.failed();
if (length > maxBytes) throw ArchiveInputStream.tooLarge();
return new Archive(new ArchiveInputStream(input, length, maxBytes, deadline), length);
} catch (RuntimeException error) {
try { input.close(); } catch (IOException ignored) { }
if (error instanceof NumberFormatException) throw ArchiveInputStream.failed();
throw error;
}
} catch (HttpTimeoutException error) {
throw ArchiveInputStream.timedOut();
} catch (InterruptedException error) {
Thread.currentThread().interrupt();
throw ArchiveInputStream.timedOut();
} catch (IOException error) {
GiteaFailure failure = ArchiveInputStream.failed();
failure.initCause(error);
throw failure;
}
}
private HttpResponse<byte[]> request(String method, String path, Object body) {
return request(method, path, body, properties.getGitea().getRequestTimeout());
}
@@ -156,6 +200,22 @@ public class RestGiteaClient implements GiteaClient {
return branch(repo, "main");
}
@Override
public String head(Repository repo, Duration timeout) {
try {
JsonNode branch = parse(request("GET", repoPath(repo) + "/branches/main", null, timeout).body(), false);
return FilePaths.sha(branch.path("commit").path("id").asText());
} catch (GiteaFailure error) {
if (error.status() == 504) throw ArchiveInputStream.timedOut();
if (error.status() == 404 || error.status() == 503) throw error;
GiteaFailure failure = ArchiveInputStream.failed();
failure.initCause(error);
throw failure;
} catch (BusinessException error) {
throw ArchiveInputStream.failed();
}
}
@Override
public String branch(Repository repo, String branchName) {
JsonNode branch =
@@ -67,6 +67,8 @@ public class HttpSafetyFilter extends OncePerRequestFilter {
if (path.equals("/api/auth/captcha")) limits.request("captcha", ip);
else if (path.equals("/api/auth/login")) limits.request("login-ip", ip);
else if (path.equals("/admin-api/auth/login")) limits.request("admin-login", ip);
else if (read && path.matches("/api/(community|workspace)/projects/[^/]+/archive"))
limits.request("archive-read", ip);
else if (read
&& path.matches(
"/(api/community|api/workspace|admin-api)/projects/[^/]+/files"))