/** * Process a source image and Generate a response object * * @param String $path Path to the source image * @return Boolean */ private function serveFromSource($path) { //try and load the image from the source if ($this->source->has($path)) { $file = $this->source->get($path); // Get the template object $template = $this->templates[$this->template](); // Process the image $image = $this->processImage($file, $this->imageManager, $template); // Set the headers $this->response->headers->set('Content-Type', $image->mime); $this->response->headers->set('Content-Length', strlen($image->encoded)); $lastModified = new \DateTime(); // now $this->setHttpCacheHeaders($lastModified, md5($this->getCachePath() . $lastModified->getTimestamp()), $this->maxAge); // Send the processed image in the response $this->response->setContent($image->encoded); // Setup a callback to write the processed image to the cache // This will be called after the image has been sent to the browser $this->cacheWrite = function (FilesystemInterface $cache, $path) use($image) { // use put() to write or update $cache->put($path, $image->encoded); }; return true; } return false; }
/** * Returns information about a file. * * @param string $fileIdentifier * @param array $propertiesToExtract Array of properties which are be extracted * If empty all will be extracted * @return array * @throws FileDoesNotExistException */ public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) { $relativeDriverPath = ltrim($fileIdentifier, '/'); if (!$this->filesystem->has($relativeDriverPath) || !$this->filesystem->get($relativeDriverPath)->isFile()) { throw new FileDoesNotExistException('File ' . $fileIdentifier . ' does not exist.', 1314516809); } $dirPath = PathUtility::dirname($fileIdentifier); $dirPath = $this->canonicalizeAndCheckFolderIdentifier($dirPath); return $this->extractFileInformation($relativeDriverPath, $dirPath, $propertiesToExtract); }
/** * Sets the file to stream. * * @param \SplFileInfo|string $file The file to stream * @param string $contentDisposition * @param bool $autoEtag * * @return BinaryFileResponse * * @throws FileException */ public function setFile($file, $contentDisposition = null, $autoEtag = false) { if ($file instanceof File) { $file->setFilesystem($this->filesystem); } else { $file = $this->filesystem->get($file); } if (!$this->filesystem->has($file->getPath())) { throw new FileException('File must be readable.'); } $this->file = $file; if ($autoEtag) { $this->setAutoEtag(); } if ($contentDisposition) { $this->setContentDisposition($contentDisposition); } return $this; }
/** * @param FilesystemInterface $filesystem * @param string $fileName * * @throws StorageException */ public function __construct(FilesystemInterface $filesystem, $fileName = self::DEFAULT_FILENAME) { if (!$filesystem->has($fileName)) { $filesystem->write($fileName, ''); } $handler = $filesystem->get($fileName); if (!$handler->isFile()) { throw new StorageException(sprintf('Expected path "%s" to be a file but its a "%s".', $handler->getPath(), $handler->getType())); } $this->file = $handler; }
/** * Get a file/directory handler. * * @param string $path The path to the file. * @param Handler $handler An optional existing handler to populate. * * @return Handler Either a file or directory handler. */ public function get($path, Handler $handler = null) { return $this->fileSystem->get($path, $handler); }