nfp_sites/base/media/upload.mjs
2022-08-12 23:33:50 +00:00

74 lines
No EOL
1.7 KiB
JavaScript

import config from '../config.mjs'
import Client from './client.mjs'
export function uploadMedia(file) {
const media = config.get('media')
const client = new Client()
let token = client.createJwt({ iss: media.iss }, media.secret)
let out = {
sizes: {
small: {},
medium: {},
large: {},
}
}
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
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
return client.post(media.path + '/' + out.filename + '?token=' + token, {
small: media.small.jpeg,
medium: media.medium.jpeg,
large: media.large.jpeg,
})
.then(res => {
out.sizes.small.jpeg = res.small
out.sizes.medium.jpeg = res.medium
out.sizes.large.jpeg = res.large
})
})
.then(() => {
return out
})
}
export function uploadFile(file) {
const media = config.get('media')
const client = new Client()
let token = client.createJwt({ iss: media.iss }, media.secret)
return client.upload(media.filePath + '?token=' + token, { file: {
file: file.path,
filename: file.name,
} }, 'POST').then(res => {
return {
filename: res.filename,
path: res.path,
size: file.size,
type: file.type,
}
})
}