mirror of
https://github.com/nolanlawson/pinafore
synced 2024-12-11 09:45:24 +01:00
16e66346d7
BREAKING CHANGE: Node v12.20+, v14.14+, or v16.0+ is required * fix!: remove esm package, use native Node ES modules * fix: fix some CJS imports
43 lines
1022 B
JavaScript
43 lines
1022 B
JavaScript
import FormData from 'form-data'
|
|
import { auth } from '../src/routes/_api/utils.js'
|
|
import * as loadMediaPackage from './loadMedia.cjs'
|
|
const { loadMedia } = loadMediaPackage
|
|
|
|
export async function submitMedia (accessToken, filename, alt) {
|
|
const form = new FormData()
|
|
form.append('file', loadMedia(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('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)
|
|
}
|
|
})
|
|
})
|
|
})
|
|
}
|