/** * * * @param string $ID * @param string $ServeFile */ public function index($ID = '', $ServeFile = '0') { $this->addJsFile('jquery.js'); // Define the item being downloaded if (strtolower($ID) == 'vanilla') { $ID = 'vanilla-core'; } $UrlFilename = Gdn::request()->filename(); $PathInfo = pathinfo($UrlFilename); $Ext = val('extension', $PathInfo); if ($Ext == 'zip') { $ServeFile = '1'; $ID = $Ext = val('filename', $PathInfo); } // Find the requested addon $this->Addon = $this->AddonModel->getSlug($ID, true); $this->setData('Addon', $this->Addon); if (!is_array($this->Addon) || !val('File', $this->Addon)) { $this->Addon = array('Name' => 'Not Found', 'Version' => 'undefined', 'File' => ''); } else { $AddonID = $this->Addon['AddonID']; if ($ServeFile != '1') { $this->addJsFile('get.js'); } if ($ServeFile == '1') { // Record this download $this->Database->sql()->insert('Download', array('AddonID' => $AddonID, 'DateInserted' => Gdn_Format::toDateTime(), 'RemoteIp' => @$_SERVER['REMOTE_ADDR'])); $this->AddonModel->setProperty($AddonID, 'CountDownloads', $this->Addon['CountDownloads'] + 1); if (val('Slug', $this->Addon)) { $Filename = $this->Addon['Slug']; } else { $Filename = "{$this->Addon['Name']}-{$this->Addon['Version']}"; } $Filename = Gdn_Format::url($Filename) . '.zip'; $File = $this->Addon['File']; $Url = Gdn_Upload::url($File); Gdn_FileSystem::serveFile($Url, $Filename); } } $this->addModule('AddonHelpModule'); $this->render(); }
/** * * * @param DiscussionController $Sender */ public function discussionController_download_create($Sender) { if (!$this->CanDownload) { throw PermissionException("File could not be streamed: Access is denied"); } list($MediaID) = $Sender->RequestArgs; $Media = $this->mediaModel()->getID($MediaID); if (!$Media) { return; } $Filename = Gdn::request()->filename(); if (!$Filename || $Filename == 'default') { $Filename = $Media->Name; } $DownloadPath = combinePaths(array(MediaModel::pathUploads(), val('Path', $Media))); if (in_array(strtolower(pathinfo($Filename, PATHINFO_EXTENSION)), array('bmp', 'gif', 'jpg', 'jpeg', 'png'))) { $ServeMode = 'inline'; } else { $ServeMode = 'attachment'; } $Served = false; $this->EventArguments['DownloadPath'] = $DownloadPath; $this->EventArguments['ServeMode'] = $ServeMode; $this->EventArguments['Media'] = $Media; $this->EventArguments['Served'] =& $Served; $this->fireEvent('BeforeDownload'); if (!$Served) { return Gdn_FileSystem::serveFile($DownloadPath, $Filename, $Media->Type, $ServeMode); throw new Exception('File could not be streamed: missing file (' . $DownloadPath . ').'); } exit; }