mywork / lib /uploadImage.js
DeeCeeXxx's picture
Upload 199 files
6c07b9a verified
const fetch = require('node-fetch')
const FormData = require('form-data')
const { fromBuffer } = require('file-type')
/**
* Upload image to telegra.ph
* Supported mimetype:
* - `image/jpeg`
* - `image/jpg`
* - `image/png`s
* @param {Buffer} buffer Image Buffer
*/
module.exports = async buffer => {
const { ext } = await fromBuffer(buffer)
let form = new FormData
form.append('file', buffer, 'tmp.' + ext)
let res = await fetch('https://api.shannmoderz.xyz/server/upload', {
method: 'POST',
body: form
})
let img = await res.json()
if (img.error) throw img.error
return 'https://api.shannmoderz.xyz/server/file' + img[0].src
}