jQuery-File-Upload

 view release on metacpan or  search on metacpan

lib/jQuery/File/Upload.pm  view on Meta::CPAN

	$self->{url} = $self->upload_url_base . '/' . $self->filename;
}

sub _set_uri {
	my $self = shift;
	#if catalyst, use URI already made?
	if(defined $self->ctx) {
		$self->{uri} = $self->ctx->req->uri;
	}
	else {
		$self->{uri} = URI->new($self->script_url);
	}
}

sub _generate_error {
	my $self = shift;
	return undef unless defined $self->{error} and @{$self->{error}};

	my $restrictions = join ',', @{$self->{error}->[1]};
	return $errors{$self->{error}->[0]} . " Restriction: $restrictions Provided: " . $self->{error}->[2];
}

sub _validate_file {
	my $self = shift;
	return undef unless
	$self->_validate_max_file_size and
	$self->_validate_min_file_size and
	$self->_validate_accept_file_types and
	$self->_validate_reject_file_types and
	$self->_validate_max_width and
	$self->_validate_min_width and
	$self->_validate_max_height and
	$self->_validate_min_height and
	$self->_validate_max_number_of_files;

	return 1;
}

sub _save {
	my $self = shift;

	if(@{$self->scp}) {
		$self->_save_scp;
	}
	else {
		$self->_save_local;
	}
}

sub _save_scp {
	my $self = shift;

	for(@{$self->scp}) {
		die "Must provide a host to scp" if $_->{host} eq '';

		$_->{thumbnail_upload_dir} = $_->{upload_dir} if $_->{thumbnail_upload_dir} eq '';

		my $path = $_->{upload_dir} . '/' . $self->filename;
		my $thumb_path = $_->{thumbnail_upload_dir} . '/' . $self->thumbnail_filename;

		if(($_->{user} ne '' and $_->{public_key} ne '' and $_->{private_key} ne '') or ($_->{user} ne '' and $_->{password} ne '')) {
			my $ssh2 = $self->_auth_user($_);

			#if it is an image, scp both file and thumbnail
			if($self->is_image) {
				$ssh2->scp_put($self->{tmp_file_path}, $path);
				$ssh2->scp_put($self->{tmp_thumb_path}, $thumb_path);
			}
			else {
				$ssh2->scp_put($self->{tmp_filename}, $path);
			}

			$ssh2->disconnect;
		}
		else {
			die "Must provide a user and password or user and identity file for connecting to host";
		}

	}
}

sub _auth_user {
	my $self = shift;
	my ($auth) = @_;

	my $ssh2 = Net::SSH2->new;

	$ssh2->connect($auth->{host}) or die $!;

	#authenticate
	if($auth->{user} ne '' and $auth->{public_key} ne '' and $auth->{private_key} ne '') {
		$ssh2->auth_publickey($auth->{user},$auth->{public_key},$auth->{private_key});
	}
	else {
		$ssh2->auth_password($auth->{user},$auth->{password});
	}

	unless($ssh2->auth_ok) {
		die "error authenticating with remote server";
	}

	die "upload directory must be provided with scp hash" if $auth->{upload_dir} eq '';

	return $ssh2;
}

sub _save_local {
	my $self = shift;

	#if image
	if($self->is_image) {
		rename $self->{tmp_file_path}, $self->absolute_filename;
		rename $self->{tmp_thumb_path}, $self->absolute_thumbnail_filename;
	}
	#if non-image with catalyst
	elsif(defined $self->ctx) {
		if ($self->copy_file) {
			$self->{upload}->copy_to($self->absolute_filename);
		} else {
			$self->{upload}->link_to($self->absolute_filename);
		}
	}
	#if non-image with regular CGI perl
	else {
		my $io_handle = $self->{fh}->handle;

		my $buffer;
		open (OUTFILE,'>', $self->absolute_filename);
		while (my $bytesread = $io_handle->read($buffer,1024)) {
			print OUTFILE $buffer;
		}

		close OUTFILE;
	}
}

sub _validate_max_file_size {
	my $self = shift;
	return 1 unless $self->max_file_size;

	if($self->{file_size} > $self->max_file_size) {
		$self->{error} = ['_validate_max_file_size',[$self->max_file_size],$self->{file_size}];
		return undef;
	}
	else {
		return 1;
	}
}

sub _validate_min_file_size {
	my $self = shift;
	return 1 unless $self->min_file_size;

lib/jQuery/File/Upload.pm  view on Meta::CPAN

jQuery::File::Upload - Server-side solution for the L<jQuery File Upload|https://github.com/blueimp/jQuery-File-Upload/> plugin.

=head1 SYNOPSIS

  use jQuery::File::Upload;

  #simplest implementation
  my $j_fu = jQuery::File::Upload->new;
  $j_fu->handle_request;
  $j_fu->print_response;

  #alternatively you can call $j_fu->handle_request(1) and this will call print_response for you.
  my $j_fu = jQuery::File::Upload->new;
  $j_fu->handle_request(1);

The above example is the simplest one possible, however it assumes a lot of defaults.

=head2 Assumptions

=over 4

=item default upload directory

It is assumed that your files are being uploaded to the current directory of the script that's running, plus '/files'.
So if your script is in /home/user/public_html, your files will be uploaded to /home/user/public_html/files.

=item default url

It is also assumed that the files will be hosted one directory above the running script, plus '/files'. So if the
script is located at the url http://www.mydomain.com/upload.cgi, then all files will be assumed to be at the url
http://www.mydomain.com/files/file_name.

=item default filename

Uploaded files are given a name at run time unless you specifically set the filename in the jQuery::File::Upload object.

=item same server upload

By default, jQuery::File::Upload also assumes that you're meaning to have the uploaded file uploaded to the same server
that the script is running on. jQuery::File::Upload also has the ability to SCP files to remote servers, but this is not the default.

=item CGI environment

By default, jQuery::File::Upload assumes that the script is running in a regular CGI-type environment. However, jQuery::File::Upload
also has the ability to work with L<Catalyst> by being passed the context object.

=item all files

This implementation accepts all types of files.

=back

=head2 A more complicated example

  use jQuery::File::Upload;

  my $j_fu = jQuery::File::Upload->new(
		scp => [{
			user => 'user', #remote user
			public_key => '/home/user/.ssh/id_rsa.pub',	#also possible to use password instead of keys
			private_key => '/home/user/.ssh/id_rsa',
			host => 'mydomain.com',
			upload_dir => '/var/www/html/files', #directory that files will be uploaded to
		}],

		#user validation specifications
		max_file_size => 5242880, #max file size is 5mb
		min_file_size => 1,
		accept_file_types => ['image/jpeg','image/png','image/gif','text/html'], #uploads restricted to these filetypes
		max_width => 40000, #max width for images
		max_height => 50000, #max height for images
		min_width => 1,
		min_height => 1,
		max_number_of_files => 40, #maximum number of files we can have in our upload directory
	  );


  $j_fu->handle_request;
  $j_fu->print_response;

=head2 Getting fancy

  use jQuery::File::Upload;

  my $j_fu = jQuery::File::Upload->new(
			pre_get => sub {
				my $j = shift; #jQuery::File::Upload object for current request
				#any code in here will be executed before any get requests are handled
				#jQuery File Upload makes Get request when the page first loads, so this
				#can be useful for prefilling jQuery File Upload with files if you're using
				#jQuery File upload with saved data to view/delete/upload more

				#generate starting files for jQuery File Upload
				$j->generate_output(
					[
						{
							size => 500000,
							filename =>	'my_image.jpeg',
							image => 'y', #need to let jQuery::File::Upload know this is an image
   								      #or else thumbnails won't be deleted
						},
						{
							size => 500000,
							filename =>	'my_other_image.jpeg',
							image => 'y',
						},
					]
				);

  				#The above makes assumptions yet again. It generates the url based on the defaults, unless
  				#you provide below the upload_url_base.
			},
			pre_delete => sub {
				my $j = shift;

				#here you can do something with the information in the params of the delete_url
				#you can set your own meaningful delete_params (see below)
				#NOTE: delete_urls always have the 'filename' param, so that might be enough for your needs
				my $id = param->('id')

				#DELETE FROM table WHERE id=$id

lib/jQuery/File/Upload.pm  view on Meta::CPAN


  http://www.mydomain.com/folder/upload.cgi

With a L<relative_url_path|/"relative_url_path"> '/files' would yield:

  http://www.mydomain.com/files

Whereas by default L<relative_url_path|/"relative_url_path"> and
L<thumbnail_relative_url_path|/"thumbnail_relative_url_path"> are
relative to the folder the upload script is running in.

If you use this option, make sure to set L<upload_dir|/"upload_dir">
(and/or L<thumbnail_upload_dir|/"thumbnail_upload_dir"> if necessary)
since jQuery::File::Upload can no longer do a relative path
for saving the file.

Default is undef.

=head3 field_name

  $j_fu->field_name('files[]');

This is the name of the jQuery File Uploader client side.
The default is files[], as this is the jQuery File Upload
plugin's default.

=head3 ctx

  $j_fu->ctx($c);

This is meant to set the L<Catalyst> context object if you are using
this plugin with L<Catalyst>. The default is to not use this.

=head3 cgi

  $j_fu->cgi(CGI->new);

This should be used mostly internally by jQuery::File::Upload
(assuming you haven't passed in ctx).
It is just the CGI object that the module uses, however if you already
have one you could pass it in.

=head3 should_delete

  $j_fu->should_delete(1)

This is used to decide whether to actually delete the files when jQuery::File::Upload
receives a DELETE request. The default is to delete, however this could be useful
if you wanted to maybe just mark the field as deleted in your database (using L<pre_delete|/"pre_delete">)
and then actually physically
remove it with your own clean up script later. The benefit to this could be that
if you are SCPing the files to a remote server, perhaps issuing the remote commands
to delete these files is something that seems to costly to you.

=head3 scp

  $j_fu->scp([{
			host => 'media.mydomain.com',
			user => 'user',
		  	public_key => '/home/user/.ssh/id_rsa.pub',
		  	private_key => '/home/user/.ssh/id_rsa',
			password => 'pass', #if keys are present, you do not need password
			upload_dir => '/my/remote/dir',
		}]);

This method takes in an arrayref of hashrefs, where each hashref is a remote host you would like to SCP the files to.
SCPing the uploaded files to remote hosts could be useful if say you hosted your images on a different server
than the one doing the uploading.

=head4 SCP OPTIONS

=over 4

=item

host (REQUIRED) - the remote host you want to scp the files to, i.e. 127.0.0.1 or media.mydomain.com

=item

user (REQUIRED) - used to identify the user to remote server

=item

public_key & private_key - used to make secure connection. Not needed if password is given.

=item

password - used along with user to authenticate with remote server. Not needed if keys are supplied.

=item

upload_dir (REQUIRED) - the directory you want to scp to on the remote server. Should not end with a slash

=item

thumbnail_upload_dir - Will default to upload_dir. You only need to provide this if your thumbnails are stored in a different directory than regular images. Should not end with a slash

=back

You can check L<Net::SSH2> for more information on connecting to the remote server.

=head3 max_file_size

  $j_fu->max_file_size(1024);

Sets the max file size in bytes. By default there is no max file size.

=head3 min_file_size

  $j_fu->min_file_size(1);

Sets the minimum file size in bytes. Default minimum is 1 byte. to disable a minimum file size, you can set this to undef or 0.

=head3 accept_file_types

  $j_fu->accept_file_types(['image/jpeg','image/png','image/gif','text/html']);

Sets what file types are 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 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.



( run in 0.247 second using v1.01-cache-2.11-cpan-496ff517765 )