Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
288 lines (207 loc) · 8.21 KB
/
Copy pathindex.js
File metadata and controls
288 lines (207 loc) · 8.21 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var express = require('express');
var app = express();
const PORT = process.env.PORT || 5000
var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = 'expand witness two word permit argue clarify invest entry eager wrestle bomb';
var mongoose = require ("mongoose");
var mongouristring =
process.env.MONGODB_URI
mongoose.connect(mongouristring, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + mongouristring + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + mongouristring);
}
});
var tokenMetadataSchema = new mongoose.Schema({
},{strict:false});
var STORJ_EMAIL = process.env.STORJ_EMAIL;
var STORJ_PASSWORD = process.env.STORJ_PASSWORD;
var storjCredentials = {
email: STORJ_EMAIL,
password: STORJ_PASSWORD
};
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
var storj = require('storj-lib');
var api = 'https://api.storj.io';
var client = storj.BridgeClient(api, { basicAuth: storjCredentials });
var fs = require('fs');
const uuidv4 = require('uuid/v4');
app.get('/build/contracts/:name', function(req, res){
var fileName = req.params.name;
console.log( "Requested file: %s", fileName);
res.sendfile(fileName, { root: __dirname + "/build/contracts" } );
});
var server = app.listen(PORT, function () {
});
app.post('/files/upload', function(req, res) {
var obj = {};
console.log('body: ' + JSON.stringify(req.body));
var TokenMetadata = mongoose.model('TokenMetadata', tokenMetadataSchema);
var item = new TokenMetadata (req.body);
item.save(function (err, item) {
if (err) console.log (err)
else{
var uniqueId = item._id;
res.status(200).send(uniqueId);
}
});
/*var uniqueId = uuidv4();
fs.writeFile("/tmp/" + uniqueId , JSON.stringify(req.body), function(err) {
if(err) {
return console.log(err + "\n" + uniqueId);
}
console.log("The file was saved! %s", uniqueId );
res.status(200).send(uniqueId);
console.log('Retrieving buckets...')
// Step 1
client.getBuckets(function(err, buckets) {
if (err) {
return console.error("ERR1 " + err.message);
}
// Step 1a) Use the first bucket
var bucketId = buckets[0].id;
console.log('Uploading file to: ', bucketId);
// Step 1b) Path of file
var filepath = "/tmp/" + uniqueId;
console.log('Path of file: ', filepath);
// Step 1c) Name of file
var filename = uniqueId;
console.log('Name of file: ', filename);
// Step 2) Create a filekey with username, bucketId, and filename
var filekey = getFileKey(STORJ_EMAIL, bucketId, filename);
// Step 3) Create a temporary path to store the encrypted file
var tmppath = filepath + '.crypt';
// Step 4) Instantiate encrypter
var encrypter = new storj.EncryptStream(filekey);
// Step 5) Encrypt file
fs.createReadStream(filepath)
.pipe(encrypter)
.pipe(fs.createWriteStream(tmppath))
.on('finish', function() {
console.log('Finished encrypting');
// Step 6) Create token for uploading to bucket by bucketId
client.createToken(bucketId, 'PUSH', function(err, token) {
if (err) {
console.log('ERR2 ' + err.message);
}
console.log('Created token', token.token);
// Step 7) Store the file
console.log('Storing file in bucket...');
client.storeFileInBucket(bucketId, token.token, tmppath,
function(err, file) {
if (err) {
return console.log('ERR3 ' + err.message);
}
console.log('Stored file in bucket');
// Step 8) Clean up and delete tmp encrypted file
console.log('Cleaning up and deleting temporary encrypted file...');
fs.unlink(tmppath, function(err) {
if (err) {
return console.log('ERR4 ' + err);
}
console.log('Temporary encrypted file deleted');
});
console.log(`File ${filename} successfully uploaded to ${bucketId}`);
res.status(200).send(file);
});
});
});
});
});*/
});
/**
* Lists all files in buckets
*/
app.get('/files/get/:metadataID', function(req, res) {
var tokenMetadataAddress = req.params.metadataID;
var TokenMetadata = mongoose.model('TokenMetadata', tokenMetadataSchema);
TokenMetadata.findById(tokenMetadataAddress, function (err, product) {
if (err) {
console.log('error', err.message);
}
console.log(product);
res.status(200).send(JSON.stringify(product));
});
/*
separator();
// Create object to hold all the buckets and files
var bucketFiles = {};
// Get buckets
console.log('Getting buckets...')
client.getBuckets(function(err, buckets) {
if (err) {
return console.log('error', err.message);
}
// Get all the buckets, and then return the files in the bucket
// Assign files to bucketFiles
async.each(buckets, function(bucket, callback) {
console.log('bucket', bucket.id);
client.listFilesInBucket(bucket.id, function(err, files) {
if (err) {
return callback(err);
}
// bucketFiles.myPictureBucket = [];
bucketFiles[bucket.name] = files;
callback(null);
})
}, function(err) {
if (err) {
return console.log('error');
}
console.log('bucketFiles retrieved: ', bucketFiles);
res.status(200).send(bucketFiles);
});
});*/
});
//TODO: Add some kind of encryption here Everybody can call this.
app.get('/recycler/recycle/:id&:toAddress', function(req,res){
var productId = req.params.id;
var toAddress = req.params.toAddress;
console.log(productId + ' ' + toAddress );
var TruffleContract = require("truffle-contract");
var EmineContract = null;
var Web3 = require('web3');
provider = new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/XbAOU5EG42nnsgPntAqU", 2);
var web3 = new Web3(provider);
web3.eth.getAccounts(function (error, accounts) {
if (error) {
console.log(error);
}
console.log(accounts);
var EmineArtifact = require('./build/contracts/EMine.json');
EmineContract = TruffleContract(EmineArtifact);
EmineContract.setProvider(provider);
var txHash = null;
EmineContract.deployed().then(function (instance) {
emineInstance = instance;
return emineInstance.recycleProduct(productId, toAddress, {from: accounts[0], gas: 2000000});
}).then(function (result) {
console.log("Returned Product!");
res.status(200).send("Returned Product with ID: " + productId);
}).catch(function (err) {
console.log(err.message);
});
});
});
app.get("/recycler-app.apk", function(req, res){
console.log( "Requested file: download apk");
var file = __dirname + '/app-release.apk';
res.sendFile(file); // Set disposition and send it.
});
app.get('/:name', function(req, res){
var fileName = req.params.name;
console.log( "Requested file: %s", fileName);
res.sendfile(fileName, { root: __dirname + "/src" } );
});
app.get('/:folder/:name', function(req, res){
var fileName = req.params.name;
var folderName = req.params.folder;
console.log( "Requested file: %s", fileName);
res.sendfile(fileName, { root: __dirname + "/src/" + folderName } );
});
app.get('/', function(req, res){
res.sendfile("index.html", { root: __dirname + "/src" } );
});