opnform/client/lib/file-uploads.js

38 lines
985 B
JavaScript
Raw Permalink Normal View History

2024-01-13 18:57:39 +00:00
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 = {}) => {
2024-01-13 18:57:39 +00:00
if(!useRuntimeConfig().public.s3Enabled) return storeLocalFile(file, options)
2024-01-13 18:57:39 +00:00
const response = await opnFetch(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
// Upload to S3
2024-01-13 18:57:39 +00:00
await useFetch(response.url,{
2024-01-13 17:17:24 +00:00
method: 'PUT',
body: file,
})
2024-01-13 18:57:39 +00:00
response.extension = file.name.split('.').pop()
2024-01-13 18:57:39 +00:00
return response
2024-01-13 17:17:24 +00:00
}