From ba490b2880eb10b198927c044133b35e02446bc7 Mon Sep 17 00:00:00 2001 From: pepper Date: Mon, 13 Jan 2014 18:15:20 -0800 Subject: fixed bugs in thumbnail_upload and added extra db check --- cgi-bin/thumbnail_upload | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) mode change 100755 => 100644 cgi-bin/thumbnail_upload diff --git a/cgi-bin/thumbnail_upload b/cgi-bin/thumbnail_upload old mode 100755 new mode 100644 index 96ba550..8ee9866 --- a/cgi-bin/thumbnail_upload +++ b/cgi-bin/thumbnail_upload @@ -25,6 +25,7 @@ my $aws_secret_access_key = "Dzlzh77U6n2BgQmOPldlR/dRDiO16DMUrQAXYhYc"; my $sql_username = "asdfus"; my $sql_dbname = "asdfus"; my $sql_passwd = "gTYgT&M6q"; +our $dbh = DBI->connect("DBI:mysql:$sql_dbname", $sql_username, $sql_passwd); #redis my $redis = Redis->new; @@ -87,13 +88,27 @@ sub make_thumbnail_url{ my $filename = shift; return sprintf("http://i.asdf.us/shader_thumb/%s", $filename); } +sub check_db{ + my $data_id = shift; + my $statement = 'select thumbnail_url from shaders where id = ?'; + my $sth = $dbh->prepare($statement) + or die "Couldn't prepare statement: " . $dbh->errstr; + $sth->execute($data_id) + or die "Couldn't execute statement: " . $sth->errstr; + my $data = $sth->fetchrow_hashref(); + if ($sth->rows == 0) { + error( "No ids matched $data_id."); + } + $sth->finish; + return $data->{thumbnail_url}; +} + sub add_to_db{ my $thumbnail_url = shift; my $shader_id = shift; - our $dbh = DBI->connect("DBI:mysql:$sql_dbname", $sql_username, $sql_passwd); - unless($dbh){ return undef && print STDERR "Could not connect to database: $DBI::errstr"}; + unless($dbh){ return undef && error( "Could not connect to database: $DBI::errstr")}; my $execute = $dbh->do( - "UPDATE shaders set thumbnail_url = ? WHERE id = ?". + "UPDATE shaders set thumbnail_url = ? WHERE id = ?", undef, $thumbnail_url, $shader_id @@ -142,6 +157,8 @@ sub main{ $maxRequestLimit, $ENV{REMOTE_ADDR}, $time_now); my $filedata = get_postdata() || get_filedata(); unless($filedata){ error("NO DATA RECIEVED") }; + unless($IN->param('id')){ error("Shader id not specified") }; + if (check_db($IN->param('id'))){ error("thumbnail already in database") }; my $filesize = length($filedata); if ($filesize < 1) { error( "EMPTY FILE"); @@ -152,8 +169,9 @@ sub main{ unless(upload_to_AmazonS3(sprintf("shader_thumb/%s", make_filename()), $filedata)){ error("UNABLE TO UPLOAD TO AMAZONS3"); }; - unless(add_to_db($IN->param('$id'))){ - print STDERR "problem adding value to db"; + unless(add_to_db($thumbnail_url, $IN->param('id'))){ + print "can you see anything here???"; + error( "problem adding value to db"); } my $resp = { success => "true", -- cgit v1.2.3-70-g09d2