Fix file submissions preview

This commit is contained in:
Julien Nahum
2024-01-13 19:57:39 +01:00
parent bf98497711
commit 91432c4aed
12 changed files with 73 additions and 69 deletions

View File

@@ -1,18 +1,19 @@
import opnformConfig from "~/opnform.config.js";
async function storeLocalFile(file, options={}) {
let formData = new FormData()
formData.append('file', file)
const response = await opnFetch('/upload-file', {
method: 'POST',
body: formData
})
response.extension = file.name.split('.').pop()
return response
}
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
}
if(!useRuntimeConfig().public.s3Enabled) return storeLocalFile(file, options)
const response = await useOpnApi(options.signedStorageUrl ? options.signedStorageUrl : '/vapor/signed-storage-url', {
const response = await opnFetch(options.signedStorageUrl || 'vapor/signed-storage-url', {
method: 'POST',
body: options.data,
bucket: options.bucket || '',
@@ -24,26 +25,13 @@ export const storeFile = async (file, options = {}) => {
...options.options
})
console.log(response)
const headers = response.data.headers
if ('Host' in headers) {
delete headers.Host
}
if (typeof options.progress === 'undefined') {
options.progress = () => {}
}
// Upload to S3
await useFetch(response.data.url,{
await useFetch(response.url,{
method: 'PUT',
body: file,
headers: headers,
})
response.data.extension = file.name.split('.').pop()
response.extension = file.name.split('.').pop()
return response.data
return response
}