/** * 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; }
public function getResource($spiBinaryFileId) { try { return $this->filesystem->readStream($spiBinaryFileId); } catch (FlysystemNotFoundException $e) { throw new BinaryFileNotFoundException($spiBinaryFileId, $e); } }
/** * Set the stream response content. * @param StreamedResponse $response The response object. * @return StreamedResponse */ public function setContent(StreamedResponse $response) { $stream = $this->cache->readStream($this->path); $response->setCallback(function () use($stream) { rewind($stream); fpassthru($stream); fclose($stream); }); return $response; }
public function getFileHash($path) { $stream = $this->filesystem->readStream($path); if ($stream !== false) { $context = hash_init(self::HASH_ALGORITHM); hash_update_stream($context, $stream); return hash_final($context); } return false; }
function it_throws_an_exception_when_the_file_can_not_be_read_on_the_filesystem(FileInterface $file, FilesystemInterface $filesystem) { $file->getKey()->willReturn('path/to/file.txt'); $filesystem->has('path/to/file.txt')->willReturn(true); $filesystem->readStream('path/to/file.txt')->willReturn(false); $this->shouldThrow(new FileTransferException('Unable to fetch the file "path/to/file.txt" from the filesystem.'))->during('fetch', [$file, $filesystem]); }
/** @return resource */ private function getFileStream(UuidInterface $fileUuid) { $stream = $this->fileSystem->readStream($fileUuid->toString()); if (!$stream) { throw new \RuntimeException('Could not retrieve stream for file.'); } return $stream; }
/** * @return resource|false */ public function open() { $file = $this->getConfig(self::FILE_LOC); if ($this->fileSystem) { list($stream) = $this->fileSystem->readStream($file); return $stream; } return fopen($file, 'r+'); }
/** * 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); }
/** * @return resource */ public function getGDHandle() { if (!$this->is_image) { return null; } $size = $this->_filesystem->getSize($this->getPath()); $handle = $this->_filesystem->readStream($this->getPath()); return imagecreatefromstring(fread($handle, $size)); }
/** * 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); }
/** * {@inheritdoc} */ public function fetch(FileInterface $file, FilesystemInterface $filesystem) { if (!$filesystem->has($file->getKey())) { throw new \LogicException(sprintf('The file "%s" is not present on the filesystem.', $file->getKey())); } $localPathname = tempnam(sys_get_temp_dir(), 'raw_file_fetcher_'); if (false === ($stream = $filesystem->readStream($file->getKey()))) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $file->getKey())); } if (false === file_put_contents($localPathname, $stream)) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $file->getKey())); } return new \SplFileInfo($localPathname); }
/** * 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); }
/** * Sends the file. */ public function sendContent() { if (!$this->isSuccessful()) { parent::sendContent(); return; } if (0 === $this->maxlen) { return; } $out = fopen('php://output', 'wb'); $file = $this->filesystem->readStream($this->file->getPath()); stream_copy_to_stream($file, $out, $this->maxlen, $this->offset); fclose($out); fclose($file); }
/** * {@inheritdoc} */ public function fetch(FilesystemInterface $filesystem, $fileKey) { if (!$filesystem->has($fileKey)) { throw new \LogicException(sprintf('The file "%s" is not present on the filesystem.', $fileKey)); } if (false === ($stream = $filesystem->readStream($fileKey))) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $fileKey)); } if (!$this->tmpFilesystem->has(dirname($fileKey))) { $this->tmpFilesystem->createDir(dirname($fileKey)); } $localPathname = $this->tmpFilesystem->getAdapter()->getPathPrefix() . $fileKey; if (false === file_put_contents($localPathname, $stream)) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $fileKey)); } return new \SplFileInfo($localPathname); }
public function it_can_fetch_a_file_by_its_full_path(\PDOStatement $statement) { $uuid = Uuid::uuid4(); $name = 'the-enemy-within.ep'; $path = '/sg-1/season1'; $mime = 'stargate/sg-1'; $stream = tmpfile(); $this->pdo->prepare(new Argument\Token\StringContainsToken('FROM files'))->willReturn($statement); $statement->execute(['full_path' => $path . $name])->shouldBeCalled(); $statement->rowCount()->willReturn(1); $statement->fetch(\PDO::FETCH_ASSOC)->willReturn(['file_uuid' => $uuid->getBytes(), 'name' => $name, 'path' => $path, 'mime_type' => $mime, 'created' => date('Y-m-d H:i:s'), 'updated' => date('Y-m-d H:i:s')]); $this->fileSystem->readStream($uuid->toString())->willReturn($stream); $file = $this->getByFullPath($path . $name); $file->shouldBeAnInstanceOf(File::class); $file->getName()->toString()->shouldReturn($name); $file->getPath()->toString()->shouldReturn($path); $file->getMimeType()->toString()->shouldReturn($mime); $file->getStream()->shouldReturn($stream); }
/** * {@inheritdoc} */ public function fetch(FilesystemInterface $filesystem, $fileKey, array $options = []) { if (!$filesystem->has($fileKey)) { throw new \LogicException(sprintf('The file "%s" is not present on the filesystem.', $fileKey)); } if (false === ($stream = $filesystem->readStream($fileKey))) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $fileKey)); } if (!$this->tmpFilesystem->has(dirname($fileKey))) { $this->tmpFilesystem->createDir(dirname($fileKey)); } // TODO: we should not get the path prefix like that // TODO: it should be injected in the constructor $localPathname = $this->tmpFilesystem->getAdapter()->getPathPrefix() . $fileKey; if (false === file_put_contents($localPathname, $stream)) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $fileKey)); } return new \SplFileInfo($localPathname); }
/** * {@inheritdoc} */ public function fetch(FilesystemInterface $filesystem, $fileKey, array $options = []) { if (!isset($options['filePath']) || '' === $options['filePath']) { throw new \InvalidArgumentException('Options "filePath" has to be filled'); } if (!$filesystem->has($fileKey)) { throw new \LogicException(sprintf('The file "%s" is not present on the filesystem.', $fileKey)); } if (false === ($stream = $filesystem->readStream($fileKey))) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $fileKey)); } $filePath = DIRECTORY_SEPARATOR !== substr($options['filePath'], -1) ? $options['filePath'] . DIRECTORY_SEPARATOR : $options['filePath']; $filename = !isset($options['filename']) || '' === $options['filename'] ? basename($fileKey) : $options['filename']; $localPathname = $filePath . $filename; $localFilesystem = new Filesystem(); try { $localFilesystem->dumpFile($localPathname, $stream); } catch (IOException $e) { throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $fileKey), $e->getCode(), $e); } return new \SplFileInfo($localPathname); }
/** * Get the file's data as a stream. This uses php://temp which will write to file if the file is over a certain size, otherwise it will use memory. * @return bool|resource */ public function stream() { if ($this->size == 0 && $this->preloadLen == 0) { return false; } // temp has a memory limit, so it'll let us use bigger files a lot better. $stream = fopen('php://temp', 'r+'); if ($this->size == 0 && $this->preloadLen > 0) { fwrite($stream, $this->payload); } else { if ($this->size > 0 && $this->preloadLen > 0) { // concat preloaded and external data $fh = $this->filesystem->readStream($this->file); fseek($fh, $this->offset); // Write to our temp stream fwrite($stream, $this->payload); $remaining = $this->size; while ($remaining > 0) { $chunk = fread($fh, $remaining > 1024 ? 1024 : $remaining); fwrite($stream, $chunk); $remaining -= strlen($chunk); } fclose($fh); } else { $fh = $this->filesystem->readStream($this->file); fseek($fh, $this->offset); $read = 0; while ($read < $this->size) { $remaining = $this->size - $read; $chunk = fread($fh, $remaining > 8192 ? 8192 : $remaining); fwrite($stream, $chunk); $read += strlen($chunk); } fclose($fh); } } rewind($stream); return $stream; }
/** * Open file and return file pointer * * @param string $path file path * @param string $mode * @return resource|false **/ protected function _fopen($path, $mode = "rb") { return $this->fs->readStream($path); }
/** * @param string $containerName * @param string $fileName * @param string|null $relativePath * * @return false|resource */ public function readFileStream($containerName, $fileName, $relativePath) { $fullFileName = $this->getFullFileName($containerName, $fileName, $relativePath); return $this->filesystem->readStream($fullFileName); }
/** * Retrieves a read-stream for a path. * * @param string $path The path to the file. * * @throws FileNotFoundException * * @return resource|false The path resource or false on failure. */ public function readStream($path) { return $this->fileSystem->readStream($path); }
/** * @inheritdoc */ public function readStream($path) { $this->migrateFile($path); $path = $this->strategy->encode($path); return $this->filesystem->readStream($path); }
/** * Load the archive. * * @throws VPKException * @throws \Exception * @throws \VLib\Buffer\BufferUnderflowException */ private function load() { $this->multiChunk = strpos($this->path, '_dir') !== false; // Load it from our filesystem. This uses Flysystem to support remote files. $fh = $this->filesystem->readStream($this->path); if (!$fh) { throw new VPKException('Unable to open path ' . $this->path); } /** * Use a temporary buffer for the header data. */ $buffer = new ResourceBuffer($fh); $sig = $buffer->getInteger(); if ($sig !== 1437209140) { throw new VPKException('Invalid file signature.'); } $this->version = $buffer->getInteger(); switch ($this->version) { case 1: $headerSize = 12; break; case 2: $headerSize = 28; $buffer->skip(16); break; default: throw new VPKException('Invalid file version.'); } $this->dictSize = $buffer->getInteger(); $this->pathEntries = []; while ($type = $buffer->getString()) { while ($dir = $buffer->getString()) { while ($name = $buffer->getString()) { $crc32 = $buffer->getInteger(); $preloadSize = $buffer->getShort(); $chunkIndex = $buffer->getShort(); $offset = $buffer->getInteger(); $size = $buffer->getInteger(); $term = $buffer->getShort(); if ($term !== 0xffff) { throw new VPKException('Unexpected termination character.'); } $preload = ''; if ($preloadSize > 0) { $preload = $buffer->read($preloadSize); } if ($this->multiChunk) { $entryName = sprintf('%s_%03d.vpk', $this->vpkName, $chunkIndex); $entryName = $this->dir . '/' . $entryName; } else { $entryName = $this->file; if ($this->version == 1) { $offset += $headerSize + $this->dictSize; } } $entry = new VPKEntry($this->filesystem, $entryName); $entry->setType($type); $entry->setName($name); $entry->setDir($dir); $entry->setCRC32($crc32); $entry->setOffset($offset); $entry->setSize($size); $entry->setPreload($preload); $this->pathEntries[$entry->getPath()] = $entry; } } } }