Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Merge branch 'download-resuming' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Dec 12, 2017
2 parents a29c887 + dc60dbe commit 861b1e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
49 changes: 31 additions & 18 deletions core/src/core/src/pydio/Core/Http/Response/FileReaderResponse.php 100644 → 100755
Expand Up @@ -240,8 +240,13 @@ public function readFile($node = null, $filePath = null, $data = null, $headerTy

$localName = ($localName =="" ? basename((isSet($originalFilePath)?$originalFilePath:$filePathOrData)) : $localName);

if ($headerType == "plain") {

header("Pragma: public");
//header("Expires: -1");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
//header("Content-Disposition: attachment; filename=\"$file_name\"");

if ($headerType == "plain") {
header("Content-type:text/plain");
header("Content-Length: ".$size);

Expand All @@ -254,13 +259,13 @@ public function readFile($node = null, $filePath = null, $data = null, $headerTy
} else {

if ($isFile) {
header("Accept-Ranges: 0-$size");
$this->logDebug("Sending accept range 0-$size");
$tsstring = gmdate('D, d M Y H:i:s ', filemtime($filePathOrData)) . 'GMT';
header("Last-Modified: $tsstring");
}

// Check if we have a range header (we are resuming a transfer)
if ( isset($serverParams['HTTP_RANGE']) && $isFile && $size != 0 ) {

if ($headerType == "stream_content") {

if (extension_loaded('fileinfo') && (( $node !== null && !$node->wrapperIsRemote()) || $filePath !== null)) {
Expand All @@ -282,16 +287,27 @@ public function readFile($node = null, $filePath = null, $data = null, $headerTy
// multiple ranges, which can become pretty complex, so ignore it for now
$ranges = explode('=', $_SERVER['HTTP_RANGE']);
$offsets = explode('-', $ranges[1]);
$offset = floatval($offsets[0]);
if(!$offsets[0]) {
$offset = 0;
}else{
$offset = floatval($offsets[0]);
}

if (!$offsets[1]) {
$length = $size - $offset;
}else{
$length = floatval($offsets[1]) - $offset;
if ($length + $offset > $size || $length < 0) {
$length = $size - $offset;
}
}

$length = floatval($offsets[1]) - $offset;
if (!$length) $length = $size - $offset;
if ($length + $offset > $size || $length < 0) $length = $size - $offset;
$dataSizeFromOffset = ($offset + $length - 1) >= 0 ? ($offset + $length - 1): 0;
$this->logDebug('Content-Range: bytes ' . $offset . '-' . $length . '/' . $size);
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $size);

header('Content-Range: bytes ' . $offset . '-' . $dataSizeFromOffset . '/' . $size);
header("Content-Length: ". $length);
header("Accept-Ranges: $offset-".$dataSizeFromOffset);
$file = fopen($filePathOrData, 'rb');
if(!is_resource($file)){
throw new \Exception("Failed opening file ".$filePathOrData);
Expand All @@ -304,32 +320,29 @@ public function readFile($node = null, $filePath = null, $data = null, $headerTy
$relOffset -= 2000000000;
// This works because we never overcome the PHP 32 bit limit
}
fseek($file, $relOffset, SEEK_CUR);
$fseek = fseek($file, $relOffset, SEEK_CUR);

while(ob_get_level()) ob_end_flush();
$readSize = 0.0;
$bufferSize = 1024 * 8;
while (!feof($file) && $readSize < $length && connection_status() == 0) {
$this->logDebug("dl reading $readSize to $length", ["httpRange" => $serverParams["HTTP_RANGE"]]);
echo fread($file, $bufferSize);
echo fread($file, $bufferSize);
$readSize += $bufferSize;
flush();
}

fclose($file);
return;

} else {

}
else {
header("Accept-Ranges: 0-$size");
if ($confGzip) {

$gzippedData = ($data?gzencode($filePathOrData,9):gzencode(file_get_contents($filePathOrData), 9));
$size = strlen($gzippedData);

}

HTMLWriter::emitAttachmentsHeaders($localName, $size, $isFile, $confGzip);

if ($confGzip && isSet($gzippedData)) {
print $gzippedData;
return;
Expand Down Expand Up @@ -448,7 +461,7 @@ protected function sendToAccelerator($accelConfiguration, $localPathOrNode, $ser
// Pydio Agent acceleration - We make sure that request was really proxied by Agent, by checking a specific header.
if($accelConfiguration === "pydio" && array_key_exists("HTTP_X_PYDIO_DOWNLOAD_SUPPORTED", $serverParams)
&& ApiKeysService::requestHasValidHeadersForAdminTask($serverParams, PYDIO_BOOSTER_TASK_IDENTIFIER)) {

if ($localPathOrNode instanceof AJXP_Node) {
$options = MetaStreamWrapper::getResolvedOptionsForNode($localPathOrNode);
if($options["TYPE"] === "php"){
Expand Down
6 changes: 5 additions & 1 deletion core/src/plugins/core.access/src/MetaStreamWrapper.php 100644 → 100755
Expand Up @@ -601,7 +601,11 @@ public function stream_read($count)
public function stream_seek($offset, $whence = SEEK_SET)
{
if(isSet($this->handle) && is_resource($this->handle)){
return fseek($this->handle, $offset, $whence);
// fseek returns 0 if success, otherwise -1
// but steam_seek return true/false
if(fseek($this->handle, $offset, $whence) == 0) {
return true;
}
}
return false;
}
Expand Down

0 comments on commit 861b1e7

Please sign in to comment.