コード例 #1
0
ファイル: BaseFileDownload.php プロジェクト: VNovotna/MP2014
 /**
  * Download the file!
  * @param IDownloader $downloader
  */
 function download(IDownloader $downloader = null)
 {
     $req = \Nette\Environment::getHttpRequest();
     $res = \Nette\Environment::getHttpResponse();
     if (self::$closeSession) {
         $ses = \Nette\Environment::getSession();
         if ($ses->isStarted()) {
             $ses->close();
         }
     }
     if ($this->getContentDisposition() == "inline" and is_null($this->enableBrowserCache)) {
         $this->enableBrowserCache = true;
     } else {
         $this->enableBrowserCache = false;
     }
     if ($downloader === null) {
         $downloaders = self::getFileDownloaders();
     } else {
         $downloaders = array($downloader);
     }
     if (count($downloaders) <= 0) {
         throw new \Nette\InvalidStateException("There is no registred downloader!");
     }
     krsort($downloaders);
     $lastException = null;
     foreach ($downloaders as $downloader) {
         if ($downloader instanceof IDownloader and $downloader->isCompatible($this)) {
             try {
                 FDTools::clearHeaders($res);
                 // Delete all headers
                 $this->transferredBytes = 0;
                 $this->onBeforeDownloaderStarts($this, $downloader);
                 $downloader->download($this);
                 // Start download
                 $this->onComplete($this, $downloader);
                 die;
                 // If all gone ok -> die
             } catch (FDSkypeMeException $e) {
                 if ($res->isSent()) {
                     throw new \Nette\InvalidStateException("Headers are already sent! Can't skip downloader.");
                 } else {
                     continue;
                 }
             } catch (Exception $e) {
                 if (!$res->isSent()) {
                     FDTools::clearHeaders($res);
                 }
                 throw $e;
             }
         }
     }
     // Pokud se soubor nějakým způsobem odešle - toto už se nespustí
     if ($lastException instanceof Exception) {
         FDTools::clearHeaders(\Nette\Environment::getHttpResponse(), TRUE);
         throw $lastException;
     }
     if ($req->getHeader("Range")) {
         FDTools::_HTTPError(416);
     } else {
         $res->setCode(500);
     }
     throw new \Nette\InvalidStateException("There is no compatible downloader (all downloader returns downloader->isComplatible()=false or was skipped)!");
 }