<commit_before><?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
69328 Lyon, FRANCE.

See Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.
=========================================================================*/

$chunkSize = 1*(1024*8);
$buffer = '';
$fileSize = @filesize($this->path);
$handle = fopen($this->path, 'rb');

$contenType=$this->mimetype;

if ($handle === false)
  {
  throw new Zend_Exception("Unable to open the file ");
  }
$modified = gmdate('D, d M Y H:i:s').' GMT';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Last-Modified: $modified");

// if pdf set the content-type acordingly
if(!isset($contentType)&&pathinfo($this->name, PATHINFO_EXTENSION) == "pdf")
  {
  $contentType = 'application/pdf';
  $enableContentDisposition = false;
  }
  
if(!isset($contentType))
  {
  $contentType = 'application/octet-stream';
  } 

// Hack for .vsp files (for OSA)
if(!isset($contentType)&&strlen($this->name)>4 && substr($this->name,strlen($this->name)-4,4)==".vsp")
  {
  $contentType = 'application/isp';
  }
  


$agent = env('HTTP_USER_AGENT');
if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent))
  {
  header('Content-Type: '.$contentType);
  header("Content-Disposition: attachment; filename=\"".$this->name."\";");
  header("Expires: 0");
  header('Accept-Ranges: bytes');
  header("Cache-Control: private", false);
  header("Pragma: private");
  $httpRange = env('HTTP_RANGE');
  if ( isset ($httpRange))
    {
    list ($toss, $range) = explode("=", $httpRange);
    str_replace($range, "-", $range);
    $size = $fileSize-1;
    $length = $fileSize-$range;
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $length");
    header("Content-Range: bytes $range$size/$fileSize");
    fseek($handle, $range);
    }
  else
    {
    header("Content-Length: ".$fileSize);
    }
  }
else
  {
  header('Accept-Ranges: bytes');
  header("Expires: 0");
  header("Content-Type: ".$contentType);
  header("Content-Length: ".$fileSize);
  if(!isset($enableContentDisposition) || $enableContentDisposition==true) 
    {
    header("Content-Disposition: attachment; filename=\"".$this->name."\";");
    }
  if ( isset ($httpRange))
    {
    list ($toss, $range) = explode("=", $httpRange);
    str_replace($range, "-", $range);
    $size = $fileSize-1;
    $length = $fileSize-$range;
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $length");
    header("Content-Range: bytes $range$size/$fileSize");
    fseek($handle, $range);
    }
  }
@ob_end_clean();
@ob_start();

while (!feof($handle) && connection_status() == 0)
  {
  set_time_limit(0);
  $buffer = fread($handle, $chunkSize);
  echo $buffer;
  @flush();
  @ob_flush();
  }
fclose($handle);
exit ((connection_status() == 0) && !connection_aborted());



/**
 * Gets an environment variable from available sources, and provides emulation
 * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
 * IIS, or SCRIPT_NAME in CGI mode).  Also exposes some additional custom
 * environment information.
 *
 * @param  string $key Environment variable name.
 * @return string Environment variable setting.<commit_msg>BUG: refs #0260. Fix item download functionality for single bitstream items<commit_after><?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
69328 Lyon, FRANCE.

See Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.
=========================================================================*/

$chunkSize = 1024 * 8;
$buffer = '';
$fileSize = filesize($this->path);
$handle = fopen($this->path, 'rb');

$contentType = $this->mimetype;

if($handle === false)
  {
  throw new Zend_Exception("Unable to open the file ");
  }
$modified = gmdate('D, d M Y H:i:s').' GMT';
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.$modified);

// if pdf set the content-type acordingly
if(!isset($contentType) && pathinfo($this->name, PATHINFO_EXTENSION) == 'pdf')
  {
  $contentType = 'application/pdf';
  $enableContentDisposition = false;
  }
  
if(!isset($contentType))
  {
  $contentType = 'application/octet-stream';
  } 

// Hack for .vsp files (for OSA)
if(!isset($contentType) && strlen($this->name) > 4 && substr($this->name,strlen($this->name) - 4, 4) == '.vsp')
  {
  $contentType = 'application/isp';
  }

$agent = env('HTTP_USER_AGENT');
if(preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent))
  {
  header('Content-Type: '.$contentType);
  header('Content-Disposition: attachment; filename="'.$this->name.'";');
  header('Expires: 0');
  header('Accept-Ranges: bytes');
  header('Cache-Control: private', false);
  header('Pragma: private');
  $httpRange = env('HTTP_RANGE');
  if(isset($httpRange))
    {
    list ($toss, $range) = explode("=", $httpRange);
    str_replace($range, "-", $range);
    $size = $fileSize - 1;
    $length = $fileSize - $range;
    header('HTTP/1.1 206 Partial Content');
    header('Content-Length: '.$length);
    header('Content-Range: bytes '.$range.$size.'/'.$fileSize);
    fseek($handle, $range);
    }
  else
    {
    header('Content-Length: '.$fileSize);
    }
  }
else
  {
  header('Accept-Ranges: bytes');
  header('Expires: 0');
  header('Content-Type: '.$contentType);
  header('Content-Length: '.$fileSize);
  if(!isset($enableContentDisposition) || $enableContentDisposition == true) 
    {
    header('Content-Disposition: attachment; filename="'.$this->name.'";');
    }
  if(isset($httpRange))
    {
    list($toss, $range) = explode('=', $httpRange);
    str_replace($range, '-', $range);
    $size = $fileSize - 1;
    $length = $fileSize - $range;
    header('HTTP/1.1 206 Partial Content');
    header('Content-Length: '.$length);
    header('Content-Range: bytes '.$range.$size.'/'.$fileSize);
    fseek($handle, $range);
    }
  }

set_time_limit(0);

//kill the whole ob stack (Zend uses double nested output buffers)
while(ob_get_level() > 0)
  {
  ob_end_clean();
  }

while(!feof($handle) && connection_status() == 0)
  {
  $buffer = fread($handle, $chunkSize);
  echo $buffer;
  }
fclose($handle);
exit(connection_status() == 0 && !connection_aborted());



/**
 * Gets an environment variable from available sources, and provides emulation
 * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
 * IIS, or SCRIPT_NAME in CGI mode).  Also exposes some additional custom
 * environment information.
 *
 * @param  string $key Environment variable name.
 * @return string Environment variable setting.
{'fraction_non_alphanumeric': 0.217155567913249, 'fraction_numerical': 0.013624357013763381, 'mean_word_length': 4.508422664624809, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '960125', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2286, 'n_words': 547}