jQuery-File-Upload
view release on metacpan or search on metacpan
lib/jQuery/File/Upload.pm view on Meta::CPAN
package jQuery::File::Upload;
use 5.008008;
use strict;
#use warnings;
use CGI;
use JSON::XS;
use JSON;
use Net::SSH2;
use Net::SSH2::SFTP;
use Image::Magick;
use Cwd 'abs_path';
use URI;
use Data::GUID;
#use LWP::UserAgent;
#use LWP::Protocol::https;
our $VERSION = '0.32';
my %errors = (
'_validate_max_file_size' => 'File is too big',
'_validate_min_file_size' => 'File is too small',
'_validate_accept_file_types' => 'Filetype not allowed',
'_validate_reject_file_types' => 'Filetype not allowed',
'_validate_max_number_of_files' => 'Maximum number of files exceeded',
'_validate_max_width' => 'Image exceeds maximum width',
'_validate_min_width' => 'Image requires a minimum width',
'_validate_max_height' => 'Image exceeds maximum height',
'_validate_min_height' => 'Image requires a minimum height'
);
#GETTERS/SETTERS
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
field_name => 'files[]',
ctx => undef,
cgi => undef,
thumbnail_width => 80,
thumbnail_height => 80,
thumbnail_quality => 70,
thumbnail_format => 'jpg',
thumbnail_density => undef,
format => 'jpg',
quality => 70,
process_images => 1,
thumbnail_filename => undef,
thumbnail_prefix => 'thumb_',
thumbnail_postfix => '',
filename => undef,
client_filename => undef,
show_client_filename => 1,
use_client_filename => undef,
filename_salt => '',
copy_file => 0,
script_url => undef,
tmp_dir => '/tmp',
should_delete => 1,
absolute_filename => undef,
absolute_thumbnail_filename => undef,
delete_params => [],
upload_dir => undef,
thumbnail_upload_dir => undef,
upload_url_base => undef,
thumbnail_url_base => undef,
lib/jQuery/File/Upload.pm view on Meta::CPAN
if($self->use_client_filename) {
$self->filename($self->client_filename);
}
else {
my $filename = Data::GUID->new->as_string . $self->filename_salt;
$self->thumbnail_filename($self->thumbnail_prefix . $filename . $self->thumbnail_postfix . '.' . $self->thumbnail_format) unless $self->thumbnail_filename;
if($self->is_image) {
$filename .= '.' . $self->format;
}
else {
#add extension if present
if($self->client_filename =~ qr/.*\.(.*)/) {
$filename .= '.' . $1;
}
}
$self->filename($filename) unless $self->filename;
}
return $self->filename;
}
sub _set_absolute_filenames {
my $self = shift;
$self->absolute_filename($self->upload_dir . '/' . $self->filename) unless $self->absolute_filename;
$self->absolute_thumbnail_filename($self->thumbnail_upload_dir . '/' . $self->thumbnail_filename) unless $self->absolute_thumbnail_filename;
}
sub _set_file_type {
my $self = shift;
if(defined $self->ctx) {
$self->{file_type} = $self->{upload}->type;
}
else {
$self->{file_type} = $self->cgi->uploadInfo($self->client_filename)->{'Content-Type'};
}
return $self->{file_type};
}
sub _set_is_image {
my $self = shift;
if($self->process_images and ($self->process_images and ($self->{file_type} eq 'image/jpeg' or $self->{file_type} eq 'image/jpg' or $self->{file_type} eq 'image/png' or $self->{file_type} eq 'image/gif'))) {
$self->{is_image} = 1;
}
else {
$self->{is_image} = 0;
}
return $self->is_image;
}
sub _set_image_magick {
my $self = shift;
return unless $self->is_image;
#if used in persistent setting, don't recreate object
$self->{image_magick} = Image::Magick->new unless defined $self->{image_magick};
$self->{image_magick}->Read(file => $self->{fh});
return $self->{image_magick};
}
sub _set_width {
my $self = shift;
return unless $self->is_image;
$self->{width} = $self->{image_magick}->Get('width');
}
sub _set_height {
my $self = shift;
return unless $self->is_image;
$self->{height} = $self->{image_magick}->Get('height');
}
sub _set_tmp_filename {
my $self = shift;
my $tmp_filename;
if(defined $self->ctx) {
$self->{tmp_filename} = $self->{upload}->tempname;
}
else {
$self->{tmp_filename} = $self->cgi->tmpFileName($self->client_filename);
}
}
sub _set_upload_obj {
my $self = shift;
if(defined $self->ctx) {
$self->{upload} = $self->ctx->request->upload($self->field_name);
}
else {
$self->{upload} = $self->cgi->upload($self->field_name);
}
return defined $self->{upload};
}
sub _set_fh {
my $self = shift;
if(defined $self->ctx) {
$self->{fh} = $self->{upload}->fh;
}
else {
$self->{fh} = $self->{upload};
}
return $self->{fh};
}
sub _set_num_files_in_dir {
my $self = shift;
lib/jQuery/File/Upload.pm view on Meta::CPAN
=head3 reject_file_types
#None of these types are allowed.
$j_fu->reject_file_types(['image/jpeg','image/png','image/gif','text/html']);
Sets what file types are NOT allowed to be uploaded. By default, all file types are allowed.
File types should be in the format of the Content-Type header sent on requests.
=head3 require_image
$j_fu->require_image(1);
If set to 1, it requires that all uploads must be an image. Setting this is equivalent
to calling:
$j_fu->accept_file_types(['image/jpeg','image/jpg','image/png','image/gif']);
Default is undef.
=head3 delete_params
$j_fu->delete_params(['key1','val1','key2','val2']);
Sets the keys and values of the params added to the delete_url.
This can be useful when used with L<pre_delete|/"pre_delete">,
because if you are keeping track of these files in a database,
you can add unique identifiers to the params so that in L<pre_delete|/"pre_delete">
you can get these unique identifiers and use them to remove or edit the file
in the databse. By default filename will also be a param unless you
set the delete_url manually.
=head3 delete_url
$j_fu->delete_url('http://www.mydomain.com/upload.cgi?filename=file.jpg');
This can be used to set the delete_url that will be requested when
a user deletes a file. However, it is recommended that you do not
set this manually and rather use L<delete_params|/"delete_params">
if you want to add your own params to the delete_url.
=head3 thumbnail_width
$j_fu->thumbnail_width(80);
This sets the width for the thumbnail that will be created if the
file is an image. Default is 80.
=head3 thumbnail_height
$j_fu->thumbnail_height(80);
This sets the height for the thumbnail that will be created if the
file is an image. Default is 80.
=head3 thumbnail_quality
$j_fu->thumbnail_quality(70);
This sets the quality of the thumbnail image. Default is 70 and it
can be on a scale of 0-100. See L<Image::Magick> for more information.
=head3 thumbnail_format
$j_fu->thumbnail_format('jpg');
Sets the format for the generated thumbnail. Can be jpg, png, or gif.
See L<Image::Magick> for more information. Defaults to jpg.
=head3 thumbnail_density
$j_fu->thumbnail_density('80x80');
Sets the density for the generated thumbnail. Default is width x height.
See L<Image::Magick> for more information.
=head3 thumbnail_prefix
$j_fu->thumbnail_prefix('thumb_');
Added before the image filename to create the thumbnail unique filename.
Default is 'thumb_'.
=head3 thumbnail_postfix
$j_fu->thumbnail_postfix('_thumb');
Added after the image filename to create the thumbnail unique filename.
Default is ''.
=head3 thumbnail_final_width
my $final_width = $j_fu->thumbnail_final_width;
Because the thumbnails are scaled proportionally, the thumbnail width
may not be what you orignally suggested. This gets you the final width.
=head3 thumbnail_final_height
my $final_height = $j_fu->thumbnail_final_height;
Because the thumbnails are scaled proportionally, the thumbnail height
may not be what you orignally suggested. This gets you the final height.
=head3 quality
$j_fu->quality(70);
This sets the quality of the uploaded image. Default is 70 and it
can be on a scale of 0-100. See L<Image::Magick> for more information.
=head3 process_images
$j_fu->process_images(1);
Create thumbnails for uploaded image files when set to 1. When set to undef, L<jQuery::File::Upload> will skip creating
thumbnails even when the uploaded file is an image. Images are simply treated like any other file. The default is 1.
=head3 format
$j_fu->format('jpg');
Sets the format for the generated thumbnail. Can be jpg,png, or gif.
See L<Image::Magick> for more information. Defaults to jpg.
=head3 final_width
my $final_width = $j_fu->final_width;
Returns the final width of the uploaded image.
=head3 final_height
my $final_height = $j_fu->final_height;
Returns the final height of the uploaded image.
=head3 max_width
$j_fu->max_width(10000);
Sets the maximum width of uploaded images. Will return an error to browser if not
valid. Default is any width.
=head3 max_height
$j_fu->max_height(10000);
Sets the maximum height of uploaded images. Will return an error to browser if not
valid. Default is any height.
=head3 min_width
$j_fu->min_width(10000);
Sets the minimum width of uploaded images. Will return an error to browser if not
valid. Default is 1.
=head3 min_height
$j_fu->min_height(10000);
Sets the minimum height of uploaded images. Will return an error to browser if not
valid. Default is 1.
=head3 max_number_of_files
$j_fu->max_number_of_files(20);
Sets the maximum number of files the upload directory can contain. Returns an error
to the browser if number is reached. Default is any number of files. If you have
listed multiple remote directories, the maximum file count out of all of these directories
is what will be used.
=head3 filename
my $filename = $j_fu->filename;
Returns the resulting filename after processing the request.
$j_fu->filename('my_name.txt');
You can also set the filename to use for this request before you call
L<handle_request|/"handle_request">. However, unless you're sure
lib/jQuery/File/Upload.pm view on Meta::CPAN
files to prepopulate the form with. This method can
be useful to prepopulate the jQuery File Upload form
by combining saved information about the files you want to
load and using L<generate_output|/"generate_output"> to
prepare the output that you would like to send to the
jQuery File Upload form.
=head3 post_get
$j_fu->post_get(sub { my $j_fu = shift });
or
$j_fu->post_get(\&mysub);
post_get will be called after a get request is handled.
=head2 EXPORT
None by default.
=head1 Catalyst Performance - Persistent jQuery::File::Upload
A jQuery::File::Upload object shouldn't be too expensive to create, however
if you'd like to only create the object once you could create it as an
L<Moose> attribute to the class:
use jQuery::File::Upload;
has 'j_uf' => (isa => 'jQuery::File::Upload', is => 'rw',
lazy => 0, default => sub { jQuery::File::Upload->new } );
However, if you do this it is possible that you could run into issues
with values of the jQuery::File::Upload object that were not cleared
messing with the current request. The _clear method is called before
every L<handle_request|/"handle_request"> which clears the values of
the jQuery::File::Upload object, but it's possible I may have
missed something.
=head1 SEE ALSO
=over 4
=item
L<CGI>
=item
L<JSON::XS>
=item
L<Net::SSH2>
=item
L<Net::SSH2::SFTP>
=item
L<Image::Magick>
=item
L<Cwd>
=item
L<Digest::MD5>
=item
L<URI>
=item
L<jQuery File Upload|https://github.com/blueimp/jQuery-File-Upload/>
=back
=head1 AUTHOR
Adam Hopkins, E<lt>srchulo@cpan.org>
=head1 Bugs
I haven't tested this too thoroughly beyond my needs, so it is possible
that I have missed something. If I have, please feel free to submit a bug
to the bug tracker, and you can send me an email letting me know that you
submitted a bug if you want me to see it sooner :)
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2013 by Adam Hopkins
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.
=cut
( run in 1.649 second using v1.01-cache-2.11-cpan-e93a5daba3e )