public function setLastModified($timestamp = 0, $max_age = 0) { $time = new \DateTime('@' . $timestamp); $etag = md5($this->builder->getTheme()->getDir() . $this->builder->getStyle() . '|' . $timestamp . '|' . $max_age); $this->response->headers->addCacheControlDirective('must-revalidate', true); $this->response->setLastModified($time); $this->response->setEtag($etag); $this->response->setMaxAge($max_age); }
/** * Set the streamed response headers. * @param StreamedResponse $response The response object. * @return StreamedResponse */ public function setHeaders(StreamedResponse $response) { $response->headers->set('Content-Type', $this->cache->getMimetype($this->path)); $response->headers->set('Content-Length', $this->cache->getSize($this->path)); $response->setPublic(); $response->setMaxAge(31536000); $response->setExpires(date_create()->modify('+1 years')); $response->setLastModified(date_create()->setTimestamp($this->cache->getTimestamp($this->path))); $response->isNotModified($this->request); return $response; }
public function execute(Request $request, WorkingFolder $workingFolder, EventDispatcher $dispatcher, Config $config) { $fileName = (string) $request->query->get('fileName'); $thumbnailFileName = (string) $request->query->get('thumbnail'); if (!File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException(sprintf('Invalid file name: %s', $fileName)); } $cacheLifetime = (int) $request->query->get('cache'); if (!$workingFolder->containsFile($fileName)) { throw new FileNotFoundException(); } if ($thumbnailFileName) { if (!File::isValidName($thumbnailFileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException(sprintf('Invalid resized image file name: %s', $fileName)); } if (!$workingFolder->getResourceType()->isAllowedExtension(pathinfo($thumbnailFileName, PATHINFO_EXTENSION))) { throw new InvalidExtensionException(); } $resizedImageRespository = $this->app->getResizedImageRepository(); $file = $resizedImageRespository->getExistingResizedImage($workingFolder->getResourceType(), $workingFolder->getClientCurrentFolder(), $fileName, $thumbnailFileName); $dataStream = $file->readStream(); } else { $file = new DownloadedFile($fileName, $this->app); $file->isValid(); $dataStream = $workingFolder->readStream($file->getFilename()); } $proxyDownload = new ProxyDownloadEvent($this->app, $file); $dispatcher->dispatch(CKFinderEvent::PROXY_DOWNLOAD, $proxyDownload); if ($proxyDownload->isPropagationStopped()) { throw new AccessDeniedException(); } $response = new StreamedResponse(); $response->headers->set('Content-Type', $file->getMimeType()); $response->headers->set('Content-Length', $file->getSize()); $response->headers->set('Content-Disposition', 'inline; filename="' . $fileName . '"'); if ($cacheLifetime > 0) { Utils::removeSessionCacheHeaders(); $response->setPublic(); $response->setEtag(dechex($file->getTimestamp()) . "-" . dechex($file->getSize())); $lastModificationDate = new \DateTime(); $lastModificationDate->setTimestamp($file->getTimestamp()); $response->setLastModified($lastModificationDate); if ($response->isNotModified($request)) { return $response; } $response->setMaxAge($cacheLifetime); $expireTime = new \DateTime(); $expireTime->modify('+' . $cacheLifetime . 'seconds'); $response->setExpires($expireTime); } $chunkSize = 1024 * 100; $response->setCallback(function () use($dataStream, $chunkSize) { if ($dataStream === false) { return false; } while (!feof($dataStream)) { echo fread($dataStream, $chunkSize); flush(); @set_time_limit(8); } return true; }); return $response; }