/** * Stream fallback delegator. * * @param string $path * @param resource $resource * @param Config $config * @param string $fallback * * @return mixed fallback result */ protected function stream($path, $resource, Config $config, $fallback) { Util::rewindStream($resource); $contents = stream_get_contents($resource); $fallbackCall = [$this, $fallback]; return call_user_func($fallbackCall, $path, $contents, $config); }
/** * {@inheritdoc} */ public function updateStream($path, $resource, array $config = []) { if (!is_resource($resource)) { throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.'); } $path = Util::normalizePath($path); $config = $this->prepareConfig($config); $this->assertPresent($path); Util::rewindStream($resource); return (bool) $this->getAdapter()->updateStream($path, $resource, $config); }
/** * Get stream as a file * * @param resource $stream * @return string Filename of resulting stream content * @throws Exception */ protected function getStreamAsFile($stream) { // Get temporary file and name $file = tempnam(sys_get_temp_dir(), 'ssflysystem'); $buffer = fopen($file, 'w'); if (!$buffer) { throw new Exception("Could not create temporary file"); } // Transfer from given stream Util::rewindStream($stream); stream_copy_to_stream($stream, $buffer); if (!fclose($buffer)) { throw new Exception("Could not write stream to temporary file"); } return $file; }
/** * Update a file with the contents of a stream. * * @param string $path * @param resource $resource * @param mixed $config Config object or visibility setting * * @throws InvalidArgumentException * * @return bool success boolean */ public function updateStream($path, $resource, array $config = []) { if (!is_resource($resource)) { throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.'); } $path = Util::normalizePath($path); $config = $this->prepareConfig($config); $this->assertPresent($path); Util::rewindStream($resource); if (!($object = $this->adapter->updateStream($path, $resource, $config))) { return false; } $this->cache->updateObject($path, $object + ['contents' => false], true); return true; }
/** * Update an existing file using a stream. * * @param string $path The path of the existing file. * @param resource $resource The file handle. * @param array $config An optional configuration array. * * @throws InvalidArgumentException If $resource is not a file handle. * @throws FileNotFoundException * * @return bool True on success, false on failure. */ public function updateStream($path, $resource, array $config = []) { $media = $this->find($path, true); $realPath = $media->realPath; if (!($destination = fopen($realPath . '.new', 'w+'))) { return false; } stream_copy_to_stream($resource, $destination); if (!fclose($destination)) { return false; } $oldPath = $realPath . 'old' . time(); if (!rename($realPath, $oldPath) || !rename($realPath . '.new', $realPath)) { return false; } unlink($oldPath); Util::rewindStream($resource); return $media !== null; }
/** * Update a file with the contents of a stream * * @param string $path * @param resource $resource * @param mixed $config Config object or visibility setting * @return bool success boolean * @throws InvalidArgumentException */ public function updateStream($path, $resource, $config = null) { if (!is_resource($resource)) { throw new InvalidArgumentException(__METHOD__ . ' expects argument #2 to be a valid resource.'); } $path = Util::normalizePath($path); $config = Util::ensureConfig($config); $config->setFallback($this->getConfig()); $this->assertPresent($path); Util::rewindStream($resource); if (!($object = $this->adapter->updateStream($path, $resource, $config))) { return false; } $this->cache->updateObject($path, $object, true); return true; }
/** * {@inheritdoc} */ public function putStream($path, $resource, $config = []) { $resource = $this->normalizeResource($resource, __METHOD__); $path = $this->normalizePath($path); $config = $this->prepareConfig($config); Flysystem\Util::rewindStream($resource); $this->doPutStream($path, $resource, $config); }