Always return proper file size

Legacy upstream had a weird way of doing this that could lead to empty files being requested with a range header that did not match what was available on the server. I'm assuming this code used to work aroud some media-monitor feature that has been since refactored into analyzer which handles these cases properly.
This commit is contained in:
Lucas Bickel 2017-04-17 11:08:11 +02:00
parent 54ded86f6d
commit fd6f3230ed
1 changed files with 14 additions and 8 deletions

View File

@ -26,8 +26,16 @@ class Application_Common_FileIO
}
//Note that $size is allowed to be zero. If that's the case, it means we don't
//know the filesize, and we just won't send the Content-Length header.
if ($size < 0) {
//know the filesize, and we need to figure one out so modern browsers don't get
//confused. This should only affect files imported by legacy upstream since
//media monitor did not always set the proper size in the database but analyzer
//seems to always have a value for this.
if ($size === 0) {
$fstats = fstat($fm);
$size = $fstats['size'];
}
if ($size <= 0) {
throw new Exception("Invalid file size returned for file at $filePath");
}
@ -56,11 +64,9 @@ class Application_Common_FileIO
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
if ($size > 0) {
header('Content-Length:' . (($end - $begin) + 1));
if (isset($_SERVER['HTTP_RANGE'])) {
header("Content-Range: bytes $begin-$end/$size");
}
header('Content-Length:' . (($end - $begin) + 1));
if (isset($_SERVER['HTTP_RANGE'])) {
header("Content-Range: bytes $begin-$end/$size");
}
//We can have multiple levels of output buffering. Need to
@ -77,4 +83,4 @@ class Application_Common_FileIO
}
fclose($fm);
}
}
}