/** * Download all monograph files as an archive * @param $monographId integer * @param $monographFiles ArrayItemIterator * @return boolean */ function downloadFilesArchive($monographId, &$monographFiles) { $filesDir = MonographFileManager::_getFilesDir($monographId); $filePaths = array(); while ($monographFile =& $monographFiles->next()) { /* @var $monographFile MonographFile */ // Remove absolute path so the archive doesn't include it (otherwise all files are organized by absolute path) $filePath = str_replace($filesDir, '', $monographFile->getFilePath()); // Add files to be archived to array $filePaths[] = escapeshellarg($filePath); } // Create the archive and download the file $archivePath = $filesDir . "monograph_" . $monographId . "_files.tar.gz"; $tarCommand = "tar czf " . $archivePath . " -C \"" . $filesDir . "\" " . implode(" ", $filePaths); exec($tarCommand); if (file_exists($archivePath)) { parent::downloadFile($archivePath); return true; } else { return false; } }
/** * Download all of the monograph files as one compressed file. * @param $args array * @param $request Request */ function downloadAllFiles($args, &$request) { // Retrieve the monograph. $monograph =& $this->getMonograph(); $monographId = $monograph->getId(); // Find out the paths of all files in this grid. import('classes.file.MonographFileManager'); $filesDir = MonographFileManager::_getFilesDir($monographId); $filePaths = array(); foreach ($this->getGridDataElements($request) as $submissionFileData) { $monographFile =& $submissionFileData['submissionFile']; /* @var $monographFile MonographFile */ // Remove absolute path so the archive doesn't include it (otherwise all files are organized by absolute path) $filePath = str_replace($filesDir, '', $monographFile->getFilePath()); // Add files to be archived to array $filePaths[] = escapeshellarg($filePath); unset($monographFile); } // Create a temporary file. $archivePath = tempnam('/tmp', 'sf-'); // Create the archive and download the file. exec(Config::getVar('cli', 'tar') . ' -c -z ' . '-f ' . escapeshellarg($archivePath) . ' ' . '-C ' . escapeshellarg($filesDir) . ' ' . implode(' ', array_map('escapeshellarg', $filePaths))); if (file_exists($archivePath)) { FileManager::downloadFile($archivePath); FileManager::deleteFile($archivePath); } else { fatalError('Creating archive with submission files failed!'); } }