public function create(BinaryFileCreateStruct $binaryFileCreateStruct) { try { $this->filesystem->writeStream($binaryFileCreateStruct->id, $binaryFileCreateStruct->getInputStream(), array('mimetype' => $binaryFileCreateStruct->mimeType, 'visibility' => AdapterInterface::VISIBILITY_PUBLIC)); } catch (FileExistsException $e) { $this->filesystem->updateStream($binaryFileCreateStruct->id, $binaryFileCreateStruct->getInputStream(), array('mimetype' => $binaryFileCreateStruct->mimeType, 'visibility' => AdapterInterface::VISIBILITY_PUBLIC)); } }
/** * {@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; } }
/** * @dataProvider adapterProvider */ public function testUpdateStream(FilesystemInterface $filesystem, $adapter, $mock) { $stream = tmpfile(); $mock->shouldReceive('put')->andReturn(true, false); $mock->shouldReceive('stat')->andReturn(['type' => NET_SFTP_TYPE_DIRECTORY, 'mtime' => time(), 'size' => 20, 'permissions' => 0777]); $this->assertTrue($filesystem->updateStream('something', $stream)); $this->assertFalse($filesystem->updateStream('something_else.txt', $stream)); fclose($stream); }
/** * 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 \League\Flysystem\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 = []) { return $this->fileSystem->updateStream($path, $resource, $config); }