[WebUI] Allow multiple torrent uploads in Add dialog

Add a new `multiple` field to FileUploadField to allow selecting
multiple files. Include a fallback for if browser does not support
multiple file selection.

Update Add window to upload and parse multiple torrent files at once.
This commit is contained in:
Calum Lind 2018-10-09 18:37:57 +01:00
parent c90cf301df
commit a980f8e959
3 changed files with 45 additions and 17 deletions

View File

@ -127,6 +127,7 @@ Deluge.add.AddWindow = Ext.extend(Deluge.add.Window, {
xtype: 'fileuploadfield', xtype: 'fileuploadfield',
id: 'torrentFile', id: 'torrentFile',
name: 'file', name: 'file',
multiple: true,
buttonCfg: { buttonCfg: {
iconCls: 'x-deluge-add-file', iconCls: 'x-deluge-add-file',
text: _('File'), text: _('File'),
@ -241,34 +242,51 @@ Deluge.add.AddWindow = Ext.extend(Deluge.add.Window, {
onFileSelected: function() { onFileSelected: function() {
if (this.fileUploadForm.isValid()) { if (this.fileUploadForm.isValid()) {
this.torrentId = this.createTorrentId(); var torrentIds = [];
var files = this.fileUploadForm.findField('torrentFile').value;
var randomId = this.createTorrentId();
Array.prototype.forEach.call(
files,
function(file, i) {
// Append index for batch of unique torrentIds.
var torrentId = randomId + i.toString();
torrentIds.push(torrentId);
this.onTorrentBeforeAdd(torrentId, file.name);
}.bind(this)
);
this.fileUploadForm.submit({ this.fileUploadForm.submit({
url: deluge.config.base + 'upload', url: deluge.config.base + 'upload',
waitMsg: _('Uploading your torrent...'), waitMsg: _('Uploading your torrent...'),
success: this.onUploadSuccess, success: this.onUploadSuccess,
scope: this, scope: this,
torrentIds: torrentIds,
}); });
var name = this.fileUploadForm.findField('torrentFile').value;
name = name.split('\\').slice(-1)[0];
this.onTorrentBeforeAdd(this.torrentId, name);
} }
}, },
onUploadSuccess: function(fp, upload) { onUploadSuccess: function(fp, upload) {
if (upload.result.success) { if (!upload.result.success) {
var filename = upload.result.files[0]; this.clear();
this.fileUploadForm.findField('torrentFile').setValue(''); return;
}
upload.result.files.forEach(
function(filename, i) {
deluge.client.web.get_torrent_info(filename, { deluge.client.web.get_torrent_info(filename, {
success: this.onGotInfo, success: this.onGotInfo,
scope: this, scope: this,
filename: filename, filename: filename,
torrentId: upload.options.torrentIds[i],
}); });
} }.bind(this)
);
this.fileUploadForm.reset();
}, },
onGotInfo: function(info, obj, response, request) { onGotInfo: function(info, obj, response, request) {
info.filename = request.options.filename; info.filename = request.options.filename;
this.onTorrentAdd(this.torrentId, info); torrentId = request.options.torrentId;
this.onTorrentAdd(torrentId, info);
}, },
onTorrentBeforeAdd: function(torrentId, text) { onTorrentBeforeAdd: function(torrentId, text) {

View File

@ -24,6 +24,6 @@ Deluge.add.Window = Ext.extend(Ext.Window, {
* Create an id for the torrent before we have any info about it. * Create an id for the torrent before we have any info about it.
*/ */
createTorrentId: function() { createTorrentId: function() {
return new Date().getTime(); return new Date().getTime().toString();
}, },
}); });

View File

@ -29,6 +29,13 @@ Ext.ux.form.FileUploadField = Ext.extend(Ext.form.TextField, {
* (defaults to 3). Note that this only applies if {@link #buttonOnly} = false. * (defaults to 3). Note that this only applies if {@link #buttonOnly} = false.
*/ */
buttonOffset: 3, buttonOffset: 3,
/**
* @cfg {Boolean} multiple True to select more than one file. (defaults to false).
* Note that this only applies if the HTML doc is using HTML5.
*/
multiple: false,
/** /**
* @cfg {Object} buttonCfg A standard {@link Ext.Button} config object. * @cfg {Object} buttonCfg A standard {@link Ext.Button} config object.
*/ */
@ -114,9 +121,11 @@ Ext.ux.form.FileUploadField = Ext.extend(Ext.form.TextField, {
]); ]);
}, },
change: function() { change: function() {
var v = this.fileInput.dom.value; var value = this.fileInput.dom.files;
this.setValue(v); // Fallback to value.
this.fireEvent('fileselected', this, v); if (!value) value = this.fileInput.dom.value;
this.setValue(value);
this.fireEvent('fileselected', this, value);
}, },
}); });
}, },
@ -130,6 +139,7 @@ Ext.ux.form.FileUploadField = Ext.extend(Ext.form.TextField, {
type: 'file', type: 'file',
size: 1, size: 1,
}); });
this.fileInput.dom.multiple = this.multiple;
}, },
reset: function() { reset: function() {