/** * Create the response. * * @param \League\Flysystem\FilesystemInterface $cache The cache file system. * @param string $path The cached file path. * * @return \Cake\Network\Response The response object. */ public function create(FilesystemInterface $cache, $path) { $stream = $cache->readStream($path); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); $response = new Response(); $response->type($contentType); $response->header('Content-Length', $contentLength); $response->body(function () use($stream) { rewind($stream); fpassthru($stream); fclose($stream); }); return $response; }
/** * {@inheritdoc} */ public function find($path) { if ($this->filesystem->has($path) === false) { throw new NotLoadableException(sprintf('Source image "%s" not found.', $path)); } $mimeType = $this->filesystem->getMimetype($path); return new Binary($this->filesystem->read($path), $mimeType, $this->extensionGuesser->guess($mimeType)); }
/** * 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; }
/** * Return stat for given path. * Stat contains following fields: * - (int) size file size in b. required * - (int) ts file modification time in unix time. required * - (string) mime mimetype. required for folders, others - optionally * - (bool) read read permissions. required * - (bool) write write permissions. required * - (bool) locked is object locked. optionally * - (bool) hidden is object hidden. optionally * - (string) alias for symlinks - link target path relative to root path. optionally * - (string) target for symlinks - link target path. optionally * * If file does not exists - returns empty array or false. * * @param string $path file path * @return array|false **/ protected function _stat($path) { $stat = array('mime' => 'directory', 'ts' => time(), 'read' => true, 'write' => true, 'locked' => false, 'hidden' => false, 'size' => 0); // If root, just return from above if ($this->root == $path) { $stat['name'] = $this->root; return $stat; } // If not exists, return empty if (!$this->fs->has($path)) { return array(); } $meta = $this->fs->getMetadata($path); // Get timestamp/size $stat['ts'] = isset($meta['timestamp']) ? $meta['timestamp'] : $this->fs->getTimestamp($path); $stat['size'] = isset($meta['size']) ? $meta['size'] : $this->fs->getSize($path); // Check if file, if so, check mimetype if ($meta['type'] == 'file') { $stat['mime'] = isset($meta['mimetype']) ? $meta['mimetype'] : $this->fs->getMimetype($path); $imgMimes = ['image/jpeg', 'image/png', 'image/gif']; if ($this->urlBuilder && in_array($stat['mime'], $imgMimes)) { $stat['url'] = $this->urlBuilder->getUrl($path, ['ts' => $stat['ts']]); $stat['tmb'] = $this->urlBuilder->getUrl($path, ['ts' => $stat['ts'], 'w' => $this->tmbSize, 'h' => $this->tmbSize, 'fit' => $this->options['tmbCrop'] ? 'crop' : 'contain']); } } if (!isset($stat['url']) && $this->fs->getUrl()) { $stat['url'] = 1; } return $stat; }
/** * Return stat for given path. * Stat contains following fields: * - (int) size file size in b. required * - (int) ts file modification time in unix time. required * - (string) mime mimetype. required for folders, others - optionally * - (bool) read read permissions. required * - (bool) write write permissions. required * - (bool) locked is object locked. optionally * - (bool) hidden is object hidden. optionally * - (string) alias for symlinks - link target path relative to root path. optionally * - (string) target for symlinks - link target path. optionally * * If file does not exists - returns empty array or false. * * @param string $path file path * @return array|false **/ protected function _stat($path) { $stat = array('size' => 0, 'ts' => time(), 'read' => true, 'write' => true, 'locked' => false, 'hidden' => false, 'mime' => 'directory'); // If root, just return from above if ($this->root == $path) { $stat['name'] = $this->root; return $stat; } // If not exists, return empty if (!$this->fs->has($path)) { // Check if the parent doesn't have this path if ($this->_dirExists($path)) { return $stat; } // Neither a file or directory exist, return empty return array(); } try { $meta = $this->fs->getMetadata($path); } catch (\Exception $e) { return array(); } if (false === $meta) { return $stat; } // Set item filename.extension to `name` if exists if (isset($meta['filename']) && isset($meta['extension'])) { $stat['name'] = $meta['filename']; if ($meta['extension'] !== '') { $stat['name'] .= '.' . $meta['extension']; } } // Get timestamp/size if available if (isset($meta['timestamp'])) { $stat['ts'] = $meta['timestamp']; } if (isset($meta['size'])) { $stat['size'] = $meta['size']; } // Check if file, if so, check mimetype when available if ($meta['type'] == 'file') { if (isset($meta['mimetype'])) { $stat['mime'] = $meta['mimetype']; } else { $stat['mime'] = $this->fs->getMimetype($path); } $imgMimes = ['image/jpeg', 'image/png', 'image/gif']; if ($this->urlBuilder && in_array($stat['mime'], $imgMimes)) { $stat['url'] = $this->urlBuilder->getUrl($path, ['ts' => $stat['ts']]); $stat['tmb'] = $this->urlBuilder->getUrl($path, ['ts' => $stat['ts'], 'w' => $this->tmbSize, 'h' => $this->tmbSize, 'fit' => $this->options['tmbCrop'] ? 'crop' : 'contain']); } } if (!isset($stat['url']) && $this->fs->getUrl()) { $stat['url'] = 1; } return $stat; }
/** * @override * @inheritDoc */ public function getMimetype($path) { try { return $this->fs->getMimetype($path); } catch (Error $ex) { } catch (Exception $ex) { } throw new ReadException("File {$path} mimetype could not be determined.", $ex); }
/** * Create response. * @param FilesystemInterface $cache Cache file system. * @param string $path Cached file path. * @return Response Response object. */ public function create(FilesystemInterface $cache, $path) { $stream = $this->streamCallback->__invoke($cache->readStream($path)); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); $cacheControl = 'max-age=31536000, public'; $expires = date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT'; return $this->response->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength)->withHeader('Cache-Control', $cacheControl)->withHeader('Expires', $expires); }
/** * Generate and output image. * @param string $path Image path. * @param array $params Image manipulation params. * @throws InvalidArgumentException */ public function outputImage($path, array $params) { $path = $this->makeImage($path, $params); header('Content-Type:' . $this->cache->getMimetype($path)); header('Content-Length:' . $this->cache->getSize($path)); header('Cache-Control:' . 'max-age=31536000, public'); header('Expires:' . date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT'); $stream = $this->cache->readStream($path); rewind($stream); fpassthru($stream); fclose($stream); }
/** * Create response. * * @param \League\Flysystem\FilesystemInterface $cache Cache file system. * @param string $path Cached file path. * * @return \Psr\Http\Message\ResponseInterface Response object. */ public function create(FilesystemInterface $cache, $path) { $stream = new Stream($cache->readStream($path)); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); if ($contentType === false) { throw new FilesystemException('Unable to determine the image content type.'); } if ($contentLength === false) { throw new FilesystemException('Unable to determine the image content length.'); } return (new Response())->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength); }
/** * Create response. * @param FilesystemInterface $cache Cache file system. * @param string $path Cached file path. * @return ResponseInterface Response object. */ public function create(FilesystemInterface $cache, $path) { $stream = $this->streamCallback->__invoke($cache->readStream($path)); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); $cacheControl = 'max-age=31536000, public'; $expires = date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT'; if ($contentType === false) { throw new FilesystemException('Unable to determine the image content type.'); } if ($contentLength === false) { throw new FilesystemException('Unable to determine the image content length.'); } return $this->response->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength)->withHeader('Cache-Control', $cacheControl)->withHeader('Expires', $expires); }
protected function uploadFile(UploadedFileInterface $content, $mime, $filePath) { $uri = $content->getStream()->getMetadata('uri'); if (!$uri) { throw new SaveResourceErrorException('Unknown error: can\'t get uploaded file uri'); } $uploadedMime = $this->filesystem->getMimetype($uri); if ($mime !== $uploadedMime) { /** * Try to remove unnecessary file because UploadFile object can be emulated * @see \Staticus\Middlewares\ActionPostAbstract::download */ $this->filesystem->delete($uri); throw new WrongRequestException('Bad request: incorrect mime-type of the uploaded file'); } $content->moveTo($filePath); }
/** * Generate an {@see SS_HTTPResponse} for the given file from the source filesystem * @param FilesystemInterface $flysystem * @param string $fileID * @return SS_HTTPResponse */ protected function createResponseFor(FilesystemInterface $flysystem, $fileID) { // Build response body // @todo: gzip / buffer response? $body = $flysystem->read($fileID); $mime = $flysystem->getMimetype($fileID); $response = new SS_HTTPResponse($body, 200); // Add headers $response->addHeader('Content-Type', $mime); $headers = Config::inst()->get(get_class($this), 'file_response_headers'); foreach ($headers as $header => $value) { $response->addHeader($header, $value); } return $response; }
/** * Get the file's mimetype. * * @return string */ public function getMimetype() { return $this->filesystem->getMimetype($this->path); }
/** * Get a file's mime-type. * * @param string $path The path to the file. * * @throws FileNotFoundException * * @return string|false The file mime-type or false on failure. */ public function getMimetype($path) { return $this->fileSystem->getMimetype($path); }
public function getMimeType($spiBinaryFileId) { return $this->filesystem->getMimetype($spiBinaryFileId); }
/** * Get the mime-type of a given file. * * @param string $path * @return string|false */ public function mimeType($path) { return $this->driver->getMimetype($path); }
/** * @param string $path * * @return \livetyping\hermitage\foundation\entities\Image * @throws \livetyping\hermitage\foundation\exceptions\ImageNotFoundException */ public function get(string $path) : Image { $this->assertPresent($path); $image = new Image($this->filesystem->read($path), $this->filesystem->getMimetype($path), $path); return $image; }