This repository has been archived on 2025-06-22. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
perl-dam/Dam/Components/Controls/Upload.pm
2020-04-10 12:48:19 +02:00

184 lines
5.1 KiB
Perl

=head1 NAME
Dam::Components::Controls::Upload
=head1 SYNOPSIS
my ($filehandle, $filename, $filetype) = Component__Get(CONTROL_UPLOAD, ['upload']);
=head1 DESCRIPTION
Control para subir un archivo al servidor.
=head1 ARGUMENTS
(
TYPE => 'Upload',
ID => 'upload' (default),
INFO => 1 (show info in header; default) or 0 (don't show),
LABEL => 'Upload file' (default),
LABEL_INFO => 'File' (default),
REQUIRED => 1 (control required; default) or 0 (not required)
)
=cut
use strict;
use warnings;
use utf8;
package Dam::Components::Controls::Upload;
use Exporter qw(import);
our @EXPORT = qw(
Control__html Control__js Get
);
use File::Copy qw(copy);
use Dam::Util;
use Dam::Debug;
use Dam::DamLogic;
use Dam::Var;
my $ID_DEFAULT = 'upload';
sub __arguments {
my $arg_ref = shift;
check_arguments($arg_ref,
TYPE => [ ARG_REQUIRED ],
ID => [ ARG_DEFAULT, $ID_DEFAULT ],
INFO => [ ARG_DEFAULT, 1, 0 ],
LABEL => [ ARG_DEFAULT, _t('Upload file') ],
LABEL_INFO => [ ARG_DEFAULT, _t('File') ],
REQUIRED => [ ARG_DEFAULT, 1, 0 ]
);
}
sub Control__html {
my ($self, $arg_ref, $info_ref) = @_;
__arguments($arg_ref);
# Previous file info:
my $filename = cgiapp_param(strval($$arg_ref{ID}, '_name')) || '';
my $filesafe = cgiapp_param(strval($$arg_ref{ID}, '_safe')) || '';
my $filetype = cgiapp_param(strval($$arg_ref{ID}, '_type')) || '';
if (!is_empty($filename)) {
report_info(_t('If no other file is selected, then <--file--> previously uploaded will be used.', file => strval('<strong>', $filename, '</strong>')));
}
push(@{$info_ref}, { DATA => $$arg_ref{LABEL_INFO}, VALUE => $filename }) if $$arg_ref{INFO} && !is_empty($filename);
return strval('
<div class="form-group form-group-', $ID_DEFAULT, '">
<label for="', $$arg_ref{ID}, $$arg_ref{REQUIRED} ? '" class="required">' : '">', $$arg_ref{LABEL}, '</label>
<div class="form-div form-control-', $ID_DEFAULT, '">
<input type="file" class="form-control input-lg" id="', $$arg_ref{ID}, '" name="', $$arg_ref{ID}, '"', $$arg_ref{REQUIRED} ? ' required="required"' : '', ' />
<input type="hidden" id="', $$arg_ref{ID}, '_name" name="', $$arg_ref{ID}, '_name" value="', $filename, '" />
<input type="hidden" id="', $$arg_ref{ID}, '_safe" name="', $$arg_ref{ID}, '_safe" value="', $filesafe, '" />
<input type="hidden" id="', $$arg_ref{ID}, '_type" name="', $$arg_ref{ID}, '_type" value="', $filetype, '" />
</div>
</div>
');
}
sub Control__js {
my ($self, $arg_ref) = @_;
__arguments($arg_ref);
return strval('
$(function(){
if ($("#', $$arg_ref{ID}, '_name").val()) {
$("#', $$arg_ref{ID}, '").prop("required", false);
}
});
');
}
sub Get {
my ($self, $id) = @_;
$id = $ID_DEFAULT if is_empty($id);
# File handler:
my $filehandle = cgiapp_upload($id);
# File name:
my $filename = cgiapp_param($id);
my $filesafe = $filename;
# File type:
my $filetype = !is_empty($filename) ? cgiapp_uploadInfo($filename)->{'Content-Type'} : undef;
if ($filehandle) {
my $safe_characters = "a-zA-Z0-9_.-";
$filesafe =~ tr/ /_/;
$filesafe =~ s/[^$safe_characters]//g;
if ($filesafe =~ /^([$safe_characters]+)$/) {
$filesafe = strval('file-', time(), '_', $1);
if (copy($filehandle, strval(CONFIG('DIR_UPLOADS'), '/', $filesafe)) && open($filehandle, '<', strval(CONFIG('DIR_UPLOADS'), '/', $filesafe))) {
cgiapp_param(strval($id, '_name'), $filename);
cgiapp_param(strval($id, '_safe'), $filesafe);
cgiapp_param(strval($id, '_type'), $filetype);
return ($filehandle, $filename, $filetype);
}
}
}
else {
$filename = cgiapp_param(strval($id, '_name'));
$filesafe = cgiapp_param(strval($id, '_safe'));
$filetype = cgiapp_param(strval($id, '_type'));
if (!is_empty($filesafe) && open($filehandle, '<', strval(CONFIG('DIR_UPLOADS'), '/', $filesafe))) {
return ($filehandle, $filename, $filetype);
}
}
return (undef, undef, undef);
}
1;
=head1 AUTHOR
Manuel Cillero C<< <manuel@cillero.es> >>
=head1 COPYRIGHT
The MIT License (MIT)
Copyright (c) 2004-2020 Manuel Cillero. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=cut