From 5ab40cbe8ed407e6788822bdda81183f9993690e Mon Sep 17 00:00:00 2001 From: josch Date: Thu, 10 Apr 2008 11:57:31 +0000 Subject: [PATCH] added deeply nested if/else clauses to prevent all kinds of errors concerning not enough rights to write files git-svn-id: http://yolanda.mister-muffin.de/svn@293 7eef14d0-6ed0-489d-bf55-20463b2d70db --- trunk/tools/daemon.pl | 32 ++++++++++++----------- trunk/uploader.pl | 61 +++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/trunk/tools/daemon.pl b/trunk/tools/daemon.pl index 0bdf81f..c3690c5 100755 --- a/trunk/tools/daemon.pl +++ b/trunk/tools/daemon.pl @@ -60,27 +60,29 @@ while(1) $vwidth = int($vheight*($width/$height)/8 + .5)*8; $abitrate = 64; - $vbitrate = int(($filesize*8) / $duration + .5) - $abitrate; + $vbitrate = int((int(($filesize*8) / $duration + .5) - $abitrate)/1000); + #check if the bitrate is lower than 16000 (maximum for ffmpeg) + $vbitrate = $vbitrate <= 16000 ? $vbitrate : 16000; #TODO: add metadata information - system "ffmpeg2theora --optimize --videobitrate $vbitrate --audiobitrate $abitrate --sharpness 0 --width $vwidth --height $vheight --output $root/videos/$id /tmp/$id"; + $ffmpeg = system "ffmpeg2theora --optimize --videobitrate $vbitrate --audiobitrate $abitrate --sharpness 0 --width $vwidth --height $vheight --output $root/videos/$id /tmp/$id"; - appendlog $id, $audio, $video, $vwidth, $vheight, $fps, $duration, $sha; + appendlog $id, $vbitrate, $filesize, $vwidth, $vheight, $fps, $duration, $sha, $ffmpeg; - $filesize = -s "$root/videos/$id"; + #only insert into videos table when everything went right + if($ffmpeg == 0) + { + $filesize = -s "$root/videos/$id"; + + #add video to videos table + $dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator, + subject, source, language, coverage, rights, license, ?, duration, ?, ?, fps, hash, 0, 0 + from uploaded where id = ?}, undef, $filesize, $vwidth, + $vheight, $id) or interrupt $dbh->errstr; + } - #add video to videos table - $dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator, - subject, source, language, coverage, rights, license, ?, duration, ?, ?, fps, hash, 0, 0 - from uploaded where id = ?}, undef, $filesize, $vwidth, - $vheight, $id) or interrupt $dbh->errstr; - - #delete temp file + #delete from uploaded table and from /tmp unlink "/tmp/$id"; - - #TODO:create torrent file - - #delete from uploaded table $dbh->do(qq{delete from uploaded where id = ?}, undef, $id) or interrupt $dbh->errstr; } else diff --git a/trunk/uploader.pl b/trunk/uploader.pl index f58f6d5..07c89ce 100644 --- a/trunk/uploader.pl +++ b/trunk/uploader.pl @@ -125,37 +125,52 @@ if($userinfo->{'id'} && $query->param("DC.Title") && $tnheight = $tnmaxheight; $tnwidth = int($tnheight*($width/$height)/2 + .5)*2; - system "ffmpeg -i /tmp/$id -vcodec mjpeg -vframes 1 -an -f rawvideo -ss $thumbnailsec -s ".$tnwidth."x$tnheight $root/video-stills/thumbnails/$id"; - system "ffmpeg -i /tmp/$id -vcodec mjpeg -vframes 1 -an -f rawvideo -ss $previewsec $root/video-stills/previews/$id"; + $ffthumb = system "ffmpeg -i /tmp/$id -vcodec mjpeg -vframes 1 -an -f rawvideo -ss $thumbnailsec -s ".$tnwidth."x$tnheight $root/video-stills/thumbnails/$id"; + $ffprev = system "ffmpeg -i /tmp/$id -vcodec mjpeg -vframes 1 -an -f rawvideo -ss $previewsec $root/video-stills/previews/$id"; - $vmaxheight = 640; - - #check if the upload already is in the right format and smaller/equal max-width/height - if ($container eq 'ogg' and $video eq 'theora' and ($audio eq 'vorbis' or not $audio) and $height <= $vmaxheight) + #if thumbnail was created successfully + if($ffthumb == 0 and $ffprev == 0) { - #add video to videos table - $dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator, - subject, source, language, coverage, rights, license, ?, ?, ?, ?, ?, ?, 0, 0 - from uploaded where id = ?}, undef, $filesize, $duration, $width, - $height, $fps, $sha, $id) or die $dbh->errstr; - - #delete from uploaded table - $dbh->do(qq{delete from uploaded where id = ?}, undef, $id) or die $dbh->errstr; + $vmaxheight = 640; - move("/tmp/$id", "$root/videos/$id"); + #check if the upload already is in the right format and smaller/equal max-width/height + if ($container eq 'ogg' and $video eq 'theora' and ($audio eq 'vorbis' or not $audio) and $height <= $vmaxheight) + { + if(move("/tmp/$id", "$root/videos/$id")) + { + #add video to videos table + $dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator, + subject, source, language, coverage, rights, license, ?, ?, ?, ?, ?, ?, 0, 0 + from uploaded where id = ?}, undef, $filesize, $duration, $width, + $height, $fps, $sha, $id) or die $dbh->errstr; + + #delete from uploaded table + $dbh->do(qq{delete from uploaded where id = ?}, undef, $id) or die $dbh->errstr; + } + else + { + #delete from uploaded table + $dbh->do(qq{delete from uploaded where id = ?}, undef, $id) or die $dbh->errstr; + die "cannot move video to $root/videos/$id - check your permissions!" + } + } + else + { + #write all valueable information to database so the daemon can fetch it + $dbh->do(qq{update uploaded set filesize = ?, duration = ?, width = ?, + height = ?, fps = ?, hash = ? where id = ?}, undef, $filesize, $duration, $width, + $height, $fps, $sha, $id) or die $dbh->errstr; + } - #TODO:create torrent file + #print success to the user + print $query->redirect("index.pl?information=information_uploaded&value=$domain/video/".urlencode($query->param("DC.Title"))."/$id/"); } else { - #write all valueable information to database so the daemon can fetch it - $dbh->do(qq{update uploaded set filesize = ?, duration = ?, width = ?, - height = ?, fps = ?, hash = ? where id = ?}, undef, $filesize, $duration, $width, - $height, $fps, $sha, $id) or die $dbh->errstr; + #delete from uploaded table + $dbh->do(qq{delete from uploaded where id = ?}, undef, $id) or die $dbh->errstr; + die "cannot create thumbnails - check your permissions!"; } - - #print success to the user - print $query->redirect("index.pl?information=information_uploaded&value=$domain/video/".urlencode($query->param("DC.Title"))."/$id/"); } } }