Skip to content

Commit

Permalink
download layer file
Browse files Browse the repository at this point in the history
  • Loading branch information
kimura-developer committed Nov 8, 2023
1 parent 39dabee commit ee8ad1d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 67 deletions.
19 changes: 19 additions & 0 deletions service/src/routes/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
57 changes: 23 additions & 34 deletions web-app/src/ng1/admin/layers/layer.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ class AdminLayerController {
});
}

isLayerFileBased() {
return this.layer && !!this.layer.file;
}

$postLink() {
this.uploadSnackbar = new snackbar.MDCSnackbar(
document.querySelector('#upload-snackbar')
Expand Down Expand Up @@ -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);
}

Expand All @@ -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({});
}
Expand Down
29 changes: 0 additions & 29 deletions web-app/src/ng1/admin/layers/layer.download.component.js

This file was deleted.

3 changes: 1 addition & 2 deletions web-app/src/ng1/admin/layers/layer.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +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 ng-if="$ctrl.isLayerFileBased()" (click)="downloadLayer()">Download</button>
</h2>
<button ng-if="$ctrl.isLayerFileBased()" ng-click="$ctrl.downloadLayer()">Download</button>
</div>
</div>
<hr>
Expand Down
4 changes: 2 additions & 2 deletions web-app/src/ng1/factories/layer.resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function Layer($resource) {
},
download: {
method: 'GET',
url: '/api/layers/:layerId',
responseType: 'json'
url: '/api/layers/:layerId/file',
responseType: 'blob'
},
}
);
Expand Down

0 comments on commit ee8ad1d

Please sign in to comment.