1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 18:25:58 +01:00

Simplify peer-to-peer file transfer.

This commit is contained in:
Juliusz Chroboczek 2024-08-11 16:45:29 +02:00
parent cb7a087ea2
commit 4fb0b3334a

View file

@ -1703,7 +1703,7 @@ function TransferredFile(sc, userid, id, up, username, name, mimetype, size) {
* dictionary. * dictionary.
*/ */
TransferredFile.prototype.fullid = function() { TransferredFile.prototype.fullid = function() {
return this.userid + (this.up ? '+' : '-') + this.id; return this.userid + '-' + this.id;
}; };
/** /**
@ -1711,11 +1711,10 @@ TransferredFile.prototype.fullid = function() {
* *
* @param {string} userid * @param {string} userid
* @param {string} fileid * @param {string} fileid
* @param {boolean} up
* @returns {TransferredFile} * @returns {TransferredFile}
*/ */
ServerConnection.prototype.getTransferredFile = function(userid, fileid, up) { ServerConnection.prototype.getTransferredFile = function(userid, fileid) {
return this.transferredFiles[userid + (up ? '+' : '-') + fileid]; return this.transferredFiles[userid + '-' + fileid];
}; };
/** /**
@ -2182,37 +2181,35 @@ ServerConnection.prototype.fileTransfer = function(id, username, message) {
return; return;
} }
if(f.fullid() in sc.transferredFiles) { let fid = f.fullid();
if(fid in sc.transferredFiles) {
if(id === sc.id) {
f.cancel('cannot send file to self');
} else {
console.error('Duplicate id for file transfer'); console.error('Duplicate id for file transfer');
f.cancel("duplicate id (this shouldn't happen)"); f.cancel("duplicate id (this shouldn't happen)");
}
return; return;
} }
sc.transferredFiles[f.fullid()] = f; sc.transferredFiles[fid] = f;
break;
}
case 'offer': {
let f = sc.getTransferredFile(id, message.id, true);
if(!f) {
console.error('Unexpected offer for file transfer');
return;
}
f.answer(message.sdp).catch(e => f.cancel(e));
break; break;
} }
case 'offer':
case 'answer': { case 'answer': {
let f = sc.getTransferredFile(id, message.id, false); let f = sc.getTransferredFile(id, message.id);
if(!f) { if(!f) {
console.error('Unexpected answer for file transfer'); console.error(`Unexpected ${message.type} for file transfer`);
return; return;
} }
if(message.type === 'offer')
f.answer(message.sdp).catch(e => f.cancel(e));
else
f.receiveFile(message.sdp).catch(e => f.cancel(e)); f.receiveFile(message.sdp).catch(e => f.cancel(e));
break; break;
} }
case 'downice': case 'ice':
case 'upice': { {
let f = sc.getTransferredFile( let f = sc.getTransferredFile(id, message.id);
id, message.id, message.type === 'downice',
);
if(!f || !f.pc) { if(!f || !f.pc) {
console.warn(`Unexpected ${message.type} for file transfer`); console.warn(`Unexpected ${message.type} for file transfer`);
return; return;
@ -2223,9 +2220,8 @@ ServerConnection.prototype.fileTransfer = function(id, username, message) {
f.candidates.push(message.candidate); f.candidates.push(message.candidate);
break; break;
} }
case 'cancel': case 'cancel': {
case 'reject': { let f = sc.getTransferredFile(id, message.id);
let f = sc.getTransferredFile(id, message.id, message.type === 'reject');
if(!f) { if(!f) {
console.error(`Unexpected ${message.type} for file transfer`); console.error(`Unexpected ${message.type} for file transfer`);
return; return;
@ -2234,6 +2230,15 @@ ServerConnection.prototype.fileTransfer = function(id, username, message) {
f.close(); f.close();
break; break;
} }
case 'upice':
case 'downice':
case 'abort': {
let f = sc.getTransferredFile(id, message.id);
if(f)
f.cancel(`Obsolete file transfer message ${message.type};` +
' please upgrade your client');
return;
}
default: default:
console.error(`Unknown filetransfer message ${message.type}`); console.error(`Unknown filetransfer message ${message.type}`);
break; break;