/** * Cache the response 1 year (31536000 sec) */ protected function cacheResponse(Response $response) { $response->setSharedMaxAge(31536000); $response->setMaxAge(31536000); $response->setExpires(new \DateTime('+1 year')); return $response; }
public static function get($file = null) { $directory = env('DJEM_DEBUG', false) ? 'panel' : 'panel-compiled'; if (empty($file)) { $file = 'index.html'; } $public = realpath(__DIR__ . '/../../../' . $directory); $file = realpath($public . '/' . preg_replace('|[^-_0-9a-z/.]|i', '', $file)); if (!is_file($file) || substr($file, 0, strlen($public)) !== $public) { abort(404); } // @codeCoverageIgnore $response = new Response(file_get_contents($file), 200, ['Content-type' => self::getContentType($file)]); $response->setSharedMaxAge(3600); $response->setMaxAge(3600); $response->setExpires(new \DateTime('+1 hour')); return $response; }
/** * 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); } } }
/** * 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; }