feat(Delete image in folder + db ): delete in folder + db, add in folder + db

This commit is contained in:
Laurian-Dufrechou
2023-03-31 01:28:22 +02:00
parent 03cb13030e
commit ae33f29c94
5 changed files with 134 additions and 92 deletions
+19
View File
@@ -0,0 +1,19 @@
const fs = require("fs");
export const config = {
api: {
bodyParser: {
sizeLimit: "1mb",
},
},
};
const deleteImage = async (req, res) => {
const body = await JSON.parse(req.body);
await fs.unlinkSync(`./public/imageUsers/${body.fileName}`);
res.status(200).send("image deleted");
};
export default (req, res) => {
req.method === "DELETE" ? deleteImage(req, res) : res.status(404).send("");
};
-45
View File
@@ -1,45 +0,0 @@
import fs from "fs";
import formidable from "formidable";
export const config = {
api: {
bodyParser: false,
},
};
const post = async (req, res) => {
const form = new formidable.IncomingForm({
maxFileSize: 5 * 1024 * 1024,
uploadDir: "./public/imageUsers",
keepExtensions: true,
});
form.parse(req, async function (err, fields, files) {
saveFile(files.file);
return res.status(201).send("image saved");
});
};
const saveFile = async (file) => {
const data = fs.readFileSync(file.path);
fs.writeFileSync(`./public/imageUsers/${file.name}`, data);
await fs.unlinkSync(file.path);
return;
};
const deleteImage = async (req, res) => {
const body = req.body;
await fs.unlinkSync(`./public/imageUsers/${body.name}`);
res.status(200).send("image deleted");
};
export default (req, res) => {
req.method === "POST"
? post(req, res)
: req.method === "PUT"
? console.log("PUT")
: req.method === "DELETE"
? deleteImage(req, res)
: req.method === "GET"
? console.log("GET")
: res.status(404).send("");
};
+37
View File
@@ -0,0 +1,37 @@
const fs = require("fs");
import formidable from "formidable";
export const config = {
api: {
bodyParser: false,
},
};
const post = async (req, res) => {
const form = new formidable.IncomingForm({
maxFileSize: 5 * 1024 * 1024,
uploadDir: "./public/imageUsers",
keepExtensions: true,
});
form.parse(req, async function (err, fields, files) {
if (err) {
res.status(500).json({ error: err });
res.end();
return;
}
saveFile(files.file);
return res.status(201).send("image saved");
});
};
const saveFile = async (file) => {
const newPath = `./public/imageUsers/${file.newFilename}`; //nom donnée par formidable (pas utile ici)
const finalPath = `./public/imageUsers/${file.originalFilename}`; //nom avec l'id de l'utilisateur
const data = await fs.readFileSync(newPath);
fs.writeFileSync(finalPath, data);
await fs.unlinkSync(newPath);
return;
};
export default (req, res) => {
req.method === "POST" ? post(req, res) : res.status(404).send("");
};
+2 -6
View File
@@ -38,7 +38,6 @@ export default function UserProfile() {
const { handleSubmit, control } = useForm();
const [userData, setUserData] = useState({});
const [files, setFiles] = useState([]);
const { data: session, status } = useSession();
if (status === "unauthenticated") router.push("/login");
@@ -86,13 +85,14 @@ export default function UserProfile() {
});
// router.reload();
})
.catch(() => {
.catch((err) => {
setIsLoading(false);
toast({
title: `Erreur lors de l'envoi des modifications`,
status: "error",
isClosable: true,
});
console.log(err);
});
}
};
@@ -124,8 +124,6 @@ export default function UserProfile() {
userData={userData}
user={user}
images={user.images}
files={files}
setFiles={setFiles}
/>
) : (
<ModalModifyImages
@@ -133,8 +131,6 @@ export default function UserProfile() {
userData={userData}
user={user}
images={userData.images}
files={files}
setFiles={setFiles}
/>
)}