2018-12-01 08:10:30 +00:00
|
|
|
import FormData from 'form-data'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
2018-12-11 15:31:48 +00:00
|
|
|
import { auth } from '../src/routes/_api/utils'
|
2018-12-01 08:10:30 +00:00
|
|
|
|
|
|
|
export async function submitMedia (accessToken, filename, alt) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const form = new FormData()
|
2018-12-01 08:10:30 +00:00
|
|
|
form.append('file', fs.createReadStream(path.join(__dirname, 'images', filename)))
|
|
|
|
form.append('description', alt)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
form.submit({
|
|
|
|
host: 'localhost',
|
|
|
|
port: 3000,
|
|
|
|
path: '/api/v1/media',
|
|
|
|
headers: auth(accessToken)
|
|
|
|
}, (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
let data = ''
|
|
|
|
|
|
|
|
res.on('data', chunk => {
|
|
|
|
data += chunk
|
|
|
|
})
|
|
|
|
|
|
|
|
res.on('end', () => resolve(JSON.parse(data)))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|