2018-12-01 08:10:30 +00:00
|
|
|
import FormData from 'form-data'
|
2021-07-05 04:19:04 +01:00
|
|
|
import { auth } from '../src/routes/_api/utils.js'
|
|
|
|
import * as loadMediaPackage from './loadMedia.cjs'
|
|
|
|
const { loadMedia } = loadMediaPackage
|
2018-12-01 08:10:30 +00:00
|
|
|
|
|
|
|
export async function submitMedia (accessToken, filename, alt) {
|
2019-08-03 21:49:37 +01:00
|
|
|
const form = new FormData()
|
2021-07-05 04:19:04 +01:00
|
|
|
form.append('file', loadMedia(filename))
|
2018-12-01 08:10:30 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2021-07-05 04:19:04 +01:00
|
|
|
res.on('error', err => {
|
|
|
|
console.error(err)
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
res.on('end', () => {
|
|
|
|
try {
|
|
|
|
const parsed = JSON.parse(data)
|
|
|
|
resolve(parsed)
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Malformed response, expected JSON: ' + data)
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
2018-12-01 08:10:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|