const fs = require('fs');
const path = require('path');
var mime = require('mime-types');

async function get(req) {
  try {
    let { dir_prefix } = req.query;
    let zip_file_path = `./tmp/${dir_prefix}/image.jpg`;
    let file_content = await fs.readFileSync(zip_file_path);
    let filename = path.basename(zip_file_path);
    let file_mime = mime.lookup(zip_file_path);

    return [file_mime, file_content, filename];
  } catch (error) {
    console.error(error);
  }
}

export default async function handler(req, res) {
  try {
    let [mime, buffer, filename] = await get(req);

    res.setHeader('Content-Type', mime);
    // res.setHeader('Content-Disposition', `attachment; filename=${filename}`);

    return res.status(200).send(buffer);
  } catch (err) {
    console.log(err);
    res.status(200).send({ status: 'error', message: 'helloworld error' });
  }
}
