diff --git a/CEO/.idea/.name b/CEO/.idea/.name index 1c746d5..12fdd11 100644 --- a/CEO/.idea/.name +++ b/CEO/.idea/.name @@ -1 +1 @@ -external_endpoints.js \ No newline at end of file +sending_file_confirmation.js \ No newline at end of file diff --git a/CEO/backup_directories/59c712d9-e6dd-438f-aec9-31ca2ae750e6/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx b/CEO/backup_directories/59c712d9-e6dd-438f-aec9-31ca2ae750e6/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx new file mode 100644 index 0000000..d8b87bb Binary files /dev/null and b/CEO/backup_directories/59c712d9-e6dd-438f-aec9-31ca2ae750e6/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx differ diff --git a/CEO/backup_directories/59c712d9-e6dd-438f-aec9-31ca2ae750e6/backup_dir/fisiere/New Microsoft Word Document.docx b/CEO/backup_directories/59c712d9-e6dd-438f-aec9-31ca2ae750e6/backup_dir/fisiere/New Microsoft Word Document.docx new file mode 100644 index 0000000..ff94689 Binary files /dev/null and b/CEO/backup_directories/59c712d9-e6dd-438f-aec9-31ca2ae750e6/backup_dir/fisiere/New Microsoft Word Document.docx differ diff --git a/CEO/backup_directories/7c0ef9d5-23ed-47a6-bfb4-fdcfce436904/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx b/CEO/backup_directories/7c0ef9d5-23ed-47a6-bfb4-fdcfce436904/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx new file mode 100644 index 0000000..8f3963d Binary files /dev/null and b/CEO/backup_directories/7c0ef9d5-23ed-47a6-bfb4-fdcfce436904/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx differ diff --git a/CEO/backup_directories/7c0ef9d5-23ed-47a6-bfb4-fdcfce436904/backup_dir/fisiere/New Microsoft Word Document.docx b/CEO/backup_directories/7c0ef9d5-23ed-47a6-bfb4-fdcfce436904/backup_dir/fisiere/New Microsoft Word Document.docx new file mode 100644 index 0000000..ebcad29 Binary files /dev/null and b/CEO/backup_directories/7c0ef9d5-23ed-47a6-bfb4-fdcfce436904/backup_dir/fisiere/New Microsoft Word Document.docx differ diff --git a/CEO/jobs/clean.js b/CEO/jobs/clean.js new file mode 100644 index 0000000..236f48a --- /dev/null +++ b/CEO/jobs/clean.js @@ -0,0 +1,28 @@ +const fs = require('fs'); +const path = require('path'); + +// List of files you want to check and delete +const filesToDelete = [ + path.join(__dirname, '..', 'backupSchemes.json'), + path.join(__dirname, '..', 'dirBackup.json'), + path.join(__dirname, '..', 'dirShare.json'), + path.join(__dirname, '..', 'usersInSystem.json'), + path.join(__dirname, '..', 'loginData.json'), + path.join(__dirname, '..', 'ipConfig.json') +]; + +filesToDelete.forEach(file => { + fs.access(file, fs.constants.F_OK, (err) => { + if (!err) { + fs.unlink(file, err => { + if (err) { + console.error(`Failed to delete ${file}: ${err}`); + } else { + console.log(`${file} was deleted successfully.`); + } + }); + } else { + console.log(`${file} does not exist.`); + } + }); +}); diff --git a/CEO/jobs/external_endpoints.js b/CEO/jobs/external_endpoints.js index 8b12e01..c8add05 100644 --- a/CEO/jobs/external_endpoints.js +++ b/CEO/jobs/external_endpoints.js @@ -99,7 +99,7 @@ async function addFilePathToJson(filePath) { if (await fsExtra.pathExists(jsonFilePath)) { await lockFile(jsonFilePath); data = await fsExtra.readJson(jsonFilePath); - }else{ + } else { await lockFile(jsonFilePath); } @@ -160,10 +160,10 @@ app.post('/upload', (req, res) => { await fsExtra.move(req.file.path, finalPath, { overwrite: true }); await addFilePathToJson(finalPath); - res.status(httpStatus.OK).json({message: 'Upload successful.'}) + res.status(httpStatus.OK).json({ message: 'Upload successful.' }); } catch (error) { console.error('Failed to move file:', error); - res.status(httpStatus.INTERNAL_SERVER_ERROR).json({message: 'Server error while moving the file.'}); + res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Server error while moving the file.' }); } }); }); diff --git a/CEO/package.json b/CEO/package.json index 1377cee..a46a982 100644 --- a/CEO/package.json +++ b/CEO/package.json @@ -5,6 +5,7 @@ "description": "Aplicatie P2P pentru stocarea resurselor digitale", "main": "src/main/main.js", "scripts": { + "clean": "node ./jobs/clean.js", "start": "electron --trace-warnings ./src/main/main.js", "dev": "electronmon --trace-warnings ./src/main/main.js" }, diff --git a/CEO/src/main/main.js b/CEO/src/main/main.js index 388cb49..6cbedb3 100644 --- a/CEO/src/main/main.js +++ b/CEO/src/main/main.js @@ -72,6 +72,13 @@ const checkForServerConnection = async () => { } catch (error) { console.error("Error:", error); return false; + } finally{ + try{ + fs.unlinkSync(pathToIpConfig); + } + catch{ + + } } }; diff --git a/CEO/src/renderer/js/ip_submit.js b/CEO/src/renderer/js/ip_submit.js index 7c78d06..bcc69e9 100644 --- a/CEO/src/renderer/js/ip_submit.js +++ b/CEO/src/renderer/js/ip_submit.js @@ -19,9 +19,10 @@ document.addEventListener('DOMContentLoaded', async function () { await window.electronAPI.writeFile('ipConfig.json', JSON.stringify({ip: ipAddress})); fadeOut('login.html'); }) - .catch(error => { - console.error('Error:', error.message); - throw new Error(error.message); + .catch(async error => { + await window.electronAPI.showAlert('Can\'t reach server.') + .then(() => console.log('Alert window opened')) + .catch(error => console.error('Error showing alert:', error)); }); } catch (error) { console.error('Error:', error.message); diff --git a/CEO/src/renderer/js/sending_file_confirmation.js b/CEO/src/renderer/js/sending_file_confirmation.js index f1b46f6..9ee88d0 100644 --- a/CEO/src/renderer/js/sending_file_confirmation.js +++ b/CEO/src/renderer/js/sending_file_confirmation.js @@ -6,13 +6,26 @@ document.addEventListener("DOMContentLoaded", async function () { const uploadPromises = uploadData.users.map(async (user) => { try { + const response = await fetch(uploadData.filePath); + if (!response.ok) { + throw new Error('Failed to fetch file'); + } + const file = await response.blob(); + if (!file.size) { + throw new Error('File is empty or could not be loaded'); + } + const formData = new FormData(); - const file = await fetch(uploadData.filePath).then(response => response.blob()); formData.append('file', file); formData.append('idUser', user.userId); formData.append('nameOfFile', user.fileName); + formData.append('sizeOfFile', file.size); + + // Debugging FormData content + for (let [key, value] of formData.entries()) { + console.log(key, value instanceof Blob ? `Blob (${value.size} bytes)` : value); + } - await timeout(5000); // Timeout to simulate delay or wait const url = `http://${user.destIp}:${user.destPort}/upload`; const uploadResponse = await fetch(url, { method: 'POST', @@ -20,7 +33,13 @@ document.addEventListener("DOMContentLoaded", async function () { }); if (!uploadResponse.ok) { - throw new Error(`HTTP error during file upload to ${user.userId}: status ${uploadResponse.status}`); + try { + const errorResponse = await uploadResponse.json(); + throw new Error(`HTTP error during file upload to ${user.userId}: ${errorResponse.message}`); + } catch (parseError) { + const responseText = await uploadResponse.text(); + throw new Error(`HTTP error during file upload to ${user.userId}: ${responseText}`); + } } return {userId: user.userId, success: true, message: `Upload successful for user ${user.userId}`}; @@ -32,9 +51,7 @@ document.addEventListener("DOMContentLoaded", async function () { const results = await Promise.all(uploadPromises); results.forEach(result => { - if (result.success) { - console.log(result.message); - } + console.log(result.message); }); console.log('All files processed. Check the console for detailed results.'); } catch (error) { @@ -49,6 +66,6 @@ document.addEventListener("DOMContentLoaded", async function () { } await performUploads(); - await timeout(10000); + await timeout(10000); // Consider the necessity of this delay in production fadeOut('main_menu.html'); -}); \ No newline at end of file +}); diff --git a/User/.idea/.name b/User/.idea/.name index d3296ce..12fdd11 100644 --- a/User/.idea/.name +++ b/User/.idea/.name @@ -1 +1 @@ -login.css \ No newline at end of file +sending_file_confirmation.js \ No newline at end of file diff --git a/User/jobs/clean.js b/User/jobs/clean.js new file mode 100644 index 0000000..236f48a --- /dev/null +++ b/User/jobs/clean.js @@ -0,0 +1,28 @@ +const fs = require('fs'); +const path = require('path'); + +// List of files you want to check and delete +const filesToDelete = [ + path.join(__dirname, '..', 'backupSchemes.json'), + path.join(__dirname, '..', 'dirBackup.json'), + path.join(__dirname, '..', 'dirShare.json'), + path.join(__dirname, '..', 'usersInSystem.json'), + path.join(__dirname, '..', 'loginData.json'), + path.join(__dirname, '..', 'ipConfig.json') +]; + +filesToDelete.forEach(file => { + fs.access(file, fs.constants.F_OK, (err) => { + if (!err) { + fs.unlink(file, err => { + if (err) { + console.error(`Failed to delete ${file}: ${err}`); + } else { + console.log(`${file} was deleted successfully.`); + } + }); + } else { + console.log(`${file} does not exist.`); + } + }); +}); diff --git a/User/package.json b/User/package.json index 23b5a48..eb223c3 100644 --- a/User/package.json +++ b/User/package.json @@ -5,6 +5,7 @@ "description": "Aplicatie P2P pentru stocarea resurselor digitale", "main": "src/main/main.js", "scripts": { + "clean": "node ./jobs/clean.js", "start": "electron --trace-warnings ./src/main/main.js", "dev": "electronmon --trace-warnings ./src/main/main.js" }, diff --git a/User/src/renderer/js/ip_submit.js b/User/src/renderer/js/ip_submit.js index 7c78d06..bcc69e9 100644 --- a/User/src/renderer/js/ip_submit.js +++ b/User/src/renderer/js/ip_submit.js @@ -19,9 +19,10 @@ document.addEventListener('DOMContentLoaded', async function () { await window.electronAPI.writeFile('ipConfig.json', JSON.stringify({ip: ipAddress})); fadeOut('login.html'); }) - .catch(error => { - console.error('Error:', error.message); - throw new Error(error.message); + .catch(async error => { + await window.electronAPI.showAlert('Can\'t reach server.') + .then(() => console.log('Alert window opened')) + .catch(error => console.error('Error showing alert:', error)); }); } catch (error) { console.error('Error:', error.message); diff --git a/User/src/renderer/js/sending_file_confirmation.js b/User/src/renderer/js/sending_file_confirmation.js index f1b46f6..9ffe881 100644 --- a/User/src/renderer/js/sending_file_confirmation.js +++ b/User/src/renderer/js/sending_file_confirmation.js @@ -11,6 +11,7 @@ document.addEventListener("DOMContentLoaded", async function () { formData.append('file', file); formData.append('idUser', user.userId); formData.append('nameOfFile', user.fileName); + formData.append('sizeOfFile', file.size); await timeout(5000); // Timeout to simulate delay or wait const url = `http://${user.destIp}:${user.destPort}/upload`; @@ -20,7 +21,8 @@ document.addEventListener("DOMContentLoaded", async function () { }); if (!uploadResponse.ok) { - throw new Error(`HTTP error during file upload to ${user.userId}: status ${uploadResponse.status}`); + const response = await uploadResponse.json() + throw new Error(`HTTP error during file upload to ${user.userId}: status ${response.message}`); } return {userId: user.userId, success: true, message: `Upload successful for user ${user.userId}`}; diff --git a/dirs/ceo/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx b/dirs/ceo/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx new file mode 100644 index 0000000..0253ced Binary files /dev/null and b/dirs/ceo/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx differ diff --git a/dirs/ceo/backup_dir/fisiere/New Microsoft Word Document.docx b/dirs/ceo/backup_dir/fisiere/New Microsoft Word Document.docx new file mode 100644 index 0000000..ee863bf Binary files /dev/null and b/dirs/ceo/backup_dir/fisiere/New Microsoft Word Document.docx differ diff --git a/dirs/user/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx b/dirs/user/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx new file mode 100644 index 0000000..0253ced Binary files /dev/null and b/dirs/user/backup_dir/fisiere/New Microsoft Excel Worksheet.xlsx differ diff --git a/dirs/user/backup_dir/fisiere/New Microsoft Word Document.docx b/dirs/user/backup_dir/fisiere/New Microsoft Word Document.docx new file mode 100644 index 0000000..ee863bf Binary files /dev/null and b/dirs/user/backup_dir/fisiere/New Microsoft Word Document.docx differ