view release on metacpan or search on metacpan
'Readonly'=>0,
'Data::FormValidator'=>0,
'GD'=>0,
'autodie'=>0,
},
meta_merge => {
resources => {
repository =>
'http://github.com/periapt/CGI-Application-Plugin-AJAXUpload/tree',
},
keywords => [ 'CGI::Application', 'File upload', 'Image resizing', 'HTML', 'YUI rich text editor' ],
},
create_makefile_pl => 'traditional',
create_readme=>1,
add_to_cleanup => [ 'CGI-Application-Plugin-AJAXUpload-*' ],
);
$builder->create_build_script();
---
abstract: 'Run mode to handle a file upload and return a JSON response'
author:
- 'Nicholas Bamber <nicholas@periapt.co.uk>'
build_requires:
CGI::Application::Plugin::JSON: 0
File::Temp: 0
Test::CGI::Multipart: 0
Test::Image::GD: 0
Test::More: 0.94
Test::NoWarnings: 0
Test::Warn: 0
configure_requires:
Module::Build: 0.36
generated_by: 'Module::Build version 0.3607'
keywords:
- CGI::Application
- 'File upload'
- 'Image resizing'
- HTML
- 'YUI rich text editor'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
name: CGI-Application-Plugin-AJAXUpload
provides:
CGI::Application::Plugin::AJAXUpload:
NAME
CGI::Application::Plugin::AJAXUpload - Run mode to handle a file upload
and return a JSON response
VERSION
This document describes CGI::Application::Plugin::AJAXUpload version
0.0.3
SYNOPSIS
use MyWebApp;
use CGI::Application::Plugin::JSON qw(to_json);
use CGI::Application::Plugin::AJAXUpload;
sub setup {
my $c = shift;
$c->ajax_upload_httpdocs('/var/www/vhosts/mywebapp/httpdocs');
$c->ajax_upload_setup(
run_mode=>'file_upload',
upload_subdir=>'/img/uploads',
);
return;
}
DESCRIPTION
This module provides a customisable run mode that handles a file upload
and responds with a JSON message like the following:
{status: 'UPLOADED', image_url: '/img/uploads/666.png'}
or on failure
{status: 'The image was too big.'}
This is specifically intended to provide a CGI::Application based back
end for <AllMyBrain.com>'s <image upload extension> to the <YUI rich
text editor>. However as far as I can see it could be used as a back end
for any CGI::Application website that uploads files behind the scenes
using AJAX. In any case this module does NOT provide any of that client
side code and you must also map the run mode onto the URL used by
client-side code. That said a working example is provided which could
form the basis of a rich text editor.
INTERFACE
ajax_upload_httpdocs
The module needs to know the document root because it will need to to
copy the file to a sub-directory of the document root, and it will need
to pass that sub-directory back to the client as part of the URL. If
passed a value it will store that as the document root. If not passed a
value it will return the document root.
ajax_upload_setup
This method sets up a run mode to handle a file upload and return a JSON
message providing status. It takes a number of named parameters:
upload_subdir
This is the sub-directory of *httpdocs_dir* where the files will
actually be written to. It must be writeable. It defaults to
'/img/uploads'.
dfv_profile
This is a Data::FormValidator profile. The hash array that is
validated consists of the fields described below. A very basic
profile is provided by default.
*value* This is contains the actual data contained in the upload. It
will be untainted. One can of course apply filters that resize the
image (assuming it is an image) or scrub the HTML (if that is
appropriate).
*file_name* This is the filename given by the browser. By default it
will be required to be no more than 30 alphanumeric, hyphen or full
stop, underscore characters; it will be untainted and passed through
unmodified. One could however specify a filter that completely
ignores the filename, generates a safe one and does other
housekeeping.
*mime_type* This is the file extension passed by the browser.
*data_size* By default this is required to be less than 512K.
Note that this module's handling of file upload and data validation
is somewhat different from that expected by
Data::FormValidator::Constraints::Upload and
Data::FormValidator::Filters::Image. Those modules work with file
handles. The Data::FormValidator profiles required by this module
are expected to work with the data and meta data.
run_mode
This is the name of the run mode that will handle this upload. It
defaults to *ajax_upload_rm*.
ajax_upload_default_profile
This returns a hash reference to the default Data::FormValidator
profile. It can be called as a class method.
_ajax_upload_rm
This private method forms the implementation of the run mode. It
requires a *file* CGI query parameter that provides the file data.
Optionally it also takes a *validate* parameter that will make other
more paranoid checks. These checks are only optional because if the
system is set up correctly they should never fail.
It takes the following actions:
-- It will get the filename and data associated with the upload and
pass the data through the Data::FormValidator if a profile is
supplied.
-- If it fails the Data::FormValidator test a failed message will be
passed back to the caller.
-- If the *validate* parameter is set the setup will check. If there is
a problem a status message will be passed back to the user.
-- The data will then be copied to the given file, its path being the
combination of the *httpdocs_dir* parameter, the *upload_subdir* and
the generated file name.
- The successful JSON message will be passed back to the client.
DIAGNOSTICS
Most error messages will be passed back to the client as a JSON message,
though in a sanitised form. One error 'Internal Error' is fairly generic
and so the underlying error message is written to standard error.
CONFIGURATION AND ENVIRONMENT
environment variables. However the client side code, the URL to run mode
dispatching and the general web server setup is not supplied.
DEPENDENCIES
This is using the "to_json" method from CGI::Application::Plugin::JSON.
As such that module needs to be exported before this module. Or of
course you could just define your own.
BUGS AND LIMITATIONS
Please report any bugs or feature requests to
"bug-cgi-application-plugin-ajaxupload@rt.cpan.org", or through the web
interface at <http://rt.cpan.org>.
One really odd thing is that the content header of the AJAX reply cannot
be 'application/json' as one would expect. This module sets it to
'text/javascript' which works. There is a very short discussion on the
<YUI forum>.
AUTHOR
Nicholas Bamber "<nicholas@periapt.co.uk>"
example/httpdocs/js/editor.js view on Meta::CPAN
catch(l) {
alert(l.message);
}
});
this.toolbar.on ('insertimageClick', function(o) {
try {
var imgPanel=new YAHOO.util.Element(elementId + '-panel');
imgPanel.on ( 'contentReady', function() {
try {
var Dom=YAHOO.util.Dom;
if (! Dom.get(elementId + '_insertimage_upload')) {
var label=document.createElement('label');
label.innerHTML='<strong>Upload:</strong><input type="file" id="' +
elementId + '_insertimage_upload" name="file" size="10" style="width: 300px" /><input type="hidden" name="rm" value="ajax_upload_rm"/>';
var img_elem=Dom.get(elementId + '_insertimage_url');
Dom.getAncestorByTagName(img_elem, 'form').encoding = 'multipart/form-data';
Dom.insertAfter(label, img_elem.parentNode);
var labels = Dom.getElementsBy(
function(o) {
return true;
},
'label',
example/httpdocs/js/editor.js view on Meta::CPAN
}
return false;
},
'input',
labels[l]
).length > 0) {
labels[l].className="hide";
}
}
YAHOO.util.Event.on ( elementId + '_insertimage_upload', 'change', function(ev) {
YAHOO.util.Event.stopEvent(ev); // no default click action
YAHOO.util.Connect.setForm ( img_elem.form, true);
var c=YAHOO.util.Connect.asyncRequest(
'POST',
'/cgi-bin/template.cgi', {
upload: function(o) {
var resp=o.responseText.replace( /<pre>/i, '').replace ( /<\/pre>/i, '');
var data = YAHOO.lang.JSON.parse(resp);
if (data.status == "UPLOADED") {
Dom.get(elementId + '_insertimage_upload').value='';
Dom.get(elementId + '_insertimage_url').value=data.image_url;
// tell the image panel the url changed
// hack instead of fireEvent('blur')
// which for some reason isn't working
Dom.get(elementId + '_insertimage_url').focus();
Dom.get(elementId + '_insertimage_upload').focus();
}
else {
alert(data.status);
}
},
}
);
return false;
});
example/template.cgi view on Meta::CPAN
#!/usr/bin/perl
#
# Sample application
#
# To get this working you need to copy the relevant files to their correct places.
# This file (template.cgi) to the cgi-bin directory.
# The templates directory to your preferred template location and change $TEMPLATE_DIR accordingly.
# The httpdocs directory contents will need to be copied and the value in
# ajax_upload_httpdocs updated accordingly.
# The directory corresponding to he /img/uploads needs to be writeable.
# You can of course change that location.
# You will of course need to install vaious modules - not all of which
# are dependencies of the module.
# You will also require internet access as the web page loads a lot of YUI code.
#
use strict;
use warnings;
use Readonly;
# This bit needs to be modified for the local system.
example/template.cgi view on Meta::CPAN
use CGI::Application::Plugin::JSON qw(json_body to_json);
use CGI::Application::Plugin::AJAXUpload;
use CGI::Application::Plugin::ValidateRM;
use Data::FormValidator::Filters::ImgData;
use CGI::Carp qw(fatalsToBrowser);
sub setup {
my $self = shift;
$self->start_mode('one');
$self->ajax_upload_httpdocs('/var/www/vhosts/editor/httpdocs');
my $profile = $self->ajax_upload_default_profile;
$profile->{field_filters}->{value} =
filter_resize($IMAGE_WIDTH,$IMAGE_HEIGHT);
$self->ajax_upload_setup(dfv_profile=>$profile);
}
sub one : Runmode {
my $self = shift;
my $tmpl_obj = $self->load_tmpl('one.tmpl');
return $tmpl_obj->output;
}
sub two : Runmode {
my $c = shift;
lib/CGI/Application/Plugin/AJAXUpload.pm view on Meta::CPAN
use warnings;
use strict;
use Carp;
use base qw(Exporter);
use vars qw(@EXPORT);
use Perl6::Slurp;
use Readonly;
use Data::FormValidator;
@EXPORT = qw(
ajax_upload_httpdocs
ajax_upload_setup
ajax_upload_default_profile
_ajax_upload_rm
_ajax_upload_compile_messages
);
use version; our $VERSION = qv('0.0.3');
# Module implementation here
Readonly my $FIELD_NAME => 'file';
Readonly my $MAX_UPLOAD => 512*1024;
sub ajax_upload_httpdocs {
my $self = shift;
my $httpdocs = shift;
if ($httpdocs) {
$self->{__CAP__AJAXUPLOAD_HTTPDOCS} = $httpdocs;
return;
}
return $self->{__CAP__AJAXUPLOAD_HTTPDOCS};
}
sub ajax_upload_setup {
my $self = shift;
my %args = @_;
my $upload_subdir = $args{upload_subdir} || '/img/uploads';
my $dfv_profile = $args{dfv_profile};
if (!$dfv_profile) {
$dfv_profile = $self->ajax_upload_default_profile();
}
my $run_mode = $args{run_mode} || 'ajax_upload_rm';
$self->run_modes(
$run_mode => sub {
my $c = shift;
$c->header_props(
-type=>'text/javascript',
-encoding=>'utf-8',
-charset=>'utf-8'
);
my $r = eval {
$c->_ajax_upload_rm($upload_subdir, $dfv_profile);
};
if ($@) {
carp $@;
return $c->to_json({status=> 'Internal Error'});
}
return $r;
}
);
return;
}
sub _ajax_upload_rm {
use autodie qw(open close);
my $self = shift;
my $upload_subdir = shift;
my $dfv_profile = shift;
my $httpdocs_dir = $self->ajax_upload_httpdocs;
return $self->to_json({status => 'No document root specified'})
if not defined $httpdocs_dir;
my $full_upload_dir = "$httpdocs_dir/$upload_subdir";
my $query = $self->query;
my $lightweight_fh = $query->upload('file');
return $self->to_json({status=>'No file handle obtained'})
if !defined $lightweight_fh;
my $fh = $lightweight_fh->handle;
return $self->to_json({status => 'No file handle promoted'})
if not $fh;
my $value = slurp $fh;
close $fh;
my $filename = $query->param('file');
my $info = $query->uploadInfo($filename);
return $self->to_json({status => 'No file name obtained'})
if not $filename;
$filename = "$filename"; # force $filename to be a strict string
my $mime_type = 'text/plain';
if ($info and exists $info->{'Content-Type'}) {
$mime_type = $info->{'Content-Type'};
}
my $data = {
value => $value,
file_name => $filename,
mime_type => $mime_type,
data_size => length $value,
};
my $results = Data::FormValidator->check($data, $dfv_profile);
return $self->_ajax_upload_compile_messages($results->msgs)
if ! $results->success;
$value = $results->valid('value');
$filename = $results->valid('file_name');
if ($query->param('validate')) {
return $self->to_json({status => 'Document root is not a directory'})
if not -d $httpdocs_dir;
return $self->to_json({status => 'Upload folder is not a directory'})
if not -d $full_upload_dir;
return $self->to_json({status => 'Upload folder is not writeable'})
if not -w $full_upload_dir;
return $self->to_json({status => 'No data uploaded'})
if not $value;
}
open $fh, '>', "$full_upload_dir/$filename";
print {$fh} $value;
close $fh;
return $self->to_json({
status=>'UPLOADED',
image_url=>"$upload_subdir/$filename"
});
}
sub _ajax_upload_compile_messages {
my $self = shift;
my $msgs = shift;
my $text = '';
foreach my $key (keys %$msgs) {
$text .= "$key: $msgs->{$key}, ";
}
return $self->to_json({status=>$text});
}
sub ajax_upload_default_profile {
return {
required=>[qw(value file_name mime_type data_size)],
untaint_all_constraints=>1,
constraint_methods => {
value=>qr{\A.+\z}xms,
file_name=>qr/^[\w\.\-\_]{1,30}$/,
data_size=>sub {
my ($dfv, $val) = @_;
$dfv->set_current_constraint_name('data_size');
return $val < $MAX_UPLOAD;
lib/CGI/Application/Plugin/AJAXUpload.pm view on Meta::CPAN
format => '%s',
},
};
}
1; # Magic true value required at end of module
__END__
=head1 NAME
CGI::Application::Plugin::AJAXUpload - Run mode to handle a file upload and return a JSON response
=head1 VERSION
This document describes CGI::Application::Plugin::AJAXUpload version 0.0.3
=head1 SYNOPSIS
use MyWebApp;
use CGI::Application::Plugin::JSON qw(to_json);
use CGI::Application::Plugin::AJAXUpload;
sub setup {
my $c = shift;
$c->ajax_upload_httpdocs('/var/www/vhosts/mywebapp/httpdocs');
$c->ajax_upload_setup(
run_mode=>'file_upload',
upload_subdir=>'/img/uploads',
);
return;
}
=head1 DESCRIPTION
This module provides a customisable run mode that handles a file upload
and responds with a JSON message like the following:
{status: 'UPLOADED', image_url: '/img/uploads/666.png'}
or on failure
{status: 'The image was too big.'}
This is specifically intended to provide a L<CGI::Application> based back
end for L<AllMyBrain.com|http://allmybrain.com>'s
L<image upload extension|http://allmybrain.com/2007/10/16/an-image-upload-extension-for-yui-rich-text-editor> to the
L<YUI rich text editor|http://developer.yahoo.com/yui/editor>. However as far as
I can see it could be used as a back end for any L<CGI::Application> website that uploads files behind the scenes using AJAX. In any case this module does NOT
provide any of that client side code and you must also map the run mode onto the URL used by client-side code.
That said a working example is provided which could form the basis of
a rich text editor.
=head1 INTERFACE
=head2 ajax_upload_httpdocs
The module needs to know the document root because it will need to
to copy the file to a sub-directory of the document root,
and it will need to pass that sub-directory back to the client as part
of the URL. If passed a value it will store that as the document root.
If not passed a value it will return the document root.
=head2 ajax_upload_setup
This method sets up a run mode to handle a file upload
and return a JSON message providing status. It takes a number of named
parameters:
=over
=item upload_subdir
This is the sub-directory of I<httpdocs_dir> where the files will actually
be written to. It must be writeable. It defaults to '/img/uploads'.
=item dfv_profile
This is a L<Data::FormValidator> profile. The hash array that is validated
consists of the fields described below. A very basic profile is provided by
default.
=over 4
=item I<value> This is contains the actual data contained in the upload. It will
be untainted. One can of course apply filters that resize the image (assuming
it is an image) or scrub the HTML (if that is appropriate).
=item I<file_name> This is the filename given by the browser. By default it will
be required to be no more than 30 alphanumeric, hyphen or full stop,
underscore characters; it will be untainted and passed through unmodified. One
could however specify a filter that completely ignores the filename, generates
a safe one and does other housekeeping.
=item I<mime_type> This is the file extension passed by the browser.
=item I<data_size> By default this is required to be less than 512K.
=back
Note that this module's handling of file upload and data validation is
somewhat different from that expected by
L<Data::FormValidator::Constraints::Upload> and
L<Data::FormValidator::Filters::Image>. Those modules work with file handles.
The L<Data::FormValidator> profiles required by this module are expected
to work with the data and meta data.
=item run_mode
This is the name of the run mode that will handle this upload. It defaults to
I<ajax_upload_rm>.
=back
=head2 ajax_upload_default_profile
This returns a hash reference to the default L<Data::FormValidator>
profile. It can be called as a class method.
=head2 _ajax_upload_rm
This private method forms the implementation of the run mode. It requires a
I<file> CGI query parameter that provides the file data. Optionally it also
takes a I<validate> parameter that will make other more paranoid checks.
These checks are only optional because if the system is set up correctly
they should never fail.
It takes the following actions:
=over
=item --
It will get the filename and data associated with the upload and
pass the data through the L<Data::FormValidator> if a profile is
supplied.
=item --
If it fails the L<Data::FormValidator> test a failed message will be passed
back to the caller.
=item --
If the I<validate> parameter is set the setup will check. If there
is a problem a status message will be passed back to the user.
=item --
The data will then be copied to the given file, its path being the
combination of the I<httpdocs_dir> parameter, the
I<upload_subdir> and the generated file name.
=item -
The successful JSON message will be passed back to the client.
=back
=head1 DIAGNOSTICS
Most error messages will be passed back to the client as a JSON
lib/CGI/Application/Plugin/AJAXUpload.pm view on Meta::CPAN
=head1 DEPENDENCIES
This is using the C<to_json> method from L<CGI::Application::Plugin::JSON>.
As such that module needs to be exported before this module. Or of course you
could just define your own.
=head1 BUGS AND LIMITATIONS
Please report any bugs or feature requests to
C<bug-cgi-application-plugin-ajaxupload@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
One really odd thing is that the content header of the AJAX reply cannot
be 'application/json' as one would expect. This module sets it to
'text/javascript' which works. There is a very short discussion on the
L<YUI forum|http://yuilibrary.com/forum/viewtopic.php?f=89&t=4743&p=16459&hilit=POST+connection#p16459>.
=head1 AUTHOR
Nicholas Bamber C<< <nicholas@periapt.co.uk> >>
lib/Data/FormValidator/Filters/ImgData.pm view on Meta::CPAN
=head1 SYNOPSIS
use MyWebApp;
use CGI::Application::Plugin::JSON qw(to_json);
use CGI::Application::Plugin::AJAXUpload;
use Data::FormValidator::Filters::ImgData;
sub setup {
my $c = shift;
$c->ajax_upload_httpdocs('/var/www/vhosts/mywebapp/httpdocs');
my $profile = $c->ajax_upload_default_profile;
$profile->{field_filters}->{value} = filter_resize(300,200);
$c->ajax_upload_setup(
run_mode=>'file_upload',
upload_subdir=>'/img/uploads',
dfv_profile=>$profile,
);
return;
}
=head1 DESCRIPTION
This module rewrites and formats an image. It is intended specifically to work
with L<CGI::Application::Plugin::AJAXUpload> and hence
L<Data::FormValidator>. Unlike, for example L<Data::FormValidator::Filters::Image>,
lib/Data/FormValidator/Filters/ImgData.pm view on Meta::CPAN
The core of the code is copied from L<Image::Resize>. However the constructor
for that module does not take raw data.
=head1 DEPENDENCIES
This module uses the L<GD> module.
=head1 BUGS AND LIMITATIONS
Please report any bugs or feature requests to
C<bug-cgi-application-plugin-ajaxupload@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head1 AUTHOR
Nicholas Bamber C<< <nicholas@periapt.co.uk> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2010, Nicholas Bamber C<< <nicholas@periapt.co.uk> >>. All rights reserved.
t/02.warnings.t view on Meta::CPAN
use File::Temp;
use TestWebApp;
Readonly my $CONTENT_RE =>
qr{
\A
Encoding:\s+utf-8\s+Content-Type:\s+text/javascript
(?:;\s+charset=utf-8)?
}xms;
my $profile = TestWebApp->ajax_upload_default_profile();
$profile->{constraint_methods}->{mime_type} = qr{^text/plain$};
$profile->{required} = [qw(value file_name data_size)];
$profile->{optional} = [qw(mime_type)];
sub nonexistent_dir {
my $new_dir = File::Temp->newdir;
return $new_dir->dirname;
}
sub valid_dir {
my $tmpdir = File::Temp->newdir;
my $tmpdir_name = $tmpdir->dirname;
mkdir "$tmpdir_name/img";
mkdir "$tmpdir_name/img/uploads";
return $tmpdir;
}
my $tcm = Test::CGI::Multipart->new;
$tcm->set_param(name=>'rm', value=>'ajax_upload_rm');
$tcm->upload_file(name=>'file', value=>'This is a test!',file=>'test.txt');
subtest 'httpdocs_dir not specified' => sub{
plan tests => 3;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {},
ajax_spec=> {
dfv_profile=>$profile
},
t/02.warnings.t view on Meta::CPAN
);
};
subtest 'httpdocs_dir does not exist' => sub{
plan tests => 3;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs(nonexistent_dir());
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
t/02.warnings.t view on Meta::CPAN
};
subtest 'httpdocs_dir not a directory' => sub{
plan tests => 3;
my $actually_a_file = File::Temp->new;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($actually_a_file->filename);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(rm=>'ajax_upload_rm');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Document root is not a directory"}/,
'httpdocs_dir not a directory'
);
};
subtest 'upload_subdir does not exist' => sub{
plan tests => 3;
my $tmpdir = File::Temp->newdir;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir->dirname);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Upload folder is not a directory"}/,
'upload folder does not exist'
);
};
subtest 'upload_subdir is not writeable' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
chmod 300, "$tmpdir_name/img/uploads";
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Upload folder is not writeable"}/,
'Upload folder is not writeable'
);
};
my $tcm2 = Test::CGI::Multipart->new;
$tcm2->set_param(name=>'rm', value=>'ajax_upload_rm');
subtest 'no file parameter' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm2->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr/{"status":"No file handle obtained"}/,
'no file parameter'
);
};
my $tcm4 = Test::CGI::Multipart->new;
$tcm4->set_param(name=>'rm', value=>'ajax_upload_rm');
$tcm4->upload_file(name=>'file', value=>'This is a test!',file=>'test*blah.txt');
subtest 'DFV messages' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm4->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr/{"status":"file_name: Invalid, "}/,
t/02.warnings.t view on Meta::CPAN
local $profile->{field_filters} = {
file_name => [
sub {croak "Help!"},
],
};
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr/{"status":"Internal Error"}/,
'Internal Error',
qr/Help!/
);
};
my $tcm5 = Test::CGI::Multipart->new;
$tcm5->set_param(name=>'rm', value=>'ajax_upload_rm');
$tcm5->upload_file(name=>'file', value=>'',file=>'test.txt');
subtest 'no data' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
local $profile->{constraint_methods} = {
value => qr/^.{0,100}$/,
};
local $profile->{required} = [
qw(file_name data_size)
];
local $profile->{optional} = [
qw(mime_type value)
];
my $app = TestWebApp->new(
QUERY=>$tcm5->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"No data uploaded"}/,
'No data uploaded'
);
};
my $tcm3 = Test::CGI::Multipart->new;
$tcm3->set_param(name=>'rm', value=>'file_upload');
$tcm3->upload_file(name=>'file', value=>'This is a test!',file=>'test.txt');
subtest 'options' => sub{
plan tests => 4;
my $upload_subdir = '/images';
my $tmpdir = File::Temp->newdir;
my $tmpdir_name = $tmpdir->dirname;
mkdir "$tmpdir_name$upload_subdir";
my $app = TestWebApp->new(
QUERY=>$tcm3->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
run_mode=>'file_upload',
dfv_profile=>$profile,
upload_subdir=>$upload_subdir,
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"$upload_subdir/test.txt"}!xms,
'UPLOADED'
);
is(slurp("$tmpdir_name$upload_subdir/test.txt"), "This is a test!", 'file contents');
};
subtest 'UPLOADED' => sub{
plan tests => 4;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
dfv_profile=>$profile
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"/img/uploads/test.txt"}!xms,
'UPLOADED'
);
is(slurp("$tmpdir_name/img/uploads/test.txt"), "This is a test!", 'file contents');
};
t/03.images.t view on Meta::CPAN
sub nonexistent_dir {
my $new_dir = File::Temp->newdir;
return $new_dir->dirname;
}
sub valid_dir {
my $tmpdir = File::Temp->newdir;
my $tmpdir_name = $tmpdir->dirname;
mkdir "$tmpdir_name/img";
mkdir "$tmpdir_name/img/uploads";
return $tmpdir;
}
my $tcm = Test::CGI::Multipart->new;
$tcm->set_param(name=>'rm', value=>'ajax_upload_rm');
$tcm->upload_file(
name=>'file',
width=>400,
height=>250,
instructions=>$IMAGE_INSTRUCTIONS,
file=>'test.jpeg',
type=>'image/jpeg'
);
subtest 'httpdocs_dir not specified' => sub{
plan tests => 3;
t/03.images.t view on Meta::CPAN
);
};
subtest 'httpdocs_dir does not exist' => sub{
plan tests => 3;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs(nonexistent_dir());
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Document root is not a directory"}/,
'httpdocs_dir does not exist'
);
};
subtest 'httpdocs_dir not a directory' => sub{
plan tests => 3;
my $actually_a_file = File::Temp->new;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($actually_a_file->filename);
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(rm=>'ajax_upload_rm');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Document root is not a directory"}/,
'httpdocs_dir not a directory'
);
};
subtest 'upload_subdir does not exist' => sub{
plan tests => 3;
my $tmpdir = File::Temp->newdir;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir->dirname);
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Upload folder is not a directory"}/,
'upload folder does not exist'
);
};
subtest 'upload_subdir is not writeable' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
chmod 300, "$tmpdir_name/img/uploads";
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Upload folder is not writeable"}/,
'Upload folder is not writeable'
);
};
my $tcm2 = Test::CGI::Multipart->new;
$tcm2->set_param(name=>'rm', value=>'ajax_upload_rm');
subtest 'no file parameter' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm2->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr/{"status":"No file handle obtained"}/,
'no file parameter'
);
};
my $tcm4 = Test::CGI::Multipart->new;
$tcm4->set_param(name=>'rm', value=>'ajax_upload_rm');
$tcm4->upload_file(
name=>'file',
width=>400,
height=>250,
instructions=>$IMAGE_INSTRUCTIONS,
file=>'test*blah.jpeg',
type=>'image/jpeg'
);
subtest 'DFV messages' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm4->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr/{"status":"file_name: Invalid, "}/,
'DFV messages'
);
};
my $tcm3 = Test::CGI::Multipart->new;
$tcm3->set_param(name=>'rm', value=>'file_upload');
$tcm3->upload_file(
name=>'file',
width=>400,
height=>250,
instructions=>$IMAGE_INSTRUCTIONS,
file=>'test.jpeg',
type=>'image/jpeg'
);
subtest 'options' => sub{
plan tests => 4;
my $upload_subdir = '/images';
my $tmpdir = File::Temp->newdir;
my $tmpdir_name = $tmpdir->dirname;
mkdir "$tmpdir_name$upload_subdir";
my $app = TestWebApp->new(
QUERY=>$tcm3->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
run_mode=>'file_upload',
upload_subdir=>$upload_subdir,
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"$upload_subdir/test.jpeg"}!xms,
'UPLOADED'
);
is(-s "$tmpdir_name$upload_subdir/test.jpeg", 4046, 'file size');
};
subtest 'UPLOADED' => sub{
plan tests => 4;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"/img/uploads/test.jpeg"}!xms,
'UPLOADED'
);
is(-s "$tmpdir_name/img/uploads/test.jpeg", 4046, 'file size');
};
t/04-resize.t view on Meta::CPAN
sub nonexistent_dir {
my $new_dir = File::Temp->newdir;
return $new_dir->dirname;
}
sub valid_dir {
my $tmpdir = File::Temp->newdir;
my $tmpdir_name = $tmpdir->dirname;
mkdir "$tmpdir_name/img";
mkdir "$tmpdir_name/img/uploads";
return $tmpdir;
}
my $tcm = Test::CGI::Multipart->new;
$tcm->set_param(name=>'rm', value=>'ajax_upload_rm');
$tcm->upload_file(
name=>'file',
width=>400,
height=>250,
instructions=>$IMAGE_INSTRUCTIONS,
file=>'test.jpeg',
type=>'image/jpeg'
);
my $profile = CGI::Application::Plugin::AJAXUpload->ajax_upload_default_profile;
$profile->{field_filters}->{value} = filter_resize(300,200);
subtest 'httpdocs_dir not specified' => sub{
plan tests => 3;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {},
ajax_spec=>{
dfv_profile=>$profile,
t/04-resize.t view on Meta::CPAN
);
};
subtest 'httpdocs_dir does not exist' => sub{
plan tests => 3;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs(nonexistent_dir());
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
t/04-resize.t view on Meta::CPAN
};
subtest 'httpdocs_dir not a directory' => sub{
plan tests => 3;
my $actually_a_file = File::Temp->new;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($actually_a_file->filename);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(rm=>'ajax_upload_rm');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Document root is not a directory"}/,
'httpdocs_dir not a directory'
);
};
subtest 'upload_subdir does not exist' => sub{
plan tests => 3;
my $tmpdir = File::Temp->newdir;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir->dirname);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Upload folder is not a directory"}/,
'upload folder does not exist'
);
};
subtest 'upload_subdir is not writeable' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
chmod 300, "$tmpdir_name/img/uploads";
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr/{"status":"Upload folder is not writeable"}/,
'Upload folder is not writeable'
);
};
my $tcm2 = Test::CGI::Multipart->new;
$tcm2->set_param(name=>'rm', value=>'ajax_upload_rm');
subtest 'no file parameter' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm2->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr/{"status":"No file handle obtained"}/,
'no file parameter'
);
};
my $tcm4 = Test::CGI::Multipart->new;
$tcm4->set_param(name=>'rm', value=>'ajax_upload_rm');
$tcm4->upload_file(
name=>'file',
width=>400,
height=>250,
instructions=>$IMAGE_INSTRUCTIONS,
file=>'test*blah.jpeg',
type=>'image/jpeg'
);
subtest 'DFV messages' => sub{
plan tests => 3;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm4->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr/{"status":"file_name: Invalid, "}/,
'DFV messages'
);
};
my $tcm3 = Test::CGI::Multipart->new;
$tcm3->set_param(name=>'rm', value=>'file_upload');
$tcm3->upload_file(
name=>'file',
width=>400,
height=>250,
instructions=>$IMAGE_INSTRUCTIONS,
file=>'test.jpeg',
type=>'image/jpeg'
);
subtest 'options' => sub{
plan tests => 4;
my $upload_subdir = '/images';
my $tmpdir = File::Temp->newdir;
my $tmpdir_name = $tmpdir->dirname;
mkdir "$tmpdir_name$upload_subdir";
my $app = TestWebApp->new(
QUERY=>$tcm3->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=> {
run_mode=>'file_upload',
upload_subdir=>$upload_subdir,
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"$upload_subdir/test.jpeg"}!xms,
'UPLOADED'
);
size_ok("$tmpdir_name$upload_subdir/test.jpeg", [300,int(250*300/400)], "size 300x200");
};
subtest 'UPLOADED' => sub{
plan tests => 4;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"/img/uploads/test.jpeg"}!xms,
'UPLOADED'
);
size_ok("$tmpdir_name/img/uploads/test.jpeg", [300,int(250*300/400)], "size 300x200");
};
subtest 'png' => sub{
plan tests => 4;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
local $profile->{field_filters}->{value} = filter_resize(300,200,'png');
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"/img/uploads/test.jpeg"}!xms,
'UPLOADED'
);
size_ok("$tmpdir_name/img/uploads/test.jpeg", [300,int(250*300/400)], "size 300x200");
};
subtest 'square' => sub{
plan tests => 4;
my $tmpdir = valid_dir();
my $tmpdir_name = $tmpdir->dirname;
local $profile->{field_filters}->{value} = filter_resize(300,50);
my $app = TestWebApp->new(
QUERY=>$tcm->create_cgi(),
PARAMS=>{
document_root=>sub {
my $c = shift;
$c->ajax_upload_httpdocs($tmpdir_name);
},
ajax_spec=>{
dfv_profile=>$profile,
},
},
);
isa_ok($app, 'CGI::Application');
$app->query->param(validate=>1);
$app->response_like(
$CONTENT_RE,
qr!{"status":"UPLOADED","image_url":"/img/uploads/test.jpeg"}!xms,
'UPLOADED'
);
size_ok("$tmpdir_name/img/uploads/test.jpeg", [80,50], "size 300x50");
};
t/lib/TestWebApp.pm view on Meta::CPAN
$self->header_props(-encoding=>'utf-8',-charset=>'utf-8');
if ($self->param('document_root')) {
my $setup = $self->param('document_root');
$self->$setup();
}
else {
$self->{_TESTWEBAPP_TEMPDIR} = File::Temp->newdir();
my $httpdocs_dir = $self->{_TESTWEBAPP_TEMPDIR}->dirname;
mkdir "${httpdocs_dir}/img";
mkdir "${httpdocs_dir}/img/uploads";
$self->ajax_upload_httpdocs($httpdocs_dir);
}
if ($self->param('ajax_spec')) {
$self->ajax_upload_setup(%{$self->param('ajax_spec')});
}
else {
$self->ajax_upload_setup();
}
return;
}
sub response_like {
my $self = shift;
my $header_re = shift;
my $body_re = shift;
my $comment = shift;