opnform/client/lib/file-uploads.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

import opnformConfig from "~/opnform.config.js";
export const storeFile = async (file, options = {}) => {
if(!opnformConfig.s3_enabled) { // If not s3 then upload to local temp
let formData = new FormData()
formData.append('file', file)
const response = await useOpnApi('/upload-file', {
method: 'POST',
body: formData
})
response.data.extension = file.name.split('.').pop()
return response.data
}
const response = await useOpnApi(options.signedStorageUrl ? options.signedStorageUrl : '/vapor/signed-storage-url', {
method: 'POST',
body: options.data,
bucket: options.bucket || '',
content_type: options.contentType || file.type,
expires: options.expires || '',
visibility: options.visibility || '',
baseURL: options.baseURL || null,
headers: options.headers || {},
...options.options
})
2024-01-13 17:17:24 +00:00
console.log(response)
const headers = response.data.headers
if ('Host' in headers) {
delete headers.Host
}
if (typeof options.progress === 'undefined') {
options.progress = () => {}
}
2024-01-13 17:17:24 +00:00
// Upload to S3
await useFetch(response.data.url,{
method: 'PUT',
body: file,
headers: headers,
})
response.data.extension = file.name.split('.').pop()
return response.data
2024-01-13 17:17:24 +00:00
}