|
|
@ -17,8 +17,8 @@ export default async function handler(
|
|
|
|
req: NextApiRequest,
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse,
|
|
|
|
res: NextApiResponse,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
if (req.method === 'POST') {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (req.method === 'POST') {
|
|
|
|
const form = formidable({ keepExtensions: true });
|
|
|
|
const form = formidable({ keepExtensions: true });
|
|
|
|
form.parse(req, async (err, fields, files) => {
|
|
|
|
form.parse(req, async (err, fields, files) => {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
@ -45,8 +45,26 @@ export default async function handler(
|
|
|
|
url: `${BASE_FILE_URL}/${key}/${filePath}`,
|
|
|
|
url: `${BASE_FILE_URL}/${key}/${filePath}`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (error: unknown) {
|
|
|
|
} else if (req.method === 'DELETE') {
|
|
|
|
return Promise.reject(error);
|
|
|
|
const { key, fileUrl } = req.query;
|
|
|
|
|
|
|
|
const storageKey = key as string;
|
|
|
|
|
|
|
|
const url = fileUrl as string;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const filePath = url.substring(url.lastIndexOf('/') + 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { error } = await supabase.storage
|
|
|
|
|
|
|
|
.from(storageKey)
|
|
|
|
|
|
|
|
.remove([filePath]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
|
|
message: `File ${filePath} has been deleted`,
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|