Skip to content

Commit

Permalink
Fixed layer download functionality to handle JSON error responses and…
Browse files Browse the repository at this point in the history
… ensured correct filename and download button visibility for file-based layers.
  • Loading branch information
kimura-developer committed Oct 5, 2023
1 parent 4727c11 commit a8a175e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
60 changes: 42 additions & 18 deletions web-app/src/ng1/admin/layers/layer.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class AdminLayerController {
$onInit() {
this.Layer.get({ id: this.$stateParams.layerId }, (layer) => {
this.layer = layer;
this.layer.isFileBased = !['WMS', 'XYZ'].includes(this.layer.type);

if (this.layer.state !== 'available') {
this.$timeout(this.checkLayerProcessingStatus.bind(this), 1000);
Expand Down Expand Up @@ -172,24 +173,47 @@ class AdminLayerController {

downloadLayer() {
this.Layer.download({ layerId: this.layer.id })
.$promise.then((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;

// Dynamically generating the filename
const currentDate = new Date().toISOString().slice(0, 10);
a.download = `layer-${this.layer.id}-${currentDate}.ext`; // Adjust extension as needed

document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
.catch((err) => {
console.error('Error downloading the layer', err);
});
.$promise.then(this.handleLayerDownloadResponse.bind(this))
.catch(this.handleLayerDownloadError.bind(this));
}

handleLayerDownloadResponse(response) {
if (response.type === 'application/json') {
this.handleJSONDownloadError(response);
return;
}

this.triggerFileDownload(response);
}

handleLayerDownloadError(err) {
console.error('Error downloading the layer', err);
}

handleJSONDownloadError(response) {
response.text().then((text) => {
const errorObj = JSON.parse(text);
console.error(
'Error downloading the layer',
errorObj.message || errorObj
);
});
}

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() {
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/ng1/admin/layers/layer.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h2>
<button class="btn btn-default pull-right" ng-click="$ctrl.editLayer($ctrl.layer)"><i class="fa fa-edit"></i> Edit</button>
</h2>
<h2>
<button class="btn btn-default pull-right" ng-click="$ctrl.downloadLayer()"><i class="fa fa-download"></i> Download</button>
<button ng-if="layer.isFileBased" (click)="downloadLayer()">Download</button>
</h2>
</div>
</div>
Expand Down

0 comments on commit a8a175e

Please sign in to comment.