/** * Download file(s) * * @return void redirect */ protected function _download() { // Incoming $render = Request::getVar('render', 'download'); $hash = Request::getVar('hash', ''); // Metadata collector $collector = array(); // Combine file and folder data $items = $this->_sortIncoming(); // Get stored remote connections $remotes = $this->_getRemoteConnections(false); // Params for repo call $params = array('subdir' => $this->subdir, 'remoteConnections' => $remotes); // Collect items if (!$items) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_NO_FILES_TO_SHOW_HISTORY')); } else { foreach ($items as $element) { foreach ($element as $type => $item) { // Get type and item name break; } // Must have a name if (trim($item) == '') { continue; } // Build metadata object $collector[] = $this->repo->getMetadata($item, $type, $params); } } // Check that we have item(s) to download if (empty($collector)) { // Throw error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } // File preview? if ($render == 'preview') { // Output HTML $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'files', 'name' => 'preview')); $view->file = isset($collector[0]) ? $collector[0] : NULL; // Get last revision if (!$view->file->get('converted') && !$hash) { $params['file'] = $view->file; $hash = $this->repo->getLastRevision($params); $view->file->set('hash', $hash); } $view->option = $this->_option; $view->model = $this->model; if (!$view->file instanceof \Components\Projects\Models\File) { $view->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_FILE_INFO_NOT_FOUND')); } return $view->loadTemplate(); } // Other rendering? if ($render == 'thumb' || $render == 'inline' || $render == 'medium') { $file = isset($collector[0]) ? $collector[0] : NULL; if (!$file instanceof \Components\Projects\Models\File) { App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } // Get last revision if (!$file->get('converted') && !$hash) { $params['file'] = $file; $hash = $this->repo->getLastRevision($params); } $image = $file->getPreview($this->model, $hash, 'fullPath', $render); // Serve image if ($image && is_file($image)) { $server = new \Hubzero\Content\Server(); $server->filename($image); $server->serve_inline($image); exit; } } // File download if (count($items) > 1) { $archive = $this->_archiveFiles($items); if (!$archive) { $this->setError($this->getError() . ' ' . Lang::txt('PLG_PROJECTS_FILES_ARCHIVE_ERROR')); } else { $downloadPath = $archive['path']; $serveas = 'Project Files ' . Date::toSql() . '.zip'; } } else { $file = isset($collector[0]) ? $collector[0] : NULL; if (!$file instanceof \Components\Projects\Models\File) { App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } $serveas = $file->get('name'); // Open converted file if (!empty($this->_remoteService) && $file->get('converted') && $this->_task == 'open') { // Is user connected? $connected = $this->_connect->getStoredParam($this->_remoteService . '_token', $this->_uid); if (!$connected) { // Redirect to connect screen \Notify::message(Lang::txt('PLG_PROJECTS_FILES_REMOTE_PLEASE_CONNECT'), 'success', 'projects'); // Redirect App::redirect(Route::url($this->model->link('files') . '&action=connect')); } // Load remote resource $this->_connect->setUser($this->model->get('owned_by_user')); $resource = $this->_connect->loadRemoteResource($this->_remoteService, $this->model->get('owned_by_user'), $file->get('remoteId')); $openLink = $resource && isset($resource['alternateLink']) ? $resource['alternateLink'] : ''; if (!$openLink) { // Throw error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND') . ' ' . $file->get('name')); } // Redirect App::redirect($openLink); } // Import & download converted file if (!empty($this->_remoteService) && $file->get('converted')) { $temp_path = sys_get_temp_dir(); // Load remote resource $this->_connect->setUser($this->model->get('owned_by_user')); $resource = $this->_connect->loadRemoteResource($this->_remoteService, $this->model->get('owned_by_user'), $file->get('remoteId')); // Tex file? $tex = Components\Projects\Helpers\Compiler::isTexFile($file->get('remoteTitle'), $file->get('originalFormat')); $cExt = $tex ? 'tex' : \Components\Projects\Helpers\Google::getGoogleImportExt($file->get('mimeType')); $url = \Components\Projects\Helpers\Google::getDownloadUrl($resource, $cExt); $data = $this->_connect->sendHttpRequest($this->_remoteService, $this->model->get('owned_by_user'), $url); // Clean up data from Windows characters - important! $data = $tex ? preg_replace('/[^(\\x20-\\x7F)\\x0A]*/', '', $data) : $data; $ftname = \Components\Projects\Helpers\Google::getImportFilename($file->get('remoteTitle'), $cExt); $serveas = $ftname; $this->_connect->fetchFile($data, $ftname, $temp_path); $downloadPath = $temp_path . DS . $ftname; } elseif ($hash) { $tempPath = 'temp-' . \Components\Projects\Helpers\Html::generateCode(4, 4, 0, 1, 0) . $serveas; $downloadPath = sys_get_temp_dir() . DS . $tempPath; // Get file content $params = array('fileName' => $file->get('localPath'), 'hash' => $hash, 'target' => $downloadPath); $this->repo->getFileContent($params); } else { // Viewing current file $serveas = urldecode(Request::getVar('serveas', $file->get('name'))); $downloadPath = $file->get('fullPath'); } } // Now we can actually download if (!empty($downloadPath)) { // Ensure the file exist if (!file_exists($downloadPath)) { // Throw error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } // Cannot download zero byte files if (filesize($downloadPath) == 0) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_ZERO_BYTE')); } // Proceed with download if (!$this->getError()) { // Initiate a new content server and serve up the file $server = new \Hubzero\Content\Server(); $server->filename($downloadPath); $server->disposition('attachment'); $server->acceptranges(false); $server->saveas($serveas); $result = $server->serve_attachment($downloadPath, $serveas, false); if (!$result) { // Should only get here on error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_SERVER_ERROR')); } else { // Clean up the /tmp directory from zip files (download multiple files) $temp_path = sys_get_temp_dir(); $matches = array(); preg_match('/^(\\/tmp.*?\\.zip)/is', $downloadPath, $matches); if (!empty($matches)) { \Hubzero\Filesystem::delete($downloadPath); } exit; } } } // Pass success or error message if ($this->getError()) { \Notify::message($this->getError(), 'error', 'projects'); } // Redirect to file list $url = $this->model->link('files') . '&action=browse'; $url .= $this->repo->isLocal() ? '' : '&repo=' . $this->repo->get('name'); $url .= $this->subdir ? '&subdir=' . urlencode($this->subdir) : ''; // Redirect App::redirect(Route::url($url)); }