/**
  * Sends the given file by whatever method is appropriate. Uses php's
  * header() instead of silverstripe's HTTPResponse class to prevent other
  * headers from being added and so we can just use readfile() instead of
  * pulling the whole file into a string for setBody().
  *
  * @param File $file
  */
 protected function sendFile(File $file)
 {
     // this is for optional compatibility with markguinn/silverstripe-cloudassets
     if ($file->hasExtension('CloudFileExtension') && $file->CloudStatus === 'Live') {
         header('Location: ' . $file->getAbsoluteURL());
         exit;
     }
     // this is the normal way to send the files
     header('Content-Type: application/octet-stream');
     header('Content-Disposition: attachment; filename="' . $file->Name . '"');
     if (Config::inst()->get('Downloadable', 'use_xsendfile')) {
         header('X-Sendfile: ' . $file->getURL());
     } else {
         header('Content-Length: ' . $file->getAbsoluteSize());
         readfile($file->getFullPath());
     }
     exit;
 }
 /**
  * Given a File object, create an array of its properties and values
  * ready to be transformed to JSON
  * 	
  * @param  Folder $folder
  * @return array
  */
 public function createFileJSON(File $file, $folder = null)
 {
     $isImage = $file instanceof Image;
     $w = $isImage ? self::IMAGE_WIDTH : self::ICON_SIZE;
     $h = $isImage ? self::IMAGE_HEIGHT : self::ICON_SIZE;
     $folder = $folder ?: $file->Parent();
     return array('id' => $file->ID, 'parentID' => $file->ParentID, 'title' => $file->Title, 'filename' => basename($file->Filename), 'path' => $file->Filename, 'filesize' => $file->getAbsoluteSize(), 'folderName' => $folder->Filename, 'type' => $isImage ? 'image' : 'file', 'extension' => $file->getExtension(), 'created' => $file->Created, 'updated' => $file->LastEdited, 'iconURL' => $file->getPreviewThumbnail($w, $h)->URL, 'canEdit' => $file->canEdit(), 'canCreate' => $file->canCreate(), 'canDelete' => $file->canDelete());
 }
 public function getAbsoluteSize()
 {
     $this->createLocalIfNeeded();
     return $this->CloudStatus == 'Live' ? $this->CloudSize : parent::getAbsoluteSize();
 }
 /**
  * 
  * @param File $file
  * @return void
  */
 public function sendFileToBrowser($file)
 {
     $path = $file->getFullPath();
     if (SapphireTest::is_running_test()) {
         return file_get_contents($path);
     }
     $basename = basename($path);
     $length = $file->getAbsoluteSize();
     $type = HTTP::get_mime_type($file->getRelativePath());
     //handling time limitation
     if ($threshold = $this->config()->bandwidth_threshold) {
         increase_time_limit_to((int) ($length / $threshold));
     } else {
         increase_time_limit_to();
     }
     // send header
     header('Content-Description: File Transfer');
     /*
      * allow inline 'save as' popup, double quotation is present to prevent browser (eg. Firefox) from handling
      * wrongly a file with a name with whitespace, through a file in SilverStripe should not contain any whitespace
      * if the file is uploaded through SS interface
      * http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download
      */
     header('Content-Type: ' . $type);
     header('Content-Disposition: inline; filename=' . $basename);
     header('Content-Transfer-Encoding: binary');
     header('Content-Length: ' . $length);
     /**
      * issue fixes for IE6,7,8  when downloading a file over HTTPS (http://support.microsoft.com/kb/812935)
      * http://www.dotvoid.com/2009/10/problem-with-downloading-files-with-internet-explorer-over-https/
      */
     if (Director::is_https()) {
         header('Pragma: ');
     }
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     /**
      * unload the php session file
      * see http://konrness.com/php5/how-to-prevent-blocking-php-requests
      */
     session_write_close();
     // if output buffering is active, we clear it to prevent the script from trying to to allocate memory for entire file.
     while (ob_get_level() > 0) {
         ob_end_clean();
     }
     flush();
     readfile($path);
     exit(0);
 }
 /**
  *
  * COPIED CODE!!!!!
  *
  * This is copied from here:
  * https://github.com/silverstripe-labs/silverstripe-secureassets/blob/master/code/SecureFileController.php
  *
  * @param File $file
  */
 protected function sendFile($file)
 {
     $path = $file->getFullPath();
     if (SapphireTest::is_running_test()) {
         return file_get_contents($path);
     }
     header('Content-Description: File Transfer');
     // Quotes needed to retain spaces (http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download)
     header('Content-Disposition: inline; filename="' . basename($path) . '"');
     header('Content-Length: ' . $file->getAbsoluteSize());
     header('Content-Type: ' . HTTP::get_mime_type($file->getRelativePath()));
     header('Content-Transfer-Encoding: binary');
     // Fixes IE6,7,8 file downloads over HTTPS bug (http://support.microsoft.com/kb/812935)
     header('Pragma: ');
     if ($this->config()->min_download_bandwidth) {
         // Allow the download to last long enough to allow full download with min_download_bandwidth connection.
         increase_time_limit_to((int) (filesize($path) / ($this->config()->min_download_bandwidth * 1024)));
     } else {
         // Remove the timelimit.
         increase_time_limit_to(0);
     }
     // Clear PHP buffer, otherwise the script will try to allocate memory for entire file.
     while (ob_get_level() > 0) {
         ob_end_flush();
     }
     // Prevent blocking of the session file by PHP. Without this the user can't visit another page of the same
     // website during download (see http://konrness.com/php5/how-to-prevent-blocking-php-requests/)
     session_write_close();
     readfile($path);
     die;
 }