Commit be8c84ec authored by Luc Didry's avatar Luc Didry
Browse files

Use configured size's delay

parent 41cb17e2
......@@ -28,11 +28,29 @@ sub upload {
my @records = LufiDB::Files->select('WHERE short = ?', $json->{id});
$f = Lufi::File->new(record => $records[0]);
} else {
my $delay;
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
unless (defined $delay) {
$delay = ($json->{delay} <= $c->config('max_delay') || $c->config('max_delay') == 0) ? $json->{delay} : $c->config('max_delay');
}
$f = Lufi::File->new(
record => $c->get_empty,
created_by => $c->ip,
delete_at_first_view => ($json->{del_at_first_view}) ? 1 : 0,
delete_at_day => $json->{delay},
delete_at_day => $delay,
mediatype => $json->{type},
filename => $json->{name},
filesize => $json->{size},
......
......@@ -54,6 +54,7 @@
# size thresholds: if you want to define max delays for different sizes of file
# the keys are size in Bytes, you can't have 10*1000*10000 as key
# if a file is smaller than the smallest configured size, it will have a expiration delay of max_delay (see above)
# optional, default is using max_delay (see above) for all sizes
#delay_for_size => {
# 10000000 => 90, # between 10MB and 50MB => max is 90 days, less than 10MB => max is max_delay (see above)
......
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
window.fc = 0;
// Copy a link to clipboard
function copyToClipboard(el) {
el = el.previousSibling;
var textArea = document.createElement('textarea');
......@@ -31,6 +33,8 @@ function copyToClipboard(el) {
document.body.removeChild(textArea);
}
// Copy all links to clipboard
function copyAllToClipboard() {
var text = new Array();
var a = document.getElementsByClassName('link-input');
......@@ -67,6 +71,8 @@ function copyAllToClipboard() {
document.body.removeChild(textArea);
}
// Add item to localStorage
function addItem(name, url, size, del_at_first_view, created_at, delay, short, token) {
var files = localStorage.getItem('files');
if (files === null) {
......@@ -110,7 +116,7 @@ function uploadFile(i, delay, del_at_first_view) {
var r = document.getElementById('ul-results');
var w = document.createElement('li');
w.setAttribute('class', 'list-group-item');
w.innerHTML='<div><p id="name-'+i+'">'+file.name+'</p></div><div class="progress"><div id="progress-'+i+'" style="width: 0%;" data-key="'+randomkey+'" data-name="'+file.name+'" aria-valuemax="100" aria-valuemin="0" aria-valuenow="0" role="progressbar" class="progress-bar"><span class="sr-only">'+file.name+'0%</span></div></div>';
w.innerHTML='<div></a><p id="name-'+window.fc+'">'+file.name+'</p></div><div class="progress"><div id="progress-'+window.fc+'" style="width: 0%;" data-key="'+randomkey+'" data-name="'+file.name+'" aria-valuemax="100" aria-valuemin="0" aria-valuenow="0" role="progressbar" class="progress-bar"><span class="sr-only">'+file.name+'0%</span></div></div>';
r.appendChild(w);
sliceAndUpload(randomkey, i, parts, 0, delay, del_at_first_view, null);
......@@ -167,17 +173,18 @@ function updateProgressBar(data) {
var created_at = data.created_at;
var delay = data.delay;
var dp = document.getElementById('progress-'+i);
var dp = document.getElementById('progress-'+window.fc);
var key = dp.getAttribute('data-key');
if (j + 1 === parts) {
var n = document.getElementById('name-'+i);
var n = document.getElementById('name-'+window.fc);
var d = document.createElement('div');
var baseURL = document.location.href.replace(/#$/, '');
var url = baseURL+'r/'+short+'#'+key;
var del_url = baseURL+'d/'+short+'/'+data.token;
var links = encodeURIComponent('["'+short+'"]');
n.innerHTML = n.innerHTML+' <a href="'+baseURL+'m?links='+links+'"><span class="icon icon-mail"></span></a>';
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;
d.innerHTML = '<div class="form-group"><label class="sr-only" for="'
+short
+'">'
......@@ -231,6 +238,7 @@ function updateProgressBar(data) {
// Add the file to localStorage
addItem(data.name, url, data.size, del_at_first_view, created_at, delay, data.short, data.token);
window.fc++;
i++;
if (i < window.files.length) {
uploadFile(i, delay, del_at_first_view);
......
......@@ -9,15 +9,25 @@
<p><%= l('This server sets limitations according to the file size. The expiration delay of your file will be the minimum between what you choose and the following limitations:') %></p>
<ul>
% my $delays = config('delay_for_size');
% $delays->{0} = 1;
% $delays->{0} = config('max_delay');
% my @keys = sort keys %{$delays};
% my $i = 0;
% for my $key (@keys) {
% my $delay = $delays->{$keys[$i]};
% if ($i + 1 < scalar(@keys)) {
<li><%= l('between %1 and %2, the file will be kept %3 day(s)', format_bytes($keys[$i++]), format_bytes($keys[$i]), $delays->{$keys[$i]}) %></li>
% if ($delay) {
<li><%= l('between %1 and %2, the file will be kept %3 day(s).', format_bytes($keys[$i]), format_bytes($keys[$i + 1]), $delay) %></li>
% } else {
<li><%= l('between %1 and %2, the file will be kept forever.', format_bytes($keys[$i]), format_bytes($keys[$i + 1]), $delay) %></li>
% }
% } else {
<li><%= l('for %1 and more, the file will be kept %2 day(s)', format_bytes($keys[$i]), $delays->{$keys[$i]}) %></li>
% if ($delay) {
<li><%= l('for %1 and more, the file will be kept %2 day(s)', format_bytes($keys[$i]), $delay) %></li>
% } else {
<li><%= l('for %1 and more, the file will be kept forever.', format_bytes($keys[$i]), $delay) %></li>
% }
% }
% $i++;
% }
</ul>
</div>
......
......@@ -31,7 +31,7 @@
files.forEach(function(element, index, array) {
var del_view = (element.del_at_first_view) ? '<span class="glyphicon glyphicon-ok"></span>' : '<span class="glyphicon glyphicon-remove"></span>';
var dlink = '<%== url_for('/')->to_abs() %>d/'+element.short+'/'+element.token;
var limit = (element.limit === 0) ? '<%= l('No limit') %>' : moment.unix(element.delay * 86400 + element.created_at).locale(window.navigator.language).format('LLLL');
var limit = (element.delay === 0) ? '<%= l('No limit') %>' : moment.unix(element.delay * 86400 + element.created_at).locale(window.navigator.language).format('LLLL');
var created_at = moment.unix(element.created_at).locale(window.navigator.language).format('LLLL');
var tr = document.createElement('tr');
......
......@@ -63,18 +63,21 @@
</div>
</div>
%= javascript begin
var ws_url = '<%= url_for('upload')->to_abs() %>';
var ws_url = '<%= url_for('upload')->to_abs() %>';
var i18n = {
confirmExit: '<%= l('You have attempted to leave this page. The upload will be canceled. Are you sure?') %>',
copyAll: '<%= l('Copy all links to clipboard') %>',
cpText: '<%= l('Copy to clipboard') %>',
delText: '<%= l('Deletion link') %>',
dlText: '<%= l('Download link') %>',
download: '<%= l('Download') %>',
confirmExit: '<%= l('You have attempted to leave this page. The upload will be canceled. Are you sure?') %>',
expiration: '<%= l('Expiration:') %>',
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') %>',
mailTo: '<%= l('Send all links by email') %>',
}
noLimit: '<%= l('No limit') %>',
};
% end
%= javascript '/js/sjcl.js'
%= javascript '/js/moment-with-locales.min.js'
%= javascript '/js/lufi-up.js'
......@@ -66,7 +66,7 @@
links.forEach(function(name, index, array) {
var item = findItem(name);
if (item !== null) {
var limit = (item.limit === 0) ? null : moment.unix(item.delay * 86400 + item.created_at).locale(window.navigator.language).format('LLLL');
var limit = (item.delay === 0) ? null : moment.unix(item.delay * 86400 + item.created_at).locale(window.navigator.language).format('LLLL');
text = text+'- '+item.name+'<%= l(':') %> '+item.url;
if (limit !== null) {
text = text+"\n (<%= l('deadline: ') %>"+limit+')';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment