2015-07-08 13:34:26 +00:00
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-07-08 13:34:26 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var q = require('q');
|
|
|
|
var extend = require('./extend');
|
2015-12-02 18:16:16 +00:00
|
|
|
var dis = require('./dispatcher');
|
|
|
|
var MatrixClientPeg = require('./MatrixClientPeg');
|
|
|
|
var sdk = require('./index');
|
|
|
|
var Modal = require('./Modal');
|
2015-07-08 13:34:26 +00:00
|
|
|
|
2016-11-02 16:26:10 +00:00
|
|
|
var encrypt = require("browser-encrypt-attachment");
|
|
|
|
|
2015-07-08 13:34:26 +00:00
|
|
|
function infoForImageFile(imageFile) {
|
|
|
|
var deferred = q.defer();
|
|
|
|
|
|
|
|
// Load the file into an html element
|
|
|
|
var img = document.createElement("img");
|
|
|
|
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = function(e) {
|
|
|
|
img.src = e.target.result;
|
|
|
|
|
|
|
|
// Once ready, returns its size
|
|
|
|
img.onload = function() {
|
|
|
|
deferred.resolve({
|
|
|
|
w: img.width,
|
|
|
|
h: img.height
|
|
|
|
});
|
|
|
|
};
|
|
|
|
img.onerror = function(e) {
|
|
|
|
deferred.reject(e);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
reader.onerror = function(e) {
|
|
|
|
deferred.reject(e);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(imageFile);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2016-07-19 15:05:15 +00:00
|
|
|
function infoForVideoFile(videoFile) {
|
|
|
|
var deferred = q.defer();
|
|
|
|
|
|
|
|
// Load the file into an html element
|
|
|
|
var video = document.createElement("video");
|
|
|
|
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = function(e) {
|
|
|
|
video.src = e.target.result;
|
|
|
|
|
|
|
|
// Once ready, returns its size
|
|
|
|
video.onloadedmetadata = function() {
|
|
|
|
deferred.resolve({
|
|
|
|
w: video.videoWidth,
|
|
|
|
h: video.videoHeight
|
|
|
|
});
|
|
|
|
};
|
|
|
|
video.onerror = function(e) {
|
|
|
|
deferred.reject(e);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
reader.onerror = function(e) {
|
|
|
|
deferred.reject(e);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(videoFile);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:26:10 +00:00
|
|
|
/**
|
|
|
|
* Read the file as an ArrayBuffer.
|
|
|
|
* @return {Promise} A promise that resolves with an ArrayBuffer when the file
|
|
|
|
* is read.
|
|
|
|
*/
|
|
|
|
function readFileAsArrayBuffer(file) {
|
|
|
|
var deferred = q.defer();
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = function(e) {
|
|
|
|
deferred.resolve(e.target.result);
|
|
|
|
};
|
|
|
|
reader.onerror = function(e) {
|
|
|
|
deferred.reject(e);
|
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2016-07-19 15:05:15 +00:00
|
|
|
|
2015-12-02 18:16:16 +00:00
|
|
|
class ContentMessages {
|
|
|
|
constructor() {
|
|
|
|
this.inprogress = [];
|
|
|
|
this.nextId = 0;
|
2015-10-02 17:37:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 18:16:16 +00:00
|
|
|
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';
|
2016-08-03 12:27:06 +00:00
|
|
|
infoForImageFile(file).then(imageInfo=>{
|
2015-12-02 18:16:16 +00:00
|
|
|
extend(content.info, imageInfo);
|
|
|
|
def.resolve();
|
2016-08-03 12:27:06 +00:00
|
|
|
}, error=>{
|
|
|
|
content.msgtype = 'm.file';
|
|
|
|
def.resolve();
|
2015-12-02 18:16:16 +00:00
|
|
|
});
|
2016-04-12 23:00:24 +00:00
|
|
|
} else if (file.type.indexOf('audio/') == 0) {
|
|
|
|
content.msgtype = 'm.audio';
|
|
|
|
def.resolve();
|
2016-07-19 15:05:15 +00:00
|
|
|
} else if (file.type.indexOf('video/') == 0) {
|
2016-08-03 12:27:06 +00:00
|
|
|
content.msgtype = 'm.video';
|
|
|
|
infoForVideoFile(file).then(videoInfo=>{
|
|
|
|
extend(content.info, videoInfo);
|
|
|
|
def.resolve();
|
|
|
|
}, error=>{
|
|
|
|
content.msgtype = 'm.file';
|
|
|
|
def.resolve();
|
|
|
|
});
|
2015-12-02 18:16:16 +00:00
|
|
|
} else {
|
|
|
|
content.msgtype = 'm.file';
|
2015-07-08 13:34:26 +00:00
|
|
|
def.resolve();
|
2015-12-02 18:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var upload = {
|
|
|
|
fileName: file.name,
|
|
|
|
roomId: roomId,
|
|
|
|
total: 0,
|
|
|
|
loaded: 0
|
|
|
|
};
|
|
|
|
this.inprogress.push(upload);
|
|
|
|
dis.dispatch({action: 'upload_started'});
|
|
|
|
|
2016-11-02 16:26:10 +00:00
|
|
|
var encryptInfo = null;
|
2016-02-15 19:29:56 +00:00
|
|
|
var error;
|
2015-12-02 18:16:16 +00:00
|
|
|
var self = this;
|
|
|
|
return def.promise.then(function() {
|
2016-11-02 16:26:10 +00:00
|
|
|
if (matrixClient.isRoomEncrypted(room_id)) {
|
|
|
|
// If the room is encrypted then encrypt the file before uploading it.
|
|
|
|
// First read the file into memory.
|
|
|
|
upload.promise = readFileAsArrayBuffer(file).then(function(data) {
|
|
|
|
// Then encrypt the file.
|
|
|
|
return encrypt.encryptAttachment(data);
|
|
|
|
}).then(function(encryptResult) {
|
|
|
|
// Record the information needed to decrypt the attachment.
|
|
|
|
encryptInfo = encryptResult.info;
|
|
|
|
// Pass the encrypted data as a Blob to the uploader.
|
|
|
|
var blob = new Blob([encryptResult.data]);
|
|
|
|
return matrixClient.uploadContent(blob);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
upload.promise = matrixClient.uploadContent(file);
|
|
|
|
}
|
2015-12-02 18:16:16 +00:00
|
|
|
return upload.promise;
|
|
|
|
}).progress(function(ev) {
|
|
|
|
if (ev) {
|
|
|
|
upload.total = ev.total;
|
|
|
|
upload.loaded = ev.loaded;
|
|
|
|
dis.dispatch({action: 'upload_progress', upload: upload});
|
|
|
|
}
|
|
|
|
}).then(function(url) {
|
2016-11-02 16:26:10 +00:00
|
|
|
if (encryptInfo === null) {
|
|
|
|
// If the attachment isn't encrypted then include the URL directly.
|
|
|
|
content.url = url;
|
|
|
|
} else {
|
|
|
|
// If the attachment is encrypted then bundle the URL along
|
|
|
|
// with the information needed to decrypt the attachment and
|
|
|
|
// add it under a file key.
|
|
|
|
encryptInfo.url = url;
|
|
|
|
content.file = encryptInfo;
|
|
|
|
}
|
2015-12-02 18:16:16 +00:00
|
|
|
return matrixClient.sendMessage(roomId, content);
|
|
|
|
}, function(err) {
|
2016-02-15 19:29:56 +00:00
|
|
|
error = err;
|
2015-12-03 10:52:06 +00:00
|
|
|
if (!upload.canceled) {
|
|
|
|
var desc = "The file '"+upload.fileName+"' failed to upload.";
|
|
|
|
if (err.http_status == 413) {
|
|
|
|
desc = "The file '"+upload.fileName+"' exceeds this home server's size limit for uploads";
|
|
|
|
}
|
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: "Upload Failed",
|
|
|
|
description: desc
|
|
|
|
});
|
|
|
|
}
|
2015-12-02 18:16:16 +00:00
|
|
|
}).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) {
|
|
|
|
self.inprogress.splice(k, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 19:29:56 +00:00
|
|
|
if (error) {
|
|
|
|
dis.dispatch({action: 'upload_failed', upload: upload});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dis.dispatch({action: 'upload_finished', upload: upload});
|
|
|
|
}
|
2015-07-08 13:34:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-02 18:16:16 +00:00
|
|
|
getCurrentUploads() {
|
|
|
|
return this.inprogress;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelUpload(promise) {
|
|
|
|
var inprogressKeys = Object.keys(this.inprogress);
|
|
|
|
var upload;
|
|
|
|
for (var i = 0; i < this.inprogress.length; ++i) {
|
|
|
|
var k = inprogressKeys[i];
|
|
|
|
if (this.inprogress[k].promise === promise) {
|
|
|
|
upload = this.inprogress[k];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (upload) {
|
|
|
|
upload.canceled = true;
|
|
|
|
MatrixClientPeg.get().cancelUpload(upload.promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.mx_ContentMessage === undefined) {
|
|
|
|
global.mx_ContentMessage = new ContentMessages();
|
2015-07-08 13:34:26 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 18:16:16 +00:00
|
|
|
module.exports = global.mx_ContentMessage;
|