telegraph-uploader
Version:
A package that helps you to upload media files to telegra.ph
63 lines (51 loc) • 1.46 kB
JavaScript
const fetch = require('node-fetch')
const FormData = require('form-data')
const isBuffer = require('is-buffer')
const isStream = require('isstream')
const toArray = require('stream-to-array')
const uploadByUrl = (url, agent) => {
return fetch(url)
.then(async (r) => {
if (!isStream(r.body)) {
throw new TypeError('Response is not a stream')
}
const array = await toArray(r.body)
const buffer = Buffer.concat(array)
if (!r.headers.get('content-type')) {
throw new Error('No content types in the response')
}
return uploadByBuffer(buffer, r.headers.get('content-type'), agent)
})
}
const uploadByBuffer = (buffer, contentType, agent) => {
if (!isBuffer(buffer)) {
throw new TypeError('Buffer is not a Buffer')
}
const form = new FormData()
form.append('photo', buffer, {
filename: 'blob',
contentType,
...agent && {agent},
})
return fetch('https://telegra.ph/upload', {
method: 'POST',
body: form
})
.then(result => result.json())
.then((result) => {
if (result.error) {
throw result.error
}
if (result[0] && result[0].src) {
return {
link: 'https://telegra.ph' + result[0].src,
path: result[0].src,
}
}
throw new Error('Unknown error')
})
}
module.exports = {
uploadByUrl,
uploadByBuffer,
}