options.uploadDir = process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH : path.join(process.cwd(), "/public/upload_images");
const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH :
path.join(process.cwd() + "/public", "/upload_images"));
} catch (error) {
console.log('directory error: ', error);
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(
process.env.RAILWAY_VOLUME_MOUNT_PATH ?
process.env.RAILWAY_VOLUME_MOUNT_PATH :
path.join(process.cwd() + "/public", "/upload_images"));
}
try {
const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
} catch (e) {
console.log('error: ', e);
res.json({ done: "nok" });
}
};
export default handler;