-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuploadImgbb.js
60 lines (53 loc) · 1.9 KB
/
uploadImgbb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Upload images to imgbb.
* @param {string} file a path to the file to upload
* @param {string} apiKey the API key to access imgbb.com
* @param {object} extraOptions dictionary with additional options that may be not supported
* by every upload method. imgbb supports name and expiration (in seconds 60-15552000)
* @see https://api.imgbb.com/ for information about API
*/
async function uploadFile(file, apiKey, extraOptions) {
const assert = require('assert');
assert(typeof apiKey === 'string' && apiKey.trim().length > 0, 'apiKey must be a non-empty string');
const axios = require('axios');
const FormData = require('form-data');
const data = new FormData();
const base64Image = file.toString('base64');
const params = {key: apiKey};
if ('name' in extraOptions) {
console.log('name: ' + extraOptions.name);
params.name = extraOptions.name;
}
if ('expiration' in extraOptions) {
params.expiration = extraOptions.expiration;
}
data.append('image', base64Image);
const config = {
method: 'post',
url: 'https://api.imgbb.com/1/upload',
params: params,
headers: {
...data.getHeaders(),
},
data: data,
};
return axios(config)
.then(function(response) {
if ('expiration' in extraOptions && response.data.data.expiration != extraOptions.expiration) {
console.warn(
'expiration is not the same. expected: ' + extraOptions.expiration +
' actual: ' + response.data.data.expiration +
'\n Could be caused by reupload of an existing image.');
}
console.log(JSON.stringify(response.data));
return {
url: response.data.data.url,
expiration: response.data.data.expiration,
delete_url: response.data.data.delete_url,
};
})
.catch(function(error) {
console.log(error);
});
}
module.exports = uploadFile;