Un-commit accidentally committed file upload stuff

This commit is contained in:
David Baker 2015-12-01 10:25:35 +00:00
parent 1df03aa82a
commit 1de5d68e95

View File

@ -18,7 +18,6 @@ limitations under the License.
var q = require('q');
var extend = require('./extend');
var dis = require('./dispatcher');
function infoForImageFile(imageFile) {
var deferred = q.defer();
@ -49,94 +48,39 @@ function infoForImageFile(imageFile) {
return deferred.promise;
}
class ContentMessages {
constructor() {
this.inprogress = [];
this.nextId = 0;
function sendContentToRoom(file, roomId, matrixClient) {
var content = {
body: file.name,
info: {
size: file.size,
}
};
// if we have a mime type for the file, add it to the message metadata
if (file.type) {
content.info.mimetype = file.type;
}
sendContentToRoom(file, roomId, matrixClient) {
var content = {
body: file.name,
info: {
size: file.size,
}
};
// if we have a mime type for the file, add it to the message metadata
if (file.type) {
content.info.mimetype = file.type;
}
var def = q.defer();
if (file.type.indexOf('image/') == 0) {
content.msgtype = 'm.image';
infoForImageFile(file).then(function(imageInfo) {
extend(content.info, imageInfo);
def.resolve();
});
} else {
content.msgtype = 'm.file';
var def = q.defer();
if (file.type.indexOf('image/') == 0) {
content.msgtype = 'm.image';
infoForImageFile(file).then(function(imageInfo) {
extend(content.info, imageInfo);
def.resolve();
}
var upload = {
fileName: file.name,
roomId: roomId,
total: 0,
loaded: 0
};
this.inprogress.push(upload);
dis.dispatch({action: 'upload_started'});
var self = this;
return def.promise.then(function() {
upload.promise = matrixClient.uploadContent(file);
return upload.promise;
}).then(function(url) {
content.url = url;
return matrixClient.sendMessage(roomId, content);
}).progress(function(ev) {
if (ev) {
upload.total = ev.total;
upload.loaded = ev.loaded;
dis.dispatch({action: 'upload_progress', upload: upload});
}
}).then(function() {
dis.dispatch({action: 'upload_finished', upload: upload});
}, function(err) {
dis.dispatch({action: 'upload_failed', upload: upload});
}).finally(function() {
var inprogressKeys = Object.keys(self.inprogress);
for (var i = 0; i < self.inprogress.length; ++i) {
var k = inprogressKeys[i];
if (self.inprogress[k].promise === upload_promise) {
delete self.inprogress[k];
break;
}
}
});
} else {
content.msgtype = 'm.file';
def.resolve();
}
getCurrentUploads() {
return this.inprogress;
}
cancelUpload(promise) {
var inprogressKeys = Object.keys(self.inprogress);
for (var i = 0; i < self.inprogress.length; ++i) {
var k = inprogressKeys[i];
if (self.inprogress[k].promise === promise) {
self.inprogress[k].canceled = true;
break;
}
}
MatrixClientPeg.get().cancelUpload(promise);
}
return def.promise.then(function() {
return matrixClient.uploadContent(file);
}).then(function(url) {
content.url = url;
return matrixClient.sendMessage(roomId, content);
});
}
if (global.mx_ContentMessage === undefined) {
global.mx_ContentMessage = new ContentMessages();
}
module.exports = global.mx_ContentMessage;
module.exports = {
sendContentToRoom: sendContentToRoom
};