module.exports = (
  list, listDeep, join, removeRootDir, { storageRoot }
) => (req, res) => {
  const path = join(storageRoot, req.getDir());
  const listPath = req.isDeep() ? listDeep : list;
  return listPath(path)
    .then((entries) => {
      res.status(200).json({
        result: entries.files.map(
          f => removeRootDir(f, storageRoot)
        )
      });
    })
    .catch((error) => {
      if (error.code === 'ENOENT') {
        res.status(404).json({
          error: `dir /${req.getDir()} does not exist`
        });
      } else {
        console.error(error);
        res.status(500).end();
      }
    });
};
