/** * send_zip * * takes array of full paths to medias * zips them and sends them * * @param string $name name of the zip file to be created * @param array $media_files array of full paths to medias to zip create w/ call to get_media_files */ function send_zip($name, $media_files) { /* Require needed library */ require_once AmpConfig::get('prefix') . '/modules/ZipStream/ZipStream.php'; $arc = new ZipStream\ZipStream($name . ".zip"); $options = array('comment' => AmpConfig::get('file_zip_comment')); foreach ($media_files as $dir => $files) { foreach ($files as $file) { $arc->addFileFromPath($dir . "/" . basename($file), $file, $options); } } $arc->finish(); }
/** Download remote script action. */ public function downloadAction() { while (ob_get_level() > 0) { ob_end_clean(); } $this->requireAdminPrivileges(); $this->disableLayout(); $this->disableView(); ob_start(); $zip = new \ZipStream\ZipStream('RemoteScript.zip'); $file = BASE_PATH . '/modules/remoteprocessing/remotescript/main.py'; $zip->addFileFromPath(basename($file), $file); $file = BASE_PATH . '/modules/remoteprocessing/remotescript/config.cfg'; $zip->addFileFromPath(basename($file), $file); $dirname = BASE_PATH . '/modules/remoteprocessing/remotescript/pydas/'; $dir = opendir($dirname); while ($file = readdir($dir)) { if ($file != '.' && $file != '..' && !is_dir($dirname . $file)) { $zip->addFileFromPath('pydas/' . basename($dirname . $file), $dirname . $file); } } $zip->finish(); exit; }
/** * send_zip * * takes array of full paths to medias * zips them and sends them * * @param string $name name of the zip file to be created * @param array $media_files array of full paths to medias to zip create w/ call to get_media_files */ function send_zip($name, $media_files) { /* Require needed library */ if (!@(include_once AmpConfig::get('prefix') . '/lib/vendor/maennchen/zipstream-php/src/ZipStream.php')) { throw new Exception('Missing ZipStream dependency.'); } $arc = new ZipStream\ZipStream($name . ".zip"); $options = array('comment' => AmpConfig::get('file_zip_comment')); foreach ($media_files as $dir => $files) { foreach ($files as $file) { $arc->addFileFromPath($dir . "/" . basename($file), $file, $options); } } $arc->finish(); }
/** * Index action. * * @param folders = 12-13 (will download a zip of the folder 12 and 13, recursively) * @param folders = 12, 1-13, 1 (will download a zip of the folder 12 and 13, recursively) // Need testing * @param items = 12-13 (will download a zip containing the last revisions of the items 12 and 13) * @param items = 12, 1-13 (will download a zip containing the revision 1 of item 12 and last revision of item 13) * @param items = 12, 1 (will download the revision 1 of the item 12, a zip if there are multiple bitstream or simply the file) * @param bitstream = 1 (will download related bitstream) * @param offset The offset in bytes if downloading a bitstream (defaults to 0) * @param name Alternate filename when downloading a bitstream (defaults to bitstream name) * @throws Zend_Exception */ public function indexAction() { $this->disableLayout(); $itemIds = $this->getParam('items'); $folderIds = $this->getParam('folders'); $bitsreamid = $this->getParam('bitstream'); $sessionUser = $this->userSession->Dao; $testingMode = Zend_Registry::get('configGlobal')->environment == 'testing'; if ($sessionUser != null) { // Make sure this is a copy and not a reference $sessionUser = $this->User->load($sessionUser->getKey()); } else { // see if module can authenticate with a special parameter $authToken = $this->getParam('authToken'); if (isset($authToken)) { $responses = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_PARAMETER_AUTHENTICATION', array('authToken' => $authToken)); foreach ($responses as $user) { $sessionUser = $user; break; } } } $offset = $this->getParam('offset'); if (!isset($offset)) { $offset = 0; } if (isset($bitsreamid) && is_numeric($bitsreamid)) { $name = $this->getParam('name'); $bitstream = $this->Bitstream->load($bitsreamid); if (!$bitstream) { throw new Zend_Exception('Invalid bitstream id', 404); } if (isset($name)) { $bitstream->setName($name); // don't save name, just set it on this dao } $revision = $bitstream->getItemrevision(); $item = $revision->getItem(); if ($item == false || !$this->Item->policyCheck($item, $sessionUser)) { throw new Zend_Exception('Permission denied'); } $this->Component->DownloadBitstream->download($bitstream, $offset, true); return; } if (!isset($itemIds) && !isset($folderIds)) { throw new Zend_Exception('No parameters, expecting itemIds or folderIds.'); } $folderIds = explode('-', $folderIds); $folders = $this->Folder->load($folderIds); $item = null; $itemIds = explode('-', $itemIds); $revisions = array(); if (!empty($itemIds)) { foreach ($itemIds as $itemId) { // check revision $tmp = explode(',', $itemId); if (empty($tmp[0])) { continue; } $item = $this->Item->load($tmp[0]); if (!$item) { throw new Zend_Exception('Item does not exist: ' . $tmp[0], 404); } if (!$this->Item->policyCheck($item, $sessionUser)) { throw new Zend_Exception('Read permission required on item ' . $tmp[0], 403); } if (isset($tmp[1])) { $tmp = $this->Item->getRevision($item, $tmp[1]); if ($tmp !== false) { $revisions[] = $tmp; } } else { $tmp = $this->Item->getLastRevision($item); if ($tmp !== false) { $revisions[] = $tmp; } } } } if (empty($folders) && empty($revisions)) { // download an empty zip with the name of item (if it exists), then exit $this->_downloadEmptyItem($item); } elseif (empty($folders) && count($revisions) == 1) { $revision = $revisions[0]; $bitstreams = $revision->getBitstreams(); if ($testingMode) { $bitstreams = array($bitstreams[0]); } if (count($bitstreams) == 0) { // download an empty zip with the name of item (if it exists), then exit $this->Item->incrementDownloadCount($revision->getItem()); $this->_downloadEmptyItem($revision->getItem()); } elseif (count($bitstreams) == 1) { if (preg_match('/^https?:\\/\\//', $bitstreams[0]->getPath())) { $this->redirect($bitstreams[0]->getPath()); return; } $this->disableView(); $this->Component->DownloadBitstream->testingmode = $testingMode; $this->Component->DownloadBitstream->download($bitstreams[0], $offset, true); } else { ob_start(); $this->_helper->viewRenderer->setNoRender(); $name = $revision->getItem()->getName(); $name = substr($name, 0, 50); session_write_close(); // unlock session writing for concurrent access $zip = new \ZipStream\ZipStream($name . '.zip'); foreach ($bitstreams as $bitstream) { $filename = $bitstream->getName(); $path = $bitstream->getAssetstore()->getPath() . '/' . $bitstream->getPath(); Zend_Registry::get('dbAdapter')->closeConnection(); $zip->addFileFromPath($filename, $path); } $zip->finish(); $this->Item->incrementDownloadCount($revision->getItem()); exit; } } else { $this->_helper->viewRenderer->setNoRender(); if (count($folders) == 1 && empty($revisions)) { $name = $folders[0]->getName(); $name = substr($name, 0, 50); $rootFolder = $this->Folder->getRoot($folders[0]); if ($rootFolder) { // Find the Community or the User which have the folder $rootCom = $this->Community->getByFolder($rootFolder); if (!$rootCom) { $user = $this->User->getByFolder($rootFolder); $name = $user->getFirstname() . $user->getLastname() . '-' . $name; } else { $name = $rootCom->getName() . '-' . $name; } } } else { $name = 'Custom'; } session_write_close(); // unlock session writing for concurrent access ob_start(); $zip = new \ZipStream\ZipStream($name . '.zip'); UtilityComponent::disableMemoryLimit(); foreach ($revisions as $revision) { $item = $revision->getItem(); $bitstreams = $revision->getBitstreams(); $count = count($bitstreams); foreach ($bitstreams as $bitstream) { if ($count > 1 || $bitstream->getName() != $item->getName()) { $path = $item->getName() . '/'; } else { $path = ''; } $filename = $path . $bitstream->getName(); $fullpath = $bitstream->getAssetstore()->getPath() . '/' . $bitstream->getPath(); Zend_Registry::get('dbAdapter')->closeConnection(); $zip->addFileFromPath($filename, $fullpath); } $this->Item->incrementDownloadCount($item); unset($item); unset($bitstreams); } foreach ($folders as $folder) { if (!$this->Folder->policyCheck($folder, $sessionUser)) { continue; } $this->Folder->zipStream($zip, $folder->getName(), $folder, $sessionUser); } $zip->finish(); exit; } }