nfp_sites/base/media/upload.mjs

84 lines
2 KiB
JavaScript
Raw Normal View History

2022-07-20 00:33:06 +00:00
import config from '../config.mjs'
import Client from './client.mjs'
2022-08-12 23:33:50 +00:00
export function uploadMedia(file) {
2022-07-20 00:33:06 +00:00
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
})
2022-08-12 23:33:50 +00:00
}
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,
}
})
}
export function deleteFile(filename) {
const media = config.get('media')
const client = new Client()
let token = client.createJwt({ iss: media.iss }, media.secret)
return client.delete(media.removePath + filename + '?token=' + token, { })
}