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 @@