mirror of
https://github.com/amark/gun.git
synced 2026-03-04 08:15:25 +00:00
Merge branch 'master' into manhattan
This commit is contained in:
@@ -70,13 +70,98 @@
|
||||
Public Key: <input id="pub">
|
||||
</div></div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/gun/examples/jquery.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
|
||||
<script src="../jquery.js"></script>
|
||||
<script src="../../../gun/gun.js"></script>
|
||||
<script src="../../../gun/sea.js"></script>
|
||||
|
||||
<script>
|
||||
//var gun = Gun();
|
||||
var gun = Gun('http://localhost:8080/gun');
|
||||
|
||||
// extend SEA functions to base64 encode encrypted data
|
||||
// workaround for https://github.com/amark/gun/issues/783
|
||||
|
||||
(() => {
|
||||
const _encrypt = SEA.encrypt;
|
||||
SEA.encrypt = function(...args) {
|
||||
return _encrypt.apply(this, args).then(enc => btoa(JSON.stringify(enc)));
|
||||
}
|
||||
|
||||
const _decrypt = SEA.decrypt;
|
||||
SEA.decrypt = function(data, ...args) {
|
||||
try { data = JSON.parse(atob(data)); }
|
||||
finally { return _decrypt.apply(this, [data, ...args]); }
|
||||
}
|
||||
})();
|
||||
|
||||
// override User functions to fix several issues
|
||||
// see https://github.com/amark/gun/issues/808
|
||||
|
||||
SEA.Gun.User.prototype.grant = function grant(to, cb) {
|
||||
const gun = this; const user = gun.back(-1).user();
|
||||
const pair = user._.sea; let path = '';
|
||||
|
||||
gun.back(at => { if (at.has) { path += at.get; } });
|
||||
|
||||
(async () => {
|
||||
let enc, sec;
|
||||
|
||||
if (sec = await user.get('trust').get(pair.pub).get(path).then()) {
|
||||
sec = await SEA.decrypt(sec, pair);
|
||||
|
||||
} else {
|
||||
sec = SEA.random(24).toString();
|
||||
enc = await SEA.encrypt(sec, pair);
|
||||
|
||||
user.get('trust').get(pair.pub).get(path).put(enc);
|
||||
}
|
||||
|
||||
let pub = to.get('pub') .then();
|
||||
let epub = to.get('epub').then();
|
||||
|
||||
pub = await pub; epub = await epub;
|
||||
|
||||
const dh = await SEA.secret (epub, pair);
|
||||
enc = await SEA.encrypt(sec, dh);
|
||||
|
||||
// if pub is not already in trust, first put an empty node
|
||||
// workaround for https://github.com/amark/gun/issues/844
|
||||
|
||||
if (!await user.get('trust').get(pub).then()) {
|
||||
await user.get('trust').get(pub).get(path).put({}).then();
|
||||
}
|
||||
|
||||
user.get('trust').get(pub).get(path).put(enc, cb);
|
||||
})();
|
||||
|
||||
return gun;
|
||||
}
|
||||
|
||||
SEA.Gun.User.prototype.secret = function(data, cb) {
|
||||
const gun = this; const user = gun.back(-1).user();
|
||||
const pair = user._.sea; let path = '';
|
||||
|
||||
gun.back(at => { if (at.has) { path += at.get; } });
|
||||
|
||||
(async () => {
|
||||
let enc, sec;
|
||||
|
||||
if (sec = await user.get('trust').get(pair.pub).get(path).then()) {
|
||||
sec = await SEA.decrypt(sec, pair);
|
||||
|
||||
} else {
|
||||
sec = SEA.random(24).toString();
|
||||
enc = await SEA.encrypt(sec, pair);
|
||||
|
||||
user.get('trust').get(pair.pub).get(path).put(enc);
|
||||
}
|
||||
|
||||
enc = await SEA.encrypt(data, sec);
|
||||
gun.put(enc, cb);
|
||||
})();
|
||||
|
||||
return gun;
|
||||
}
|
||||
|
||||
var gun = Gun('http://localhost:8765/gun');
|
||||
var user = gun.user();
|
||||
var LI = {};
|
||||
|
||||
@@ -94,7 +179,7 @@ $('#sign').on('submit', function(e){
|
||||
gun.on('auth', function(){
|
||||
$('#sign').hide();
|
||||
$('#profile').show();
|
||||
var pub = user.pair().pub;
|
||||
var pub = user._.sea.pub;
|
||||
$('#pub').val(pub);
|
||||
return;
|
||||
$("#search").val(pub).trigger('blur');
|
||||
@@ -126,13 +211,24 @@ $('#search').on('blur', function(e){
|
||||
ev.off();
|
||||
return;
|
||||
}
|
||||
Gun.node.is(data, async function(v, k){
|
||||
if(k === LI.busy){ return }
|
||||
var key = await find.get('trust').get(user.pair().pub).get(k+'profile').then();
|
||||
var mix = await Gun.SEA.secret(await find.get('epub').then(), user.pair());
|
||||
key = await Gun.SEA.decrypt(key, mix);
|
||||
var val = await Gun.SEA.decrypt(v, key);
|
||||
$('#'+k).val(val || v);
|
||||
|
||||
Gun.node.is(data, async (enc, id) => {
|
||||
if (id === LI.busy) { return; }
|
||||
|
||||
const pair = user._.sea;
|
||||
let key, val;
|
||||
|
||||
if (key =
|
||||
await find.get('trust').get(pair.pub).get(id + 'profile').then()) {
|
||||
const mix = await Gun.SEA.secret(await find.get('epub').then(), pair);
|
||||
|
||||
key = await Gun.SEA.decrypt(key, mix);
|
||||
val = await Gun.SEA.decrypt(enc, key);
|
||||
|
||||
// decode encrypted data to show 'SEA{...}'
|
||||
} else { val = JSON.parse(atob(enc)); }
|
||||
|
||||
$('#' + id).val(val);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<video id="video" width="100%"></video>
|
||||
<center>
|
||||
<button id="record">Record</button>
|
||||
<button id="play">Play</button>
|
||||
</center>
|
||||
|
||||
<script src="../jquery.js"></script>
|
||||
<script src="../../../gun/gun.js"></script>
|
||||
|
||||
<script>
|
||||
var gun = Gun(location.origin + '/gun');
|
||||
var record = {recorder: null, recording: false};
|
||||
|
||||
$('#record').on('click', ()=>{
|
||||
if(!record.ing){ return record.stream() }
|
||||
$('#record').text("Record");
|
||||
if(record.ing.stop){ record.ing.stop() }
|
||||
record.ing = false;
|
||||
})
|
||||
|
||||
record.stream = function(){
|
||||
navigator.mediaDevices.getDisplayMedia({ video: true }).then(stream => {
|
||||
var chunks = []; // we have a stream, we can record it
|
||||
record.ing = new MediaRecorder(stream);
|
||||
record.ing.ondataavailable = eve => chunks.push(eve.data);
|
||||
record.ing.onstop = eve => record.save(new Blob(chunks));
|
||||
record.ing.start()
|
||||
$('#record').text("End");
|
||||
}, err => { console.log(err) });
|
||||
}
|
||||
|
||||
record.save = function(data){
|
||||
record.file = record.file || new FileReader();
|
||||
record.file.readAsDataURL(data);
|
||||
record.file.onloadend = function(){
|
||||
var b64 = record.file.result;
|
||||
b64 = "data:video/webm" + b64.slice(b64.indexOf(';'));
|
||||
gun.get('test').get('screen').put(b64);
|
||||
}
|
||||
}
|
||||
|
||||
$('#play').on('click', ()=>{
|
||||
if(record.playing){
|
||||
$('#play').text("Play")
|
||||
$('#video').get(0).pause();
|
||||
record.playing = false;
|
||||
return;
|
||||
}
|
||||
$('#play').text("Stop");
|
||||
record.playing = true;
|
||||
gun.get('test').get('screen').once((data)=>{
|
||||
if(!data){ return }
|
||||
$('#video').get(0).src = data;
|
||||
$('#video').get(0).play()
|
||||
})
|
||||
})
|
||||
</script>
|
||||
@@ -1,9 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<<<<<<< HEAD
|
||||
<video id="video" width="100%"></video>
|
||||
<center>
|
||||
<span id="up">Drag & Drop Video to Upload!</span>
|
||||
<button id="play">Play</button>
|
||||
=======
|
||||
<div class="model" style="display: none;">
|
||||
<video width="100%" controls autoplay></video>
|
||||
<audio width="100%" controls autoplay></audio>
|
||||
<img style="max-width: 100%;">
|
||||
</div>
|
||||
<center>
|
||||
<p>Drag & drop videos, songs, or images! <input type="file" multiple></p>
|
||||
>>>>>>> master
|
||||
</center>
|
||||
|
||||
<script src="../jquery.js"></script>
|
||||
@@ -12,6 +22,7 @@
|
||||
|
||||
<script>
|
||||
var gun = Gun(location.origin + '/gun');
|
||||
<<<<<<< HEAD
|
||||
var video = {data: null};
|
||||
|
||||
$('body').upload(function(e){
|
||||
@@ -34,5 +45,22 @@ $('#play').on('click', ()=>{
|
||||
$('#play').text("Stop");
|
||||
video.playing = true;
|
||||
$('#video').get(0).play()
|
||||
=======
|
||||
|
||||
$('html').upload(function resize(eve, up){
|
||||
if(up){ return up.shrink(eve, resize, 1024) }
|
||||
var b64 = (eve.base64 || ((eve.event || eve).target || eve).result || eve);
|
||||
gun.get('test').get(eve.id).put(b64);
|
||||
});
|
||||
|
||||
gun.get('test').map().on(function(data){
|
||||
if(!data){ return }
|
||||
var type = data.split(';')[0], ui;
|
||||
if(type.indexOf('image') + 1){ ui = $("img").get(0) }
|
||||
if(type.indexOf('video') + 1){ ui = $('video').get(0) }
|
||||
if(type.indexOf('audio') + 1){ ui = $('audio').get(0) }
|
||||
if(!ui){ return }
|
||||
$(ui).clone().prependTo('center').get(0).src = data;
|
||||
>>>>>>> master
|
||||
})
|
||||
</script>
|
||||
62
examples/basic/video.html
Normal file
62
examples/basic/video.html
Normal file
@@ -0,0 +1,62 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<video id="video" width="100%" controls autoplay></video>
|
||||
<center>
|
||||
<input id="pass" placeholder="password">
|
||||
Record <button class="record">Camera</button> or <button class="record">Screen</button>
|
||||
</center>
|
||||
|
||||
<script src="../jquery.js"></script>
|
||||
<script src="../../../gun/gun.js"></script>
|
||||
<script src="../../../gun/sea.js"></script>
|
||||
|
||||
<script>
|
||||
var gun = Gun(location.origin + '/gun');
|
||||
|
||||
gun.get('test').get('video').on(async function(data){
|
||||
if($('#pass').val()){ data = await SEA.decrypt(data, $('#pass').val()) }
|
||||
$('#video').get(0).src = data;
|
||||
})
|
||||
|
||||
$('.record').on('click', function(){
|
||||
if(record.ing){
|
||||
if(record.ing.stop){ record.ing.stop() }
|
||||
$(this).text(record.type);
|
||||
record.ing = false;
|
||||
return;
|
||||
}
|
||||
record(record.type = $(this).text());
|
||||
$(this).text("End");
|
||||
})
|
||||
|
||||
function record(type){
|
||||
if('Camera' === type){
|
||||
navigator.getMedia({ video: true, audio: true }, load, error);
|
||||
}
|
||||
if('Screen' === type){
|
||||
navigator.mediaDevices.getDisplayMedia({ video: true, audio: true }).then(load, error);
|
||||
}
|
||||
function load(media){
|
||||
var chunks = [];
|
||||
record.ing = new MediaRecorder(media);
|
||||
record.ing.ondataavailable = function(eve){ chunks.push(eve.data) }
|
||||
record.ing.onstop = function(eve){record.save(new Blob(chunks)) }
|
||||
record.ing.start();
|
||||
}
|
||||
function error(err){ console.log(err) }
|
||||
}
|
||||
|
||||
record.save = function(data){
|
||||
record.file = record.file || new FileReader();
|
||||
record.file.readAsDataURL(data);
|
||||
record.file.onloadend = async function(){
|
||||
var b64 = record.file.result, pass;
|
||||
b64 = $('#video').get(0).src = "data:video/webm" + b64.slice(b64.indexOf(';'));
|
||||
if($('#pass').val()){ b64 = await SEA.encrypt(b64, $('#pass').val()) }
|
||||
gun.get('test').get('video').put(b64);
|
||||
}
|
||||
}
|
||||
|
||||
navigator.getMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia
|
||||
|| navigator.mozGetUserMedia || navigator.msGetUserMedia);
|
||||
</script>
|
||||
Reference in New Issue
Block a user