Skip to content

Commit

Permalink
pulled down dev changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewaltman1 committed Apr 3, 2023
2 parents 604ce03 + da6f57e commit 5f7644f
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 93 deletions.
34 changes: 32 additions & 2 deletions client/components/Origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { useEffect } from 'react';
import {
updateOriginSecretKey,
updateOriginAccessId,
updateAccountId,
updateAccountId,,
updateOriginBuckets
} from '../slice';
import { useDispatch, useSelector } from 'react-redux';
import { getUserBuckets } from '../services/getBuckets';
Expand All @@ -26,10 +27,39 @@ const Origin = (props) => {
);
}

let bucketSelect;

const requireAccountId = props.name === 'CloudFlare' ? true : false;

if (!requireAccountId) {
bucketSelect = origin.accessId && origin.secretKey && (
<BucketSelect remote={'origin'}></BucketSelect>
);
} else {
bucketSelect = origin.accessId && origin.secretKey && origin.accountId && (
<BucketSelect remote={'origin'}></BucketSelect>
);
}

//REFACTOR TO RTK QUERY.
//THIS GETS THE BUCKETS.
useEffect(() => {
if (origin.accessId && origin.secretKey) {
if (origin.name === 'Cloudflare' && !origin.accountId) return;
dispatch(getUserBuckets('origin'));
(async () => {
const res = await fetch('/listBuckets', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
accessId: origin.accessId,
secretKey: origin.secretKey
})
});
const data = await res.json();
dispatch(updateOriginBuckets(data));
})();
}
}, [origin.accessId, origin.secretKey, origin.name, origin.accountId]);

Expand Down
49 changes: 22 additions & 27 deletions client/services/getBuckets.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
const { createAsyncThunk } = require('@reduxjs/toolkit');
const AWS = require('aws-sdk');

//CAN REFACTOR DOWN TO RTK-QUERY.

export const getUserBuckets = createAsyncThunk(
'GUI/userBuckets',
async (originOrDestination, { useSelector }) => {
//Get the accessId and secretKey from store.
let accessId, secretKey;
if (originOrDestination === 'origin')
({ accessId, secretKey } = useSelector((state) => state.GUI.origin));
else if (originOrDestination === 'destination')
({ accessId, secretKey } = useSelector((state) => state.GUI.destination));

//Set the config file for retrieving buckets.
AWS.config.update({
accessKeyId: accessId,
secretAccessKey: secretKey
});
const s3 = new AWS.S3();
async ({ originOrDestination, accessId, secretKey }) => {
try {
//Set the config file for retrieving buckets.
AWS.config.update({
accessKeyId: accessId,
secretAccessKey: secretKey
});
const s3 = new AWS.S3();
s3.cors;

//Return the list of buckets, names only.
//SEE WHAT HAPPENS IF THE BUCKET LIST IS EMPTY. DOES IT THROW AN ERROR OR NOT.
//YOU CAN ERROR CHECK HERE TO SEE IF CREDENTIALS ARE INVALID!!!
//YOU'LL PROBABLY WANT TO DO A LOT OF ERROR HANDLING HERE!!
const data = await s3
.listBuckets(function (err, data) {
if (err) {
console.log('Error', err);
}
})
.promise();
const buckets = data.Buckets.map((bucket) => bucket.Name);
return { buckets, originOrDestination };
//Return the list of buckets, names only.
//SEE WHAT HAPPENS IF THE BUCKET LIST IS EMPTY. DOES IT THROW AN ERROR OR NOT.
//YOU CAN ERROR CHECK HERE TO SEE IF CREDENTIALS ARE INVALID!!!
//YOU'LL PROBABLY WANT TO DO A LOT OF ERROR HANDLING HERE!!
const data = await s3.listBuckets().promise();
const buckets = data.Buckets.map((bucket) => bucket.Name);
console.log('DATA', data);
return { buckets, originOrDestination };
} catch (e) {
console.log(e);
}
}
);
7 changes: 5 additions & 2 deletions client/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ const slice = createSlice({
state.origin = origin;
state.destination = destination;
},
updateSelectedBucket: (state, action) => {
updateOriginBuckets: (state, action) => {
state.origin.buckets = action.payload;
} updateSelectedBucket: (state, action) => {
state[action.payload.remote].selectedBucket = action.payload.bucket;
},
},
},
extraReducers: (builder) => {
builder.addCase(getUserBuckets.fulfilled),
(state, action) => {
Expand All @@ -77,5 +79,6 @@ export const {
updateDestinationSecretKey,
updateDestinationAccessId,
updateAccountId,
updateOriginBuckets,
updateSelectedBucket,
} = slice.actions;
28 changes: 27 additions & 1 deletion server/controllers/rcloneController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const rclone = require('rclone.js');
const { resolve } = require('path');
const AWS = require('aws-sdk');

const rcloneCopy = rclone(
'copy',
Expand All @@ -13,7 +14,32 @@ const rcloneCopy = rclone(
}
);

module.exports = rcloneCopy;
const rcloneListBuckets = async (req, res, next) => {
try {
const { accessId, secretKey } = req.body;

//Set the config file for retrieving buckets.
AWS.config.update({
accessKeyId: accessId,
secretAccessKey: secretKey
});
const s3 = new AWS.S3();

//Return the list of buckets, names only.
//SEE WHAT HAPPENS IF THE BUCKET LIST IS EMPTY. DOES IT THROW AN ERROR OR NOT.
//YOU CAN ERROR CHECK HERE TO SEE IF CREDENTIALS ARE INVALID!!!
//YOU'LL PROBABLY WANT TO DO A LOT OF ERROR HANDLING HERE!!
const data = await s3.listBuckets().promise();
const buckets = data.Buckets.map((bucket) => bucket.Name);
res.locals.buckets = buckets;
return next();
} catch (e) {
console.log(e);
return next(e);
}
};

module.exports = { rcloneCopy, rcloneListBuckets };

// rcloneCopy.stdout.on('data', (data) => {
// console.log(data.toString());
Expand Down
124 changes: 83 additions & 41 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,90 @@
// const { S3Client, GetBucketLocationCommand, ListBucketsCommand, listRegions } = require("@aws-sdk/client-s3")
const AWS = require('aws-sdk');
const { debug } = require('console');
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
// // const { S3Client, GetBucketLocationCommand, ListBucketsCommand, listRegions } = require("@aws-sdk/client-s3")
// const AWS = require('aws-sdk');
// const { debug } = require('console');
// const fs = require('fs');
// const { v4: uuidv4 } = require('uuid');

/* Note: To supply the Multi-region Access Point (MRAP) to Bucket, you need to install the "@aws-sdk/signature-v4-crt" package to your project dependencies.
related links to getBucketLocation :
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3.html#getbucketlocation
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/getbucketlocationrequest.html
// /* Note: To supply the Multi-region Access Point (MRAP) to Bucket, you need to install the "@aws-sdk/signature-v4-crt" package to your project dependencies.
// related links to getBucketLocation :
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3.html#getbucketlocation
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/getbucketlocationrequest.html

An alternative to trying to access the bucket locations is giving the use the availaale regions in their account:
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-account/classes/listregionscommand.html
*/
// An alternative to trying to access the bucket locations is giving the use the availaale regions in their account:
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-account/classes/listregionscommand.html
// */

// AWS.config.update({
// accessKeyId: 'AKIAUBWLANRIRWTZGEX6',
// secretAccessKey: '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'
// });
// const s3 = new AWS.S3();

const getBucketLoc = async () => {
try {
console.log('in bucket location');
const data = await s3
.getBucketLocation({ Bucket: 'test-bucket-please-work-osp' })
.promise();
let location = data.LocationConstraint;
//NEED TO RETURN US-EAST-1 IF STRING IS ''.
if (location === '') location = 'us-east-1';
} catch (err) {
console.log('Error:', err);
}
};

//Need error handling here.
const getBucketLists = async () => {
const data = await s3
.listBuckets(function (err, data) {
if (err) {
console.log('Error', err);
}
})
.promise();
//data.Buckets is an array of objects.
//for each object, you want the .Name property.
// data.Buckets.forEach((bucket) => console.log(bucket.Name));
};

module.exports = { getBucketLoc, getBucketLists };
// const getBucketLoc = async () => {
// try {
// console.log('in bucket location');
// const data = await s3
// .getBucketLocation({ Bucket: 'test-bucket-please-work-osp' })
// .promise();
// let location = data.LocationConstraint;
// //NEED TO RETURN US-EAST-1 IF STRING IS ''.
// if (location === '') location = 'us-east-1';
// } catch (err) {
// console.log('Error:', err);
// }
// };

// //Need error handling here.
// const getBucketLists = async () => {
// const data = await s3.listBuckets().promise();
// //data.Buckets is an array of objects.
// //for each object, you want the .Name property.
// return data.Buckets.map((bucket) => console.log(bucket.Name));
// };

// getBucketLists();

// module.exports = { getBucketLoc, getBucketLists };

const path = require('path');
const express = require('express');
const fsController = require('./controllers/fsController.js');
const {
rcloneCopy,
rcloneListBuckets
} = require('./controllers/rcloneController');

const app = express();

//PUT ALL THE WEBSOCKET HERE FOR NOW.

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

//Don't think we need this.
// app.get('/', (req, res) => {
// res.sendFile(path.resolve(__dirname, '../client/index.html'));
// });

app.post('/listBuckets', rcloneListBuckets, (req, res) => {
res.status(200).json(res.locals.buckets);
});

app.post('/transfer', fsController.config, (req, res) => {
res.sendStatus(200);
});

//404 NEEDED

//GLOBAL ERROR HANDLER NEEDED.
app.use((err, req, res, next) => {
const defaultErr = {
log: 'Express error handler caught unknown middleware error',
status: 500,
message: { err: 'An error occurred' }
};
const errorObj = Object.assign({}, defaultErr, err);
console.log(errorObj.log);
return res.status(errorObj.status).json(errorObj.message);
});

app.listen(3000, () => console.log('Serving listening on port 3000...'));
40 changes: 20 additions & 20 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ module.exports = {
mode: 'development',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index_bundle.js',
filename: 'index_bundle.js'
},
target: 'web',
devServer: {
port: '8080',
// proxy: {
// '/': 'http:https://localhost:3000',
// },
proxy: {
'/': 'http:https://localhost:3000'
},
static: {
directory: path.join(__dirname, './client/public'),
directory: path.join(__dirname, './client/public')
},
open: true,
hot: true,
liveReload: true,
liveReload: true
},
resolve: {
extensions: ['.js', '.jsx', '.json'],
extensions: ['.js', '.jsx', '.json']
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: 'babel-loader',
use: 'babel-loader'
},
{
test: /\.(gif|png|jpe?g)$/,
Expand All @@ -38,11 +38,11 @@ module.exports = {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'assets/images/',
},
},
],
},
outputPath: 'assets/images/'
}
}
]
},
{
test: /\.scss?/,
exclude: /node_modules/,
Expand All @@ -52,14 +52,14 @@ module.exports = {
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
],
'sass-loader'
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './client/public/index.html'),
}),
],
template: path.resolve(__dirname, './client/public/index.html')
})
]
};

0 comments on commit 5f7644f

Please sign in to comment.