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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#!/usr/bin/perl
use strict;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use Digest::MD5;
use File::MMagic;
use CGI;
use File::Slurp;
use Data::Dumper;
use Amazon::S3;
use Digest::SHA1;
use JSON
#set the temporary directory here
my $upload_dir = '/tmp/';
#maximum filesize goes here
my $maxFileSize = 2.2 * 1024 * 1024;
#aws credentials here
my $aws_access_key_id = "AKIAIR53VPBXKJMXZIBA";
my $aws_secret_access_key = "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";
#SQL CONNECTIONS
my $sql_username = "asdfus";
my $sql_dbname = "asdfus";
my $sql_passwd = "gTYgT&M6q";
my $db_tag = "shader";
my $time_now = time();
my $IN = new CGI;
my $resp = {};
sub get_postdata{
return $IN->param('POSTDATA');
}
#qq file is the default name for the file parameter when using qq upload. change if necessary
sub get_filedata{
my $file = $IN->upload('qqfile');
if ($file){
return read_file( $file, binmode => ':raw' ) ;
}else{
return
}
}
sub get_filetype{
my $filedata = shift;
my $mm = new File::MMagic;
my $mtype = $mm->checktype_contents($filedata);
if ($mtype =~ m@^image/(gif|png|jpeg)@gi){
return $1;
}else{
return undef;
}
}
sub make_photoblaster_directory{
return substr(Digest::SHA1->sha1_hex($time_now), 0, 2);
}
sub make_photoblaster_filename{
my $filename = $IN->param("filename") || sprintf("%s_%s", "im", $time_now);
$filename =~ s/[^-a-zA-Z0-9]/-/g;
$filename =~ s/-+/-/g;
$filename =~ s/\....$//;
$filename =~ s/^-//g;
return $filename;
}
sub make_photoblaster_url{
my $photoblaster_directory = shift;
my $filename = shift;
return sprintf("http://i.asdf.us/im/%s/%s", $photoblaster_directory, $filename);
}
sub add_to_photoblaster_db{
my $pb_dir = shift;
my $filename = shift;
our $dbh = DBI->connect("DBI:mysql:$sql_dbname;host=asdf.us", $sql_username, $sql_passwd);
unless($dbh){ return undef && print STDERR "Could not connect to database: $DBI::errstr"};
my $execute = $dbh->do(
"INSERT INTO im_cmd ".
"(date, name, dir, newfile, tag, remote_addr)".
"VALUES(?,?,?,?,?,?);",
undef,
$time_now,
$IN->param("username") || "",
$pb_dir,
$filename,
$db_tag,
$ENV{REMOTE_ADDR} || '127.0.0.1'
);
return $execute;
}
sub write_to_file{
my $filepath = shift;
my $filedata = shift;
write_file( $filepath, {binmode => ':raw'}, $filedata ) or return undef;
return 1;
}
sub test_gif_size{
my $filepath = shift;
my $check_size = -s "$filepath";
print STDERR qq|Main filesize: $check_size Max Filesize: $maxFileSize nn|;
return $check_size;
}
sub optimize_gif{
my $filepath = shift;
my $tmpname = $filepath.".tmp";
#because system gives the unix exit code
not system("gifsicle -O2 --colors=256 $filepath > $tmpname") or return undef;
not system("mv -f $tmpname $filepath") or return undef;
return 1;
}
sub upload_to_AmazonS3{
my $keyname = shift;
my $value = shift;
my $s3 = Amazon::S3->new(
{ aws_access_key_id => $aws_access_key_id,
aws_secret_access_key => $aws_secret_access_key
}
);
my $bucket = $s3->bucket("i.asdf.us");
$bucket->add_key(
$keyname, $value,
{ content_type => 'image/gif',
}
);
return $bucket;
}
sub delete_file{
my $filepath = shift;
unlink($filepath);
}
sub error{
my $message = shift;
my $resp = { success => "false", error => $message };
print JSON->new->encode($resp);
exit 1;
}
sub main{
print $IN->header();
my $filedata = get_postdata() || get_filedata() || read_file("/tmp/tt.gif", binmode=>":raw");
unless($filedata){ error("NO DATA RECIEVED") };
my $filetype = get_filetype($filedata);
unless($filetype){ error("BAD FILE TYPE")};
my $photoblaster_directory = make_photoblaster_directory();
my $photoblaster_filename = sprintf("%s.%s", make_photoblaster_filename(), $filetype);
my $upload_dir = "/tmp";
my $filepath = $upload_dir . $photoblaster_filename;
if ($filetype =~ /gif$/i){
unless(write_to_file($filepath, $filedata)){
error("UNABLE TO WRITE TO FILE");
}
unless (optimize_gif($filepath)){
error("UNABLE TO OPTIMIZE GIF");
}
my $filedata = read_file($filepath, binmode=>":raw");
}
my $filesize = length($filedata);
if ($filesize < 1) {
error( "EMPTY FILE");
} elsif ($filesize > $maxFileSize) {
error("FILE TOO LARGE");
}
unless(upload_to_AmazonS3(sprintf("im/%s/%s", $photoblaster_directory, $photoblaster_filename), $filedata)){
error("UNABLE TO UPLOAD TO AMAZONS3");
};
delete_file($filepath);
my $photoblaster_url = make_photoblaster_url($photoblaster_directory, $photoblaster_filename);
unless(add_to_photoblaster_db($photoblaster_directory, $photoblaster_filename)){
print STDERR "problem adding value to db";
}
my $resp = {
success => "true",
url => $photoblaster_url,
filesize => $filesize
};
print JSON->new->encode($resp);
}
main();
|