Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web sockets #29

Merged
merged 11 commits into from
Apr 6, 2023
Prev Previous commit
Next Next commit
Basic websocket logic finished for progress bar
  • Loading branch information
mjhuff committed Apr 6, 2023
commit 9a4810c8841cdf219eb4c3e17aab24f2ef7f9c0b
2 changes: 2 additions & 0 deletions client/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const App = (props) => {
}

function onDataTransfer(value) {
//Don't update value if it isn't a valid increase.
if (value === '') return;
dispatch(updateDataTransferProgressPercent(value));
}

Expand Down
1 change: 0 additions & 1 deletion client/components/MigrationButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const StartMigrationButton = () => {
);

//Run migration logic.
//SHOULD HAVE LOGIC FOR MAKING SURE ALL STATE IS HERE BEFORE YOU ACTUALLY RUN THE MIGRATION.
useEffect(() => {
if (!isMigrating) return;
//Create the request body.
Expand Down
14 changes: 3 additions & 11 deletions server/controllers/rcloneController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const rclone = require('rclone.js');
const { resolve } = require('path');
const AWS = require('aws-sdk');
const errorGenerator = require('./errorGenerator');
const errorGenerator = require('../services/errorGenerator');

const rCloneCopyController = (req, res, next) => {
//Build the strings for rClone to do the copying.
// Build the strings for rClone to do the copying.
const {
originProvider,
destinationProvider,
Expand All @@ -16,21 +16,13 @@ const rCloneCopyController = (req, res, next) => {
const destinationString = `${destinationProvider.toLowerCase()}:${destinationBucket.toLowerCase()}`;

try {
const rcloneCopy = rclone('copy', originString, destinationString, {
res.locals.rcloneCopy = rclone('copy', originString, destinationString, {
env: {
RCLONE_CONFIG: resolve(__dirname, '../../rclone.conf')
},
progress: true
});

rcloneCopy.stdout.on('data', (data) => {
console.log(data.toString());
});

rcloneCopy.stderr.on('data', (data) => {
console.error(data.toString());
});

return next();
} catch (e) {
console.log(e);
Expand Down
19 changes: 11 additions & 8 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require('path');
const express = require('express');
const cors = require('cors');
const fsController = require('./controllers/fsController.js');
const {
rCloneCopyController,
Expand All @@ -11,7 +10,7 @@ const {
getBucketLoc
} = require('./controllers/assignController.js');
const resetAWSConfig = require('./controllers/resetAWSController.js');

const { rcloneCopyString } = require('./services/rcloneCopyString.js');
const app = express();
const http = require('http');
const server = http.createServer(app);
Expand All @@ -22,12 +21,6 @@ const io = new Server(server, {
}
});

//PUT ALL THE WEBSOCKET LOGIC HERE FOR NOW.
//CURRENT PROBLEM: I DON'T KNOW HOW TO EXPORT THIS STUFF.
io.on('connection', (socket) => {
console.log('a user connected');
});

app.use(express.json());
app.use(express.static(path.resolve(__dirname, '../client/public')));

Expand All @@ -43,6 +36,14 @@ app.post(
fsController.config,
rCloneCopyController,
(req, res) => {
res.locals.rcloneCopy.stdout.on('data', (data) => {
const relevantString = rcloneCopyString(data.toString());
io.emit('data transfer', relevantString);
});
//WILL WANT TO DO SOME ERROR HANDLING DURING THE TRANSFER PROCESS AT SOME POINT.
res.locals.rcloneCopy.stderr.on('data', (data) => {
console.error(data.toString());
});
res.sendStatus(200);
}
);
Expand All @@ -68,3 +69,5 @@ app.use((err, req, res, next) => {
});

server.listen(3000, () => console.log('Serving listening on port 3000...'));

module.exports = { io };
File renamed without changes.
25 changes: 25 additions & 0 deletions server/services/rcloneCopyString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Extract the relevant transfer data from the rclone copy output.
*/

const rcloneCopyString = (fullString) => {
//Get the string between the first and second occurence of the word "transferred".
//RIGHT NOW, JUST RETURNING AN EMPTY STRING IF THERE'S NO RELEVANT PERCENTAGE.
try {
let first = fullString.indexOf('Transferred:');
let second = fullString.indexOf('Transferred:', first + 1);
const slicedStr = fullString.slice(first, second);
console.log(slicedStr);
//Now get the overall percentage as the number.
first = slicedStr.indexOf(',');
second = slicedStr.indexOf('%');
//If finalized string isn't correct for some reason, return an empty string.
const finalString = slicedStr.slice(first + 1, second).trim();
if (isNaN(finalString)) return '';
return finalString;
} catch {
return '';
}
};

module.exports = { rcloneCopyString };
6 changes: 0 additions & 6 deletions server/test.js

This file was deleted.