Add some checks and error messages

This commit is contained in:
Luc Didry 2015-10-03 22:55:55 +02:00
parent 9430bd2db7
commit 2863f008fd
2 changed files with 65 additions and 41 deletions

View File

@ -81,6 +81,18 @@ sub download {
$c->debug('Client connected'); $c->debug('Client connected');
my @records = LufiDB::Files->select('WHERE short = ?', $short); my @records = LufiDB::Files->select('WHERE short = ?', $short);
# Do we have a file?
if (scalar @records) {
# Is the file fully uploaded?
if ($records[0]->deleted) {
$c->on(
message => sub {
my ($ws, $json) = @_;
$c->send('{"success": false, "msg": "'.$c->l('Error: the file existed but has been deleted.').'"}');
}
);
} elsif ($records[0]->complete) {
my $f = Lufi::File->new(record => $records[0]); my $f = Lufi::File->new(record => $records[0]);
$c->on( $c->on(
@ -91,8 +103,11 @@ sub download {
# Make $num an integer instead of a string # Make $num an integer instead of a string
my $num = $json->{part} + 0; my $num = $json->{part} + 0;
# Get the slice
my $e = $f->slices->[$num]; my $e = $f->slices->[$num];
my $text = slurp $e->path; my $text = slurp $e->path;
# Send the slice
$c->send($text); $c->send($text);
} elsif (defined($json->{ended}) && $json->{ended}) { } elsif (defined($json->{ended}) && $json->{ended}) {
$f->counter($f->counter + 1); $f->counter($f->counter + 1);
@ -111,6 +126,17 @@ sub download {
$c->debug('Client disconnected'); $c->debug('Client disconnected');
} }
); );
} else {
$c->on(
message => sub {
my ($ws, $json) = @_;
$c->send('{"success": false, "msg": "'.$c->l('Error: the file has not been send entirely.').'"}');
}
);
}
} else {
$c->send('{"success": false, "msg": "'.$c->l('Error: unable to find the file. Are you sure of your URL?').'"}');
}
} }
sub r { sub r {
@ -120,16 +146,13 @@ sub r {
my @records = LufiDB::Files->select('WHERE short = ?', $short); my @records = LufiDB::Files->select('WHERE short = ?', $short);
if (scalar @records) { if (scalar @records) {
my $f = Lufi::File->new(record => $records[0]); my $f = Lufi::File->new(record => $records[0]);
my $msg = $c->l('The file has been deleted and is no more available.') if $f->deleted;
return $c->render( return $c->render(
template => 'render', template => 'render',
f => $f, f => $f
msg => $msg
); );
} else { } else {
return $c->render( return $c->render(
template => 'render', template => 'render',
f => undef,
msg => $c->l('Could not find the file. Are you sure of the URL?') msg => $c->l('Could not find the file. Are you sure of the URL?')
); );
} }
@ -148,14 +171,15 @@ sub get_counter {
counter => $records[0]->counter counter => $records[0]->counter
} }
); );
} } else {
return $c->render( return $c->render(
json => { json => {
success => false, success => false,
msg => $c->l('Unable to get counter') msg => $c->l('Unable to get counter for %1. The file does not exists.', $short)
} }
); );
} }
}
sub delete { sub delete {
my $c = shift; my $c = shift;

View File

@ -1,11 +1,11 @@
% # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab: % # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab:
<div class="inner cover render"> <div class="inner cover render">
% if (!defined($f) && defined($msg)) { % if (!defined(stash('f')) && defined(stash('msg'))) {
<div class="alert alert-danger"><%= $msg %></div> <div class="alert alert-danger"><%= stash('msg') %></div>
% } else { % } else {
<h2><%= stash('f')->filename %></h2> <h2><%= stash('f')->filename %></h2>
% if (defined($msg)) { % if (defined(stash('msg'))) {
<div class="alert alert-danger"><%= $msg %></div> <div class="alert alert-danger"><%= stash('msg') %></div>
% } else { % } else {
<p id="please-wait"><%= l('Please wait while we are getting your file') %></p> <p id="please-wait"><%= l('Please wait while we are getting your file') %></p>
<div class="progress" id="pbd"> <div class="progress" id="pbd">