opnform-host-nginx/client/lib/file-uploads.js

41 lines
1008 B
JavaScript
Raw Normal View History

async function storeLocalFile(file) {
2024-01-13 19:57:39 +01:00
let formData = new FormData()
formData.append("file", file)
const response = await opnFetch("/upload-file", {
method: "POST",
body: formData,
2024-01-13 19:57:39 +01:00
})
response.extension = file.name.split(".").pop()
2024-01-13 19:57:39 +01:00
return response
}
export const storeFile = async (file, options = {}) => {
if (useFeatureFlag('storage.local'))
return storeLocalFile(file, options)
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 18:17:24 +01:00
// Upload to S3
await useFetch(response.url, {
method: "PUT",
2024-01-13 18:17:24 +01:00
body: file,
})
response.extension = file.name.split(".").pop()
2024-01-13 19:57:39 +01:00
return response
2024-01-13 18:17:24 +01:00
}