/**
  * Action to host a file. The filename is provided by the arguments as tokens
  * @return null
  */
 public function indexAction()
 {
     // get the requested path of the file
     $args = func_get_args();
     $path = implode(Request::QUERY_SEPARATOR, $args);
     if (empty($path)) {
         // no path provided, redirect to the home page
         $this->response->setRedirect($this->request->getBaseUrl(), Response::STATUS_CODE_MOVED_PERMANENTLY);
         return;
     }
     // lookup the file
     $file = $this->getFile($path);
     if (!$file) {
         // file not found, set status code
         $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
         return;
     }
     if ($file->getExtension() == 'php') {
         // the file is a PHP script, execute it
         require_once $file->getAbsolutePath();
         return;
     }
     // get needed file properties
     $fileModificationTime = $file->getModificationTime();
     $fileSize = $file->getSize();
     // set cache headers
     $eTag = md5($path . '-' . $fileModificationTime . '-' . $fileSize);
     $maxAge = 3600;
     // an hour
     $expirationTime = time() + $maxAge;
     $this->response->setETag($eTag);
     $this->response->setLastModified($fileModificationTime);
     $this->response->setExpires($expirationTime);
     $this->response->setMaxAge($maxAge);
     $this->response->setSharedMaxAge($maxAge);
     if ($this->response->isNotModified($this->request)) {
         // content is not modified, stop processing
         $this->response->setNotModified();
         return;
     }
     // set content headers
     $mime = Mime::getMimeType($this->zibo, $file);
     $this->response->setHeader(Header::HEADER_CONTENT_TYPE, $mime);
     $this->response->setHeader(Header::HEADER_CONTENT_LENGTH, $fileSize);
     if ($this->request->getMethod() != Request::METHOD_HEAD) {
         // don't send content when this is a HEAD request
         $this->response->setView(new FileView($file));
     }
 }
Example #2
0
 /**
  * @dataProvider providerGetMimeType
  */
 public function testGetMimeType($expected, File $file)
 {
     $result = Mime::getMimeType($this->zibo, $file);
     $this->assertEquals($expected, $result);
 }