cky-image-public
Version:
public image to public host
79 lines (69 loc) • 2 kB
JavaScript
const fs = require("fs");
const request = require('request').defaults({
headers: {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}
});
let helper = require('../helper');
class Imgbb {
constructor (option = {}) {
}
upload (imgPath, callback) {
let formData = {
source: {
value: fs.createReadStream(imgPath),
options: {
filename: imgPath,
contentType: 'image/jpeg'
}
},
type: 'file',
action: 'upload',
timestamp: new Date().getTime(),
}
var options = {
method: 'POST',
url: `https://imgbb.com/json`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
formData
};
request(options, (error, response, body) => {
if (error) {
return callback(error);
}
return callback(null, helper.safeParse(body));
});
}
delete (id_encoded, callback) {
let options = {
method: 'POST',
url: 'https://ibb.co/json',
headers: {
Host: 'ibb.co',
'x-requested-with': 'XMLHttpRequest',
authority: 'ibb.co',
referer: `https://ibb.co/${id_encoded}`,
accept: 'application/json, text/javascript, */*; q=0.01',
'content-type': 'multipart/form-data',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
origin: 'https://ibb.co'
},
formData: {
action: 'delete',
delete: 'image',
from: 'resource',
owner: '',
'deleting[id]': id_encoded,
'deleting[type]': 'image',
'deleting[url]': `https://ibb.co/${id_encoded}`,
'deleting[parent_url]': ''
}
};
request(options, function (error, response, body) {
return callback(error, body);
});
}
}
module.exports = Imgbb;