diff --git a/vibemake-api/src/main/java/com/linkerbot/vibemake/api/controller/ArchiveController.java b/vibemake-api/src/main/java/com/linkerbot/vibemake/api/controller/ArchiveController.java new file mode 100644 index 0000000..e6ab920 --- /dev/null +++ b/vibemake-api/src/main/java/com/linkerbot/vibemake/api/controller/ArchiveController.java @@ -0,0 +1,100 @@ +package com.linkerbot.vibemake.api.controller; + +import com.linkerbot.vibemake.common.BusinessException; +import com.linkerbot.vibemake.security.CurrentUser; +import com.linkerbot.vibemake.service.archive.ArchiveService; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.security.SecurityRequirements; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ContentDisposition; +import org.springframework.http.HttpHeaders; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +@RestController +@RequiredArgsConstructor +@Tag(name = "仓库归档下载") +public class ArchiveController { + private final ArchiveService archives; + + @GetMapping("/api/workspace/projects/{projectCode}/archive") + @Operation(summary = "下载本人的仓库 ZIP 或 TAR.GZ,可指定历史 Commit") + @ApiResponse(responseCode = "200", description = "仓库归档附件", content = { + @Content(mediaType = "application/zip", schema = @Schema(type = "string", format = "binary")), + @Content(mediaType = "application/gzip", schema = @Schema(type = "string", format = "binary"))}) + public void owner(@PathVariable String projectCode, + @RequestParam(required = false) String format, + @RequestParam(required = false) String commitSha, + HttpServletResponse response) throws IOException { + long user = CurrentUser.required(); + transfer(response, destination -> archives.owner(user, projectCode, format, commitSha, destination)); + } + + @GetMapping("/api/community/projects/{projectCode}/archive") + @Operation(summary = "下载社区当前已发布版本的 ZIP 或 TAR.GZ") + @ApiResponse(responseCode = "200", description = "已发布仓库归档附件", content = { + @Content(mediaType = "application/zip", schema = @Schema(type = "string", format = "binary")), + @Content(mediaType = "application/gzip", schema = @Schema(type = "string", format = "binary"))}) + @SecurityRequirements + public void community(@PathVariable String projectCode, + @RequestParam(required = false) String format, + HttpServletRequest request, HttpServletResponse response) throws IOException { + if (request.getParameterMap().containsKey("commitSha")) + throw BusinessException.badRequest("社区归档仅允许下载当前已发布版本"); + transfer(response, destination -> archives.community(projectCode, format, destination)); + } + + @FunctionalInterface + private interface Transfer { + void run(ArchiveService.Destination destination) throws IOException; + } + + private void transfer(HttpServletResponse response, Transfer transfer) throws IOException { + response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store"); + try { + transfer.run((filename, type, length) -> { + response.setContentType(type); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment() + .filename(filename, StandardCharsets.UTF_8).build().toString()); + if (length >= 0) response.setContentLengthLong(length); + return response.getOutputStream(); + }); + } catch (BusinessException error) { + if (!response.isCommitted()) { + resetAttachment(response); + throw error; + } + // 已发包后停止传输,禁止异常处理器向压缩包尾部追加 JSON。 + closeResponse(response); + } catch (IOException error) { + if (!response.isCommitted()) { + resetAttachment(response); + throw new BusinessException(499, "ARCHIVE_CANCELLED", "仓库归档下载已取消"); + } + closeResponse(response); + } + } + + private void resetAttachment(HttpServletResponse response) { + response.resetBuffer(); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, null); + response.setHeader(HttpHeaders.CONTENT_LENGTH, null); + response.setContentType(null); + } + + private void closeResponse(HttpServletResponse response) { + try { response.getOutputStream().close(); } catch (IOException ignored) { } + } +} diff --git a/vibemake-service/src/main/java/com/linkerbot/vibemake/service/archive/ArchiveFormat.java b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/archive/ArchiveFormat.java new file mode 100644 index 0000000..2a965d0 --- /dev/null +++ b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/archive/ArchiveFormat.java @@ -0,0 +1,34 @@ +package com.linkerbot.vibemake.service.archive; + +import com.linkerbot.vibemake.common.BusinessException; + +public enum ArchiveFormat { + ZIP("zip", "application/zip"), + TAR_GZ("tar.gz", "application/gzip"); + + private final String extension; + private final String contentType; + + ArchiveFormat(String extension, String contentType) { + this.extension = extension; + this.contentType = contentType; + } + + public String extension() { return extension; } + + public String contentType() { return contentType; } + + public static ArchiveFormat parse(String value) { + if (value == null || "zip".equals(value)) return ZIP; + if ("tar.gz".equals(value)) return TAR_GZ; + throw BusinessException.badRequest("归档格式仅支持 zip、tar.gz"); + } + + public boolean matches(byte[] prefix, int length) { + if (length < 4) return false; + if (this == TAR_GZ) return prefix[0] == (byte) 0x1f && prefix[1] == (byte) 0x8b + && prefix[2] == 8; + return prefix[0] == 'P' && prefix[1] == 'K' + && (prefix[2] == 3 && prefix[3] == 4 || prefix[2] == 5 && prefix[3] == 6); + } +} diff --git a/vibemake-service/src/main/java/com/linkerbot/vibemake/service/archive/ArchiveService.java b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/archive/ArchiveService.java new file mode 100644 index 0000000..0afcc29 --- /dev/null +++ b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/archive/ArchiveService.java @@ -0,0 +1,104 @@ +package com.linkerbot.vibemake.service.archive; + +import com.linkerbot.vibemake.bean.entity.Project; +import com.linkerbot.vibemake.common.BusinessException; +import com.linkerbot.vibemake.common.FilePaths; +import com.linkerbot.vibemake.common.RateLimitedException; +import com.linkerbot.vibemake.common.SafeExceptionLog; +import com.linkerbot.vibemake.service.ProjectService; +import com.linkerbot.vibemake.service.config.ArchiveProperties; +import com.linkerbot.vibemake.service.gitea.GiteaClient; +import com.linkerbot.vibemake.service.gitea.GiteaFailure; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.OutputStream; +import java.time.Duration; +import java.util.concurrent.Semaphore; + +@Service +public class ArchiveService { + private static final Logger log = LoggerFactory.getLogger(ArchiveService.class); + private final ProjectService projects; + private final GiteaClient gitea; + private final ArchiveProperties properties; + private final Semaphore permits; + + public ArchiveService(ProjectService projects, GiteaClient gitea, ArchiveProperties properties) { + this.projects = projects; + this.gitea = gitea; + this.properties = properties; + this.permits = new Semaphore(properties.getMaxConcurrent()); + } + + @FunctionalInterface + public interface Destination { + OutputStream open(String filename, String contentType, long contentLength) throws IOException; + } + + public void owner(long userId, String code, String format, String commitSha, Destination destination) + throws IOException { + ArchiveFormat archiveFormat = ArchiveFormat.parse(format); + Project project = projects.readyOwner(userId, code); + String requestedSha = commitSha == null ? null : FilePaths.sha(commitSha); + download(project, requestedSha, archiveFormat, destination); + } + + public void community(String code, String format, Destination destination) throws IOException { + ArchiveFormat archiveFormat = ArchiveFormat.parse(format); + Project project = projects.published(code); + download(project, FilePaths.sha(project.getPublishedCommitSha()), archiveFormat, destination); + } + + private void download(Project project, String requestedSha, ArchiveFormat format, Destination destination) + throws IOException { + if (!permits.tryAcquire()) + throw new RateLimitedException("ARCHIVE_BUSY", "仓库归档下载繁忙,请稍后重试", 1); + long started = System.nanoTime(); + String code = project.getProjectCode(); + String sha = requestedSha; + long transferred = 0; + try { + if (sha == null) sha = FilePaths.sha(gitea.head(projects.repository(project), properties.getTimeout())); + log.info("仓库归档下载开始 projectCode={} sha={} format={}", code, sha, format.extension()); + Duration remaining = properties.getTimeout().minusNanos(System.nanoTime() - started); + if (remaining.isNegative() || remaining.isZero()) + throw new GiteaFailure(504, "ARCHIVE_TIMEOUT", "仓库归档下载超时,请稍后手动重试", false); + try (GiteaClient.Archive archive = gitea.archive(projects.repository(project), sha, format, + remaining, properties.getMaxBytes())) { + byte[] buffer = new byte[64 * 1024]; + int prefixSize = archive.input().readNBytes(buffer, 0, 4); + if (!format.matches(buffer, prefixSize)) + throw new GiteaFailure(502, "GITEA_ERROR", "仓库未返回有效的归档文件", false); + String filename = code + "-" + sha.substring(0, 12) + "." + format.extension(); + OutputStream output = destination.open(filename, format.contentType(), archive.contentLength()); + output.write(buffer, 0, prefixSize); + transferred += prefixSize; + int size; + while ((size = archive.input().read(buffer)) >= 0) { + output.write(buffer, 0, size); + transferred += size; + } + output.flush(); + } + log.info("仓库归档下载完成 projectCode={} sha={} format={} bytes={} elapsedMs={}", + code, sha, format.extension(), transferred, elapsed(started)); + } catch (BusinessException error) { + log.warn("仓库归档下载失败 projectCode={} sha={} format={} bytes={} elapsedMs={} code={}", + code, sha, format.extension(), transferred, elapsed(started), error.code()); + if (error.getCause() != null) SafeExceptionLog.write(log, "仓库归档上游异常", error); + throw error; + } catch (IOException error) { + log.info("仓库归档下载取消 projectCode={} sha={} format={} bytes={} elapsedMs={} errorType={}", + code, sha, format.extension(), transferred, elapsed(started), error.getClass().getSimpleName()); + throw error; + } finally { + permits.release(); + } + } + + private long elapsed(long started) { return (System.nanoTime() - started) / 1_000_000; } +} diff --git a/vibemake-service/src/main/java/com/linkerbot/vibemake/service/config/ArchiveProperties.java b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/config/ArchiveProperties.java new file mode 100644 index 0000000..7336bd8 --- /dev/null +++ b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/config/ArchiveProperties.java @@ -0,0 +1,30 @@ +package com.linkerbot.vibemake.service.config; + +import jakarta.validation.constraints.AssertTrue; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.Positive; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; +import org.springframework.validation.annotation.Validated; + +import java.time.Duration; + +@Getter +@Setter +@Validated +@Component +@ConfigurationProperties("vibemake.archive") +public class ArchiveProperties { + @Positive private long maxBytes = 1024L * 1024 * 1024; + @Min(1) @Max(32) private int maxConcurrent = 2; + private Duration timeout = Duration.ofMinutes(5); + + @AssertTrue(message = "归档超时必须在 100 毫秒至 1 小时之间") + public boolean isTimeoutValid() { + return timeout != null && timeout.compareTo(Duration.ofMillis(100)) >= 0 + && timeout.compareTo(Duration.ofHours(1)) <= 0; + } +} diff --git a/vibemake-service/src/main/java/com/linkerbot/vibemake/service/gitea/ArchiveInputStream.java b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/gitea/ArchiveInputStream.java new file mode 100644 index 0000000..11f8131 --- /dev/null +++ b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/gitea/ArchiveInputStream.java @@ -0,0 +1,102 @@ +package com.linkerbot.vibemake.service.gitea; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Objects; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +/** 固定内存读取,并在响应头返回后继续约束读取预算;关闭底层流会唤醒阻塞的 HTTP 读取。 */ +final class ArchiveInputStream extends InputStream { + private static final ScheduledThreadPoolExecutor DEADLINES = deadlines(); + private final InputStream input; + private final long contentLength; + private final long maximum; + private final long deadline; + private final AtomicBoolean expired = new AtomicBoolean(); + private final ScheduledFuture timeout; + private long count; + + private static ScheduledThreadPoolExecutor deadlines() { + ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2, task -> { + Thread thread = new Thread(task, "gitea-archive-deadline"); + thread.setDaemon(true); + return thread; + }); + executor.setRemoveOnCancelPolicy(true); + return executor; + } + + ArchiveInputStream(InputStream input, long contentLength, long maximum, long deadline) { + this.input = input; + this.contentLength = contentLength; + this.maximum = maximum; + this.deadline = deadline; + timeout = DEADLINES.schedule(() -> { + expired.set(true); + closeInput(); + }, Math.max(0, deadline - System.nanoTime()), TimeUnit.NANOSECONDS); + } + + private void checkDeadline() { + if (expired.get() || System.nanoTime() - deadline >= 0) throw timedOut(); + } + + static GiteaFailure timedOut() { + return new GiteaFailure(504, "ARCHIVE_TIMEOUT", "仓库归档下载超时,请稍后手动重试", false); + } + + static GiteaFailure tooLarge() { + return new GiteaFailure(413, "ARCHIVE_TOO_LARGE", "仓库归档超过下载大小限制", false); + } + + static GiteaFailure failed() { + return new GiteaFailure(502, "GITEA_ERROR", "仓库归档响应异常,请通过 requestId 排查", false); + } + + @Override + public int read() { + byte[] single = new byte[1]; + int size = read(single, 0, 1); + return size < 0 ? -1 : single[0] & 0xff; + } + + @Override + public int read(byte[] bytes, int offset, int length) { + Objects.checkFromIndexSize(offset, length, bytes.length); + if (length == 0) return 0; + checkDeadline(); + long remaining = maximum - count; + int requested = remaining >= length ? length : (int) (remaining + 1); + int size; + try { + size = input.read(bytes, offset, requested); + } catch (IOException error) { + checkDeadline(); + GiteaFailure failure = failed(); + failure.initCause(error); + throw failure; + } + checkDeadline(); + if (size < 0) { + if (contentLength >= 0 && count != contentLength) throw failed(); + } else { + count += size; + if (count > maximum) throw tooLarge(); + if (contentLength >= 0 && count > contentLength) throw failed(); + } + return size; + } + + private void closeInput() { + try { input.close(); } catch (IOException ignored) { /* 仅关闭上游,不输出可能含凭据的异常。 */ } + } + + @Override + public void close() { + timeout.cancel(false); + closeInput(); + } +} diff --git a/vibemake-service/src/main/java/com/linkerbot/vibemake/service/support/ProjectSort.java b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/support/ProjectSort.java new file mode 100644 index 0000000..359d75f --- /dev/null +++ b/vibemake-service/src/main/java/com/linkerbot/vibemake/service/support/ProjectSort.java @@ -0,0 +1,42 @@ +package com.linkerbot.vibemake.service.support; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.linkerbot.vibemake.bean.entity.Project; +import com.linkerbot.vibemake.common.BusinessException; + +/** 项目列表共用的固定排序,查询参数不能作为 SQL 表达式使用。 */ +public enum ProjectSort { + CREATED_AT_DESC, + PUBLISHED_AT_DESC, + RESOURCES_FIRST; + + private static final String COMPLETE_RESOURCES = """ + CASE WHEN COALESCE(model_glb_object_key, '') <> '' + AND COALESCE(assembly_json_object_key, '') <> '' + AND COALESCE(walking_resources_prefix, '') <> '' + AND COALESCE(model_parts_prefix, '') <> '' + THEN 1 ELSE 0 END + """; + + public static ProjectSort fromParameter(String value) { + if (value == null) return CREATED_AT_DESC; + try { + return valueOf(value); + } catch (IllegalArgumentException error) { + throw BusinessException.badRequest("项目排序参数不合法"); + } + } + + public LambdaQueryWrapper query() { + QueryWrapper query = new QueryWrapper<>(); + if (this == CREATED_AT_DESC) { + query.orderByDesc("created_at"); + } else { + if (this == RESOURCES_FIRST) query.orderByDesc(COMPLETE_RESOURCES); + query.orderByAsc("CASE WHEN published_at IS NULL THEN 1 ELSE 0 END") + .orderByDesc("published_at"); + } + return query.orderByDesc("id").lambda(); + } +}