Skip to content

Commit 7728d69

Browse files
committed
feat(file): split file storage info from agentKnowledge
1 parent 2f34808 commit 7728d69

28 files changed

Lines changed: 1526 additions & 1002 deletions

data-agent-frontend/src/services/fileUpload.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,45 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import axios from 'axios';
17+
import {ApiResponse} from "@/services/common.ts";
1618

1719
/**
1820
* 业务API服务
1921
* 封装所有业务相关的API调用
2022
*/
2123

22-
interface UploadResponse {
23-
success: boolean;
24-
message?: string;
25-
url?: string;
24+
interface FileStorage {
25+
id: number;
26+
filePath?: string;
27+
url: string;
28+
filename?: string;
2629
}
2730

31+
export type FileUploadResult = ApiResponse<FileStorage>;
32+
2833
// 文件上传API
2934
export const fileUploadApi = {
3035
// 上传头像
31-
uploadAvatar(file: File): Promise<UploadResponse> {
36+
async uploadAvatar(file: File): Promise<FileStorage | null> {
3237
const formData = new FormData();
3338
formData.append('file', file);
34-
3539
const url = '/api/upload/avatar';
36-
return fetch(url, {
37-
method: 'POST',
38-
body: formData,
39-
}).then(async response => {
40-
if (!response.ok) {
41-
const text = await response.text().catch(() => '');
42-
throw new Error(`Upload failed: ${response.status} ${text}`);
40+
try {
41+
const response = await axios.post<FileUploadResult>(url, formData, {
42+
headers: {
43+
'Content-Type': 'multipart/form-data'
44+
}
45+
});
46+
if (response.data.success) {
47+
return response.data.data ?? null;
4348
}
44-
const ct = response.headers.get('content-type') || '';
45-
if (ct.includes('application/json')) {
46-
return await response.json();
49+
throw new Error(response.data.message);
50+
} catch (error) {
51+
if (axios.isAxiosError(error) && error.response?.status === 404) {
52+
return null;
4753
}
48-
const text = await response.text();
49-
return { success: true, message: 'ok', url: text };
50-
});
54+
throw error;
55+
}
5156
},
5257
};

0 commit comments

Comments
 (0)