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