fetching data from FLO Blockchain complete

This commit is contained in:
Abhishek Sinha 2018-08-22 17:39:00 +05:30
parent 33ea6c4bb9
commit abb7175818
6 changed files with 230 additions and 13 deletions

14
package-lock.json generated
View File

@ -1897,8 +1897,7 @@
},
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"optional": true
"bundled": true
},
"concat-map": {
"version": "0.0.1",
@ -1906,8 +1905,7 @@
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"optional": true
"bundled": true
},
"core-util-is": {
"version": "1.0.2",
@ -2010,8 +2008,7 @@
},
"inherits": {
"version": "2.0.3",
"bundled": true,
"optional": true
"bundled": true
},
"ini": {
"version": "1.3.5",
@ -2021,7 +2018,6 @@
"is-fullwidth-code-point": {
"version": "1.0.0",
"bundled": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@ -2133,8 +2129,7 @@
},
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"optional": true
"bundled": true
},
"object-assign": {
"version": "4.1.1",
@ -2249,7 +2244,6 @@
"string-width": {
"version": "1.0.2",
"bundled": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",

28
public/js/funcs.js Normal file
View File

@ -0,0 +1,28 @@
const client = require('../../server')
const _ = require('lodash')
let getFloData = (tx) => {
return new Promise((resolve, reject)=>{
let transaction = _.trim(tx)
try {
return client.getRawTransaction(transaction, 1).then(res=>{
if (res==undefined) {
return reject(error)
}
return res;
}).then(response=>{
return resolve(response.floData)
})
} catch (error) {
return reject(error)
}
}).catch ((error)=>{
console.error(error);
})
}
module.exports = {
getFloData
}

View File

@ -5,6 +5,7 @@ const { matchedData } = require('express-validator/filter')
const _ = require('lodash');
const client = require('./server.js');
const funcs = require('./public/js/funcs')
var path = require('path')
var fs = require('fs');
@ -85,7 +86,7 @@ router.post('/send-to-blockchain', [
return
}
let toaddress = "oXCsMUyX3mLJEdnn8SXoH6gyPW9Jd6kjYu";
let toaddress = "oSjBiuTE1aFNBjaSGq6UNhU9ddpD2YXdg8";
let amount = 1;
try {
@ -105,7 +106,7 @@ router.post('/send-to-blockchain', [
})
router.get('/fetch-tx-comment', (req, res)=>{
router.get('/fetch-from-blockchain', (req, res)=>{
res.render('from_flo', {
data: {},
errors: {},
@ -113,4 +114,79 @@ router.get('/fetch-tx-comment', (req, res)=>{
})
})
router.post('/fetch-from-blockchain', [
check('job')
.isLength({min:1})
.withMessage('Invalid job!')
.trim(),
check('floaddr')
.isLength({min:1})
.withMessage('Invalid FLO addrress!')
.trim()
], (req, res)=>{
const errors = validationResult(req)
console.log(errors.mapped());
if(!errors.isEmpty()) {
return res.render('fetch-from-blockchain', {
data: req.body,
errors: errors.mapped(),
title: 'Please correct your errors'
})
}
let params = _.pick(req.body, ['job', 'floaddr'])
if (typeof params.job == undefined || params.job!='flo-comment') {
console.log("Unknown request");
return;
}
let floaddr = params.floaddr
if (floaddr.length<1) {
res.json({error:true, "txnid":null, msg:'FLO address is empty', data:null})
return
}
try {
let tx_arr = []
client.listTransactions().then(lt=>{
for (let t = 0; t < lt.length; t++) {
const elem = lt[t];
if (elem.address==floaddr && elem.category=='send') {
tx_arr.push(elem.txid)
}
}
return tx_arr;
}).then(tx_arr=>{
console.log(tx_arr);
let tor_arr = []
for (const tx in tx_arr) {
let promise = funcs.getFloData(_.trim(tx_arr[tx]))
tor_arr.push(promise)
}
let msg_arr = []
Promise.all(tor_arr).then(msgr=>{
msgr.forEach(op=>{
msg_arr.push(op)
})
return msg_arr
}).then(flo_data=>{
console.log(flo_data);
res.json({error:false, msg:'floData fetching complete', data:flo_data})
})
}).catch(e=>{
console.error(e)
})
} catch (error) {
console.error(error)
}
})
module.exports = router

118
views/from_flo.ejs Normal file
View File

@ -0,0 +1,118 @@
<%include partials/header.ejs %>
<div class="container">
<label for="flo-addr">Enter the flo address (oSjBiuTE1aFNBjaSGq6UNhU9ddpD2YXdg8)</label>
<input type="text" id="flo-addr" class="form-control" />
<p><button id="flo-addr-btn" class="btn btn-primary">Get Torrents List</button></p>
<ul id="flo-res"></ul>
<label for="mag_text">Provide a magnetic URL below:</label>
<textarea class="form-control" id="mag_text" rows="3"></textarea>
<p><button id="dwn-seed" class="btn btn-primary">View</button></p>
<p id="dwld"></p>
<div class="pd-10">
<div id="mag-res"></div>
</div>
</div>
<%include partials/footer.ejs %>
<script>
$(document).on('click', '#flo-addr-btn', function() {
var floaddr = $("#flo-addr").val();
if (floaddr.length<1) {
alert("Please specify a FLO address.");
return;
}
var job = 'flo-comment';
$.ajax({
url: '/fetch-from-blockchain',
type: 'post',
data: {job:job, floaddr:floaddr},
success: function(response) {
console.log(response);
var t = '';
if (response.msg.length>0) {
t += `<li><h5 class="text-info">${response.msg}</h5></li>`;
t += ``;
}
if (response.error==false && typeof response.data != undefined) {
for (let fd = 0; fd < response.data.length; fd++) {
const fdata = response.data[fd];
t += `<li><p>${fdata}</p></li>`;
}
}
$("#flo-res").html(t);
},
error: function() {
}
});
});
$(document).on('click', '#dwn-seed', function() {
var client = new WebTorrent()
var torrentId = document.getElementById('mag_text').value;
if (torrentId.trim() == '') {
alert("Please specify a magnetic url.");
return;
}
client.add(torrentId, function (torrent) {
// Torrents can contain many files. Let's use the .mp4 file
var file = torrent.files.find(function (file) {
console.log(file);
if (file.name.endsWith('.mp4')) {
return file.name.endsWith('.mp4')
} else if (file.name.endsWith('.webm')) {
return file.name.endsWith('.webm')
} else if (file.name.endsWith('.avi')) {
return file.name.endsWith('.avi')
} else if (file.name.endsWith('.pdf')) {
return file.name.endsWith('.pdf')
} else if (file.name.endsWith('.mp3')) {
return file.name.endsWith('.mp3')
} else if (file.name.endsWith('.jpg')) {
return file.name.endsWith('.jpg')
} else if (file.name.endsWith('.png')) {
return file.name.endsWith('.png')
} else if (file.name.endsWith('.gif')) {
return file.name.endsWith('.gif')
} else if (file.name.endsWith('.jpeg')) {
return file.name.endsWith('.jpeg')
}
})
console.log('Client is downloading:', torrent.infoHash)
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('#mag-res')
file.getBlobURL(function (err, url) {
if (err) throw err
var a = document.createElement('a')
a.download = file.name
a.href = url
a.textContent = 'Download ' + file.name
document.getElementById("dwld").appendChild(a)
})
})
});
</script>

View File

@ -6,6 +6,7 @@
<li><a href="/download">Download Torrent</a></li>
<li><a href="/download-magnetic-uri">Download Torrent from Magnetic URL</a></li>
<li><a href="/send-to-blockchain">Send Magnetic URL to FLO Blockchain</a></li>
<li><a href="/fetch-from-blockchain">Download torrent from FLO Blockchain</a></li>
</ul>
</div>

View File

@ -9,7 +9,7 @@
</div>
<button type="submit" id="magtor-btn" class="btn btn-primary">Submit</button>
<div class="card" id="res-div"></div>
<p id="res-div"></p>
</div>