|
|
@@ -54,7 +54,6 @@ frappe.socketio = { |
|
|
|
|
|
|
|
frappe.socketio.setup_listeners(); |
|
|
|
frappe.socketio.setup_reconnect(); |
|
|
|
frappe.socketio.uploader = new frappe.socketio.SocketIOUploader(); |
|
|
|
|
|
|
|
$(document).on('form-load form-rename', function(e, frm) { |
|
|
|
if (frm.is_new()) { |
|
|
@@ -257,114 +256,3 @@ frappe.realtime.publish = function(event, message) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
frappe.socketio.SocketIOUploader = class SocketIOUploader { |
|
|
|
constructor() { |
|
|
|
frappe.socketio.socket.on('upload-request-slice', (data) => { |
|
|
|
var place = data.currentSlice * this.chunk_size, |
|
|
|
slice = this.file.slice(place, |
|
|
|
place + Math.min(this.chunk_size, this.file.size - place)); |
|
|
|
|
|
|
|
if (this.on_progress) { |
|
|
|
// update progress |
|
|
|
this.on_progress(place / this.file.size * 100); |
|
|
|
} |
|
|
|
|
|
|
|
this.reader.readAsArrayBuffer(slice); |
|
|
|
this.started = true; |
|
|
|
this.keep_alive(); |
|
|
|
}); |
|
|
|
|
|
|
|
frappe.socketio.socket.on('upload-end', (data) => { |
|
|
|
this.reader = null; |
|
|
|
this.file = null; |
|
|
|
if (data.file_url.substr(0, 7)==='/public') { |
|
|
|
data.file_url = data.file_url.substr(7); |
|
|
|
} |
|
|
|
this.callback(data); |
|
|
|
}); |
|
|
|
|
|
|
|
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, |
|
|
|
chunk_size=24576, fallback=null} = {}) { |
|
|
|
|
|
|
|
if (this.reader) { |
|
|
|
frappe.throw(__('File Upload in Progress. Please try again in a few moments.')); |
|
|
|
} |
|
|
|
|
|
|
|
function fallback_required() { |
|
|
|
return !frappe.socketio.socket.connected |
|
|
|
|| !( !frappe.boot.sysdefaults || frappe.boot.sysdefaults.use_socketio_to_upload_file ); |
|
|
|
} |
|
|
|
|
|
|
|
if (fallback_required()) { |
|
|
|
return fallback ? fallback() : frappe.throw(__('Socketio is not connected. Cannot upload')); |
|
|
|
} |
|
|
|
|
|
|
|
this.reader = new FileReader(); |
|
|
|
this.file = file; |
|
|
|
this.chunk_size = chunk_size; |
|
|
|
this.callback = callback; |
|
|
|
this.on_progress = on_progress; |
|
|
|
this.fallback = fallback; |
|
|
|
this.started = false; |
|
|
|
|
|
|
|
this.reader.onload = () => { |
|
|
|
frappe.socketio.socket.emit('upload-accept-slice', { |
|
|
|
is_private: is_private, |
|
|
|
name: filename, |
|
|
|
type: this.file.type, |
|
|
|
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 (() => { |
|
|
|
if (!this.started) { |
|
|
|
// upload never started, so try fallback |
|
|
|
if (this.fallback) { |
|
|
|
this.fallback(); |
|
|
|
} else { |
|
|
|
this.disconnect(); |
|
|
|
} |
|
|
|
} |
|
|
|
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' |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |