diff --git a/trunk/functions.pl b/trunk/functions.pl index 7f6f200..aba29f5 100644 --- a/trunk/functions.pl +++ b/trunk/functions.pl @@ -51,3 +51,30 @@ sub get_username_from_sid { #return return $username; } + +#return a username from passed session id +sub get_userid_from_sid { + #get parameters + my ($sid) = @_; + + #connect to db + my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr; + + #prepare query + my $sth = $dbh->prepare(qq{select id from users where sid = '$sid'}) or die $dbh->errstr; + + #execute it + $sth->execute() or die $dbh->errstr; + + #save the resulting username + my ($username) = $sth->fetchrow_array() or die $dbh->errstr; + + #finish query + $sth->finish() or die $dbh->errstr; + + #close db + $dbh->disconnect() or die $dbh->errstr; + + #return + return $username; +} diff --git a/trunk/uploader.pl b/trunk/uploader.pl index 7e744e8..b85d619 100644 --- a/trunk/uploader.pl +++ b/trunk/uploader.pl @@ -8,34 +8,54 @@ $session = new CGI::Session; sub hook { #this is going to become an ajax progress bar my ($filename, $buffer, $bytes_read, $data) = @_; - print $ENV{'CONTENT_LENGTH'}; - print sha256_hex($buffer); + #print $ENV{'CONTENT_LENGTH'}; + #print sha256_hex($buffer); #open(TEMP, ">>/var/www/perl/videos/temp.temp") or die "cannot open"; - print "Read $bytes_read bytes of $filename\n"; + #print "Read $bytes_read bytes of $filename\n"; #close TEMP; } -my $username = get_username_from_sid($session->id); +my $userid = get_userid_from_sid($session->id); -if($username) { +if($userid) { #connect to db my $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass) or die $dbh->errstr; #save POST data in local variables my $title = $query->param("title"); + #video status: + # 0 - fresh upload - needs conversion + # 1 - successfully converted + # 2 - error: was not a valid video/format + # 3 - error: video is a duplicate #do query - $dbh->do(qq{insert into videos (title, username) values ('$title', '$username')}) or die $dbh->errstr; + $dbh->do(qq{insert into videos (title, userid, status) values ('$title', '$userid', 0)}) or die $dbh->errstr; - my $filename = $query->param("file"); - my $title = $query->param("title"); + #prepare query + my $sth = $dbh->prepare(qq{select last_insert_id() }) or die $dbh->errstr; + + #execute it + $sth->execute() or die $dbh->errstr; + + #save the resulting username + my ($id) = $sth->fetchrow_array() or die $dbh->errstr; + + #finish query + $sth->finish() or die $dbh->errstr; + + #save uploaded file into temppath $upload_filehandle = $query->upload("file"); - print $session->header(); + open(TEMPFILE, ">/var/www/perl/tmp/$id") or die "cannot open"; while ( <$upload_filehandle> ) { - print; + print TEMPFILE; } + close TEMPFILE; + print $session->header(); + print "passt"; + print $id; #disconnect db $dbh->disconnect() or die $dbh->errstr;