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,65 +24,76 @@ sub upload {
$c->debug('Got message'); $c->debug('Got message');
my $f; my $over_size = 0;
if (defined($json->{id})) { # Check against max_size
my @records = LufiDB::Files->select('WHERE short = ?', $json->{id}); if (defined $c->config('max_file_size')) {
$f = Lufi::File->new(record => $records[0]); if ($json->{size} > $c->config('max_file_size')) {
} else { $over_size = 1;
my $delay; $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}));
}
}
if (defined $c->config('delay_for_size')) { unless ($over_size) {
# Choose delay according to config my $f;
my $delays = $c->config('delay_for_size'); if (defined($json->{id})) {
my @keys = sort {$b <=> $a} keys %{$delays}; my @records = LufiDB::Files->select('WHERE short = ?', $json->{id});
for my $key (@keys) { $f = Lufi::File->new(record => $records[0]);
if ($json->{size} >= $key) { } else {
$delay = ($json->{delay} < $delays->{$key}) ? $json->{delay} : $delays->{$key}; my $delay;
last;
if (defined $c->config('delay_for_size')) {
# Choose delay according to config
my $delays = $c->config('delay_for_size');
my @keys = sort {$b <=> $a} keys %{$delays};
for my $key (@keys) {
if ($json->{size} >= $key) {
$delay = ($json->{delay} < $delays->{$key}) ? $json->{delay} : $delays->{$key};
last;
}
} }
} }
} # If the file size is lower than the lowest configured size or if there is no delay_for_size setting, we choose the configured max delay
# If the file size is lower than the lowest configured size or if there is no delay_for_size setting, we choose the configured max delay unless (defined $delay) {
unless (defined $delay) { $delay = ($json->{delay} <= $c->config('max_delay') || $c->config('max_delay') == 0) ? $json->{delay} : $c->config('max_delay');
$delay = ($json->{delay} <= $c->config('max_delay') || $c->config('max_delay') == 0) ? $json->{delay} : $c->config('max_delay'); }
}
$f = Lufi::File->new( $f = Lufi::File->new(
record => $c->get_empty, record => $c->get_empty,
created_by => $c->ip, created_by => $c->ip,
delete_at_first_view => ($json->{del_at_first_view}) ? 1 : 0, delete_at_first_view => ($json->{del_at_first_view}) ? 1 : 0,
delete_at_day => $delay, delete_at_day => $delay,
mediatype => $json->{type}, mediatype => $json->{type},
filename => $json->{name}, filename => $json->{name},
filesize => $json->{size}, filesize => $json->{size},
nbslices => $json->{total}, nbslices => $json->{total},
mod_token => $c->shortener($c->config('token_length')) mod_token => $c->shortener($c->config('token_length'))
);
$f->write;
}
# Create directory
my $dir = catdir('files', $f->short);
mkdir($dir, 0700) unless (-d $dir);
# Create slice file
my $file = catfile($dir, $json->{part}.'.part');
my $s = Lufi::Slice->new(
short => $f->short,
j => $json->{part},
path => $file
); );
spurt $text, $file;
push @{$f->slices}, $s;
if (($json->{part} + 1) == $json->{total}) {
$f->complete(1);
$f->created_at(time);
$c->provisioning;
}
$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", "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}));
} }
# Create directory
my $dir = catdir('files', $f->short);
mkdir($dir, 0700) unless (-d $dir);
# Create slice file
my $file = catfile($dir, $json->{part}.'.part');
my $s = Lufi::Slice->new(
short => $f->short,
j => $json->{part},
path => $file
);
spurt $text, $file;
push @{$f->slices}, $s;
if (($json->{part} + 1) == $json->{total}) {
$f->complete(1);
$f->created_at(time);
$c->provisioning;
}
$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));
} }
); );
$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,89 +191,114 @@ 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 j = data.j; var sent_delay = data.sent_delay;
var parts = data.parts;
var short = data.short;
var del_at_first_view = data.del_at_first_view; var del_at_first_view = data.del_at_first_view;
var created_at = data.created_at; if (data.success) {
var delay = data.delay; var j = data.j;
var delay = data.delay;
var parts = data.parts;
var short = data.short;
var created_at = data.created_at;
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');
if (j + 1 === parts) { if (j + 1 === parts) {
var n = document.getElementById('name-'+window.fc); var n = document.getElementById('name-'+window.fc);
var d = document.createElement('div'); var d = document.createElement('div');
var url = baseURL+'r/'+short+'#'+key; var url = baseURL+'r/'+short+'#'+key;
var del_url = baseURL+'d/'+short+'/'+data.token; var del_url = baseURL+'d/'+short+'/'+data.token;
var links = encodeURIComponent('["'+short+'"]'); var links = encodeURIComponent('["'+short+'"]');
var limit = (delay === 0) ? i18n.noLimit : i18n.expiration+' '+moment.unix(delay * 86400 + created_at).locale(window.navigator.language).format('LLLL'); var limit = (delay === 0) ? i18n.noLimit : i18n.expiration+' '+moment.unix(delay * 86400 + created_at).locale(window.navigator.language).format('LLLL');
n.innerHTML = n.innerHTML+' <a href="'+baseURL+'m?links='+links+'"><span class="icon icon-mail"></span></a><br>'+limit; n.innerHTML = n.innerHTML+' <a href="'+baseURL+'m?links='+links+'"><span class="icon icon-mail"></span></a><br>'+limit;
d.innerHTML = '<div class="form-group"><label class="sr-only" for="' d.innerHTML = '<div class="form-group"><label class="sr-only" for="'
+short +short
+'">' +'">'
+i18n.dlText +i18n.dlText
+'</label><div class="input-group"><div class="input-group-addon"><a href="' +'</label><div class="input-group"><div class="input-group-addon"><a href="'
+url +url
+'" target="_blank"><span class="icon icon-download" title="' +'" target="_blank"><span class="icon icon-download" title="'
+i18n.dlText +i18n.dlText
+'"></span></a></div><input id="' +'"></span></a></div><input id="'
+short +short
+'" class="form-control link-input" value="' +'" class="form-control link-input" value="'
+url +url
+'" readonly="" type="text" style="background-color: #FFF;"><a href="#" onclick="copyToClipboard(this);" class="input-group-addon" title="' +'" readonly="" type="text" style="background-color: #FFF;"><a href="#" onclick="copyToClipboard(this);" class="input-group-addon" title="'
+i18n.cpText +i18n.cpText
+'"><span class="icon icon-clipboard"></span></a></div></div>' +'"><span class="icon icon-clipboard"></span></a></div></div>'
+'<div class="form-group"><label class="sr-only" for="delete-' +'<div class="form-group"><label class="sr-only" for="delete-'
+short +short
+'">' +'">'
+i18n.delText +i18n.delText
+'</label><div class="input-group"><div class="input-group-addon"><a href="' +'</label><div class="input-group"><div class="input-group-addon"><a href="'
+del_url +del_url
+'" target="_blank"><span class="icon icon-trash" title="' +'" target="_blank"><span class="icon icon-trash" title="'
+i18n.delText +i18n.delText
+'"></span></a></div><input id="delete-' +'"></span></a></div><input id="delete-'
+short +short
+'" class="form-control" value="' +'" class="form-control" value="'
+del_url +del_url
+'" readonly="" type="text" style="background-color: #FFF;">'; +'" readonly="" type="text" style="background-color: #FFF;">';
var p2 = dp.parentNode; var p2 = dp.parentNode;
var p1 = p2.parentNode; var p1 = p2.parentNode;
p2.remove(); p2.remove();
p1.appendChild(d); p1.appendChild(d);
// Add copy all and mailto buttons // Add copy all and mailto buttons
var misc = document.getElementById('misc'); var misc = document.getElementById('misc');
if (misc.innerHTML === '') { if (misc.innerHTML === '') {
misc.innerHTML = '<a href="#" onclick="copyAllToClipboard();" class="btn btn-info">'+i18n.copyAll+'</a> <a id="mailto" href="'+baseURL+'m?links='+links+'" class="btn btn-info">'+i18n.mailTo+'</a>'; misc.innerHTML = '<a href="#" onclick="copyAllToClipboard();" class="btn btn-info">'+i18n.copyAll+'</a> <a id="mailto" href="'+baseURL+'m?links='+links+'" class="btn btn-info">'+i18n.mailTo+'</a>';
} else {
updateMailLink();
}
// Add the file to localStorage
addItem(data.name, url, data.size, del_at_first_view, created_at, delay, data.short, data.token);
// 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');
}
} else { } else {
updateMailLink(); j++;
// Update progress bar
var percent = Math.round(100 * j/parts);
dp.style.width = percent+'%';
dp.setAttribute('aria-valuenow', percent);
// Encrypt and upload next slice
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');
// Add the file to localStorage p.parentNode.remove();
addItem(data.name, url, data.size, del_at_first_view, created_at, delay, data.short, data.token); d.innerHTML = data.msg;
d.setAttribute('class', 'alert alert-danger');
n.parentNode.appendChild(d);
// 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);
document.getElementById('delete-day').removeAttribute('disabled'); document.getElementById('delete-day').removeAttribute('disabled');
document.getElementById('first-view').removeAttribute('disabled'); document.getElementById('first-view').removeAttribute('disabled');
} }
} else {
j++;
// Update progress bar
var percent = Math.round(100 * j/parts);
dp.style.width = percent+'%';
dp.setAttribute('aria-valuenow', percent);
// Encrypt and upload next slice
sliceAndUpload(key, i, parts, j, delay, del_at_first_view, short);
} }
} }
@ -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'