import fs from 'fs'

const GET = async (req, res) => {
  if (!req.url.includes("?path")) throw "Failed"
  const path = "../.ec_uploads/" + process.env.NEXT_PUBLIC_COMPANY_ID + decodeURIComponent(req.url.split("?path=")[1]);
  let ext = path.split(".")
  ext = ext[ext.length - 1]
  try {
    const fd = fs.readFileSync(path);
    let type = "";
    if (ext == "jpg" || ext == "jpeg") {
      type = "image/jpeg";
    } else if (ext == "ico") {
      type = "image/ico";
    } else if (ext == "png") {
      type = "image/png";
    } else if (ext == "svg") {
      type = "image/svg";
    } else if (ext == "webp") {
      type = "image/webp";
    } else if (ext == "mp3") {
      type = "audio/mpeg";
    }
    if (!ext) {
      res.status(404).send("Not Found");
    }
    res.status(200).setHeader('Content-Type', type).send(fd)
  } catch (err) {
    console.log("Err", err)
    res.status(404).send("Not Found");
  }
}


export default GET