-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3uploader.js
154 lines (145 loc) · 5.55 KB
/
web3uploader.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/JavaScript-Nodejs-File-Upload-Example-Ajax
var http = require('http');
let formidable = require('formidable');
let fs = require('fs');
const { createClient } = require('@liquidapps/dapp-client');
const fetch = require('isomorphic-fetch');
const endpoint = "https://kylin-dsp-1.liquidapps.io";
const getClient = () => createClient( { network:"kylin", httpEndpoint: endpoint, fetch });
var request = require('request');
http.createServer(function (req, res)
{
if(req.url == '/')
{
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('\
<html>\
<head>\
<script src="https://www.google.com/recaptcha/api.js"></script>\
</head>\
<body>\
<h3>Upload to LiquidStorage:</h3>\
<form action="/upload" method="post" enctype="multipart/form-data">\
<input type="file" name="fileupload">(20 MB file size limit)<br>\
<div class="g-recaptcha" data-sitekey="6Ley7YEhAAAAAKL3wJ-dL_K_s3Vgekgmi1bxbKjj"></div><br>\
<input type="submit">\
</form>\
<form action="/uploadstr" method="post" enctype="multipart/form-data">\
<input type="text" name="strupload">(100 KB file size limit)<br>\
<input type="submit">\
</form>\
</body>\
</html>'
);
}
if(req.url == '/upload')
{
// create an instance of the form object
let options = {
// set file size limit (20 MB)
maxFileSize: 20 * 1024 * 1024
};
let form = new formidable.IncomingForm(options);
form.on('error', (err) => {
console.log(err);
});
form.parse(req, function (error, fields, file)
{
// check captcha (code from: https://codeforgeek.com/google-recaptcha-node-js-tutorial/)
// g-recaptcha-response is the key that browser will generate upon form submit.
// if its blank or null means user has not selected the captcha, so return the error.
if(fields['g-recaptcha-response'] === undefined || fields['g-recaptcha-response'] === '' || fields['g-recaptcha-response'] === null)
{
res.write('{"responseCode" : 1, "responseDesc" : "Please select captcha"}');
res.end();
return;
}
// Put your secret key here.
var secretKey = "RECAPTCHA_SECRET_KEY";
// req.connection.remoteAddress will provide IP address of connected user.
var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + fields['g-recaptcha-response'] + "&remoteip=" + req.connection.remoteAddress;
// Hitting GET request to the URL, Google will respond with success or error scenario.
request(verificationUrl, function(error, response, body)
{
body = JSON.parse(body);
// Success will be true or false depending upon captcha validation.
if(body.success !== undefined && !body.success)
{
res.write('{"responseCode" : 1,"responseDesc" : "Failed captcha verification"}');
res.end();
return;
}
});
// upload file
let filepath = file.fileupload.filepath;
let newpath = '/tmp/';
newpath += file.fileupload.originalFilename;
// copy the uploaded file to a custom folder
fs.rename(filepath, newpath, async function () {
(async () => {
const service = await (await getClient()).service('storage', "zeosweb3apps");
const data = fs.readFileSync(newpath);
const key = "EOS_PRIVATE_KEY";
const permission = "active";
const options = {
// if true, DAG leaves will contain raw file data and not be wrapped in a protobuf
rawLeaves: true
};
const response = await service.upload_public_file(
data,
key,
permission,
null,
options
);
console.log(`response uri: ${response.uri}`);
res.write('File Upload Success!\n' + `response uri: \n${response.uri}`);
res.end();
})().catch((e) => { console.log(e); res.write(e); res.end(); });
});
});
}
if(req.url == '/uploadstr')
{
let options = {
// set file size limit (100 KB)
maxFileSize: 100 * 1024
};
let form = new formidable.IncomingForm(options);
form.on('error', (err) => {
console.log(err);
});
form.parse(req, function (error, fields, file)
{
if(fields['strupload'] === undefined || fields['strupload'] === '' || fields['strupload'] === null)
{
res.write('{"responseCode" : 1, "responseDesc" : "field strupload does not exist!"}');
res.end();
}
else
{
(async () => {
const service = await (await getClient()).service('storage', "zeosweb3apps");
const data = Buffer.from(fields['strupload'], "utf-8");
const key = "EOS_PRIVATE_KEY";
const permission = "active";
const options = {
// if true, DAG leaves will contain raw file data and not be wrapped in a protobuf
rawLeaves: true
};
const response = await service.upload_public_file(
data,
key,
permission,
null,
options
);
console.log(`response uri: ${response.uri}`);
res.write('String Upload Success!\n' + `response uri: \n${response.uri}`);
res.end();
})().catch((e) => { console.log(e); res.write(e); res.end(); });
}
});
}
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');