78dd776426
Initially a lot of code was copied from another project. This lead to confusing naming conventions.
159 lines
4.3 KiB
HTML
159 lines
4.3 KiB
HTML
<script>
|
|
'use strict';
|
|
window.Snapdrop = window.Snapdrop || {};
|
|
Snapdrop.FileTransferProtocol = {
|
|
properties: {
|
|
loading: {
|
|
type: Boolean,
|
|
notify: true,
|
|
value: false,
|
|
observer: '_loadingChanged'
|
|
},
|
|
buddies: {
|
|
notify: true
|
|
}
|
|
},
|
|
listeners: {
|
|
'system-event': '_onSystemMsg',
|
|
'file-received': '_onFileReceived',
|
|
},
|
|
_onSystemMsg: function(event) {
|
|
var msg = event.detail;
|
|
console.log('FTP received sysMsg:', msg);
|
|
|
|
switch (msg.type) {
|
|
case 'handshake':
|
|
this._onHandshake(msg);
|
|
break;
|
|
case 'offer':
|
|
this._onOffered(msg);
|
|
break;
|
|
case 'decline':
|
|
this._onDeclined(msg);
|
|
break;
|
|
case 'accept':
|
|
this._onAccepted(msg);
|
|
break;
|
|
case 'transfer':
|
|
this._onTransfer(msg);
|
|
break;
|
|
case 'received':
|
|
this._onReceived(msg);
|
|
break;
|
|
case 'buddies':
|
|
this._onBuddies(msg);
|
|
break;
|
|
case 'text':
|
|
this._onTextReceived(msg);
|
|
break;
|
|
}
|
|
},
|
|
sendFile: function(peerId, file) {
|
|
this.set('loading', true);
|
|
this.fileToSend = file;
|
|
this.fire('file-offered', {
|
|
to: peerId
|
|
});
|
|
this.connectToPeer(peerId, function() {
|
|
this._offer(peerId, file);
|
|
}.bind(this));
|
|
|
|
//set 15sec timeout
|
|
this._timeoutTimer = this.async(function() {
|
|
this._onError();
|
|
}, 15000);
|
|
},
|
|
_offer: function(toPeer, file) {
|
|
console.log('FTP offer file:', file, 'To:', toPeer);
|
|
|
|
this._sendSystemEvent(toPeer, {
|
|
type: 'offer',
|
|
name: file.name
|
|
});
|
|
},
|
|
_onOffered: function(offer) {
|
|
console.log('FTP offered file:', offer.name, 'From:', offer.from);
|
|
this.fire('file-offer', {
|
|
from: offer.from,
|
|
name: offer.name
|
|
});
|
|
},
|
|
decline: function(offer) {
|
|
this._sendSystemEvent(offer.from, {
|
|
type: 'decline',
|
|
name: offer.name
|
|
});
|
|
},
|
|
_onDeclined: function(offer) {
|
|
this.cancelAsync(this._timeoutTimer);
|
|
delete this.fileToSend;
|
|
this.set('loading', false);
|
|
this.fire('file-declined', offer);
|
|
},
|
|
accept: function(offer) {
|
|
this._sendSystemEvent(offer.from, {
|
|
type: 'accept',
|
|
name: offer.name
|
|
});
|
|
this.fire('download-started', {
|
|
from: offer.from
|
|
});
|
|
},
|
|
_onAccepted: function(offer) {
|
|
this.cancelAsync(this._timeoutTimer);
|
|
this._sendSystemEvent(offer.from, {
|
|
type: 'transfer',
|
|
name: offer.name
|
|
});
|
|
this.fire('upload-started', {
|
|
to: offer.from
|
|
});
|
|
this._sendFile(offer.from, this.fileToSend);
|
|
},
|
|
_onTransfer: function() {
|
|
this.loading = true;
|
|
},
|
|
_onFileReceived: function(event) {
|
|
var file = event.detail;
|
|
this.loading = false;
|
|
this._sendSystemEvent(file.from, {
|
|
type: 'received',
|
|
name: file.name
|
|
});
|
|
this.fire('download-complete', {
|
|
from: file.from
|
|
});
|
|
console.log('FTP received:', file);
|
|
},
|
|
_onReceived: function(offer) {
|
|
this.loading = false;
|
|
this.fire('upload-complete', offer);
|
|
if(window.donateDialog){
|
|
window.donateDialog.open();
|
|
}
|
|
},
|
|
_onError: function() {
|
|
this.loading = false;
|
|
this.fire('upload-error');
|
|
},
|
|
_loadingChanged: function(loading) {
|
|
window.anim(loading);
|
|
},
|
|
_onBuddies: function(msg) {
|
|
this.set('buddies', msg.buddies);
|
|
},
|
|
sendText: function(toPeer, text) {
|
|
console.log('FTP send text:', text, 'To:', toPeer);
|
|
this.connectToPeer(toPeer, function() {
|
|
this._sendSystemEvent(toPeer, {
|
|
type: 'text',
|
|
text: text
|
|
});
|
|
}.bind(this));
|
|
|
|
},
|
|
_onTextReceived: function(msg) {
|
|
this.fire('text-received', msg);
|
|
}
|
|
};
|
|
</script>
|