From 3ef66e52deb1e94a389bef783275ec1434461e24 Mon Sep 17 00:00:00 2001 From: bsourisse Date: Tue, 8 Aug 2023 13:46:38 +0200 Subject: [PATCH] fix: missing await on Node fs.writeFile() method #48 #34 --- pages/api/account/updateAppriseAlert.js | 10 +- pages/api/account/updateAppriseServices.js | 10 +- pages/api/account/updateEmail.js | 10 +- pages/api/account/updateEmailAlert.js | 10 +- pages/api/account/updatePassword.js | 10 +- pages/api/account/updateUsername.js | 10 +- pages/api/cronjob/checkStatus.js | 10 +- pages/api/cronjob/getStorageUsed.js | 10 +- pages/api/repo/add.js | 204 +++++++++++---------- pages/api/repo/id/[slug]/delete.js | 10 +- pages/api/repo/id/[slug]/displayDetails.js | 10 +- pages/api/repo/id/[slug]/edit.js | 190 +++++++++---------- 12 files changed, 271 insertions(+), 223 deletions(-) diff --git a/pages/api/account/updateAppriseAlert.js b/pages/api/account/updateAppriseAlert.js index 5a3487d..832e229 100644 --- a/pages/api/account/updateAppriseAlert.js +++ b/pages/api/account/updateAppriseAlert.js @@ -55,9 +55,13 @@ export default async function handler(req, res) { //Stringify the new users list newUsersList = JSON.stringify(newUsersList); //Write the new JSON - fs.writeFile(jsonDirectory + '/users.json', newUsersList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/users.json', + newUsersList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Successful API send' }); } catch (error) { //Log for backend diff --git a/pages/api/account/updateAppriseServices.js b/pages/api/account/updateAppriseServices.js index 0fe463e..83027bf 100644 --- a/pages/api/account/updateAppriseServices.js +++ b/pages/api/account/updateAppriseServices.js @@ -58,9 +58,13 @@ export default async function handler(req, res) { //Stringify the new users list newUsersList = JSON.stringify(newUsersList); //Write the new JSON - fs.writeFile(jsonDirectory + '/users.json', newUsersList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/users.json', + newUsersList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Successful API send' }); } catch (error) { //Log for backend diff --git a/pages/api/account/updateEmail.js b/pages/api/account/updateEmail.js index 1b2530c..33ff5ed 100644 --- a/pages/api/account/updateEmail.js +++ b/pages/api/account/updateEmail.js @@ -65,9 +65,13 @@ export default async function handler(req, res) { //Stringify the new users list newUsersList = JSON.stringify(newUsersList); //Write the new JSON - fs.writeFile(jsonDirectory + '/users.json', newUsersList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/users.json', + newUsersList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Successful API send' }); } catch (error) { //Log for backend diff --git a/pages/api/account/updateEmailAlert.js b/pages/api/account/updateEmailAlert.js index 5f1361e..fa8b28b 100644 --- a/pages/api/account/updateEmailAlert.js +++ b/pages/api/account/updateEmailAlert.js @@ -55,9 +55,13 @@ export default async function handler(req, res) { //Stringify the new users list newUsersList = JSON.stringify(newUsersList); //Write the new JSON - fs.writeFile(jsonDirectory + '/users.json', newUsersList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/users.json', + newUsersList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Successful API send' }); } catch (error) { //Log for backend diff --git a/pages/api/account/updatePassword.js b/pages/api/account/updatePassword.js index ea8bcb1..7359a63 100644 --- a/pages/api/account/updatePassword.js +++ b/pages/api/account/updatePassword.js @@ -64,9 +64,13 @@ export default async function handler(req, res) { //Stringify the new users list newUsersList = JSON.stringify(newUsersList); //Write the new JSON - fs.writeFile(jsonDirectory + '/users.json', newUsersList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/users.json', + newUsersList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Successful API send' }); } catch (error) { //Log for backend diff --git a/pages/api/account/updateUsername.js b/pages/api/account/updateUsername.js index e919872..2d158d7 100644 --- a/pages/api/account/updateUsername.js +++ b/pages/api/account/updateUsername.js @@ -65,9 +65,13 @@ export default async function handler(req, res) { //Stringify the new users list newUsersList = JSON.stringify(newUsersList); //Write the new JSON - fs.writeFile(jsonDirectory + '/users.json', newUsersList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/users.json', + newUsersList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Successful API send' }); } catch (error) { //Log for backend diff --git a/pages/api/cronjob/checkStatus.js b/pages/api/cronjob/checkStatus.js index ca28228..78ad381 100644 --- a/pages/api/cronjob/checkStatus.js +++ b/pages/api/cronjob/checkStatus.js @@ -116,9 +116,13 @@ export default async function handler(req, res) { //Stringify the repoList to write it into the json file. newRepoList = JSON.stringify(newRepoList); //Write the new json - fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/repo.json', + newRepoList, + (err) => { + if (err) console.log(err); + } + ); } catch (err) { res.status(500).json({ status: 500, diff --git a/pages/api/cronjob/getStorageUsed.js b/pages/api/cronjob/getStorageUsed.js index 33606d2..62232b1 100644 --- a/pages/api/cronjob/getStorageUsed.js +++ b/pages/api/cronjob/getStorageUsed.js @@ -69,9 +69,13 @@ export default async function handler(req, res) { //Stringify the repoList to write it into the json file. newRepoList = JSON.stringify(newRepoList); //Write the new json - fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/repo.json', + newRepoList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ success: 'Storage cron has been executed.', diff --git a/pages/api/repo/add.js b/pages/api/repo/add.js index 273f72a..b0e7e9a 100644 --- a/pages/api/repo/add.js +++ b/pages/api/repo/add.js @@ -6,111 +6,115 @@ const util = require('node:util'); const exec = util.promisify(require('node:child_process').exec); export default async function handler(req, res) { - if (req.method == 'POST') { - //Verify that the user is logged in. - const session = await getServerSession(req, res, authOptions); - if (!session) { - res.status(401).json({ message: 'You must be logged in.' }); - return; - } + if (req.method == 'POST') { + //Verify that the user is logged in. + const session = await getServerSession(req, res, authOptions); + if (!session) { + res.status(401).json({ message: 'You must be logged in.' }); + return; + } - //The data we expect to receive - const { alias, sshPublicKey, size, comment, alert, lanCommand } = - req.body; - //We check that we receive data for each variable. Only "comment" and "lanCommand" are optional in the form. - if (!alias || !sshPublicKey || !size || !alert) { - //If a variable is empty. - res.status(422).json({ - message: 'Unexpected data', - }); - //A return to make sure we don't go any further if data are incorrect. - return; - } + //The data we expect to receive + const { alias, sshPublicKey, size, comment, alert, lanCommand } = + req.body; + //We check that we receive data for each variable. Only "comment" and "lanCommand" are optional in the form. + if (!alias || !sshPublicKey || !size || !alert) { + //If a variable is empty. + res.status(422).json({ + message: 'Unexpected data', + }); + //A return to make sure we don't go any further if data are incorrect. + return; + } - try { - //console.log('API call (PUT)'); - //Find the absolute path of the json directory - const jsonDirectory = path.join(process.cwd(), '/config'); - let repoList = await fs.readFile( - jsonDirectory + '/repo.json', - 'utf8' - ); - //Parse the repoList - repoList = JSON.parse(repoList); + try { + //console.log('API call (PUT)'); + //Find the absolute path of the json directory + const jsonDirectory = path.join(process.cwd(), '/config'); + let repoList = await fs.readFile( + jsonDirectory + '/repo.json', + 'utf8' + ); + //Parse the repoList + repoList = JSON.parse(repoList); - //Find the first biggest ID available to assign it, so the highest ID is already the last added. - let newID = 0; - for (let element in repoList) { - if (newID <= repoList[element].id) { - newID = repoList[element].id + 1; - } - } - //Create the new repo object - const newRepo = { - id: newID, - alias: alias, - repository: 'repo' + newID, - status: false, - lastSave: 0, - alert: alert, - storageSize: Number(size), - storageUsed: 0, - sshPublicKey: sshPublicKey, - comment: comment, - displayDetails: true, - unixUser: '', - lanCommand: lanCommand, - }; + //Find the first biggest ID available to assign it, so the highest ID is already the last added. + let newID = 0; + for (let element in repoList) { + if (newID <= repoList[element].id) { + newID = repoList[element].id + 1; + } + } + //Create the new repo object + const newRepo = { + id: newID, + alias: alias, + repository: 'repo' + newID, + status: false, + lastSave: 0, + alert: alert, + storageSize: Number(size), + storageUsed: 0, + sshPublicKey: sshPublicKey, + comment: comment, + displayDetails: true, + unixUser: '', + lanCommand: lanCommand, + }; - ////Call the shell : createRepo.sh - //Find the absolute path of the shells directory - const shellsDirectory = path.join(process.cwd(), '/helpers'); - //Exec the shell - const { stdout, stderr } = await exec( - `${shellsDirectory}/shells/createRepo.sh ${newRepo.repository} "${newRepo.sshPublicKey}" ${newRepo.storageSize}` - ); - if (stderr) { - console.log('stderr:', stderr); - res.status(500).json({ - status: 500, - message: 'Error on creation, contact the administrator.', - }); - return; - } - newRepo.unixUser = stdout.trim(); + ////Call the shell : createRepo.sh + //Find the absolute path of the shells directory + const shellsDirectory = path.join(process.cwd(), '/helpers'); + //Exec the shell + const { stdout, stderr } = await exec( + `${shellsDirectory}/shells/createRepo.sh ${newRepo.repository} "${newRepo.sshPublicKey}" ${newRepo.storageSize}` + ); + if (stderr) { + console.log('stderr:', stderr); + res.status(500).json({ + status: 500, + message: 'Error on creation, contact the administrator.', + }); + return; + } + newRepo.unixUser = stdout.trim(); - //Create the new repoList with the new repo - let newRepoList = [newRepo, ...repoList]; + //Create the new repoList with the new repo + let newRepoList = [newRepo, ...repoList]; - //Stringify the newRepoList to write it into the json file. - newRepoList = JSON.stringify(newRepoList); + //Stringify the newRepoList to write it into the json file. + newRepoList = JSON.stringify(newRepoList); - //Write the new json - fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => { - if (err) console.log(err); - }); - res.status(200).json({ message: 'Envoi API réussi' }); - } catch (error) { - //Log for backend - console.log(error); - //Log for frontend - if (error.code == 'ENOENT') { - res.status(500).json({ - status: 500, - message: 'No such file or directory', - }); - } else { - res.status(500).json({ - status: 500, - message: 'API error, contact the administrator', - }); - } - return; - } - } else { - res.status(405).json({ - status: 405, - message: 'Method Not Allowed ', - }); - } + //Write the new json + await fs.writeFile( + jsonDirectory + '/repo.json', + newRepoList, + (err) => { + if (err) console.log(err); + } + ); + res.status(200).json({ message: 'Envoi API réussi' }); + } catch (error) { + //Log for backend + console.log(error); + //Log for frontend + if (error.code == 'ENOENT') { + res.status(500).json({ + status: 500, + message: 'No such file or directory', + }); + } else { + res.status(500).json({ + status: 500, + message: 'API error, contact the administrator', + }); + } + return; + } + } else { + res.status(405).json({ + status: 405, + message: 'Method Not Allowed ', + }); + } } diff --git a/pages/api/repo/id/[slug]/delete.js b/pages/api/repo/id/[slug]/delete.js index 717028f..2ff87a2 100644 --- a/pages/api/repo/id/[slug]/delete.js +++ b/pages/api/repo/id/[slug]/delete.js @@ -74,9 +74,13 @@ export default async function handler(req, res) { //Stringify the repoList to write it into the json file. repoList = JSON.stringify(repoList); //Write the new json - fs.writeFile(jsonDirectory + '/repo.json', repoList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/repo.json', + repoList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Envoi API réussi' }); } catch (error) { //Log for backend diff --git a/pages/api/repo/id/[slug]/displayDetails.js b/pages/api/repo/id/[slug]/displayDetails.js index a244306..1c76cba 100644 --- a/pages/api/repo/id/[slug]/displayDetails.js +++ b/pages/api/repo/id/[slug]/displayDetails.js @@ -46,9 +46,13 @@ export default async function handler(req, res) { //Stringify the newRepoList to write it into the json file. newRepoList = JSON.stringify(newRepoList); //Write the new json - fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => { - if (err) console.log(err); - }); + await fs.writeFile( + jsonDirectory + '/repo.json', + newRepoList, + (err) => { + if (err) console.log(err); + } + ); res.status(200).json({ message: 'Envoi API réussi' }); } catch (error) { //Log for backend diff --git a/pages/api/repo/id/[slug]/edit.js b/pages/api/repo/id/[slug]/edit.js index f70b9e1..217b18b 100644 --- a/pages/api/repo/id/[slug]/edit.js +++ b/pages/api/repo/id/[slug]/edit.js @@ -6,102 +6,106 @@ const util = require('node:util'); const exec = util.promisify(require('node:child_process').exec); export default async function handler(req, res) { - if (req.method == 'PUT') { - //Verify that the user is logged in. - const session = await getServerSession(req, res, authOptions); - if (!session) { - res.status(401).json({ message: 'You must be logged in.' }); - return; - } + if (req.method == 'PUT') { + //Verify that the user is logged in. + const session = await getServerSession(req, res, authOptions); + if (!session) { + res.status(401).json({ message: 'You must be logged in.' }); + return; + } - //The data we expect to receive - const { alias, sshPublicKey, size, comment, alert, lanCommand } = - req.body; - //We check that we receive data for each variable. Only "comment" and "lanCommand" are optional in the form. - if (!alias || !sshPublicKey || !size || !alert) { - //If a variable is empty. - res.status(422).json({ - message: 'Unexpected data', - }); - //A return to make sure we don't go any further if data are incorrect. - return; - } + //The data we expect to receive + const { alias, sshPublicKey, size, comment, alert, lanCommand } = + req.body; + //We check that we receive data for each variable. Only "comment" and "lanCommand" are optional in the form. + if (!alias || !sshPublicKey || !size || !alert) { + //If a variable is empty. + res.status(422).json({ + message: 'Unexpected data', + }); + //A return to make sure we don't go any further if data are incorrect. + return; + } - try { - //console.log('API call (PUT)'); - //Find the absolute path of the json directory - const jsonDirectory = path.join(process.cwd(), '/config'); - let repoList = await fs.readFile( - jsonDirectory + '/repo.json', - 'utf8' - ); - //Parse the repoList - repoList = JSON.parse(repoList); + try { + //console.log('API call (PUT)'); + //Find the absolute path of the json directory + const jsonDirectory = path.join(process.cwd(), '/config'); + let repoList = await fs.readFile( + jsonDirectory + '/repo.json', + 'utf8' + ); + //Parse the repoList + repoList = JSON.parse(repoList); - //Find the index of the repo in repoList - //NOTE : req.query.slug return a string, so parseInt to use with indexOf. - const repoIndex = repoList - .map((repo) => repo.id) - .indexOf(parseInt(req.query.slug)); + //Find the index of the repo in repoList + //NOTE : req.query.slug return a string, so parseInt to use with indexOf. + const repoIndex = repoList + .map((repo) => repo.id) + .indexOf(parseInt(req.query.slug)); - ////Call the shell : updateRepo.sh - //Find the absolute path of the shells directory - const shellsDirectory = path.join(process.cwd(), '/helpers'); - // //Exec the shell - const { stderr } = await exec( - `${shellsDirectory}/shells/updateRepo.sh ${repoList[repoIndex].unixUser} "${sshPublicKey}" ${size}` - ); - if (stderr) { - console.log('stderr:', stderr); - res.status(500).json({ - status: 500, - message: 'Error on update, contact the administrator.', - }); - return; - } + ////Call the shell : updateRepo.sh + //Find the absolute path of the shells directory + const shellsDirectory = path.join(process.cwd(), '/helpers'); + // //Exec the shell + const { stderr } = await exec( + `${shellsDirectory}/shells/updateRepo.sh ${repoList[repoIndex].unixUser} "${sshPublicKey}" ${size}` + ); + if (stderr) { + console.log('stderr:', stderr); + res.status(500).json({ + status: 500, + message: 'Error on update, contact the administrator.', + }); + return; + } - //Find the ID in the data and change the values transmitted by the form - let newRepoList = repoList.map((repo) => - repo.id == req.query.slug - ? { - ...repo, - alias: alias, - sshPublicKey: sshPublicKey, - storageSize: size, - comment: comment, - alert: alert, - lanCommand: lanCommand, - } - : repo - ); - //Stringify the newRepoList to write it into the json file. - newRepoList = JSON.stringify(newRepoList); - //Write the new json - fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => { - if (err) console.log(err); - }); - res.status(200).json({ message: 'Envoi API réussi' }); - } catch (error) { - //Log for backend - console.log(error); - //Log for frontend - if (error.code == 'ENOENT') { - res.status(500).json({ - status: 500, - message: 'No such file or directory', - }); - } else { - res.status(500).json({ - status: 500, - message: 'API error, contact the administrator', - }); - } - return; - } - } else { - res.status(405).json({ - status: 405, - message: 'Method Not Allowed ', - }); - } + //Find the ID in the data and change the values transmitted by the form + let newRepoList = repoList.map((repo) => + repo.id == req.query.slug + ? { + ...repo, + alias: alias, + sshPublicKey: sshPublicKey, + storageSize: size, + comment: comment, + alert: alert, + lanCommand: lanCommand, + } + : repo + ); + //Stringify the newRepoList to write it into the json file. + newRepoList = JSON.stringify(newRepoList); + //Write the new json + await fs.writeFile( + jsonDirectory + '/repo.json', + newRepoList, + (err) => { + if (err) console.log(err); + } + ); + res.status(200).json({ message: 'Envoi API réussi' }); + } catch (error) { + //Log for backend + console.log(error); + //Log for frontend + if (error.code == 'ENOENT') { + res.status(500).json({ + status: 500, + message: 'No such file or directory', + }); + } else { + res.status(500).json({ + status: 500, + message: 'API error, contact the administrator', + }); + } + return; + } + } else { + res.status(405).json({ + status: 405, + message: 'Method Not Allowed ', + }); + } }