Commit e0f06176 authored by JC Brand's avatar JC Brand

Don't store the `File` object in browserStorage

With the recent fixes in backbone.browserStorage, the 'file' attributes
gets persisted to browserStorage before Converse tries to upload the
file.

The File object can't actually be serialized, so an empty map gets
stored and then returned together with a 'change' event.

Instead, store the file as a direct attribute on the message object.

Fixes #1261
parent 53b12399
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
- Use [Lerna](https://lernajs.io/) to create the @converse/headless package - Use [Lerna](https://lernajs.io/) to create the @converse/headless package
- Use ES2015 modules instead of UMD. - Use ES2015 modules instead of UMD.
- #1257: Prefer 'probably' over 'maybe' when evaluating audio play support. - #1257 Prefer 'probably' over 'maybe' when evaluating audio play support.
- #1261 File upload not working
## 4.0.3 (2018-10-22) ## 4.0.3 (2018-10-22)
......
This diff is collapsed.
...@@ -60,6 +60,10 @@ converse.plugins.add('converse-message-view', { ...@@ -60,6 +60,10 @@ converse.plugins.add('converse-message-view', {
if (this.model.isOnlyChatStateNotification()) { if (this.model.isOnlyChatStateNotification()) {
this.renderChatStateNotification() this.renderChatStateNotification()
} else if (this.model.get('file') && !this.model.get('oob_url')) { } else if (this.model.get('file') && !this.model.get('oob_url')) {
if (!this.model.file) {
_converse.log("Attempted to render a file upload message with no file data");
return this.el;
}
this.renderFileUploadProgresBar(); this.renderFileUploadProgresBar();
} else if (this.model.get('type') === 'error') { } else if (this.model.get('type') === 'error') {
this.renderErrorMessage(); this.renderErrorMessage();
...@@ -204,7 +208,7 @@ converse.plugins.add('converse-message-view', { ...@@ -204,7 +208,7 @@ converse.plugins.add('converse-message-view', {
renderFileUploadProgresBar () { renderFileUploadProgresBar () {
const msg = u.stringToElement(tpl_file_progress( const msg = u.stringToElement(tpl_file_progress(
_.extend(this.model.toJSON(), { _.extend(this.model.toJSON(), {
'filesize': filesize(this.model.get('file').size), 'filesize': filesize(this.model.file.size),
}))); })));
this.replaceElement(msg); this.replaceElement(msg);
this.renderAvatar(); this.renderAvatar();
......
...@@ -68,10 +68,6 @@ converse.plugins.add('converse-chatboxes', { ...@@ -68,10 +68,6 @@ converse.plugins.add('converse-chatboxes', {
this.setVCard(); this.setVCard();
if (this.get('file')) { if (this.get('file')) {
this.on('change:put', this.uploadFile, this); this.on('change:put', this.uploadFile, this);
if (!_.includes([_converse.SUCCESS, _converse.FAILURE], this.get('upload'))) {
this.getRequestSlotURL();
}
} }
if (this.isOnlyChatStateNotification()) { if (this.isOnlyChatStateNotification()) {
window.setTimeout(this.destroy.bind(this), 20000); window.setTimeout(this.destroy.bind(this), 20000);
...@@ -132,20 +128,20 @@ converse.plugins.add('converse-chatboxes', { ...@@ -132,20 +128,20 @@ converse.plugins.add('converse-chatboxes', {
* *
* https://xmpp.org/extensions/xep-0363.html#request * https://xmpp.org/extensions/xep-0363.html#request
*/ */
const file = this.get('file'); if (_.isNil(this.file)) {
return new Promise((resolve, reject) => { return Promise.reject(new Error("file is undefined"));
}
const iq = converse.env.$iq({ const iq = converse.env.$iq({
'from': _converse.jid, 'from': _converse.jid,
'to': this.get('slot_request_url'), 'to': this.get('slot_request_url'),
'type': 'get' 'type': 'get'
}).c('request', { }).c('request', {
'xmlns': Strophe.NS.HTTPUPLOAD, 'xmlns': Strophe.NS.HTTPUPLOAD,
'filename': file.name, 'filename': this.file.name,
'size': file.size, 'size': this.file.size,
'content-type': file.type 'content-type': this.file.type
}) })
_converse.connection.sendIQ(iq, resolve, reject); return _converse.api.sendIQ(iq);
});
}, },
getRequestSlotURL () { getRequestSlotURL () {
...@@ -417,9 +413,9 @@ converse.plugins.add('converse-chatboxes', { ...@@ -417,9 +413,9 @@ converse.plugins.add('converse-chatboxes', {
}, },
sendFiles (files) { async sendFiles (files) {
_converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then((result) => { const result = await _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain),
const item = result.pop(), item = result.pop(),
data = item.dataforms.where({'FORM_TYPE': {'value': Strophe.NS.HTTPUPLOAD, 'type': "hidden"}}).pop(), data = item.dataforms.where({'FORM_TYPE': {'value': Strophe.NS.HTTPUPLOAD, 'type': "hidden"}}).pop(),
max_file_size = window.parseInt(_.get(data, 'attributes.max-file-size.value')), max_file_size = window.parseInt(_.get(data, 'attributes.max-file-size.value')),
slot_request_url = _.get(item, 'id'); slot_request_url = _.get(item, 'id');
...@@ -427,7 +423,7 @@ converse.plugins.add('converse-chatboxes', { ...@@ -427,7 +423,7 @@ converse.plugins.add('converse-chatboxes', {
if (!slot_request_url) { if (!slot_request_url) {
this.messages.create({ this.messages.create({
'message': __("Sorry, looks like file upload is not supported by your server."), 'message': __("Sorry, looks like file upload is not supported by your server."),
'type': 'error', 'type': 'error'
}); });
return; return;
} }
...@@ -436,20 +432,22 @@ converse.plugins.add('converse-chatboxes', { ...@@ -436,20 +432,22 @@ converse.plugins.add('converse-chatboxes', {
return this.messages.create({ return this.messages.create({
'message': __('The size of your file, %1$s, exceeds the maximum allowed by your server, which is %2$s.', 'message': __('The size of your file, %1$s, exceeds the maximum allowed by your server, which is %2$s.',
file.name, filesize(max_file_size)), file.name, filesize(max_file_size)),
'type': 'error', 'type': 'error'
}); });
} else { } else {
this.messages.create( const message = this.messages.create(
_.extend( _.extend(
this.getOutgoingMessageAttributes(), { this.getOutgoingMessageAttributes(), {
'file': file, 'file': file,
'progress': 0, 'progress': 0,
'slot_request_url': slot_request_url 'slot_request_url': slot_request_url
}) }), {'silent': true}
); );
message.file = file;
this.messages.trigger('add', message);
message.getRequestSlotURL();
} }
}); });
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
}, },
getReferencesFromStanza (stanza) { getReferencesFromStanza (stanza) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment