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); }
/** * 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; }
/** * 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; }