2007-10-20 22:37:52 +00:00
|
|
|
require "functions.pl";
|
|
|
|
|
2007-10-24 13:17:31 +00:00
|
|
|
#initialize session data
|
2008-04-15 18:56:13 +00:00
|
|
|
CGI::Session->name($config->{"page_cookie_name"});
|
2007-10-20 22:37:52 +00:00
|
|
|
$query = new CGI;
|
2007-10-24 13:17:31 +00:00
|
|
|
$session = new CGI::Session;
|
2007-10-20 22:37:52 +00:00
|
|
|
|
|
|
|
#do we have an id?
|
|
|
|
if($query->param('id'))
|
|
|
|
{
|
2008-02-14 22:15:38 +00:00
|
|
|
#check if video with requested id is in the database
|
|
|
|
my $sth = $dbh->prepare(qq{select title from videos where id = ? });
|
|
|
|
$sth->execute($query->param('id'));
|
|
|
|
|
|
|
|
if(($title) = $sth->fetchrow_array())
|
2008-04-11 08:45:54 +00:00
|
|
|
{
|
2008-02-14 22:15:38 +00:00
|
|
|
#are we only watching this video or downloading it?
|
|
|
|
if($query->param('view'))
|
|
|
|
{
|
|
|
|
#seems we only want to watch it - update viewcount
|
|
|
|
$dbh->do(qq{update videos set viewcount=viewcount+1 where id = ? }, undef, $query->param('id')) or die $dbh->errstr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#video is being downloaded - update downloadcount
|
|
|
|
$dbh->do(qq{update videos set downloadcount=downloadcount+1 where id = ? }, undef, $query->param('id')) or die $dbh->errstr;
|
|
|
|
}
|
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#open video file
|
2008-04-06 21:23:59 +00:00
|
|
|
$file = open(FILE, "<$root/videos/".$query->param('id'));
|
2008-02-14 22:15:38 +00:00
|
|
|
|
2008-04-01 16:16:04 +00:00
|
|
|
if($file)
|
2008-02-14 22:15:38 +00:00
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#TODO: replace all of this with fastcgi x-sendfile
|
|
|
|
#get video filesize
|
2008-04-01 16:16:04 +00:00
|
|
|
$filesize = -s "$root/videos/".$query->param('id');
|
2008-04-29 07:02:07 +00:00
|
|
|
|
|
|
|
#get http query range
|
|
|
|
#TODO: also allow range end
|
2008-04-01 16:16:04 +00:00
|
|
|
$range = $query->http('range');
|
|
|
|
$range =~ s/bytes=([0-9]+)-/$1/;
|
|
|
|
|
|
|
|
#if a specific range is requested send http partial content headers and seek in the inputfile
|
|
|
|
if($range)
|
2008-02-14 22:15:38 +00:00
|
|
|
{
|
2008-04-01 16:16:04 +00:00
|
|
|
#if $range is equal or more than filesize throw http 416 header
|
|
|
|
if($range >= $filesize)
|
|
|
|
{
|
|
|
|
print $query->header(-status=>'416 Requested Range Not Satisfiable');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#print correct http partial header
|
2008-04-01 16:16:04 +00:00
|
|
|
print $query->header(-type=>'application/ogg',
|
|
|
|
-content_length=> $filesize-$range,
|
|
|
|
-status=>'206 Partial Content',
|
|
|
|
-attachment=>$title.".ogv",
|
|
|
|
-accept_ranges=> "bytes",
|
|
|
|
-content_range=> "bytes $range-".($filesize-1)."/$filesize"
|
|
|
|
);
|
2008-04-29 07:02:07 +00:00
|
|
|
|
|
|
|
#seek file to the requested position
|
2008-04-01 16:16:04 +00:00
|
|
|
seek FILE, $range, 0;
|
|
|
|
}
|
2008-02-14 22:15:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#print normal header
|
2008-02-14 22:15:38 +00:00
|
|
|
print $query->header(-type=>'application/ogg',
|
2008-04-01 16:16:04 +00:00
|
|
|
-content_length=> $filesize,
|
|
|
|
-attachment=>$title.".ogv"
|
2008-02-14 22:15:38 +00:00
|
|
|
);
|
|
|
|
}
|
2008-04-01 16:16:04 +00:00
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#in both cases - do some slurp-eaze to the browser
|
2008-04-01 16:16:04 +00:00
|
|
|
while (my $BytesRead = read (FILE, $buff, 8192))
|
|
|
|
{
|
|
|
|
print $buff;
|
|
|
|
}
|
|
|
|
close(FILE);
|
2008-02-14 22:15:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#the requested file should be there but is not - throw server error
|
2008-04-01 16:16:04 +00:00
|
|
|
print $session->header(
|
|
|
|
-status=>'500 Internal Server Error'
|
|
|
|
)
|
2008-02-14 22:15:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#no such video exists - 404
|
2008-04-01 16:16:04 +00:00
|
|
|
print $session->header(
|
|
|
|
-status=>'404 Not found'
|
|
|
|
)
|
2008-02-14 22:15:38 +00:00
|
|
|
}
|
2007-10-20 22:37:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#no if was supplied
|
2008-04-27 00:42:15 +00:00
|
|
|
print $query->redirect("index.pl?error=error_202c");
|
2007-10-20 22:37:52 +00:00
|
|
|
}
|