-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpload.js
More file actions
136 lines (122 loc) · 3.85 KB
/
Upload.js
File metadata and controls
136 lines (122 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React from 'react';
import Spinner from "react-bootstrap/Spinner";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";
import axios from 'axios';
import server from '../server';
import {NotificationManager} from "react-notifications";
import NavigationBar from './NavigationBar';
import * as CCJ from "copy-clipboard-js";
import * as publicIp from 'public-ip';
class Upload extends React.Component{
state={
link:'',
status:'ready',//ready,uploading,done,copied
file:undefined,
ip:undefined
};
constructor(props){
super(props);
this.onInputChange=this.onInputChange.bind(this);
this.upload=this.upload.bind(this);
this.copyLink=this.copyLink.bind(this);
}
componentWillMount() {
publicIp.v4().then((ip)=>{
console.log(ip);
this.setState({ip:ip})
})
}
copyLink(){
CCJ.copyText(this.state.link);
this.setState({status:'copied'});
NotificationManager.success("Copied to clipboard",'',3000);
}
upload(){
let file;
try{
file=this.state.file[0];
}
catch (e) {
return;
}
let url=server.server+"/upload";
let form=new FormData();
form.append("file",file);
form.append("ip",this.state.ip);
axios.post(url,form)
.then((res)=>this.uploadDone(res.data))
.catch((err)=>NotificationManager.error(err,'',2000));
}
uploadDone(res){
NotificationManager.success("Success",'',3000);
this.setState({status:'done'});
this.setState({link:res.fileDownloadUri})
}
onInputChange(e){
if(e.target.files.length!==0){
this.setState({file:e.target.files});
}
};
render() {
let status=this.state.status;
console.log(status);
console.log(this.state.file);
if(status==='ready'){
return(
<div>
<NavigationBar/>
<div className="center">
<input multiple={false} type="file" onChange={this.onInputChange}/>
<Button onClick={this.upload} variant={ this.state.file!==undefined ? "outline-success" : "dead"}>Upload</Button>
</div>
</div>
);
}
else if(status==='uploading'){
return(
<div>
<NavigationBar/>
<div className="center">
<Spinner
as="span"
animation="grow"
size=""
role="status"
aria-hidden="true"
/>
</div>
</div>
);
}
else if(status==='done'){
return(
<div>
<NavigationBar/>
<div className="center">
<Form>
<Form.Group >
<Form.Control value={this.state.link} readOnly onClick={this.copyLink}/>
</Form.Group>
</Form>
</div>
</div>
);
}
else if(status==='copied'){
return(
<div>
<NavigationBar/>
<div className="center">
<Form>
<Form.Group >
<Form.Control value={this.state.link} readOnly isValid onClick={this.copyLink}/>
</Form.Group>
</Form>
</div>
</div>
);
}
}
}
export default Upload;