maylinejix commited on
Commit
bf4e1b8
·
verified ·
1 Parent(s): 9ea7f67

Update lib/uploadImage.js

Browse files
Files changed (1) hide show
  1. lib/uploadImage.js +17 -16
lib/uploadImage.js CHANGED
@@ -1,4 +1,4 @@
1
- const fetch = require('node-fetch');
2
  const FormData = require('form-data');
3
 
4
  module.exports = async buffer => {
@@ -11,29 +11,30 @@ module.exports = async buffer => {
11
  }
12
 
13
  let { ext } = fileType;
14
- const randomName = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
15
  let bodyForm = new FormData();
16
- bodyForm.append("fileToUpload", buffer, randomName + "." + ext);
17
- bodyForm.append("reqtype", "fileupload");
18
 
19
- let res = await fetch("https://tmpfiles.org/api/v1/upload", {
20
- method: "POST",
21
- body: bodyForm,
 
 
 
22
  });
23
 
24
- if (!res.ok) {
25
- throw new Error(`Upload failed: ${res.status} ${res.statusText}`);
26
- }
27
-
28
- let data = await res.json();
29
-
30
- if (data.success && data.data.url) {
31
- return data.data.url;
32
  } else {
33
  throw new Error('Upload failed: Invalid response');
34
  }
35
  } catch (error) {
36
- console.error('Upload error:', error);
 
 
 
 
37
  throw error;
38
  }
39
  }
 
1
+ const axios = require('axios');
2
  const FormData = require('form-data');
3
 
4
  module.exports = async buffer => {
 
11
  }
12
 
13
  let { ext } = fileType;
14
+ const randomName = Math.random().toString(36).substring(2, 8);
15
  let bodyForm = new FormData();
16
+
17
+ bodyForm.append("file", buffer, `${randomName}.${ext}`);
18
 
19
+ let res = await axios.post("https://cdnme.idnet.my.id/upload", bodyForm, {
20
+ headers: {
21
+ ...bodyForm.getHeaders()
22
+ },
23
+ maxContentLength: Infinity,
24
+ maxBodyLength: Infinity
25
  });
26
 
27
+ if (res.data.success && res.data.url) {
28
+ return res.data.url;
 
 
 
 
 
 
29
  } else {
30
  throw new Error('Upload failed: Invalid response');
31
  }
32
  } catch (error) {
33
+ console.error('Upload error:', error.message);
34
+ if (error.response) {
35
+ console.error('Response data:', error.response.data);
36
+ console.error('Response status:', error.response.status);
37
+ }
38
  throw error;
39
  }
40
  }