/** * Write the contents of a file. * * @param string $path * @param string|resource $contents * @param string $visibility * * @return bool */ public function put($path, $contents, $visibility = null) { $config = ['visibility' => $this->parseVisibility($visibility)]; if (is_resource($contents)) { return $this->driver->putStream($path, $contents, $config); } return $this->driver->put($path, $contents, $config); }
public function updateContent(File $file) { if (!$this->fileSystem->putStream($file->getUuid()->toString(), $file->getStream())) { throw new \RuntimeException('Failed to update file content.'); } $this->objectRepository->update(File::TYPE, $file->getUuid()); $file->metaDataSetUpdateTimestamp(new \DateTimeImmutable()); }
/** * @inheritdoc */ public function write($path, SplFileInfo $file) { $stream = fopen($file->getRealPath(), 'r+'); $result = $this->filesystem->putStream($path, $stream); if (is_resource($stream)) { fclose($stream); } return $result; }
/** * @param $filePath * @param $content */ protected function writeFile($filePath, $content) { if (is_resource($content)) { $result = $this->filesystem->putStream($filePath, $content); } else { $result = $this->filesystem->put($filePath, $content); } if (!$result) { throw new SaveResourceErrorException('File cannot be written to the path ' . $filePath); } }
/** * Write the contents of a file. * * @param string $path * @param string|resource $contents * @param array $options * @return bool */ public function put($path, $contents, $options = []) { if (is_string($options)) { $options = ['visibility' => $options]; } if ($contents instanceof File || $contents instanceof UploadedFile) { return $this->putFile($path, $contents, $options); } if (is_resource($contents)) { return $this->driver->putStream($path, $contents, $options); } else { return $this->driver->put($path, $contents, $options); } }
/** * Write the contents of a file. * * @param string $path * @param string|resource $contents * @param string $visibility * @return bool */ public function put($path, $contents, $visibility = null) { if ($contents instanceof File || $contents instanceof UploadedFile) { return $this->putFile($path, $contents, $visibility); } if ($visibility = $this->parseVisibility($visibility)) { $config = ['visibility' => $visibility]; } else { $config = []; } if (is_resource($contents)) { return $this->driver->putStream($path, $contents, $config); } else { return $this->driver->put($path, $contents, $config); } }
/** * {@inheritdoc} */ public function push(BackupInterface $backup) { $backupDirectory = $backup->getName(); if (!$this->flysystem->has($backupDirectory) && !$this->flysystem->createDir($backupDirectory)) { throw new DestinationException(sprintf('Unable to create backup directory "%s" in flysystem destination.', $backupDirectory)); } $removedBackupFiles = $this->getFiles($backupDirectory); /** * @var FileInterface $backupFile */ foreach ($backup->getFiles() as $backupFile) { if (isset($removedBackupFiles[$backupFile->getRelativePath()])) { unset($removedBackupFiles[$backupFile->getRelativePath()]); } $path = $backupDirectory . '/' . $backupFile->getRelativePath(); try { if ($this->flysystem->has($path)) { if ($backupFile->getModifiedAt() > new \DateTime('@' . $this->flysystem->getTimestamp($path))) { $resource = fopen($backupFile->getPath(), 'r'); $this->flysystem->updateStream($path, $resource); fclose($resource); } } else { $resource = fopen($backupFile->getPath(), 'r'); $this->flysystem->putStream($path, $resource); fclose($resource); } } catch (\Exception $e) { throw new DestinationException(sprintf('Unable to backup file "%s" to flysystem destination.', $backupFile->getPath()), 0, $e); } } /** * @var FileInterface $removedBackupFile */ foreach ($removedBackupFiles as $removedBackupFile) { $path = $backupDirectory . '/' . $removedBackupFile->getRelativePath(); try { $this->flysystem->delete($path); } catch (\Exception $e) { throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e); } } $this->removeEmptyDirectories($backupDirectory); if (is_array($this->backups)) { $this->backups[$backup->getName()] = $backup; } }
/** * @inheritdoc */ public function writeStream($path, $resource, $append = false) { $path = $this->strategy->encode($path); if ($append === false && $this->filesystem->has($path)) { $this->filesystem->delete($path); } $this->filesystem->putStream($path, $resource); }
/** * Create new file and write into it from file pointer. * Return new file path or false on error. * * @param resource $fp file pointer * @param string $dir target dir path * @param string $name file name * @param array $stat file stat (required by some virtual fs) * @return bool|string **/ protected function _save($fp, $dir, $name, $stat) { $path = $this->_joinPath($dir, $name); if ($this->fs->putStream($path, $fp)) { return $path; } return false; }
/** * Create new file and write into it from file pointer. * Return new file path or false on error. * * @param resource $fp file pointer * @param string $dir target dir path * @param string $name file name * @param array $stat file stat (required by some virtual fs) * @return bool|string **/ protected function _save($fp, $dir, $name, $stat) { $path = $this->_joinPath($dir, $name); $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); $config = []; if (isset(self::$mimetypes[$ext])) { $config['mimetype'] = self::$mimetypes[$ext]; } return $this->_resultPath($this->fs->putStream($path, $fp, $config), $path); return false; }
public function it_can_errors_on_upload_failure_with_update_file_content(File $file) { $uuid = Uuid::uuid4(); $stream = tmpfile(); $file->getUuid()->willReturn($uuid); $file->getStream()->willReturn($stream); $this->fileSystem->putStream($uuid->toString(), $stream)->willReturn(false); $this->objectRepository->update(File::TYPE, $uuid)->shouldNotBeCalled(); $this->shouldThrow(\RuntimeException::class)->duringUpdateContent($file); // cleanup fclose($stream); }
/** * Create a file or update if exists. * * @param string $path The path to the file. * @param resource $resource The file handle. * @param array $config An optional configuration array. * * @throws \League\Flysystem\InvalidArgumentException Thrown if $resource is not a resource. * * @return bool True on success, false on failure. */ public function putStream($path, $resource, array $config = []) { return $this->fileSystem->putStream($path, $resource, $config); }