optimize file upload

pull/1/head
ansuz 8 years ago
parent eb78ed072a
commit f631973f1f

@ -84,6 +84,7 @@ var unescapeKeyCharacters = function (key) {
return key.replace(/\-/g, '/'); return key.replace(/\-/g, '/');
}; };
// TODO Rename to getSession ?
var beginSession = function (Sessions, key) { var beginSession = function (Sessions, key) {
var safeKey = escapeKeyCharacters(key); var safeKey = escapeKeyCharacters(key);
if (Sessions[safeKey]) { if (Sessions[safeKey]) {
@ -625,6 +626,7 @@ var makeFileStream = function (root, id, cb) {
var stream = Fs.createWriteStream(full, { var stream = Fs.createWriteStream(full, {
flags: 'a', flags: 'a',
encoding: 'binary', encoding: 'binary',
highWaterMark: Math.pow(2, 16),
}); });
stream.on('open', function () { stream.on('open', function () {
cb(void 0, stream); cb(void 0, stream);
@ -637,12 +639,15 @@ var makeFileStream = function (root, id, cb) {
var upload = function (Env, publicKey, content, cb) { var upload = function (Env, publicKey, content, cb) {
var paths = Env.paths; var paths = Env.paths;
var dec = new Buffer(Nacl.util.decodeBase64(content)); // jshint ignore:line var dec;
try { dec = Buffer.from(content, 'base64'); }
catch (e) { return void cb(e); }
var len = dec.length; var len = dec.length;
var session = beginSession(Env.Sessions, publicKey); var session = beginSession(Env.Sessions, publicKey);
if (typeof(session.currentUploadSize) !== 'number') { if (typeof(session.currentUploadSize) !== 'number' ||
typeof(session.currentUploadSize) !== 'number') {
// improperly initialized... maybe they didn't check before uploading? // improperly initialized... maybe they didn't check before uploading?
// reject it, just in case // reject it, just in case
return cb('NOT_READY'); return cb('NOT_READY');
@ -670,6 +675,12 @@ var upload = function (Env, publicKey, content, cb) {
var upload_cancel = function (Env, publicKey, cb) { var upload_cancel = function (Env, publicKey, cb) {
var paths = Env.paths; var paths = Env.paths;
var session = beginSession(Env.Sessions, publicKey);
delete session.currentUploadSize;
delete session.pendingUploadSize;
if (session.blobstage) { session.blobstage.close(); }
var path = makeFilePath(paths.staging, publicKey); var path = makeFilePath(paths.staging, publicKey);
if (!path) { if (!path) {
console.log(paths.staging, publicKey); console.log(paths.staging, publicKey);
@ -797,7 +808,7 @@ var isAuthenticatedCall = function (call) {
'UPDATE_LIMITS', 'UPDATE_LIMITS',
'GET_LIMIT', 'GET_LIMIT',
'GET_MULTIPLE_FILE_SIZE', 'GET_MULTIPLE_FILE_SIZE',
'UPLOAD', //'UPLOAD',
'UPLOAD_COMPLETE', 'UPLOAD_COMPLETE',
'UPLOAD_CANCEL', 'UPLOAD_CANCEL',
].indexOf(call) !== -1; ].indexOf(call) !== -1;

@ -129,6 +129,24 @@ types of messages:
return sendMsg(ctx, data, cb); return sendMsg(ctx, data, cb);
}; };
send.unauthenticated = function (type, msg, cb) {
if (!ctx.connected) {
return void window.setTimeout(function () {
cb('DISCONNECTED');
});
}
// construct an unsigned message
var data = [null, edPublicKey, null, type, msg];
if (ctx.cookie && ctx.cookie.join) {
data[2] = ctx.cookie.join('|');
} else {
data[2] = ctx.cookie;
}
return sendMsg(ctx, data, cb);
};
network.on('message', function (msg) { network.on('message', function (msg) {
onMsg(ctx, msg); onMsg(ctx, msg);
}); });

@ -65,7 +65,7 @@ define([
var enc = Nacl.util.encodeBase64(box); var enc = Nacl.util.encodeBase64(box);
chunks.push(box); chunks.push(box);
Cryptpad.rpc.send('UPLOAD', enc, function (e, msg) { Cryptpad.rpc.send.unauthenticated('UPLOAD', enc, function (e, msg) {
console.log(box); console.log(box);
cb(e, msg); cb(e, msg);
}); });

Loading…
Cancel
Save