Add max size handling

This commit is contained in:
Luc Didry 2015-10-04 01:39:52 +02:00
parent 7585be5c0a
commit 10a8dd8603
4 changed files with 160 additions and 123 deletions

View File

@ -7,6 +7,7 @@ use LufiDB;
use Lufi::File; use Lufi::File;
use Lufi::Slice; use Lufi::Slice;
use File::Spec::Functions; use File::Spec::Functions;
use Number::Bytes::Human qw(format_bytes);
sub upload { sub upload {
my $c = shift; my $c = shift;
@ -23,6 +24,16 @@ sub upload {
$c->debug('Got message'); $c->debug('Got message');
my $over_size = 0;
# Check against max_size
if (defined $c->config('max_file_size')) {
if ($json->{size} > $c->config('max_file_size')) {
$over_size = 1;
$c->send(sprintf('{"success": false, "msg":"'.$c->l('Your file is too big: %1 (maximum size allowed: %2)', format_bytes($json->{size}), format_bytes($c->config('max_file_size'))).'", "sent_delay": %d, "i": %d}', $json->{delay}, $json->{i}));
}
}
unless ($over_size) {
my $f; my $f;
if (defined($json->{id})) { if (defined($json->{id})) {
my @records = LufiDB::Files->select('WHERE short = ?', $json->{id}); my @records = LufiDB::Files->select('WHERE short = ?', $json->{id});
@ -81,7 +92,8 @@ sub upload {
$f->write; $f->write;
$ws->send(sprintf('{"success": true, "i": %d, "j": %d, "parts": %d, "short": "%s", "name": "%s", "size": %d, "del_at_first_view": %s, "created_at": %d, "delay": %d, "token": "%s"}', $json->{i}, $json->{part}, $json->{total}, $f->short, $f->filename, $f->filesize, (($f->delete_at_first_view) ? 'true' : 'false'), $f->created_at, $f->delete_at_day, $f->mod_token)); $ws->send(sprintf('{"success": true, "i": %d, "j": %d, "parts": %d, "short": "%s", "name": "%s", "size": %d, "del_at_first_view": %s, "created_at": %d, "delay": %d, "token": "%s", "sent_delay": %d}', $json->{i}, $json->{part}, $json->{total}, $f->short, $f->filename, $f->filesize, (($f->delete_at_first_view) ? 'true' : 'false'), $f->created_at, $f->delete_at_day, $f->mod_token, $json->{delay}));
}
} }
); );
$c->on( $c->on(

View File

@ -33,6 +33,11 @@
# optional, default is 32 # optional, default is 32
#token_length => 32, #token_length => 32,
# max file size, in octets
# you can write it 100*1024*1024
# optional, no default
#max_file_size => 104857600,
# if you want to have piwik statistics, provide a piwik image tracker # if you want to have piwik statistics, provide a piwik image tracker
# only the image tracker is allowed, no javascript # only the image tracker is allowed, no javascript
# optional, no default # optional, no default

View File

@ -191,12 +191,14 @@ function sliceAndUpload(randomkey, i, parts, j, delay, del_at_first_view, short)
// Update the progress bar // Update the progress bar
function updateProgressBar(data) { function updateProgressBar(data) {
var i = data.i; var i = data.i;
var sent_delay = data.sent_delay;
var del_at_first_view = data.del_at_first_view;
if (data.success) {
var j = data.j; var j = data.j;
var delay = data.delay;
var parts = data.parts; var parts = data.parts;
var short = data.short; var short = data.short;
var del_at_first_view = data.del_at_first_view;
var created_at = data.created_at; var created_at = data.created_at;
var delay = data.delay;
var dp = document.getElementById('progress-'+window.fc); var dp = document.getElementById('progress-'+window.fc);
var key = dp.getAttribute('data-key'); var key = dp.getAttribute('data-key');
@ -255,10 +257,11 @@ function updateProgressBar(data) {
// Add the file to localStorage // Add the file to localStorage
addItem(data.name, url, data.size, del_at_first_view, created_at, delay, data.short, data.token); addItem(data.name, url, data.size, del_at_first_view, created_at, delay, data.short, data.token);
// Upload next file
window.fc++; window.fc++;
i++; i++;
if (i < window.files.length) { if (i < window.files.length) {
uploadFile(i, delay, del_at_first_view); uploadFile(i, sent_delay, del_at_first_view);
} else { } else {
// We have finished // We have finished
window.removeEventListener('onbeforeunload', confirmExit); window.removeEventListener('onbeforeunload', confirmExit);
@ -275,6 +278,28 @@ function updateProgressBar(data) {
// Encrypt and upload next slice // Encrypt and upload next slice
sliceAndUpload(key, i, parts, j, delay, del_at_first_view, short); sliceAndUpload(key, i, parts, j, delay, del_at_first_view, short);
} }
} else {
var n = document.getElementById('name-'+window.fc);
var p = document.getElementById('progress-'+window.fc);
var d = document.createElement('div');
p.parentNode.remove();
d.innerHTML = data.msg;
d.setAttribute('class', 'alert alert-danger');
n.parentNode.appendChild(d);
// Upload next file
window.fc++;
i++;
if (i < window.files.length) {
uploadFile(i, sent_delay, del_at_first_view);
} else {
// We have finished
window.removeEventListener('onbeforeunload', confirmExit);
document.getElementById('delete-day').removeAttribute('disabled');
document.getElementById('first-view').removeAttribute('disabled');
}
}
} }
// Dropzone events functions // Dropzone events functions
@ -304,12 +329,7 @@ function spawnWebsocket(callback) {
console.log('Connection is closed.'); console.log('Connection is closed.');
} }
ws.onmessage = function(e) { ws.onmessage = function(e) {
var data = JSON.parse(e.data); updateProgressBar(JSON.parse(e.data));
if (data.success) {
updateProgressBar(data);
} else {
alert(data.msg);
}
} }
ws.onerror = function() { ws.onerror = function() {
console.log('error'); console.log('error');

View File

@ -76,7 +76,7 @@ var i18n = {
hit: '<%= l('Hit Enter, then Ctrl+C to copy the download link') %>', hit: '<%= l('Hit Enter, then Ctrl+C to copy the download link') %>',
hits: '<%= l('Hit Enter, then Ctrl+C to copy all the download links') %>', hits: '<%= l('Hit Enter, then Ctrl+C to copy all the download links') %>',
mailTo: '<%= l('Send all links by email') %>', mailTo: '<%= l('Send all links by email') %>',
noLimit: '<%= l('No limit') %>', noLimit: '<%= l('No expiration delay') %>',
}; };
% end % end
%= javascript '/js/sjcl.js' %= javascript '/js/sjcl.js'