diff --git a/src/api.js b/src/api.js index 5f22e58803540389dcdd1322c4b551b1e146210d..202c9126d139d73df6b2883e331ba95edecdf342 100644 --- a/src/api.js +++ b/src/api.js @@ -446,5 +446,31 @@ export const api = { } catch (error) { return null } + }, + async download (id, type, name) { + // c'est un peu hacky + // on créé un 'a' invisble avec un attribut 'download' + // le problème, c'est de créé un attribut dynamique + console.log('Download ' + id + ' | ' + name + ' | ' + type) + const url = serverurl + type + '/download?id=' + id + const token = await localStorage.getItem('token') + + fetch(url, { + headers: { + Authorization: 'Bearer ' + token + } + }) + .then(response => response.blob()) + .then(blob => { + const url = window.URL.createObjectURL(blob) + const a = document.createElement('a') + a.style.display = 'none' + a.href = url + a.download = name + document.body.appendChild(a) + a.click() + window.URL.revokeObjectURL(url) + }) + .catch(() => this.$buefy.toast.open('Download error')) } } diff --git a/src/components/ModelUpdate.vue b/src/components/ModelUpdate.vue index ee91af24b8c83c7fce186752dbe2133ecdc7e3ae..b860f3a8339eb51259403617196ceea0df73fdf3 100644 --- a/src/components/ModelUpdate.vue +++ b/src/components/ModelUpdate.vue @@ -87,47 +87,50 @@ export default { if (response.error) { this.setError(response.message) return - } else { - this.modelId = response.message } if (this.model.file !== undefined) { console.log('Upload model ' + this.model.file) this.setProgress(1) + this.subProgress = true if (!await api.uploadModelFile(this.model.id, this.model.file)) { + this.subProgress = false this.setError('Model upload error') return } + this.subProgress = false } if (this.model.customLayers !== undefined) { console.log('Upload layers ' + this.model.customLayers) this.setProgress(2) - if (!await this.uploadLayers()) { + this.subProgress = true + if (!await this.uploadLayers(this.model.id)) { + this.subProgress = false this.setError('Custom layer upload error') return } + this.subProgress = false } this.setProgress(3) }, - async uploadLayers () { - this.subProgress = true + async uploadLayers (id) { const token = await localStorage.getItem('token') for (var i = 0; i < this.model.customLayers.length; i++) { const layer = this.model.customLayers[i] const data = new FormData() - const url = this.$serverurl + 'models/uploadLayer' + const url = this.$serverurl + 'layers/upload' this.subMessage = 'Uploading ' + layer.name - console.log('Uploading layer ' + i + ' | ' + layer.file) + console.log('Uploading layer ' + i + ' | ' + layer.file + ' to ' + id) if (layer.file === undefined) continue if (layer.name === undefined) continue data.append('file', layer.file) data.append('name', layer.name) - data.append('id', this.modelId) + data.append('id', id) try { const response = await fetch(url, { @@ -142,7 +145,6 @@ export default { return false } } - this.subProgress = false return true } } diff --git a/src/components/ModelUpload.vue b/src/components/ModelUpload.vue index 8f451e4316424cbaf72c2f5551ec1168aa1a2475..bf595b62b4b4d9b4e7ee64d55d6dadd90797fbdc 100644 --- a/src/components/ModelUpload.vue +++ b/src/components/ModelUpload.vue @@ -94,34 +94,39 @@ export default { if (this.model.file !== undefined) { console.log('Upload model ' + this.model.file) this.setProgress(1) + this.subProgress = true if (!await api.uploadModelFile(this.modelId, this.model.file)) { + this.subProgress = false this.setError('Model upload error') return } + this.subProgress = false } if (this.model.customLayers !== undefined) { console.log('Upload layers ' + this.model.customLayers) this.setProgress(2) + this.subProgress = true if (!await this.uploadLayers()) { + this.subProgress = false this.setError('Custom layer upload error') return } + this.subProgress = false } this.setProgress(3) }, async uploadLayers () { - this.subProgress = true const token = await localStorage.getItem('token') for (var i = 0; i < this.model.customLayers.length; i++) { const layer = this.model.customLayers[i] const data = new FormData() - const url = this.$serverurl + 'models/uploadLayer' + const url = this.$serverurl + 'layers/upload' this.subMessage = 'Uploading ' + layer.name - console.log('Uploading layer ' + i + ' | ' + layer.file) + console.log('Uploading layer ' + i + ' | ' + layer.file + ' to ' + this.modelId) if (layer.file === undefined) continue if (layer.name === undefined) continue @@ -142,7 +147,6 @@ export default { return false } } - this.subProgress = false return true } } diff --git a/src/views/Model.vue b/src/views/Model.vue index b21cd698fc83d685684e676f51b1182b15a568fc..7994dfcb5d4fd68c67061b0412de7ac4ff241b6c 100644 --- a/src/views/Model.vue +++ b/src/views/Model.vue @@ -32,7 +32,7 @@ <br> <br> <div v-if="model.checksum"> - <b-button v-if="model.checksum" type="is-primary" size="is-medium" icon-left="download"> + <b-button v-if="model.checksum" type="is-primary" size="is-medium" icon-left="download" @click="download(model.id, 'models', model.fileName)"> Download model </b-button> <br> @@ -44,7 +44,7 @@ <strong>{{model.customLayers.length}} customs layers</strong> <br> <template v-for="layer in model.customLayers"> - <b-button class="customLayer" type="is-primary" icon-left="download" v-bind:key="layer.name" @click="downloadLayer(layer.id)"> + <b-button class="customLayer" type="is-primary" icon-left="download" v-bind:key="layer.name" @click="download(layer.id, 'layers', layer.fileName)"> Download {{layer.name}} </b-button> </template> @@ -112,9 +112,9 @@ export default { this.downloadUrl = this.$serverurl + 'models/download?id=' + this.model.id }, methods: { - downloadModel (id) { - }, - downloadLayer (id) { + async download (id, type, name) { + this.$buefy.toast.open('Downloading ' + name) + api.download(id, type, name) } } }