Skip to content

Instantly share code, notes, and snippets.

@thelebster
Created March 14, 2014 07:01
Show Gist options
  • Save thelebster/9543218 to your computer and use it in GitHub Desktop.
Save thelebster/9543218 to your computer and use it in GitHub Desktop.
function ithra_softlayer_download_file($account, $fid) {
$file = db_select('ithra_softlayer_objectstorage', 'os')
->fields('os')
->condition('uid', $account->uid, '=')
->condition('fid', $fid, '=')
->execute()->fetchObject();
if (!$file) {
drupal_not_found();
}
else {
$path = libraries_get_path(SOFTLAYER_LIBRARY_PATH);
$objectStorageDirectory = $path . DIRECTORY_SEPARATOR . 'lib'. DIRECTORY_SEPARATOR . 'ObjectStorage';
set_include_path(get_include_path() . PATH_SEPARATOR . $objectStorageDirectory);
require_once($objectStorageDirectory . '/Util.php');
$host = variable_get('ithra_softlayer_host', ITHRA_SOFTLAYER_HOST);
$username = variable_get('ithra_softlayer_username', ITHRA_SOFTLAYER_USERNAME);
$password = variable_get('ithra_softlayer_password', ITHRA_SOFTLAYER_PASS);
$objectStorage = new ObjectStorage($host, $username, $password);
$object = $objectStorage->with(variable_get('ithra_softlayer_container_name', 'test_container') . '/' . $account->uid . '/' . $file->filename)->get();
$authData = $objectStorage->getAuthenticationData();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $object->getUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Auth-Token: {$authData->authToken}"));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
ini_set('memory_limit','128M');
$content = curl_exec($ch);
curl_close($ch);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file->filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . strlen($content));
ob_clean();
flush();
echo $content;
flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment