nagose commited on
Commit
690bb8e
·
verified ·
1 Parent(s): f6ef993

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +17 -13
index.js CHANGED
@@ -31,26 +31,30 @@ app.get('/resize', async (req, res) => {
31
  const resizedImageBuffer = await sharp(imageBuffer).resize(size).toBuffer();
32
  console.log('压缩后的图像大小:', resizedImageBuffer.byteLength);
33
 
 
 
 
 
34
  // 创建一个 FormData 对象并添加压缩后的图像
35
- const formData = new FormData();
36
- formData.append('file', resizedImageBuffer, { filename: 'compressed.jpg', contentType: 'image/jpeg' });
37
 
38
  // 使用新的 API 地址和方法上传文件
39
- const uploadResponse = await fetch('https://ours.pages.dev/upload', {
40
- method: 'POST',
41
- body: formData
42
- });
43
 
44
- if (!uploadResponse.ok) {
45
- throw new Error(`上传图像失败: ${uploadResponse.statusText}`);
46
- }
47
 
48
- const uploadResult = await uploadResponse.text();
49
- console.log('上传结果:', uploadResult);
50
 
51
  // Parse the JSON response and construct the full URL
52
- const uploadResultJson = JSON.parse(uploadResult);
53
- const fullUrl = `https://ours.pages.dev${uploadResultJson[0].src}`;
54
 
55
  res.setHeader('Content-Type', 'text/plain');
56
  res.setHeader('Access-Control-Allow-Origin', '*');
 
31
  const resizedImageBuffer = await sharp(imageBuffer).resize(size).toBuffer();
32
  console.log('压缩后的图像大小:', resizedImageBuffer.byteLength);
33
 
34
+ // 将压缩后的图像转换为 Base64 编码的数据
35
+ const base64Image = resizedImageBuffer.toString('base64');
36
+
37
+
38
  // 创建一个 FormData 对象并添加压缩后的图像
39
+ // const formData = new FormData();
40
+ // formData.append('file', resizedImageBuffer, { filename: 'compressed.jpg', contentType: 'image/jpeg' });
41
 
42
  // 使用新的 API 地址和方法上传文件
43
+ // const uploadResponse = await fetch('https://ours.pages.dev/upload', {
44
+ // method: 'POST',
45
+ // body: formData
46
+ // });
47
 
48
+ // if (!uploadResponse.ok) {
49
+ // throw new Error(`上传图像失败: ${uploadResponse.statusText}`);
50
+ // }
51
 
52
+ // const uploadResult = await uploadResponse.text();
53
+ // console.log('上传结果:', uploadResult);
54
 
55
  // Parse the JSON response and construct the full URL
56
+ // const uploadResultJson = JSON.parse(uploadResult);
57
+ // const fullUrl = `https://ours.pages.dev${uploadResultJson[0].src}`;
58
 
59
  res.setHeader('Content-Type', 'text/plain');
60
  res.setHeader('Access-Control-Allow-Origin', '*');