Sfoglia il codice sorgente

[minor] handle timeouts, disconnects and errors

version-14
Rushabh Mehta 7 anni fa
parent
commit
7acefeb228
2 ha cambiato i file con 73 aggiunte e 27 eliminazioni
  1. +39
    -0
      frappe/public/js/frappe/socketio_client.js
  2. +34
    -27
      socketio.js

+ 39
- 0
frappe/public/js/frappe/socketio_client.js Vedi File

@@ -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'
});
}
}
}

}

+ 34
- 27
socketio.js Vedi File

@@ -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++;

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);

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
//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);

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
});
});
});
} 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
});
}
});


Caricamento…
Annulla
Salva