(imagePath: string)
| 219 | |
| 220 | // 图片上传到 fars.ee |
| 221 | async function uploadImage(imagePath: string): Promise<string> { |
| 222 | const basename = path.basename(imagePath); |
| 223 | const url = `https://fars.ee/~${basename}`; |
| 224 | |
| 225 | const formData = new FormData(); |
| 226 | const imageBuffer = await fs.promises.readFile(imagePath); |
| 227 | const imageBlob = new Blob([imageBuffer as any]); |
| 228 | |
| 229 | formData.append("c", imageBlob, basename); |
| 230 | formData.append("sunset", "120"); |
| 231 | formData.append("private", "1"); |
| 232 | |
| 233 | const headers = { |
| 234 | Accept: "application/json", |
| 235 | }; |
| 236 | |
| 237 | try { |
| 238 | const response = await axios.post(url, formData, { |
| 239 | headers, |
| 240 | timeout: 30000, |
| 241 | }); |
| 242 | |
| 243 | if (response.status !== 200) { |
| 244 | const location = response.headers.location; |
| 245 | if (location) { |
| 246 | return location; |
| 247 | } |
| 248 | throw new Error(`响应异常: HTTP ${response.status}`); |
| 249 | } |
| 250 | |
| 251 | const data = response.data; |
| 252 | let retUrl = data.url; |
| 253 | |
| 254 | if (!retUrl) { |
| 255 | retUrl = response.headers.location; |
| 256 | } |
| 257 | |
| 258 | if (!retUrl) { |
| 259 | throw new Error("有响应但无法获取图片 URL"); |
| 260 | } |
| 261 | |
| 262 | return retUrl; |
| 263 | } catch (error: any) { |
| 264 | throw new Error(`上传图片失败: ${error.message}`); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // 下载并处理图片 |
| 269 | async function downloadAndProcessImage( |
no outgoing calls
no test coverage detected