108 lines
2.8 KiB
PHP
108 lines
2.8 KiB
PHP
<?php
|
|
/*****************************************************************************
|
|
* Download.php
|
|
* Contains the code to download an album.
|
|
*
|
|
* Kabuki CMS (C) 2013-2019, Aaron van Geffen
|
|
*****************************************************************************/
|
|
|
|
class Download
|
|
{
|
|
public function __construct()
|
|
{
|
|
// Ensure we're logged in at this point.
|
|
$user = Registry::get('user');
|
|
if (!$user->isLoggedIn())
|
|
throw new NotAllowedException();
|
|
|
|
if (!isset($_GET['tag']))
|
|
throw new UserFacingException('No album or tag has been specified for download.');
|
|
|
|
$tag = (int)$_GET['tag'];
|
|
$album = Tag::fromId($tag);
|
|
|
|
if (isset($_SESSION['current_export']))
|
|
throw new UserFacingException('An export of "' . $tag->tag . '" is ongoing. Please try again later.');
|
|
|
|
// So far so good?
|
|
$this->exportAlbum($album);
|
|
exit;
|
|
}
|
|
|
|
private function exportAlbum(Tag $album)
|
|
{
|
|
$files = [];
|
|
|
|
$album_ids = array_merge([$album->id_tag], $this->getChildAlbumIds($album->id_tag));
|
|
foreach ($album_ids as $album_id)
|
|
{
|
|
$iterator = AssetIterator::getByOptions(['id_tag' => $album_id]);
|
|
while ($asset = $iterator->next())
|
|
$files[] = join(DIRECTORY_SEPARATOR, [$asset->getSubdir(), $asset->getFilename()]);
|
|
}
|
|
|
|
$descriptorspec = [
|
|
0 => ['pipe', 'r'], // STDIN
|
|
1 => ['pipe', 'w'], // STDOUT
|
|
];
|
|
|
|
$command = 'tar -cf - -C ' . escapeshellarg(ASSETSDIR) . ' --null -T -';
|
|
|
|
$proc = proc_open($command, $descriptorspec, $pipes, ASSETSDIR);
|
|
|
|
if(!$proc)
|
|
throw new UnexpectedValueException('Could not execute TAR command');
|
|
|
|
if(!$pipes[0])
|
|
throw new UnexpectedValueException('Could not open pipe for STDIN');
|
|
|
|
if(!$pipes[1])
|
|
throw new UnexpectedValueException('Could not open pipe for STDOUT');
|
|
|
|
// STDOUT should not block.
|
|
stream_set_blocking($pipes[1], 0);
|
|
|
|
header('Pragma: no-cache');
|
|
header('Content-Description: File Download');
|
|
header('Content-disposition: attachment; filename="' . $album->tag . '.tar"');
|
|
header('Content-Type: application/octet-stream');
|
|
header('Content-Transfer-Encoding: binary');
|
|
|
|
// Write filenames to include to STDIN, separated by null bytes.
|
|
foreach ($files as $file)
|
|
fwrite($pipes[0], $file . "\0");
|
|
|
|
// Close STDIN pipe to start archiving.
|
|
fclose($pipes[0]);
|
|
|
|
do
|
|
{
|
|
// Read STDOUT as `tar` is doing its work.
|
|
echo stream_get_contents($pipes[1], 4096);
|
|
|
|
// Are we still running?
|
|
$status = proc_get_status($proc);
|
|
}
|
|
while (!empty($status) && $status['running']);
|
|
|
|
// Close STDOUT pipe and clean up process.
|
|
fclose($pipes[1]);
|
|
|
|
proc_close($proc);
|
|
}
|
|
|
|
private function getChildAlbumIds($parent_id)
|
|
{
|
|
$ids = [];
|
|
|
|
$albums = Tag::getAlbums($parent_id, 0, PHP_INT_MAX);
|
|
foreach ($albums as $album)
|
|
{
|
|
$ids[] = $album['id_tag'];
|
|
$ids = array_merge($ids, $this->getChildAlbumIds($album['id_tag']));
|
|
}
|
|
|
|
return $ids;
|
|
}
|
|
}
|