Get presigned upload token via ajax request

This commit is contained in:
richard-dds
2019-08-16 16:06:17 -04:00
parent b7f8152cc1
commit 7d1dfa1d0e
6 changed files with 25 additions and 20 deletions

View File

@@ -20,12 +20,6 @@ export default {
props: {
name: String,
token: {
type: Object,
},
objectName: {
type: String,
},
initialData: {
type: String,
},
@@ -52,8 +46,7 @@ export default {
}
},
created: function() {
this.uploader = buildUploader(this.token)
created: async function() {
emitEvent('field-mount', this, {
optional: this.optional,
name: this.name,
@@ -71,13 +64,12 @@ export default {
return
}
const response = await this.uploader.upload(file, this.objectName)
const uploader = await this.getUploader()
const response = await uploader.upload(file, this.objectName)
if (response.ok) {
this.attachment = e.target.value
this.$refs.attachmentFilename.value = file.name
this.$refs.attachmentObjectName.value = this.objectName
this.$refs.attachmentInput.disabled = true
this.$refs.attachmentObjectName.value = response.objectName
} else {
this.uploadError = true
}
@@ -111,6 +103,11 @@ export default {
this.uploadError = false
this.sizeError = false
},
getUploader: async function() {
return fetch('/upload-token')
.then(response => response.json())
.then(({ token }) => buildUploader(token))
},
},
computed: {

View File

@@ -34,7 +34,7 @@ class AzureUploader {
if (err) {
resolve({ ok: false })
} else {
resolve({ ok: true })
resolve({ ok: true, objectName })
}
}
)
@@ -57,10 +57,12 @@ class AwsUploader {
form.append('file', file)
form.set('x-amz-meta-filename', file.name)
return fetch(this.presignedPost.url, {
const response = await fetch(this.presignedPost.url, {
method: 'POST',
body: form,
})
return { ok: response.ok, objectName }
}
}
@@ -70,7 +72,7 @@ class MockUploader {
}
async upload(file, objectName) {
return Promise.resolve({ ok: true })
return Promise.resolve({ ok: true, objectName })
}
}