Last active
May 3, 2017 14:34
-
-
Save mootari/463a0e5ec31f7db314c323ac47d26d3a to your computer and use it in GitHub Desktop.
Barracuda/ArchiveStream: Wrapping remote files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\example; | |
class TarArchive extends \Barracuda\ArchiveStream\TarArchive { | |
public function add_remote_file($name, $path, $size, array $opt = array()) | |
{ | |
// send file header | |
$this->init_file_stream_transfer($name, $size, $opt); | |
// open input file | |
$fh = fopen($path, 'rb'); | |
// send file blocks | |
while ($data = fread($fh, $this->block_size)) { | |
// send data | |
$this->stream_file_part($data); | |
} | |
// close input file | |
fclose($fh); | |
// complete the file stream | |
$this->complete_file_stream(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param $archive_name | |
* The name of the Zip archive. | |
* @param array $files | |
* A list of files. Each file item is an array having the following keys: | |
* - "name": Required. The full file path inside the Zip archive. | |
* - "size": Required. The file size. | |
* - "path": Required. The Source file path. | |
* - "callback": Optional. A callback function that will be invoked prior to | |
* processing the file item. The function receives the complete file item | |
* and must return an array containing the keys "name", "path" and "size". | |
*/ | |
function example_serve($archive_name, array $files) { | |
$options = array( | |
'large_files_only' => true, | |
); | |
drupal_set_time_limit(0); | |
$user_agent = (isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : ''); | |
$archive = strpos($user_agent, 'macintosh') !== false | |
? new \Drupal\example\TarArchive("$archive_name.tar", $options) | |
: new \Drupal\example\ZipArchive("$archive_name.zip", $options); | |
foreach($files as $data) { | |
if(isset($data['callback'])) { | |
$data = $data['callback']($data); | |
if(!$data) { | |
continue; | |
} | |
} | |
$archive->add_remote_file($data['name'], $data['path'], $data['size']); | |
} | |
$archive->finish(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment