nfp_sites/base/media/upload.mjs

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-07-20 00:33:06 +00:00
import config from '../config.mjs'
import Client from './client.mjs'
export function upload(file) {
const media = config.get('media')
const client = new Client()
let token = client.createJwt({ iss: media.iss }, media.secret)
let out = {
2022-07-22 11:18:33 +00:00
sizes: {
small: {},
medium: {},
large: {},
}
2022-07-20 00:33:06 +00:00
}
return client.upload(media.path + '?token=' + token, { file: {
file: file.path,
filename: file.name,
} }, 'POST', {
preview: media.preview,
small: media.small.avif,
medium: media.medium.avif,
large: media.large.avif,
/*
small: media.small.jpeg,
medium: media.medium.avif,
large_jpeg: media.large.jpeg,*/
}).then(res => {
out.filename = res.filename
out.path = res.path
out.preview = res.preview
2022-07-22 11:18:33 +00:00
out.sizes.small.avif = res.small
out.sizes.medium.avif = res.medium
out.sizes.large.avif = res.large
out.size = file.size
out.type = file.type
2022-07-20 00:33:06 +00:00
return client.post(media.path + '/' + out.filename + '?token=' + token, {
small: media.small.jpeg,
medium: media.medium.jpeg,
large: media.large.jpeg,
})
.then(res => {
2022-07-22 11:18:33 +00:00
out.sizes.small.jpeg = res.small
out.sizes.medium.jpeg = res.medium
out.sizes.large.jpeg = res.large
2022-07-20 00:33:06 +00:00
})
})
.then(() => {
return out
})
}