forked from FineUploader/server-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
perl.cgi
75 lines (58 loc) · 2.06 KB
/
perl.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Digest::MD5;
my $uploaddir = '/folder/to/save/in/ajax_upload/tmp_uploads';
my $maxFileSize = 0.5 * 1024 * 1024; # 1/2mb max file size...
use CGI;
my $IN = new CGI;
my $file;
if ($IN->param('POSTDATA')) {
$file = $IN->param('POSTDATA');
} else {
$file = $IN->upload('qqfile');
}
my $temp_id = $IN->param('temp_id');
# make a random filename, and we guess the file type later on...
my $name = Digest::MD5::md5_base64( rand );
$name =~ s/\+/_/g;
$name =~ s/\//_/g;
my $type;
if ($file =~ /^GIF/) {
$type = "gif";
} elsif ($file =~ /PNG/) {
$type = "png";
} elsif ($file =~ /JFIF/) {
$type = "jpg";
}
if (!$type) {
print qq|{ "success": false, "error": "Invalid file type..." }|;
print STDERR "file has been NOT been uploaded... \n";
}
print STDERR "Making dir: $uploaddir/$temp_id \n";
mkdir("$uploaddir/$temp_id");
binmode(WRITEIT);
open(WRITEIT, ">$uploaddir/$name.$type") or die "Cant write to $uploaddir/$name.$type. Reason: $!";
if ($IN->param('POSTDATA')) {
print WRITEIT $file;
} else {
while (<$file>) {
print WRITEIT;
}
}
close(WRITEIT);
my $check_size = -s "$uploaddir/$name.$type";
print STDERR qq|Main filesize: $check_size Max Filesize: $maxFileSize \n\n|;
print $IN->header();
if ($check_size < 1) {
print STDERR "ooops, its empty - gonna get rid of it!\n";
print qq|{ "success": false, "error": "File is empty..." }|;
print STDERR "file has been NOT been uploaded... \n";
} elsif ($check_size > $maxFileSize) {
print STDERR "ooops, its too large - gonna get rid of it!\n";
print qq|{ "success": false, "error": "File is too large..." }|;
print STDERR "file has been NOT been uploaded... \n";
} else {
print qq|{ "success": true }|;
print STDERR "file has been successfully uploaded... thank you.\n";
}