From 2f4326b57d4f12f1056478a88abfc9df82cf5d87 Mon Sep 17 00:00:00 2001 From: kimura-developer <136853071+kimura-developer@users.noreply.github.com> Date: Tue, 3 Oct 2023 08:41:22 -0500 Subject: [PATCH] downloadLayer in AdminLayerController and layer.resource --- .../src/ng1/admin/layers/layer.component.js | 18 ++++++++++++++++-- web-app/src/ng1/factories/layer.resource.js | 6 ++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/web-app/src/ng1/admin/layers/layer.component.js b/web-app/src/ng1/admin/layers/layer.component.js index 6a55436da..c86999c1d 100644 --- a/web-app/src/ng1/admin/layers/layer.component.js +++ b/web-app/src/ng1/admin/layers/layer.component.js @@ -171,8 +171,22 @@ class AdminLayerController { } downloadLayer() { - // WIP - placeholder - this.$state.go('admin.layerEdit', { layerId: layer.id }); + this.Layer.downloadLayer(this.$stateParams.eventId, this.layer.id) + .then((response) => { + const blob = new Blob([response.data], { type: response.data.type }); + const url = window.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.style.display = 'none'; + a.href = url; + // Use a structured naming convention for the filename + a.download = `Layer-${this.layer.id}-${new Date().toISOString()}.ext`; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(url); + }) + .catch((err) => { + console.error('Error downloading the layer', err); + }); } addUploadFile() { diff --git a/web-app/src/ng1/factories/layer.resource.js b/web-app/src/ng1/factories/layer.resource.js index 40846548c..c4255632b 100644 --- a/web-app/src/ng1/factories/layer.resource.js +++ b/web-app/src/ng1/factories/layer.resource.js @@ -77,5 +77,11 @@ function Layer($resource) { } }; + Layer.downloadLayer = function (eventId, layerId) { + const url = `/api/events/${eventId}/layers/${layerId}`; + return $resource(url).get({ responseType: 'blob' }).$promise; + }; + + return Layer; }