diff --git a/frontend/src/components/console/task/create-default-task-dialog.tsx b/frontend/src/components/console/task/create-default-task-dialog.tsx index 150b95b9..5fa0a17b 100644 --- a/frontend/src/components/console/task/create-default-task-dialog.tsx +++ b/frontend/src/components/console/task/create-default-task-dialog.tsx @@ -1,5 +1,4 @@ import { - Api, ConstsCliName, ConstsGitPlatform, ConstsHostStatus, @@ -66,6 +65,7 @@ import { selectHost, selectImage, selectPreferredTaskModel, + uploadFileWithPresignedUrl, } from "@/utils/common" import { apiRequest } from "@/utils/requestUtils" import { readStoredTaskDialogParams, writeStoredTaskDialogParams } from "./task-dialog-params-storage" @@ -446,40 +446,8 @@ export default function CreateDefaultTaskDialog({ if (selectedZipFile) { setCreatingTask(true) try { - const api = new Api() - const presignResponse = await api.api.v1UploaderPresignCreate({ - filename: selectedZipFile.name, - }) - - if (presignResponse.data?.code !== 0 || !presignResponse.data?.data) { - toast.error("获取上传地址失败: " + (presignResponse.data?.message || "未知错误")) - setCreatingTask(false) - return - } - - const { upload_url, access_url } = presignResponse.data.data - - if (!upload_url || !access_url) { - toast.error("获取上传地址失败: 返回数据不完整") - setCreatingTask(false) - return - } - - const uploadResponse = await fetch(upload_url, { - method: "PUT", - body: selectedZipFile, - headers: { - "Content-Type": "application/zip", - }, - }) - - if (!uploadResponse.ok) { - toast.error("文件上传失败: " + uploadResponse.statusText) - setCreatingTask(false) - return - } - - zipUrl = access_url + const uploadedFile = await uploadFileWithPresignedUrl(selectedZipFile) + zipUrl = uploadedFile.accessUrl } catch (error) { toast.error("上传失败: " + (error as Error).message) setCreatingTask(false) diff --git a/frontend/src/components/console/task/task-file-upload.tsx b/frontend/src/components/console/task/task-file-upload.tsx index 2ff25488..605990ae 100644 --- a/frontend/src/components/console/task/task-file-upload.tsx +++ b/frontend/src/components/console/task/task-file-upload.tsx @@ -1,4 +1,3 @@ -import { Api } from "@/api/Api" import { Button } from "@/components/ui/button" import { Dialog, @@ -10,6 +9,7 @@ import { } from "@/components/ui/dialog" import { Spinner } from "@/components/ui/spinner" import { cn } from "@/lib/utils" +import { uploadFileWithPresignedUrl } from "@/utils/common" import { IconFile, IconTrash, IconUpload } from "@tabler/icons-react" import React from "react" import { toast } from "sonner" @@ -23,34 +23,13 @@ export interface TaskUploadedFile { } export async function uploadTaskFile(file: File): Promise { - const api = new Api() - const presignResponse = await api.api.v1UploaderPresignCreate({ - filename: file.name, - }) - - if (presignResponse.data?.code !== 0 || !presignResponse.data?.data) { - throw new Error("获取上传地址失败: " + (presignResponse.data?.message || "未知错误")) - } - - const { upload_url, access_url } = presignResponse.data.data - if (!upload_url || !access_url) { - throw new Error("获取上传地址失败: 返回数据不完整") - } - - const uploadResponse = await fetch(upload_url, { - method: "PUT", - body: new Blob([file]), - }) - - if (!uploadResponse.ok) { - throw new Error("文件上传失败: " + uploadResponse.statusText) - } + const uploadedFile = await uploadFileWithPresignedUrl(file) return { name: file.name, size: file.size, type: file.type, - accessUrl: access_url, + accessUrl: uploadedFile.accessUrl, } } diff --git a/frontend/src/components/console/task/task-input.tsx b/frontend/src/components/console/task/task-input.tsx index d5fb4ab1..cb4a500e 100644 --- a/frontend/src/components/console/task/task-input.tsx +++ b/frontend/src/components/console/task/task-input.tsx @@ -1,4 +1,4 @@ -import { Api, ConstsCliName, ConstsGitPlatform, ConstsHostStatus, ConstsOwnerType, ConstsTaskSubType, ConstsTaskType, ConstsUserRole, type DomainAuthRepository, type DomainGitIdentity, type DomainSkill } from "@/api/Api"; +import { ConstsCliName, ConstsGitPlatform, ConstsHostStatus, ConstsOwnerType, ConstsTaskSubType, ConstsTaskType, ConstsUserRole, type DomainAuthRepository, type DomainGitIdentity, type DomainSkill } from "@/api/Api"; import Icon from "@/components/common/Icon"; import { useCommonData } from "@/components/console/data-provider"; import { Badge } from "@/components/ui/badge"; @@ -16,7 +16,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@ import { Separator } from "@/components/ui/separator"; import { Spinner } from "@/components/ui/spinner"; import { cn } from "@/lib/utils"; -import { TASK_PROMPT_PLACEHOLDER, canUseModelBySubscription, getBrandFromModelName, getGitPlatformIcon, getHostBadges, getImageShortName, getModelPricingItem, getOSFromImageName, getOwnerTypeBadge, getRepoIcon, getRepoNameFromUrl, getSkillTagIcon, selectHost, selectImage, selectPreferredTaskModel } from "@/utils/common"; +import { TASK_PROMPT_PLACEHOLDER, canUseModelBySubscription, getBrandFromModelName, getGitPlatformIcon, getHostBadges, getImageShortName, getModelPricingItem, getOSFromImageName, getOwnerTypeBadge, getRepoIcon, getRepoNameFromUrl, getSkillTagIcon, selectHost, selectImage, selectPreferredTaskModel, uploadFileWithPresignedUrl } from "@/utils/common"; import { apiRequest } from "@/utils/requestUtils"; import { IconBug, IconChevronDown, IconHelpCircle, IconLink, IconPuzzle, IconReload, IconSend, IconSourceCode, IconTerminal2, IconUpload, IconUser, IconVocabulary, IconXboxX } from "@tabler/icons-react"; import { useEffect, useMemo, useRef, useState } from "react"; @@ -380,37 +380,9 @@ export function TaskInput({ repos, onTaskCreated }: TaskInputProps) { setUploadingZip(true); try { - const api = new Api(); - const presignResponse = await api.api.v1UploaderPresignCreate({ - filename: selectedZipFile.name, - }); + const uploadedFile = await uploadFileWithPresignedUrl(selectedZipFile); - if (presignResponse.data?.code !== 0 || !presignResponse.data?.data) { - toast.error('获取上传地址失败: ' + (presignResponse.data?.message || '未知错误')); - return; - } - - const { upload_url, access_url } = presignResponse.data.data; - - if (!upload_url || !access_url) { - toast.error('获取上传地址失败: 返回数据不完整'); - return; - } - - const uploadResponse = await fetch(upload_url, { - method: 'PUT', - body: selectedZipFile, - headers: { - 'Content-Type': 'application/zip', - }, - }); - - if (!uploadResponse.ok) { - toast.error('文件上传失败: ' + uploadResponse.statusText); - return; - } - - setSelectedRepo(access_url); + setSelectedRepo(uploadedFile.accessUrl); setSelectedRepoDisplayName(selectedZipFile.name); setSelectedRepoFromMyRepos(false); setUploadDialogOpen(false); diff --git a/frontend/src/utils/common.tsx b/frontend/src/utils/common.tsx index 366b210d..86ef69cd 100644 --- a/frontend/src/utils/common.tsx +++ b/frontend/src/utils/common.tsx @@ -548,21 +548,16 @@ export async function packFilesAsZip( } /** - * 将文件列表打包成 zip 并上传 - * @param files 文件列表 - * @param zipFilename 打包后的 zip 文件名,默认为 'project-files.zip' + * 使用后端返回的预签名地址上传文件 + * @param file 要上传的文件 * @returns 上传成功后的访问地址和文件名,失败时抛出错误 */ -export async function packAndUploadFilesAsZip( - files: File[], - zipFilename: string = 'project-files.zip' +export async function uploadFileWithPresignedUrl( + file: File ): Promise<{ accessUrl: string; filename: string }> { - const zipFile = await packFilesAsZip(files, zipFilename) - - // 获取预签名上传地址 const presignResult = await new Promise<{ upload_url: string; access_url: string }>((resolve, reject) => { apiRequest('v1UploaderPresignCreate', { - filename: zipFile.name, + filename: file.name, }, [], (resp) => { if (resp.code === 0 && resp.data?.upload_url && resp.data?.access_url) { resolve({ upload_url: resp.data.upload_url, access_url: resp.data.access_url }) @@ -574,15 +569,9 @@ export async function packAndUploadFilesAsZip( }) }) - const { upload_url, access_url } = presignResult - - // 上传文件 - const uploadResponse = await fetch(upload_url, { + const uploadResponse = await fetch(presignResult.upload_url, { method: 'PUT', - body: zipFile, - headers: { - 'Content-Type': 'application/zip', - }, + body: new Blob([file]), }) if (!uploadResponse.ok) { @@ -590,11 +579,25 @@ export async function packAndUploadFilesAsZip( } return { - accessUrl: access_url, - filename: zipFile.name, + accessUrl: presignResult.access_url, + filename: file.name, } } +/** + * 将文件列表打包成 zip 并上传 + * @param files 文件列表 + * @param zipFilename 打包后的 zip 文件名,默认为 'project-files.zip' + * @returns 上传成功后的访问地址和文件名,失败时抛出错误 + */ +export async function packAndUploadFilesAsZip( + files: File[], + zipFilename: string = 'project-files.zip' +): Promise<{ accessUrl: string; filename: string }> { + const zipFile = await packFilesAsZip(files, zipFilename) + return uploadFileWithPresignedUrl(zipFile) +} + /** * 将 markdown 文本转换为纯文字,去掉所有格式标记 * @param markdown markdown 原文