diff --git a/frappe/public/js/frappe/socketio_client.js b/frappe/public/js/frappe/socketio_client.js index e5d4bfc590..d058b8a3f4 100644 --- a/frappe/public/js/frappe/socketio_client.js +++ b/frappe/public/js/frappe/socketio_client.js @@ -291,6 +291,7 @@ frappe.socketio.SocketIOUploader = class SocketIOUploader { } this.reader.readAsArrayBuffer(slice); + this.keep_alive(); }); frappe.socketio.socket.on('upload-end', (data) => { @@ -301,6 +302,19 @@ frappe.socketio.SocketIOUploader = class SocketIOUploader { this.reader = null; this.file = null; }); + + frappe.socketio.socket.on('upload-error', (data) => { + this.disconnect(false); + frappe.msgprint({ + title: __('Upload Failed'), + message: data.error, + indicator: 'red' + }); + }); + + frappe.socketio.socket.on('disconnect', () => { + this.disconnect(); + }); } start({file=null, is_private=0, filename='', callback=null, on_progress=null, @@ -324,10 +338,35 @@ frappe.socketio.SocketIOUploader = class SocketIOUploader { size: this.file.size, data: this.reader.result }); + this.keep_alive(); }; var slice = file.slice(0, this.chunk_size); this.reader.readAsArrayBuffer(slice); } + keep_alive() { + if (this.next_check) { + clearTimeout (this.next_check); + } + this.next_check = setTimeout (() => { + this.disconnect(); + }, 3000); + } + + disconnect(with_message = true) { + if (this.reader) { + this.reader = null; + this.file = null; + frappe.hide_progress(); + if (with_message) { + frappe.msgprint({ + title: __('File Upload'), + message: __('File Upload Disconnected. Please try again.'), + indicator: 'red' + }); + } + } + } + } \ No newline at end of file diff --git a/socketio.js b/socketio.js index f5ade1f1c1..15e9263398 100644 --- a/socketio.js +++ b/socketio.js @@ -151,35 +151,42 @@ io.on('connection', function(socket) { }); socket.on('upload-accept-slice', (data) => { - if (!socket.files[data.name]) { - socket.files[data.name] = Object.assign({}, files_struct, data); - socket.files[data.name].data = []; - } + try { + if (!socket.files[data.name]) { + socket.files[data.name] = Object.assign({}, files_struct, data); + socket.files[data.name].data = []; + } - //convert the ArrayBuffer to Buffer - data.data = new Buffer(new Uint8Array(data.data)); - //save the data - socket.files[data.name].data.push(data.data); - socket.files[data.name].slice++; + //convert the ArrayBuffer to Buffer + data.data = new Buffer(new Uint8Array(data.data)); + //save the data + socket.files[data.name].data.push(data.data); + socket.files[data.name].slice++; - if (socket.files[data.name].slice * 100000 >= socket.files[data.name].size) { - // do something with the data - var fileBuffer = Buffer.concat(socket.files[data.name].data); + if (socket.files[data.name].slice * 100000 >= socket.files[data.name].size) { + // do something with the data + var fileBuffer = Buffer.concat(socket.files[data.name].data); - const file_url = path.join((socket.files[data.name].is_private ? 'private' : 'public'), - 'files', data.name); - const file_path = path.join('sites', get_site_name(socket), file_url); + const file_url = path.join((socket.files[data.name].is_private ? 'private' : 'public'), + 'files', data.name); + const file_path = path.join('sites', get_site_name(socket), file_url); - fs.writeFile(file_path, fileBuffer, (err) => { - delete socket.files[data.name]; - if (err) return socket.emit('upload error'); - socket.emit('upload-end', { - file_url: '/' + file_url + fs.writeFile(file_path, fileBuffer, (err) => { + delete socket.files[data.name]; + if (err) return socket.emit('upload error'); + socket.emit('upload-end', { + file_url: '/' + file_url + }); }); - }); - } else { - socket.emit('upload-request-slice', { - currentSlice: socket.files[data.name].slice + } else { + socket.emit('upload-request-slice', { + currentSlice: socket.files[data.name].slice + }); + } + } catch (e) { + console.log(e); + socket.emit('upload-error', { + error: e.message }); } });