opnform/resources/js/plugins/vapor.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-09-20 19:59:52 +00:00
import Vue from 'vue'
import axios from "axios"
2022-09-20 19:59:52 +00:00
const assetUrl = import.meta.env.VITE_VAPOR_ASSET_URL
? import.meta.env.VITE_VAPOR_ASSET_URL
2022-09-20 19:59:52 +00:00
: ''
Vue.mixin({
methods: {
asset (path) {
return assetUrl + '/' + path
},
/**
* Store a file in S3 and return its UUID, key, and other information.
*/
async storeFile (file, options = {}) {
const response = await axios.post(options.signedStorageUrl ? options.signedStorageUrl : '/vapor/signed-storage-url', {
bucket: options.bucket || '',
content_type: options.contentType || file.type,
expires: options.expires || '',
visibility: options.visibility || '',
...options.data
}, {
baseURL: options.baseURL || null,
headers: options.headers || {},
...options.options
})
const headers = response.data.headers
if ('Host' in headers) {
delete headers.Host
}
if (typeof options.progress === 'undefined') {
options.progress = () => {}
}
const cancelToken = options.cancelToken || ''
// Remove authorization headers
const cleanAxios = axios.create()
cleanAxios.defaults.headers.common = {}
await cleanAxios.put(response.data.url, file, {
cancelToken: cancelToken,
headers: headers,
onUploadProgress: (progressEvent) => {
options.progress(progressEvent.loaded / progressEvent.total)
}
})
response.data.extension = file.name.split('.').pop()
return response.data
}
}
})