Skip to content

Commit f4c4a6b

Browse files
Adam HopkinsAdam Hopkins
authored andcommitted
filenames now generated using Data::GUID. pre_post and pre_delete can now return errors.
1 parent 9a0522d commit f4c4a6b

File tree

10 files changed

+161
-62
lines changed

10 files changed

+161
-62
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ Revision history for jQuery-File-Upload
105105

106106
0.26
107107
fixed issue in generate_output where url was not set
108+
109+
0.27
110+
filenames now generated using Data::GUID. pre_post and pre_delete can now return errors.

MYMETA.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"requires" : {
3535
"CGI" : "0",
3636
"Cwd" : "0",
37-
"Digest::MD5" : "0",
37+
"Data::GUID" : "0",
3838
"Image::Magick" : "0",
3939
"JSON" : "0",
4040
"JSON::XS" : "0",
@@ -46,5 +46,5 @@
4646
}
4747
},
4848
"release_status" : "stable",
49-
"version" : "0.26"
49+
"version" : "0.27"
5050
}

MYMETA.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ no_index:
2020
requires:
2121
CGI: '0'
2222
Cwd: '0'
23-
Digest::MD5: '0'
23+
Data::GUID: '0'
2424
Image::Magick: '0'
2525
JSON: '0'
2626
JSON::XS: '0'
2727
Net::SSH2: '0'
2828
Net::SSH2::SFTP: '0'
2929
URI: '0'
3030
perl: '5.006'
31-
version: '0.26'
31+
version: '0.27'

Makefile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# MIN_PERL_VERSION => q[5.006]
2020
# NAME => q[jQuery::File::Upload]
2121
# PL_FILES => { }
22-
# PREREQ_PM => { URI=>q[0], JSON::XS=>q[0], Image::Magick=>q[0], CGI=>q[0], Test::More=>q[0], Net::SSH2::SFTP=>q[0], Digest::MD5=>q[0], JSON=>q[0], Cwd=>q[0], Net::SSH2=>q[0] }
22+
# PREREQ_PM => { URI=>q[0], JSON::XS=>q[0], Image::Magick=>q[0], CGI=>q[0], Test::More=>q[0], Data::GUID=>q[0], Net::SSH2::SFTP=>q[0], JSON=>q[0], Cwd=>q[0], Net::SSH2=>q[0] }
2323
# TEST_REQUIRES => { }
2424
# VERSION_FROM => q[lib/jQuery/File/Upload.pm]
2525
# clean => { FILES=>q[jQuery-File-Upload-*] }
@@ -62,11 +62,11 @@ DIRFILESEP = /
6262
DFSEP = $(DIRFILESEP)
6363
NAME = jQuery::File::Upload
6464
NAME_SYM = jQuery_File_Upload
65-
VERSION = 0.26
65+
VERSION = 0.27
6666
VERSION_MACRO = VERSION
67-
VERSION_SYM = 0_26
67+
VERSION_SYM = 0_27
6868
DEFINE_VERSION = -D$(VERSION_MACRO)=\"$(VERSION)\"
69-
XS_VERSION = 0.26
69+
XS_VERSION = 0.27
7070
XS_VERSION_MACRO = XS_VERSION
7171
XS_DEFINE_VERSION = -D$(XS_VERSION_MACRO)=\"$(XS_VERSION)\"
7272
INST_ARCHLIB = blib/arch
@@ -262,7 +262,7 @@ RCS_LABEL = rcs -Nv$(VERSION_SYM): -q
262262
DIST_CP = best
263263
DIST_DEFAULT = tardist
264264
DISTNAME = jQuery-File-Upload
265-
DISTVNAME = jQuery-File-Upload-0.26
265+
DISTVNAME = jQuery-File-Upload-0.27
266266

267267

268268
# --- MakeMaker macro section:
@@ -506,15 +506,15 @@ metafile : create_distdir
506506
$(NOECHO) $(ECHO) 'requires:' >> META_new.yml
507507
$(NOECHO) $(ECHO) ' CGI: '\''0'\''' >> META_new.yml
508508
$(NOECHO) $(ECHO) ' Cwd: '\''0'\''' >> META_new.yml
509-
$(NOECHO) $(ECHO) ' Digest::MD5: '\''0'\''' >> META_new.yml
509+
$(NOECHO) $(ECHO) ' Data::GUID: '\''0'\''' >> META_new.yml
510510
$(NOECHO) $(ECHO) ' Image::Magick: '\''0'\''' >> META_new.yml
511511
$(NOECHO) $(ECHO) ' JSON: '\''0'\''' >> META_new.yml
512512
$(NOECHO) $(ECHO) ' JSON::XS: '\''0'\''' >> META_new.yml
513513
$(NOECHO) $(ECHO) ' Net::SSH2: '\''0'\''' >> META_new.yml
514514
$(NOECHO) $(ECHO) ' Net::SSH2::SFTP: '\''0'\''' >> META_new.yml
515515
$(NOECHO) $(ECHO) ' URI: '\''0'\''' >> META_new.yml
516516
$(NOECHO) $(ECHO) ' perl: '\''5.006'\''' >> META_new.yml
517-
$(NOECHO) $(ECHO) 'version: '\''0.26'\''' >> META_new.yml
517+
$(NOECHO) $(ECHO) 'version: '\''0.27'\''' >> META_new.yml
518518
-$(NOECHO) $(MV) META_new.yml $(DISTVNAME)/META.yml
519519
$(NOECHO) $(ECHO) Generating META.json
520520
$(NOECHO) $(ECHO) '{' > META_new.json
@@ -553,7 +553,7 @@ metafile : create_distdir
553553
$(NOECHO) $(ECHO) ' "requires" : {' >> META_new.json
554554
$(NOECHO) $(ECHO) ' "CGI" : "0",' >> META_new.json
555555
$(NOECHO) $(ECHO) ' "Cwd" : "0",' >> META_new.json
556-
$(NOECHO) $(ECHO) ' "Digest::MD5" : "0",' >> META_new.json
556+
$(NOECHO) $(ECHO) ' "Data::GUID" : "0",' >> META_new.json
557557
$(NOECHO) $(ECHO) ' "Image::Magick" : "0",' >> META_new.json
558558
$(NOECHO) $(ECHO) ' "JSON" : "0",' >> META_new.json
559559
$(NOECHO) $(ECHO) ' "JSON::XS" : "0",' >> META_new.json
@@ -565,7 +565,7 @@ metafile : create_distdir
565565
$(NOECHO) $(ECHO) ' }' >> META_new.json
566566
$(NOECHO) $(ECHO) ' },' >> META_new.json
567567
$(NOECHO) $(ECHO) ' "release_status" : "stable",' >> META_new.json
568-
$(NOECHO) $(ECHO) ' "version" : "0.26"' >> META_new.json
568+
$(NOECHO) $(ECHO) ' "version" : "0.27"' >> META_new.json
569569
$(NOECHO) $(ECHO) '}' >> META_new.json
570570
-$(NOECHO) $(MV) META_new.json $(DISTVNAME)/META.json
571571

@@ -873,7 +873,7 @@ ppd :
873873
$(NOECHO) $(ECHO) ' <PERLCORE VERSION="5,006,0,0" />' >> $(DISTNAME).ppd
874874
$(NOECHO) $(ECHO) ' <REQUIRE NAME="CGI::" />' >> $(DISTNAME).ppd
875875
$(NOECHO) $(ECHO) ' <REQUIRE NAME="Cwd::" />' >> $(DISTNAME).ppd
876-
$(NOECHO) $(ECHO) ' <REQUIRE NAME="Digest::MD5" />' >> $(DISTNAME).ppd
876+
$(NOECHO) $(ECHO) ' <REQUIRE NAME="Data::GUID" />' >> $(DISTNAME).ppd
877877
$(NOECHO) $(ECHO) ' <REQUIRE NAME="Image::Magick" />' >> $(DISTNAME).ppd
878878
$(NOECHO) $(ECHO) ' <REQUIRE NAME="JSON::" />' >> $(DISTNAME).ppd
879879
$(NOECHO) $(ECHO) ' <REQUIRE NAME="JSON::XS" />' >> $(DISTNAME).ppd

Makefile.PL

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ WriteMakefile(
2525
'Net::SSH2::SFTP' => 0,
2626
'Image::Magick' => 0,
2727
'Cwd' => 0,
28-
'Digest::MD5' => 0,
2928
'URI' => 0,
29+
'Data::GUID' => 0,
3030
},
3131
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
3232
clean => { FILES => 'jQuery-File-Upload-*' },

blib/lib/jQuery/File/Upload.pm

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package jQuery::File::Upload;
22

33
use 5.008008;
44
use strict;
5-
use warnings;
5+
#use warnings;
66

77
use CGI;
88
use JSON::XS;
@@ -11,13 +11,13 @@ use Net::SSH2;
1111
use Net::SSH2::SFTP;
1212
use Image::Magick;
1313
use Cwd 'abs_path';
14-
use Digest::MD5 qw(md5_hex);
1514
use URI;
15+
use Data::GUID;
1616

1717
#use LWP::UserAgent;
1818
#use LWP::Protocol::https;
1919

20-
our $VERSION = '0.26';
20+
our $VERSION = '0.27';
2121

2222
my %errors = (
2323
'_validate_max_file_size' => 'File is too big',
@@ -111,6 +111,7 @@ sub new {
111111
width => undef,
112112
height => undef,
113113
num_files_in_dir => undef,
114+
user_error => undef,
114115
@_, # Override previous attributes
115116
};
116117
return bless $self, $class;
@@ -687,16 +688,20 @@ sub handle_request {
687688
&{$self->post_get}($self);
688689
}
689690
elsif($method eq 'PATCH' or $method eq 'POST' or $method eq 'PUT') {
690-
&{$self->pre_post}($self);
691-
$self->_post;
692-
&{$self->post_post}($self);
691+
$self->{user_error} = &{$self->pre_post}($self);
692+
unless($self->{user_error}) {
693+
$self->_post;
694+
&{$self->post_post}($self);
695+
}
696+
else { $self->_generate_output }
693697
}
694698
elsif($method eq 'DELETE') {
695-
&{$self->pre_delete}($self); #even though we may not delete, we should give user option to still run code
696-
if($self->should_delete) {
699+
$self->{user_error} = &{$self->pre_delete}($self); #even though we may not delete, we should give user option to still run code
700+
if(not $self->{user_error} and $self->should_delete) {
697701
$self->_delete;
698702
&{$self->post_delete}($self);
699703
}
704+
else { $self->_generate_output }
700705
}
701706
else {
702707
$self->_set_status(405);
@@ -761,7 +766,7 @@ sub generate_output {
761766
sub _no_ext {
762767
my $self = shift;
763768
$self->filename($_->{filename});
764-
my ($no_ext) = $self->filename =~ /(.*)\.(.*)/;
769+
my ($no_ext) = $self->filename =~ qr/(.*)\.(.*)/;
765770
return $no_ext;
766771
}
767772

@@ -846,6 +851,7 @@ sub _clear {
846851
$self->{client_filename} = undef;
847852
$self->{tmp_thumb_path} = undef;
848853
$self->{tmp_file_path} = undef;
854+
$self->{user_error} = undef;
849855
}
850856

851857
sub _post {
@@ -876,18 +882,29 @@ sub _generate_output {
876882

877883
if($method eq 'POST') {
878884
my %hash;
885+
unless($self->{user_error}) {
886+
$hash{'url'} = $self->url;
887+
$hash{'thumbnailUrl'} = $self->thumbnail_url;
888+
$hash{'deleteUrl'} = $self->_delete_url;
889+
$hash{'deleteType'} = 'DELETE';
890+
$hash{error} = $self->_generate_error;
891+
}
892+
else {
893+
$self->_prepare_file_basics;
894+
$hash{error} = $self->{user_error};
895+
}
896+
879897
$hash{'name'} = $self->show_client_filename ? $self->client_filename . "" : $self->filename;
880898
$hash{'size'} = $self->{file_size};
881-
$hash{'url'} = $self->url;
882-
$hash{'thumbnailUrl'} = $self->thumbnail_url;
883-
$hash{'deleteUrl'} = $self->_delete_url;
884-
$hash{'deleteType'} = 'DELETE';
885-
886-
$hash{'error'} = $self->_generate_error;
887899
$obj->{files} = [\%hash];
888900
}
889901
elsif($method eq 'DELETE') {
890-
$obj->{$self->_get_param('filename')} = JSON::true;
902+
unless($self->{user_error}) {
903+
$obj->{$self->_get_param('filename')} = JSON::true;
904+
}
905+
else {
906+
$obj->{error} = $self->{user_error};
907+
}
891908
}
892909

893910
my $json = JSON::XS->new->ascii->pretty->allow_nonref;
@@ -973,10 +990,7 @@ sub _prepare_file_attrs {
973990
my $self = shift;
974991

975992
#ORDER MATTERS
976-
return undef unless $self->_set_upload_obj;
977-
$self->_set_fh;
978-
$self->_set_file_size;
979-
$self->_set_client_filename;
993+
return unless $self->_prepare_file_basics;
980994
$self->_set_tmp_filename;
981995
$self->_set_file_type;
982996
$self->_set_is_image;
@@ -992,6 +1006,17 @@ sub _prepare_file_attrs {
9921006
return 1;
9931007
}
9941008

1009+
sub _prepare_file_basics {
1010+
my ($self) = @_;
1011+
1012+
return undef unless $self->_set_upload_obj;
1013+
$self->_set_fh;
1014+
$self->_set_file_size;
1015+
$self->_set_client_filename;
1016+
1017+
return 1;
1018+
}
1019+
9951020
sub _set_urls {
9961021
my $self = shift;
9971022

@@ -1284,15 +1309,15 @@ sub _set_filename {
12841309
$self->filename($self->client_filename);
12851310
}
12861311
else {
1287-
my $filename = md5_hex($self->client_filename . time() . int(rand(1000))) . time() . $self->filename_salt;
1312+
my $filename = Data::GUID->new->as_string . $self->filename_salt;
12881313
$self->thumbnail_filename($self->thumbnail_prefix . $filename . $self->thumbnail_postfix . '.' . $self->thumbnail_format) unless $self->thumbnail_filename;
12891314

12901315
if($self->is_image) {
12911316
$filename .= '.' . $self->format;
12921317
}
12931318
else {
12941319
#add extension if present
1295-
if($self->client_filename =~ /.*\.(.*)/) {
1320+
if($self->client_filename =~ qr/.*\.(.*)/) {
12961321
$filename .= '.' . $1;
12971322
}
12981323
}
@@ -1412,7 +1437,7 @@ sub _set_num_files_in_dir {
14121437
$chan->exec('ls -rt ' . $_->{upload_dir} . ' | wc -l');
14131438
my $buffer;
14141439
$chan->read($buffer,1024);
1415-
($self->{num_files_in_dir}) = $buffer =~ /(\d+)/;
1440+
($self->{num_files_in_dir}) = $buffer =~ qr/(\d+)/;
14161441
$max = $self->{num_files_in_dir} if $self->{num_files_in_dir} > $max;
14171442
}
14181443

@@ -2502,6 +2527,13 @@ to set unique identifiers (such as an id for the file or the primary key) so tha
25022527
find the file in your database easier to perform whatever operations
25032528
you want to on it. B<Note:> This will be called even if
25042529
L<should_delete|/"should_delete"> is set to false.
2530+
If your pre_delete returns a value, this will be interpreted as an error
2531+
message and the delete call will be terminated and will return the error.
2532+
For example:
2533+
2534+
$j_fu->pre_delete(sub {
2535+
return 'You cannot delete this file.'; #file will not be deleted
2536+
});
25052537
25062538
=head3 post_delete
25072539
@@ -2525,6 +2557,13 @@ or
25252557
25262558
pre_post will be called before a post request is handled.
25272559
POST requests are what happen when jQuery File Upload uploads your file.
2560+
If your pre_post returns a value, this will be interpreted as an error
2561+
message and the post call will be terminated and will return the error.
2562+
For example:
2563+
2564+
$j_fu->pre_post(sub {
2565+
return 'You have too many files.'; #file will not be uploaded
2566+
});
25282567
25292568
=head3 post_post
25302569

blib/man3/jQuery::File::Upload.3pm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
.\" ========================================================================
125125
.\"
126126
.IX Title "jQuery::File::Upload 3"
127-
.TH jQuery::File::Upload 3 "2014-07-12" "perl v5.16.2" "User Contributed Perl Documentation"
127+
.TH jQuery::File::Upload 3 "2014-07-13" "perl v5.16.2" "User Contributed Perl Documentation"
128128
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
129129
.\" way too many mistakes in technical documents.
130130
.if n .ad l
@@ -1122,6 +1122,15 @@ to set unique identifiers (such as an id for the file or the primary key) so tha
11221122
find the file in your database easier to perform whatever operations
11231123
you want to on it. \fBNote:\fR This will be called even if
11241124
should_delete is set to false.
1125+
If your pre_delete returns a value, this will be interpreted as an error
1126+
message and the delete call will be terminated and will return the error.
1127+
For example:
1128+
.PP
1129+
.Vb 3
1130+
\& $j_fu\->pre_delete(sub {
1131+
\& return \*(AqYou cannot delete this file.\*(Aq; #file will not be deleted
1132+
\& });
1133+
.Ve
11251134
.PP
11261135
\fIpost_delete\fR
11271136
.IX Subsection "post_delete"
@@ -1155,6 +1164,15 @@ or
11551164
.PP
11561165
pre_post will be called before a post request is handled.
11571166
\&\s-1POST\s0 requests are what happen when jQuery File Upload uploads your file.
1167+
If your pre_post returns a value, this will be interpreted as an error
1168+
message and the post call will be terminated and will return the error.
1169+
For example:
1170+
.PP
1171+
.Vb 3
1172+
\& $j_fu\->pre_post(sub {
1173+
\& return \*(AqYou have too many files.\*(Aq; #file will not be uploaded
1174+
\& });
1175+
.Ve
11581176
.PP
11591177
\fIpost_post\fR
11601178
.IX Subsection "post_post"

jQuery-File-Upload-0.26.tar.gz

-20.2 KB
Binary file not shown.

jQuery-File-Upload-0.27.tar.gz

20.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)