Ejemplo n.º 1
0
 public function testLastModifiedToStringReturnsHeaderFormattedString()
 {
     $this->markTestIncomplete('LastModified needs to be completed');
     $lastModifiedHeader = new LastModified();
     // @todo set some values, then test output
     $this->assertEmpty('Last-Modified: xxx', $lastModifiedHeader->toString());
 }
 public function fileAccessAction()
 {
     $request = $this->getRequest();
     $modifiedSince = $request->getHeader('If-Modified-Since');
     if ($modifiedSince instanceof IfModifiedSince) {
         $response = $this->getResponse();
         $response->setStatusCode(304);
         $this->cacheResponse($response);
         return $response;
     }
     $event = $this->getEvent();
     $data = $event->getParam('ZFContentNegotiationParameterData');
     $id = $data->getRouteParam('file_id');
     $forceDownload = (bool) $data->getQueryParam('download', false);
     $file = $this->repository->find($id);
     if (!$file) {
         //return new ApiProblem(404, 'No file exists');
         return $this->notFoundAction();
     }
     if ($file['type'] !== 'FILE') {
         return $this->notFoundAction();
     }
     if ($file['filesystem_error']) {
         return $this->notFoundAction();
     }
     $filesystemName = $file['filesystem'];
     $filesystem = $this->manager->get($filesystemName);
     $response = new \Zend\Http\Response\Stream();
     $response->setStream($filesystem->readStream($file['filesystem_path']));
     $response->setStatusCode(200);
     $headers = $response->getHeaders();
     $headers->addHeaderLine('Content-Type', $file['mime_type']);
     if ($forceDownload) {
         $headers->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file['name'] . '"');
         //$headers->addHeaderLine('Content-Length', 7687);//$file['size']);//todo fix size
     }
     /** @var $lastModified LastModified  */
     $lastModified = new LastModified();
     $lastModified->setDate($file['create_time']);
     $headers->addHeader($lastModified);
     $this->cacheResponse($response);
     //$response->setHeaders($headers);
     return $response;
 }
Ejemplo n.º 3
0
 protected function doAction(MvcEvent $event, $metadata, $documentManager)
 {
     $options = $event->getTarget()->getOptions();
     if (!($document = $event->getParam('document'))) {
         // document not set, so load it
         $document = $documentManager->createQueryBuilder()->find($metadata->name)->field($options->getProperty())->equals($event->getParam('id'))->getQuery()->getSingleResult();
         if (!$document) {
             throw new Exception\DocumentNotFoundException();
         }
     }
     $result = new Result($document);
     if (isset($metadata->stamp['updatedOn'])) {
         $lastModified = new LastModified();
         $lastModified->setDate($metadata->getFieldValue($document, $metadata->stamp['updatedOn']));
         $result->addHeader($lastModified);
     }
     $result->addHeader($options->getCacheControl());
     $event->setResult($result);
     return $result;
 }
Ejemplo n.º 4
0
 public function testLastModifiedToStringReturnsHeaderFormattedString()
 {
     $lastModifiedHeader = new LastModified();
     $lastModifiedHeader->setDate('Sun, 06 Nov 1994 08:49:37 GMT');
     $this->assertEquals('Last-Modified: Sun, 06 Nov 1994 08:49:37 GMT', $lastModifiedHeader->toString());
 }
 protected function completeGet(array $document, ClassMetadata $metadata)
 {
     $serializer = $this->options->getSerializer();
     if (isset($metadata->rest['cache'])) {
         $cacheControl = new CacheControl();
         foreach ($metadata->rest['cache'] as $key => $value) {
             $cacheControl->addDirective($key, $value);
         }
         $this->response->getHeaders()->addHeader($cacheControl);
     }
     if (isset($metadata->stamp['updatedOn'])) {
         $lastModified = new LastModified();
         $sec = $document[$metadata->stamp['updatedOn']]->sec;
         $lastModified->setDate(new \DateTime("@{$sec}"));
         $this->response->getHeaders()->addHeader($lastModified);
     }
     return $serializer->applySerializeMetadataToArray($document, $metadata->name);
 }
 /**
  * Cache Response for future requests
  *
  * @param MvcEvent $e
  * @return \Zend\Stdlib\ResponseInterface
  */
 public function onFinish(MvcEvent $e)
 {
     $request = $e->getRequest();
     if (!$request instanceof HttpRequest) {
         return;
     }
     if (!$request->isGet()) {
         return;
     }
     $response = $e->getResponse();
     if ($response instanceof HttpResponse && !$response->isOk()) {
         return;
     }
     // Do not continue if weren't able to compose a key
     if (empty($this->cache_key)) {
         return;
     }
     if (!$this->cacheAdapter->hasItem($this->cache_key)) {
         $resourceIdentifier = $e->getRouteMatch()->getParam('resource');
         $resource = call_user_func($this->getResourceLocatorService(), $resourceIdentifier);
         if (!$resource instanceof Resource || !$resource->isCacheable()) {
             return;
         }
         // Generate Response cache headers based on Resource CacheOptions
         $cacheOptions = $resource->getCacheOptions();
         $cacheControl = new CacheControl();
         $cacheControl->addDirective($cacheOptions->getAccess());
         $cacheControl->addDirective('max-age', $cacheOptions->getMaxAge());
         $cacheControl->addDirective('expires', $cacheOptions->getExpires());
         $cacheControl->addDirective('must-revalidate');
         $dateTime = new \DateTime();
         $dateTime->modify('+ ' . $cacheOptions->getExpires() . 'seconds');
         $expires = new Expires();
         $expires->setDate($dateTime);
         $lastModified = new LastModified();
         $lastModified->setDate(new \DateTime());
         // Add Headers to Response Header
         $response->getHeaders()->addHeader($cacheControl);
         $response->getHeaders()->addHeader($expires);
         $response->getHeaders()->addHeaderLine('Pragma: ' . $cacheOptions->getAccess());
         $response->getHeaders()->addHeader(Etag::fromString('Etag: ' . md5($response->getBody())));
         $response->getHeaders()->addHeader($lastModified);
         // Set cache adapter's TTL using Resource cache expires value
         $this->cacheAdapter->getOptions()->setTtl($cacheOptions->getExpires());
         $this->cacheAdapter->setItem($this->cache_key, $response);
         //return $response;
     }
 }