import mapnik from '../../../plugins/mapnik/funcs/mapnik.js';

const { ReadDir } = mapnik();

// example: GET /api/gis-files?dir=tiles/3828667893214610450
export default async function readDir({
    query,
}, reply) {
    if (!ReadDir) {
        return reply.status(400).send({ error: 'mapnik server address needed', code: 400 });
    }

    const { dir } = query;

    const {
        exists = false, count = 0, files = [], directories = [],
    } = await ReadDir({ path: dir });
    return { exists, count, data: files.map(el => ({ type: 'file', ...el })).concat(directories.map(el => ({ type: 'directory', ...el }))) };
}
