gfw-api/gfw-area

View on GitHub
app/src/routes/api/v1/download.router.js

Summary

Maintainability
C
1 day
Test Coverage
A
93%
const Router = require('koa-router');
const logger = require('logger');
const send = require('koa-send');
const fs = require('fs');
const DownloadValidator = require('validators/download.validator');
const DownloadService = require('services/download.service');

const router = new Router({
    prefix: '/download-tiles',
});

class DownloadRouter {

    static async downloadTiles(ctx) {
        // eslint-disable-next-line max-len
        logger.info(`Downloading tiles with minZoom ${ctx.params.minZoom}, maxZoom ${ctx.params.maxZoom}, geostoreId: ${ctx.params.geostoreId}, layerUrl ${ctx.query.layerUrl} and useExtension ${ctx.query.useExtension}`);
        const path = await DownloadService.getTilesZip(
            ctx.params.geostoreId,
            parseInt(ctx.params.minZoom, 10),
            parseInt(ctx.params.maxZoom, 10),
            ctx.query.layerUrl,
            ctx.request.headers['x-api-key'],
            JSON.parse(ctx.query.useExtension)
        );
        ctx.set('content-disposition', 'attachment; filename=download.zip');
        await send(ctx, path, { root: '/' });
        ctx.body.on('finish', () => {
            fs.unlinkSync(path);
        });
    }

}

router.get('/:geostoreId/:minZoom/:maxZoom', DownloadValidator.get, DownloadRouter.downloadTiles);

module.exports = router;