From ee8ad1d0f26f713e9b1eab8d4646fa648bc81641 Mon Sep 17 00:00:00 2001 From: kimura-developer <136853071+kimura-developer@users.noreply.github.com> Date: Wed, 8 Nov 2023 08:47:13 -0600 Subject: [PATCH] download layer file --- service/src/routes/layers.js | 19 +++++++ .../src/ng1/admin/layers/layer.component.js | 57 ++++++++----------- .../admin/layers/layer.download.component.js | 29 ---------- web-app/src/ng1/admin/layers/layer.html | 3 +- web-app/src/ng1/factories/layer.resource.js | 4 +- 5 files changed, 45 insertions(+), 67 deletions(-) delete mode 100644 web-app/src/ng1/admin/layers/layer.download.component.js diff --git a/service/src/routes/layers.js b/service/src/routes/layers.js index 91cf0c02d..98cc38fb1 100644 --- a/service/src/routes/layers.js +++ b/service/src/routes/layers.js @@ -314,6 +314,25 @@ module.exports = function(app, security) { } ); + // get layer file + app.get( + '/api/layers/:layerId/file', + access.authorize('READ_LAYER_ALL'), + function(req, res) { + if (!req.layer.file) { + return res.status(404).send('Layer does not have a file'); + } + const stream = fs.createReadStream( + path.join(environment.layerBaseDirectory, req.layer.file.relativePath) + ); + stream.on('open', () => { + res.type(req.layer.file.contentType); + res.header('Content-Length', req.layer.file.size); + stream.pipe(res); + }); + } + ); + // get layer app.get('/api/layers/:layerId', access.authorize('READ_LAYER_ALL'), diff --git a/web-app/src/ng1/admin/layers/layer.component.js b/web-app/src/ng1/admin/layers/layer.component.js index 109943649..c265dad8e 100644 --- a/web-app/src/ng1/admin/layers/layer.component.js +++ b/web-app/src/ng1/admin/layers/layer.component.js @@ -96,10 +96,6 @@ class AdminLayerController { }); } - isLayerFileBased() { - return this.layer && !!this.layer.file; - } - $postLink() { this.uploadSnackbar = new snackbar.MDCSnackbar( document.querySelector('#upload-snackbar') @@ -174,28 +170,37 @@ class AdminLayerController { }); } - downloadLayer() { - this.Layer.download({ layerId: this.layer.id }) - .$promise.then(this.handleLayerDownloadResponse.bind(this)) - .catch(this.handleLayerDownloadError.bind(this)); + isLayerFileBased() { + return this.layer && !!this.layer.file; } - handleLayerDownloadResponse(response) { - if (response && response.filePath) { - const desiredFileName = this.layer.fileName; - this.triggerFileDownload(response.filePath, desiredFileName); - } else { - this.handleJSONDownloadError(response); - } + getAccessToken() { + return this.LocalStorageService.getToken(); + } + + constructDownloadURL() { + const accessToken = this.getAccessToken(); + return `/api/layers/${this.layer.id}/file?access_token=${accessToken}`; } - triggerFileDownload(filePath, fileName) { + createDownloadLink() { + const downloadURL = this.constructDownloadURL(); + const a = document.createElement('a'); - a.href = filePath; - a.download = fileName; + a.href = downloadURL; + a.download = this.layer.file.name; a.style.display = 'none'; + + return a; + } + + downloadLayer() { + const a = this.createDownloadLink(); + document.body.appendChild(a); a.click(); + + // Clean up document.body.removeChild(a); } @@ -211,22 +216,6 @@ class AdminLayerController { } } - triggerFileDownload(response) { - const blob = new Blob([response], { type: response.type }); - const url = window.URL.createObjectURL(blob); - const a = document.createElement('a'); - a.style.display = 'none'; - a.href = url; - - const currentDate = new Date().toISOString().slice(0, 10); - const fileExtension = this.layer.fileType || 'ext'; - a.download = `layer-${this.layer.id}-${currentDate}.${fileExtension}`; - - document.body.appendChild(a); - a.click(); - window.URL.revokeObjectURL(url); - } - addUploadFile() { this.uploads.push({}); } diff --git a/web-app/src/ng1/admin/layers/layer.download.component.js b/web-app/src/ng1/admin/layers/layer.download.component.js deleted file mode 100644 index 23e5293a1..000000000 --- a/web-app/src/ng1/admin/layers/layer.download.component.js +++ /dev/null @@ -1,29 +0,0 @@ -class AdminLayerDownloadController { - constructor() {} - - $onInit() { - this.layer = this.resolve.layer; - } - - downloadLayer(layer) { - // Logic to handle downloading the layer goes here - - // Once complete, close the modal. - this.modalInstance.close(layer); - } - - cancel() { - this.modalInstance.dismiss('cancel'); - } -} - -AdminLayerDownloadController.$inject = []; - -export default { - template: require('./layer.download.html'), - bindings: { - resolve: '<', - modalInstance: '<' - }, - controller: AdminLayerDownloadController -}; diff --git a/web-app/src/ng1/admin/layers/layer.html b/web-app/src/ng1/admin/layers/layer.html index 2b2084842..133aeb9d5 100644 --- a/web-app/src/ng1/admin/layers/layer.html +++ b/web-app/src/ng1/admin/layers/layer.html @@ -20,8 +20,7 @@

- -

+
diff --git a/web-app/src/ng1/factories/layer.resource.js b/web-app/src/ng1/factories/layer.resource.js index a8f5a52ad..80cd40411 100644 --- a/web-app/src/ng1/factories/layer.resource.js +++ b/web-app/src/ng1/factories/layer.resource.js @@ -58,8 +58,8 @@ function Layer($resource) { }, download: { method: 'GET', - url: '/api/layers/:layerId', - responseType: 'json' + url: '/api/layers/:layerId/file', + responseType: 'blob' }, } );