CEO updated

This commit is contained in:
andrei-mihnea-cerbu
2024-04-20 03:18:44 +03:00
parent 869305a491
commit 57c2c7ec55
81 changed files with 3517 additions and 1035 deletions
+172 -24
View File
@@ -1,8 +1,27 @@
const fs = require('fs').promises; // Ensure you use the promise-based API
const fsPromises = require('fs').promises; // Ensure you use the promise-based API
const fs = require('fs');
const path = require('path');
const { decryptFileInPlace, encryptFileInPlace, encryptFileWithKey, decryptFileWithKey} = require("../src/main/aes_encrypt");
const {lockFile, unlockFile} = require("../helpers/lock_mechanism");
async function decryptAndGetServerIP(filePath) {
try {
await lockFile(filePath);
await decryptFileInPlace(filePath);
const fileContent = await fs.promises.readFile(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);
const serverIP = jsonData.ip;
await encryptFileInPlace(filePath)
await unlockFile(filePath);
return serverIP;
} catch (error) {
console.error('An error occurred:', error);
return null;
}
}
function extractFilePaths(directoryStructure) {
let paths = [];
const traverse = (dir, currentPath) => {
@@ -48,31 +67,32 @@ async function fetchFiles(ip, filePaths) {
async function removeDirectory(directoryPath) {
try {
// Check if the directory exists
const stats = await fs.stat(directoryPath);
const stats = await fsPromises.stat(directoryPath);
if (!stats.isDirectory()) {
console.log('The specified path is not a directory.');
return;
}
// Read all the contents of the directory
const files = await fs.readdir(directoryPath);
const files = await fsPromises.readdir(directoryPath);
// Loop through each file/directory and delete them
for (const file of files) {
const currentPath = path.join(directoryPath, file);
const currentStats = await fs.stat(currentPath);
const currentStats = await fsPromises.stat(currentPath);
if (currentStats.isDirectory()) {
// Recursive call for directories
await removeDirectory(currentPath);
} else {
// Delete file
await fs.unlink(currentPath);
await lockFile(currentPath);
await unlockFile(currentPath);
await fsPromises.unlink(currentPath);
}
}
// Finally, delete the directory itself
await fs.rmdir(directoryPath);
await fsPromises.rmdir(directoryPath);
//console.log(`Directory removed: ${directoryPath}`);
} catch (error) {
console.error(`Error removing directory: ${error.message}`);
@@ -87,23 +107,28 @@ async function decryptUserSystemConfig() {
async function encryptUserSystemConfig(){
const configPath = path.join(__dirname, '..', 'usersInSystem.json');
await unlockFile(configPath);
await encryptFileInPlace(configPath);
await unlockFile(configPath);
}
async function processBackup() {
await decryptUserSystemConfig();
const usersConfigPath = path.join(__dirname, '..', 'usersInSystem.json');
const backupConfigPath = path.join(__dirname, '..', 'backupSchemes.json');
const baseBackupDirPath = path.join(__dirname, '..', 'backup_directories');
if(!fs.existsSync(backupConfigPath) || !fs.existsSync(usersConfigPath)){
console.log('Can\'t start backup system: necessary files missing!');
return;
}
await decryptUserSystemConfig();
try {
// Read and parse the users configuration
const usersData = await fs.readFile(usersConfigPath, 'utf8');
const usersData = await fsPromises.readFile(usersConfigPath, 'utf8');
const usersConfig = JSON.parse(usersData);
// Read and parse the backup configuration
const backupData = await fs.readFile(backupConfigPath, 'utf8');
const backupData = await fsPromises.readFile(backupConfigPath, 'utf8');
const jsonData = JSON.parse(backupData);
for (const key in jsonData) {
@@ -115,7 +140,7 @@ async function processBackup() {
const filePaths = extractFilePaths(directoryStructure);
const files = await fetchFiles(node.ip, filePaths);
await fs.mkdir(userBackupDir, { recursive: true });
await fsPromises.mkdir(userBackupDir, { recursive: true });
// Find the encryption key for the user's department
let encryptionKey = '';
@@ -129,8 +154,8 @@ async function processBackup() {
for (const file in files) {
if (files[file]) {
const fullPath = path.join(userBackupDir, file);
await fs.mkdir(path.dirname(fullPath), { recursive: true });
await fs.writeFile(fullPath, files[file]); // Write the binary data
await fsPromises.mkdir(path.dirname(fullPath), { recursive: true });
await fsPromises.writeFile(fullPath, files[file]); // Write the binary data
// Encrypt the file in place after writing it
if (encryptionKey) {
@@ -149,7 +174,7 @@ async function processBackup() {
async function getAllFilePaths(dirPath) {
let filePaths = [];
async function recurse(currentPath) {
const entries = await fs.readdir(currentPath, { withFileTypes: true });
const entries = await fsPromises.readdir(currentPath, { withFileTypes: true });
// Create promises for each entry and process them in parallel
const entryPromises = entries.map(async (entry) => {
const resolvedPath = path.join(currentPath, entry.name);
@@ -168,12 +193,19 @@ async function getAllFilePaths(dirPath) {
return filePaths;
}
async function decryptUserFilesToDirectory(destinationDir) {
await decryptUserSystemConfig(); // Ensure user config is decrypted
async function decryptBackupFilesToDirectory(destinationDir) {
await decryptUserSystemConfig();
const usersConfigPath = path.join(__dirname, '..', 'usersInSystem.json');
const usersConfig = JSON.parse(await fs.readFile(usersConfigPath, 'utf8'));
if(!fs.existsSync(usersConfigPath)){
return false;
}
const usersConfig = JSON.parse(await fsPromises.readFile(usersConfigPath, 'utf8'));
const sourceDir = path.join(__dirname, '..', 'backup_directories');
await removeDirectory(destinationDir);
try {
for (const department in usersConfig) {
const { key, users } = usersConfig[department];
@@ -182,36 +214,152 @@ async function decryptUserFilesToDirectory(destinationDir) {
try {
const files = await getAllFilePaths(userDir);
for (const filePath of files) {
const stats = await fs.stat(filePath);
const stats = await fsPromises.stat(filePath);
if (stats.isFile()) {
// Extract the part of the file path after the userId
const relativePath = path.relative(userDir, filePath);
// Construct the destination file path
const destinationFilePath = path.join(destinationDir, userId, relativePath);
await fs.mkdir(path.dirname(destinationFilePath), { recursive: true });
await fsPromises.mkdir(path.dirname(destinationFilePath), { recursive: true });
await lockFile(filePath);
// Decrypt the file in its original location
await decryptFileWithKey(filePath, key);
// Copy the decrypted file to the destination directory
await fs.copyFile(filePath, destinationFilePath);
await fsPromises.copyFile(filePath, destinationFilePath);
console.log(`Decrypted file copied to: ${destinationFilePath}`);
// Re-encrypt the file in its original location (optional)
await encryptFileWithKey(filePath, key);
await unlockFile(filePath);
}
}
} catch (error) {
console.error(`Error processing files for user ${userId}: ${error.message}`);
await encryptUserSystemConfig();
return false;
}
}
}
} catch (error) {
console.error(`Error processing decryption: ${error.message}`);
await encryptUserSystemConfig();
return false;
}
await encryptUserSystemConfig();
return true;
}
async function decryptDepartmentFilesToDirectory() {
const dirDepartmentPath = path.join(__dirname, '..', 'dirDepartment.json');
const loginDataPath = path.join(__dirname, '..', 'loginData.json');
const usersConfigPath = path.join(__dirname, '..', 'usersInSystem.json');
const sourceDir = path.join(__dirname, '..', 'backup_directories');
processBackup();
decryptUserFilesToDirectory("C:\\Users\\Andrei Cerbu\\Documents\\decrypted");
if (!fs.existsSync(usersConfigPath) || !fs.existsSync(loginDataPath) || !fs.existsSync(dirDepartmentPath)) {
console.error("Missing one or more essential configuration files.");
return false;
}
await lockFile(loginDataPath);
await lockFile(usersConfigPath);
await decryptFileInPlace(loginDataPath);
await decryptFileInPlace(usersConfigPath);
const { path: destinationDir } = JSON.parse(await fsPromises.readFile(dirDepartmentPath, 'utf8'));
const { department } = JSON.parse(await fsPromises.readFile(loginDataPath, 'utf8'));
const usersConfig = JSON.parse(await fsPromises.readFile(usersConfigPath, 'utf8'));
// Check if department exists in users configuration
if (!usersConfig[department]) {
console.error(`No configuration found for department: ${department}`);
return false;
}
const { key, users } = usersConfig[department];
await encryptFileInPlace(loginDataPath);
await encryptFileInPlace(usersConfigPath);
await unlockFile(loginDataPath);
await unlockFile(usersConfigPath);
await removeDirectory(destinationDir);
for (const userId of users) {
const userDir = path.join(sourceDir, userId);
try {
const files = await getAllFilePaths(userDir);
console.log(`Processing files for user ${userId}: ${files}`);
for (const filePath of files) {
const stats = await fsPromises.stat(filePath);
if (stats.isFile()) {
const relativePath = path.relative(userDir, filePath);
// Construct the destination file path
const destinationFilePath = path.join(destinationDir, userId, relativePath);
await fsPromises.mkdir(path.dirname(destinationFilePath), { recursive: true });
// Decrypt the file in its original location
await decryptFileWithKey(filePath, key);
// Copy the decrypted file to the destination directory
await fsPromises.copyFile(filePath, destinationFilePath);
console.log(`Decrypted file copied to: ${destinationFilePath}`);
// Re-encrypt the file in its original location (optional)
await encryptFileWithKey(filePath, key);
}
}
} catch (error) {
console.error(`Error processing files for user ${userId}: ${error.message}`);
return false;
}
}
console.log("All files processed successfully.");
return true;
}
let serverIp = null;
decryptAndGetServerIP(path.join(__dirname, '..', 'ipConfig.json')).then(ip => {
serverIp = ip;
});
const timeoutInterval = 7 * 60 * 1000 //minutes * seconds * miliseconds
setInterval(async () => {
await decryptDepartmentFilesToDirectory();
}, timeoutInterval);
console.log('backup.js process started!');
process.on('SIGINT', async () => {
console.log('Shutdown signal received in \'backup.js\'. Cleaning up...');
try {
console.log('Performing cleanup tasks...');
console.log('Cleanup completed successfully.');
} catch (error) {
console.error('An error occurred during cleanup:', error);
} finally {
console.log('Process terminated');
process.exit(0);
}
});
process.on('message', async (message) => {
if (message.type === 'startBackup') {
console.log('BackupProcess: Received command to start backup.');
await processBackup()
}
if(message.type === 'decryptBackup'){
console.log(`BackupProcess: Received command to decrypt backup to ${message.decryptDestPath}`);
console.log(message.decryptDestPath);
await decryptBackupFilesToDirectory(message.decryptDestPath);
}
});
-79
View File
@@ -1,79 +0,0 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const Joi = require('joi');
const fetch = require('node-fetch');
const { httpStatus } = require('../helpers/http_status');
const app = express();
app.use(express.json());
const filePathSchema = Joi.object({
filePath: Joi.string().required()
});
async function getBaseDirectory() {
const filePath = path.join(__dirname, '..', 'dirBackup.json');
try {
const data = await fs.promises.readFile(filePath, 'utf8');
const jsonData = JSON.parse(data);
return jsonData.path;
} catch (error) {
console.error('Error reading the base directory:', error);
throw error;
}
}
app.post('/file_path', async (req, res) => {
const { error, value } = filePathSchema.validate(req.body);
if (error) {
return res.status(httpStatus.BAD_REQUEST).json({
message: 'Invalid input',
data: error.details
});
}
try {
const basePath = await getBaseDirectory();
let { filePath } = value;
const baseDirName = path.basename(basePath);
if (filePath.startsWith(baseDirName + path.sep)) {
filePath = filePath.slice(baseDirName.length + 1);
}
const fullPath = path.resolve(basePath, filePath);
fs.stat(fullPath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
return res.status(httpStatus.NOT_FOUND).json({
message: 'File not found'
});
}
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
message: 'Error accessing the file'
});
}
if (!stats.isFile()) {
return res.status(httpStatus.BAD_REQUEST).json({
message: 'Path is not a file'
});
}
// Stream the file as binary data
res.writeHead(httpStatus.OK, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="${path.basename(fullPath)}"`
});
const fileStream = fs.createReadStream(fullPath);
fileStream.pipe(res);
});
} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
message: 'Error processing request'
});
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
+187
View File
@@ -0,0 +1,187 @@
const express = require('express');
const fs = require('fs');
const multer = require('multer');
const fsExtra = require('fs-extra');
const path = require('path');
const Joi = require('joi');
const { httpStatus } = require('../helpers/http_status');
const {lockFile, unlockFile} = require("../helpers/lock_mechanism");
const app = express();
app.use(express.json());
app.use('/share_file', express.raw({ type: 'application/octet-stream', limit: 'Infinity' }));
const server = require('http').createServer(app);
const connections = [];
// Store active connections to close them on shutdown
server.on('connection', (conn) => {
connections.push(conn);
conn.on('close', () => {
connections.splice(connections.indexOf(conn), 1);
});
});
const filePathSchema = Joi.object({
filePath: Joi.string().required()
});
async function getBaseDirectory() {
const filePath = path.join(__dirname, '..', 'dirBackup.json');
try {
const data = await fs.promises.readFile(filePath, 'utf8');
const jsonData = JSON.parse(data);
return jsonData.path;
} catch (error) {
console.error('Error reading the base directory:', error);
throw error;
}
}
app.post('/file_path', async (req, res) => {
const { error, value } = filePathSchema.validate(req.body);
if (error) {
return res.status(httpStatus.BAD_REQUEST).json({
message: 'Invalid input',
data: error.details
});
}
try {
const basePath = await getBaseDirectory();
let { filePath } = value;
const baseDirName = path.basename(basePath);
if (filePath.startsWith(baseDirName + path.sep)) {
filePath = filePath.slice(baseDirName.length + 1);
}
const fullPath = path.resolve(basePath, filePath);
fs.stat(fullPath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
return res.status(httpStatus.NOT_FOUND).json({
message: 'File not found'
});
}
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
message: 'Error accessing the file'
});
}
if (!stats.isFile()) {
return res.status(httpStatus.BAD_REQUEST).json({
message: 'Path is not a file'
});
}
// Stream the file as binary data
res.writeHead(httpStatus.OK, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="${path.basename(fullPath)}"`
});
const fileStream = fs.createReadStream(fullPath);
fileStream.pipe(res);
});
} catch (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({
message: 'Error processing request'
});
}
});
async function addFilePathToJson(filePath) {
const jsonFilePath = path.join(__dirname, '..', 'filesReceived.json');
try {
let data = { receivedFiles: [] };
if (await fsExtra.pathExists(jsonFilePath)) {
await lockFile(jsonFilePath);
data = await fsExtra.readJson(jsonFilePath);
}else{
await lockFile(jsonFilePath);
}
data.receivedFiles.push(filePath);
await fsExtra.writeJson(jsonFilePath, data, { spaces: 2 });
await unlockFile(jsonFilePath);
console.log('File path added successfully.');
} catch (error) {
console.error('Error updating JSON file:', error);
throw error; // Rethrow the error for further handling if necessary
}
}
const shareFileSchema = Joi.object({
idUser: Joi.string().required(),
nameOfFile: Joi.string().required(),
sizeOfFile: Joi.number().required()
});
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const { error, value } = shareFileSchema.validate(req.body);
if (error) {
cb(error, undefined);
return;
}
const baseDir = path.join(__dirname, '..', 'uploads', value.idUser); // Temp storage location
fsExtra.ensureDirSync(baseDir);
cb(null, baseDir);
},
filename: function (req, file, cb) {
cb(null, req.body.nameOfFile); // Using directly assuming it has been validated already
}
});
const upload = multer({ storage: storage }).single('file');
app.post('/upload', (req, res) => {
upload(req, res, async (err) => {
if (err instanceof multer.MulterError) {
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(`Multer error: ${err.message}`);
return;
} else if (err) {
res.status(httpStatus.BAD_REQUEST).send(`Validation error: ${err.message}`);
return;
}
try {
const { idUser, nameOfFile } = req.body;
const dirConfig = await fsExtra.readJson(path.join(__dirname, '..', 'dirShare.json'));
const baseDir = dirConfig.path;
const finalPath = path.join(baseDir, idUser, nameOfFile);
await fsExtra.move(req.file.path, finalPath, { overwrite: true });
await addFilePathToJson(finalPath);
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.'});
}
});
});
console.log('external_endpoints.json started.');
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
process.on('SIGINT', () => {
console.log('SIGINT signal received. Shutting down gracefully.');
server.close(() => {
console.log('Server closed.');
// Ensure all connections are closed
connections.forEach(conn => conn.end());
setTimeout(() => connections.forEach(conn => conn.destroy()), 5000);
process.exit(0);
});
});
+43 -18
View File
@@ -1,4 +1,5 @@
const fs = require('fs').promises;
const fsPromises = require('fs').promises;
const fs = require('fs');
const path = require('path');
const ip = require('ip');
const {encryptFileInPlace, decryptFileInPlace} = require('../src/main/aes_encrypt');
@@ -19,7 +20,7 @@ async function fetchBackupSchemes(serverIp) {
// Write the fetched backup schemes to the specified file
try {
await lockFile(destPath);
await fs.writeFile(destPath, JSON.stringify(backupSchemes, null, 2)); // Use null, 2 for pretty formatting
await fsPromises.writeFile(destPath, JSON.stringify(backupSchemes, null, 2)); // Use null, 2 for pretty formatting
await unlockFile(destPath);
console.log('Backup schemes saved successfully to', destPath);
} catch (error) {
@@ -29,14 +30,17 @@ async function fetchBackupSchemes(serverIp) {
async function fetchUsersAndDepartments(serverIp) {
try {
console.log('se asteapta depart');
// Fetch departments
const depResponse = await fetch(`http://${serverIp}/users/departments`, {
method: 'GET',
headers: {'x-api-key': 'uc_api'}
});
const departmentsJson = await depResponse.json();
const departments = departmentsJson['data'];
console.log('se asteapta users')
// Fetch users
const userResponse = await fetch(`http://${serverIp}/users`, {
method: 'GET',
@@ -56,8 +60,11 @@ async function fetchUsersAndDepartments(serverIp) {
// Write combined data to a file
const filePath = path.join(__dirname, '..', 'usersInSystem.json');
if(!fs.existsSync(filePath)){
await fsPromises.writeFile(filePath, '');
}
await lockFile(filePath);
await fs.writeFile(filePath, JSON.stringify(combinedData, null, 2), 'utf8');
await fsPromises.writeFile(filePath, JSON.stringify(combinedData, null, 2), 'utf8');
await encryptFileInPlace(filePath);
await unlockFile(filePath);
@@ -72,7 +79,7 @@ async function decryptAndGetServerIP(filePath) {
await lockFile(filePath);
await decryptFileInPlace(filePath);
const fileContent = await fs.readFile(filePath, 'utf8');
const fileContent = await fsPromises.readFile(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);
const serverIP = jsonData.ip;
@@ -87,7 +94,7 @@ async function decryptAndGetServerIP(filePath) {
async function getDirectoryStructure(dirPath) {
const baseName = path.basename(dirPath);
const entries = await fs.readdir(dirPath, {withFileTypes: true});
const entries = await fsPromises.readdir(dirPath, {withFileTypes: true});
const result = {};
result[baseName] = {files: []};
let totalSize = 0;
@@ -101,7 +108,7 @@ async function getDirectoryStructure(dirPath) {
totalSize += subSize;
} else {
// Add file name to the 'files' array and calculate total size
const stats = await fs.stat(entryPath);
const stats = await fsPromises.stat(entryPath);
result[baseName].files.push(entry.name);
totalSize += stats.size;
}
@@ -114,7 +121,7 @@ async function createBackupScheme(serverIp, structure, size) {
await lockFile(filePath);
await decryptFileInPlace(filePath);
const userContent = await fs.readFile(filePath, 'utf8');
const userContent = await fsPromises.readFile(filePath, 'utf8');
const userJson = JSON.parse(userContent);
await encryptFileInPlace(filePath)
@@ -151,7 +158,7 @@ async function createBackupScheme(serverIp, structure, size) {
async function watchDirectoryChanges(serverIp) {
async function readBackupConfig(filePath) {
await lockFile(filePath);
const fileContent = await fs.readFile(filePath, 'utf8');
const fileContent = await fsPromises.readFile(filePath, 'utf8');
await unlockFile(filePath);
return JSON.parse(fileContent);
}
@@ -163,11 +170,15 @@ async function watchDirectoryChanges(serverIp) {
size: size
};
await lockFile(filePath);
await fs.writeFile(path.join(__dirname, '..', 'dirBackup.json'), JSON.stringify(data, null, 2));
await fsPromises.writeFile(path.join(__dirname, '..', 'dirBackup.json'), JSON.stringify(data, null, 2));
await unlockFile(filePath);
}
const configPath = path.join(__dirname, '..', 'dirBackup.json');
if(!fs.existsSync(configPath)){
console.log('backupDir file doesn\'t exists');
return;
}
const backupConfig = await readBackupConfig(configPath);
const storedStructure = backupConfig.structure;
@@ -178,32 +189,46 @@ async function watchDirectoryChanges(serverIp) {
await createBackupScheme(serverIp, currentStructure, currentSize);
await updateBackupConfig(backupConfig.path, currentStructure, currentSize);
console.log('FetcherProcess: start backup.');
process.send({type: 'startBackup'});
} else {
console.log("No changes detected in the directory structure.");
}
}
process.on('SIGINT', () => {
console.log('Process terminated');
process.exit(0);
});
let serverIp = null
decryptAndGetServerIP(path.join(__dirname, '..', 'ipConfig.json')).then(ip => {
serverIp = ip;
});
const timeoutInterval = 6 * 60 * 1000 //minutes * seconds * miliseconds
setInterval(() => {
fetchUsersAndDepartments(serverIp);
}, 5000);
}, timeoutInterval);
setInterval(() => {
watchDirectoryChanges(serverIp);
}, 5000);
}, timeoutInterval);
setInterval(() => {
fetchBackupSchemes(serverIp);
}, 5000);
}, timeoutInterval);
console.log("Service running. Press CTRL+C to stop.");
console.log("fetcher.js process started.");
process.on('SIGINT', async () => {
console.log('Shutdown signal received in \'fetcher.js\'. Cleaning up...');
try {
console.log('Performing cleanup tasks...');
console.log('Cleanup completed successfully.');
} catch (error) {
console.error('An error occurred during cleanup:', error);
} finally {
console.log('Process terminated');
process.exit(0);
}
});
+90
View File
@@ -0,0 +1,90 @@
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const FormData = require('form-data');
const {lockFile, unlockFile} = require("../helpers/lock_mechanism");
const {decryptFileInPlace, encryptFileInPlace} = require("../src/main/aes_encrypt");
async function decryptAndGetServerIP(filePath) {
try {
await lockFile(filePath);
await decryptFileInPlace(filePath);
const fileContent = await fs.promises.readFile(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);
const serverIP = jsonData.ip;
await encryptFileInPlace(filePath)
await unlockFile(filePath);
return serverIP;
} catch (error) {
console.error('An error occurred:', error);
return null;
}
}
async function extractId() {
try {
// Path to the JSON file
const jsonFilePath = path.join(__dirname, '..', 'loginData.json');
// Read and parse the JSON file
const jsonData = await fs.promises.readFile(jsonFilePath, 'utf-8');
const { id } = JSON.parse(jsonData);
return id;
} catch (error) {
console.error('Failed to read file or parse data:', error);
throw error;
}
}
async function uploadFile(filePath, serverIp) {
try {
const idUser = await extractId(); // Fetch ID from JSON
const nameOfFile = path.basename(filePath); // Extract the file name from the path
// Ensure the file exists and get its size
if (!fs.existsSync(filePath)) {
console.error("File does not exist: " + filePath);
process.exit(1);
}
const fileSizeInBytes = fs.statSync(filePath).size;
const form = new FormData();
form.append('idUser', idUser);
form.append('nameOfFile', nameOfFile);
form.append('sizeOfFile', fileSizeInBytes);
form.append('file', fs.createReadStream(filePath));
const response = await axios.post(`http://${serverIp}/upload`, form, {
headers: {
...form.getHeaders(),
},
});
console.log('File uploaded successfully:', response.data);
process.exit(0); // Exit with status 0 (success)
} catch (error) {
console.error('Error uploading file:', error.message);
process.exit(1); // Exit with status 1 (error)
}
}
// Command-line argument for file path
if (process.argv.length < 3) {
console.log('Usage: node <script> <filePath>');
process.exit(1);
}
const filePath = process.argv[2]; // The third command-line argument is the file path
let serverIp = null
decryptAndGetServerIP(path.join(__dirname, '..', 'ipConfig.json')).then(ip => {
serverIp = ip;
});
uploadFile(filePath, serverIp).catch(err => {
console.error(err);
process.exit(1); // Ensure to exit with status 1 in case of promise rejection
});