Skip to content

Commit 3e844e1

Browse files
committed
feat: Add scripts for file upload
1 parent fd28ab1 commit 3e844e1

File tree

11 files changed

+251
-11
lines changed

11 files changed

+251
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ chrome-user-data
2424
*.swp
2525
*.swo
2626

27+
.env
28+
2729
packages/react-devtools-core/dist
2830
packages/react-devtools-extensions/chrome/build
2931
packages/react-devtools-extensions/chrome/*.crx

package-lock.json

Lines changed: 69 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
9+
"aws-sdk": "^2.714.0",
910
"axios": "^0.19.2",
11+
"dotenv": "^8.2.0",
1012
"prop-types": "^15.7.2",
1113
"react": "^16.13.1",
1214
"react-dom": "^16.13.1",

src/Routes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import Register from './components/Register'
55
import Dashboard from './components/Dashboard'
66
import Form from './components/Form'
77
import Submission from './components/Submission'
8-
import { login, register, dashboard, form, submission } from './urls'
8+
import Upload from './components/Upload'
9+
import { login, register, dashboard, form, submission, file } from './urls'
910
import {PrivateRoute} from './PrivateRoute'
1011
import { AuthRoute } from './AuthRoute'
1112

@@ -19,6 +20,7 @@ export default class Routes extends Component {
1920
<PrivateRoute exact path={submission()} component={Submission} />
2021
<AuthRoute path={login()} component={Login} />
2122
<AuthRoute path={register()} component={Register} />
23+
<Route path={file()} component={Upload} />
2224
<Route render={() => <Redirect to='/' />} />
2325
</Switch>
2426
</>

src/actions/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export const GET_USER_INFO = 'GET_USER_INFO';
99
export const POST_USER_INFO = 'POST_USER_INFO';
1010
export const UPDATE_USER_INFO = 'UPDATE_USER_INFO';
1111
export const USER_INFO_ERRORS = 'USER_INFO_ERRORS';
12+
export const UPLOAD_FILE = 'UPLOAD_FILE';
13+
export const FILE_ERRORS = 'FILE_ERRORS';

src/actions/upload.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import axios from 'axios'
2+
import {
3+
upload
4+
} from '../urls'
5+
import { sign_s3 } from '../aws'
6+
import {
7+
UPLOAD_FILE,
8+
FILE_ERRORS
9+
} from './types'
10+
import { FILE } from 'dns';
11+
12+
export const postUpload = (data, callback) => async dispatch => {
13+
try {
14+
const config = {
15+
headers: {
16+
'content-type': 'multipart/form-data'
17+
}
18+
}
19+
const res = await axios.post(upload(), data, config);
20+
dispatch({
21+
type: UPLOAD_FILE,
22+
payload: res.data
23+
});
24+
callback()
25+
}
26+
catch (err) {
27+
dispatch({
28+
type: FILE_ERRORS,
29+
payload: err.response.data
30+
});
31+
callback()
32+
}
33+
34+
}

src/aws.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var aws = require('aws-sdk');
2+
require('dotenv').config();
3+
4+
aws.config.update({
5+
region: 'ap-south-1',
6+
accessKeyId: process.env.AWSAccessKeyId,
7+
secretAccessKey: process.env.AWSSecretKey
8+
})
9+
10+
const S3_BUCKET = process.env.Bucket
11+
12+
exports.sign_s3 = (req, res) => {
13+
const s3 = new aws.S3();
14+
const fileName = req.body.fileName;
15+
const fileType = req.body.fileType;
16+
17+
const s3Params = {
18+
Bucket: S3_BUCKET,
19+
Key: fileName,
20+
Expires: 500,
21+
ContentType: fileType,
22+
ACL: 'public-read'
23+
};
24+
25+
s3.getSignedUrl('putObject', s3Params, (err, data) => {
26+
if(err){
27+
console.log(err);
28+
res.json({success: false, error: err})
29+
}
30+
31+
const returnData = {
32+
signedRequest: data,
33+
url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
34+
};
35+
36+
res.json({success:true, data:{returnData}});
37+
});
38+
}

src/components/Upload.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React, { Component } from 'react'
2+
import { postUpload } from '../actions/upload'
3+
import PropTypes from 'prop-types'
4+
import { connect } from 'react-redux'
5+
6+
class Upload extends Component {
7+
constructor(props) {
8+
super(props)
9+
this.state = {
10+
success: false,
11+
url: ''
12+
}
13+
}
14+
15+
handleChange = (e) => {
16+
this.setState({
17+
success: false,
18+
url: ''
19+
})
20+
}
21+
22+
handleUpload = (e) => {
23+
let file = this.uploadInput.files[0]
24+
let fileParts = this.uploadInput.files[0].name.split('.')
25+
let fileName = fileParts[0]
26+
let fileType = fileParts[1]
27+
this.props.postUpload({fileName: fileName, fileType: fileType}, this.callback)
28+
}
29+
30+
callback = () => {}
31+
32+
render() {
33+
const Success_message = () => (
34+
<div style={{padding:50}}>
35+
<h3 style={{color: 'green'}}>SUCCESSFUL UPLOAD</h3>
36+
<a href={this.state.url}>Access the file here</a>
37+
<br/>
38+
</div>
39+
)
40+
return (
41+
<div className="App">
42+
<center>
43+
<h1>UPLOAD A FILE</h1>
44+
{this.state.success ? <Success_message/> : null}
45+
<input onChange={this.handleChange} ref={(ref) => { this.uploadInput = ref; }} type="file"/>
46+
<br/>
47+
<button onClick={this.handleUpload}>UPLOAD</button>
48+
</center>
49+
</div>
50+
);
51+
}
52+
53+
}
54+
55+
56+
Upload.propTypes = {
57+
postUpload: PropTypes.func.isRequired,
58+
};
59+
60+
const mapStateToProps = state => ({
61+
upload: state.upload.upload,
62+
})
63+
64+
export default connect(
65+
mapStateToProps,
66+
{ postUpload }
67+
)(Upload)

src/reducers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { combineReducers } from 'redux'
22
import loginReducer from './login'
33
import infoReducer from './info'
44
import userReducer from './user'
5+
import uploadReducer from './upload'
56

67
const rootReducers = combineReducers ({
78
login: loginReducer,
89
info: infoReducer,
9-
user: userReducer
10+
user: userReducer,
11+
upload: uploadReducer
1012
})
1113

1214
export default rootReducers

src/reducers/upload.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
UPLOAD_FILE
3+
} from '../actions/types'
4+
5+
const initialState = {
6+
uploadfile: ''
7+
}
8+
9+
const uploadReducer = (state = initialState, action) => {
10+
switch(action.type) {
11+
case UPLOAD_FILE:
12+
return {
13+
...state,
14+
uploadfile: action.payload,
15+
}
16+
default:
17+
return state
18+
}
19+
}
20+
21+
export default uploadReducer

0 commit comments

Comments
 (0)