11import crypto from "crypto" ;
2+ import axios from "axios" ;
23import { RegionInfo , request } from "@/api/controllers/core.ts" ;
34import { RegionUtils } from "@/lib/region-utils.ts" ;
45import { createSignature } from "@/lib/aws-signature.ts" ;
@@ -69,8 +70,9 @@ export async function uploadImageBuffer(
6970
7071 let applyResponse ;
7172 try {
72- applyResponse = await fetch ( applyUrl , {
73+ applyResponse = await axios ( {
7374 method : 'GET' ,
75+ url : applyUrl ,
7476 headers : {
7577 'accept' : '*/*' ,
7678 'accept-language' : 'zh-CN,zh;q=0.9' ,
@@ -87,19 +89,20 @@ export async function uploadImageBuffer(
8789 'x-amz-date' : timestamp ,
8890 'x-amz-security-token' : session_token ,
8991 } ,
92+ validateStatus : ( ) => true ,
9093 } ) ;
9194 } catch ( fetchError : any ) {
9295 logger . error ( `Fetch请求失败,目标URL: ${ applyUrl } ` ) ;
9396 logger . error ( `错误详情: ${ fetchError . message } ` ) ;
9497 throw new Error ( `网络请求失败 (${ applyUrlHost } ): ${ fetchError . message } . 请检查: 1) 网络连接是否正常 2) 是否需要配置代理 3) DNS是否能解析该域名` ) ;
9598 }
9699
97- if ( ! applyResponse . ok ) {
98- const errorText = await applyResponse . text ( ) ;
100+ if ( applyResponse . status < 200 || applyResponse . status >= 300 ) {
101+ const errorText = typeof applyResponse . data === 'string' ? applyResponse . data : JSON . stringify ( applyResponse . data ) ;
99102 throw new Error ( `申请上传权限失败: ${ applyResponse . status } - ${ errorText } ` ) ;
100103 }
101104
102- const applyResult = await applyResponse . json ( ) ;
105+ const applyResult = applyResponse . data ;
103106
104107 if ( applyResult ?. ResponseMetadata ?. Error ) {
105108 throw new Error ( `申请上传权限失败: ${ JSON . stringify ( applyResult . ResponseMetadata . Error ) } ` ) ;
@@ -123,8 +126,9 @@ export async function uploadImageBuffer(
123126 // 第三步:上传图片文件
124127 let uploadResponse ;
125128 try {
126- uploadResponse = await fetch ( uploadUrl , {
129+ uploadResponse = await axios ( {
127130 method : 'POST' ,
131+ url : uploadUrl ,
128132 headers : {
129133 'Accept' : '*/*' ,
130134 'Accept-Language' : 'zh-CN,zh;q=0.9' ,
@@ -140,16 +144,17 @@ export async function uploadImageBuffer(
140144 'Sec-Fetch-Site' : 'cross-site' ,
141145 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36' ,
142146 } ,
143- body : imageBuffer ,
147+ data : imageBuffer ,
148+ validateStatus : ( ) => true ,
144149 } ) ;
145150 } catch ( fetchError : any ) {
146151 logger . error ( `图片文件上传fetch请求失败,目标URL: ${ uploadUrl } ` ) ;
147152 logger . error ( `错误详情: ${ fetchError . message } ` ) ;
148153 throw new Error ( `图片上传网络请求失败 (${ uploadHost } ): ${ fetchError . message } . 请检查网络连接` ) ;
149154 }
150155
151- if ( ! uploadResponse . ok ) {
152- const errorText = await uploadResponse . text ( ) ;
156+ if ( uploadResponse . status < 200 || uploadResponse . status >= 300 ) {
157+ const errorText = typeof uploadResponse . data === 'string' ? uploadResponse . data : JSON . stringify ( uploadResponse . data ) ;
153158 throw new Error ( `图片上传失败: ${ uploadResponse . status } - ${ errorText } ` ) ;
154159 }
155160
@@ -174,8 +179,9 @@ export async function uploadImageBuffer(
174179
175180 let commitResponse ;
176181 try {
177- commitResponse = await fetch ( commitUrl , {
182+ commitResponse = await axios ( {
178183 method : 'POST' ,
184+ url : commitUrl ,
179185 headers : {
180186 'accept' : '*/*' ,
181187 'accept-language' : 'zh-CN,zh;q=0.9' ,
@@ -194,20 +200,21 @@ export async function uploadImageBuffer(
194200 'x-amz-security-token' : session_token ,
195201 'x-amz-content-sha256' : payloadHash ,
196202 } ,
197- body : commitPayload ,
203+ data : commitPayload ,
204+ validateStatus : ( ) => true ,
198205 } ) ;
199206 } catch ( fetchError : any ) {
200207 logger . error ( `提交上传fetch请求失败,目标URL: ${ commitUrl } ` ) ;
201208 logger . error ( `错误详情: ${ fetchError . message } ` ) ;
202209 throw new Error ( `提交上传网络请求失败 (${ applyUrlHost } ): ${ fetchError . message } . 请检查网络连接` ) ;
203210 }
204211
205- if ( ! commitResponse . ok ) {
206- const errorText = await commitResponse . text ( ) ;
212+ if ( commitResponse . status < 200 || commitResponse . status >= 300 ) {
213+ const errorText = typeof commitResponse . data === 'string' ? commitResponse . data : JSON . stringify ( commitResponse . data ) ;
207214 throw new Error ( `提交上传失败: ${ commitResponse . status } - ${ errorText } ` ) ;
208215 }
209216
210- const commitResult = await commitResponse . json ( ) ;
217+ const commitResult = commitResponse . data ;
211218
212219 if ( commitResult ?. ResponseMetadata ?. Error ) {
213220 throw new Error ( `提交上传失败: ${ JSON . stringify ( commitResult . ResponseMetadata . Error ) } ` ) ;
@@ -247,12 +254,14 @@ export async function uploadImageFromUrl(
247254 try {
248255 logger . info ( `开始从URL下载并上传图片: ${ imageUrl } ` ) ;
249256
250- const imageResponse = await fetch ( imageUrl ) ;
251- if ( ! imageResponse . ok ) {
257+ const imageResponse = await axios . get ( imageUrl , {
258+ responseType : 'arraybuffer' ,
259+ } ) ;
260+ if ( imageResponse . status < 200 || imageResponse . status >= 300 ) {
252261 throw new Error ( `下载图片失败: ${ imageResponse . status } ` ) ;
253262 }
254263
255- const imageBuffer = await imageResponse . arrayBuffer ( ) ;
264+ const imageBuffer = imageResponse . data ;
256265 return await uploadImageBuffer ( imageBuffer , refreshToken , regionInfo ) ;
257266 } catch ( error : any ) {
258267 logger . error ( `从URL上传图片失败: ${ error . message } ` ) ;
0 commit comments