2015-12-23 13:57:13 +01:00
|
|
|
<script>
|
|
|
|
'use strict';
|
|
|
|
window.Chat = window.Chat || {};
|
|
|
|
Chat.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) {
|
2015-12-26 13:33:16 +01:00
|
|
|
case 'handshake':
|
|
|
|
this._onHandshake(msg);
|
|
|
|
break;
|
2015-12-23 13:57:13 +01:00
|
|
|
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;
|
2016-01-02 14:23:51 +01:00
|
|
|
case 'text':
|
|
|
|
this._onTextReceived(msg);
|
|
|
|
break;
|
2015-12-23 13:57:13 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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);
|
2016-05-05 07:13:07 +02:00
|
|
|
if(window.donateDialog){
|
|
|
|
window.donateDialog.open();
|
|
|
|
}
|
2015-12-23 13:57:13 +01:00
|
|
|
},
|
|
|
|
_onError: function() {
|
|
|
|
this.loading = false;
|
|
|
|
this.fire('upload-error');
|
|
|
|
},
|
|
|
|
_loadingChanged: function(loading) {
|
|
|
|
window.anim(loading);
|
|
|
|
},
|
|
|
|
_onBuddies: function(msg) {
|
|
|
|
this.set('buddies', msg.buddies);
|
2016-01-02 14:23:51 +01:00
|
|
|
},
|
|
|
|
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);
|
2015-12-23 13:57:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|