Example #1
0
 public function getResource(Request $request, Log $logger, $path)
 {
     // Get date metadata and calculate ETag
     $timestamp = $this->fs->getTimestamp($path);
     $ETag = md5($path . $timestamp);
     $date = new DateTime("@{$timestamp}");
     $response = new Response();
     $response->setLastModified($date)->setEtag($ETag, true)->setPublic();
     // Check modified date and Etag before attempting to retrieve actual content
     if ($response->isNotModified($request)) {
         return $response;
     }
     $content = $this->fs->read($path);
     if (!$content) {
         // We had a false positive from the cache!!
         $this->deleteFromCache($path);
         $logger->warning('File not found (cache false positive)', array('path' => $path));
         abort(404);
     }
     $contentType = $this->fs->getMimetype($path);
     if (env('EXAMPLE_DIR') && starts_with($path, env('EXAMPLE_DIR'))) {
         // Path is within our "examples" directory, force the file to be downloaded
         $disposition = $response->headers->makeDisposition(HeaderBag::DISPOSITION_ATTACHMENT, basename($path));
         $response->header('Content-Disposition', $disposition);
     }
     return $response->header('Content-Type', $contentType)->setContent($content);
 }
Example #2
0
 /**
  * Set cache headers and 304 not modify if needed.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Illuminate\Http\Response $response
  */
 protected function setCacheHeaders($request, $response)
 {
     if (starts_with($request->getPathInfo(), ['/images'])) {
         $stat = stat(session()->pull('requestImagePath'));
     } else {
         if (($view = $response->getOriginalContent()) instanceof View) {
             $stat = stat($view->getPath());
         }
     }
     if (isset($stat)) {
         $response->setCache(['etag' => md5("{$stat['ino']}|{$stat['mtime']}|{$stat['size']}"), 'public' => true]);
         $response->setExpires(Carbon::now()->addDays(30));
         if (null !== ($etag = $request->headers->get('If-None-Match')) || null !== $request->headers->get('If-Modified-Since')) {
             $etags = explode('-', $etag, -1);
             $request->headers->set('If-None-Match', count($etags) ? $etags[0] . '"' : $etag);
             $response->isNotModified($request);
         }
     }
 }
 /**
  * TODO: добавить кеширование вывода
  * 
  * @param FrontendPage $frontPage
  * @return \Illuminate\View\View|null
  * @throws LayoutNotFoundException
  */
 protected function render(FrontendPage $frontPage)
 {
     event('frontend.found', [$frontPage]);
     app()->singleton('frontpage', function () use($frontPage) {
         return $frontPage;
     });
     $layout = $frontPage->getLayoutView();
     if (is_null($layout)) {
         throw new LayoutNotFoundException();
     }
     $widgetCollection = new PageWidgetCollection($frontPage);
     $html = (string) $frontPage->getLayoutView();
     if (auth()->check() and auth()->user()->hasRole(['administrator', 'developer'])) {
         $injectHTML = (string) view('cms::app.partials.toolbar');
         // Insert system HTML before closed tag body
         $matches = preg_split('/(<\\/body>)/i', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
         if (count($matches) > 1) {
             /* assemble the HTML output back with the iframe code in it */
             $html = $matches[0] . $injectHTML . $matches[1] . $matches[2];
         }
     }
     $response = new Response();
     $response->header('Content-Type', $frontPage->getMime());
     if (config('cms.show_response_sign', TRUE)) {
         $response->header('X-Powered-CMS', \CMS::NAME . '/' . \CMS::VERSION);
     }
     $response->setContent($html);
     // Set the ETag header
     $response->setEtag(sha1($html));
     $response->setLastModified($frontPage->getCreatedAt());
     // mark the response as either public or private
     $response->setPublic();
     // Check that the Response is not modified for the given Request
     if ($response->isNotModified($this->request)) {
         // return the 304 Response immediately
         return $response;
     }
     return $response;
 }
 /**
  * @param View   $layout
  * @param string $mime
  *
  * @return \Illuminate\View\View|null
  * @throws LayoutNotFoundException
  */
 protected function render(View $layout = null, $mime = 'text\\html')
 {
     if (is_null($layout)) {
         throw new LayoutNotFoundException(trans('pages::core.messages.layout_not_set'));
     }
     $html = $layout->render();
     $response = new Response();
     $response->header('Content-Type', $mime);
     if (config('cms.show_response_sign', true)) {
         $response->header('X-Powered-CMS', CMS::getFullName());
     }
     $response->setContent($html);
     // Set the ETag header
     $response->setEtag(md5($html));
     // mark the response as either public or private
     $response->setPublic();
     // Check that the Response is not modified for the given Request
     if ($response->isNotModified($this->request)) {
         // return the 304 Response immediately
         return $response;
     }
     return $response;
 }
Example #5
0
 /**
  * Display an attachment file such as image
  *
  * @param Project    $project
  * @param Issue      $issue
  * @param Attachment $attachment
  * @param Request    $request
  *
  * @return Response
  */
 public function getDisplayAttachment(Project $project, Issue $issue, Attachment $attachment, Request $request)
 {
     $issue->setRelation('project', $project);
     $attachment->setRelation('issue', $issue);
     $path = config('tinyissue.uploads_dir') . '/' . $issue->project_id . '/' . $attachment->upload_token . '/' . $attachment->filename;
     $storage = \Storage::disk('local');
     $length = $storage->size($path);
     $time = $storage->lastModified($path);
     $type = $storage->getDriver()->getMimetype($path);
     $response = new Response();
     $response->setEtag(md5($time . $path));
     $response->setExpires(new \DateTime('@' . ($time + 60)));
     $response->setLastModified(new \DateTime('@' . $time));
     $response->setPublic();
     $response->setStatusCode(200);
     $response->header('Content-Type', $type);
     $response->header('Content-Length', $length);
     $response->header('Content-Disposition', 'inline; filename="' . $attachment->filename . '"');
     $response->header('Cache-Control', 'must-revalidate');
     if ($response->isNotModified($request)) {
         // Return empty response if not modified
         return $response;
     }
     // Return file if first request / modified
     $response->setContent($storage->get($path));
     return $response;
 }