function it_uploads_all_files_to_gaufrette(Filesystem $filesystem) { $filesystem->write('myArchive/aaa/a.txt', 'Test Cocoders content 123')->shouldBeCalled(); $filesystem->write('myArchive/aaa/t.txt', 'Test Cocoders content!')->shouldBeCalled(); $filesystem->write('myArchive/bbb', 'CDE !')->shouldBeCalled(); $this->upload('myArchive', [vfsStream::url('tmp/23a/aaa/a.txt'), vfsStream::url('tmp/23a/aaa/t.txt'), vfsStream::url('tmp/23a/bbb')]); }
public function upload($name, $paths) { $rootParent = $this->fetchRootPath($paths); foreach ($paths as $path) { $key = $this->generateKey($name, $rootParent, $path); $this->filesystem->write($key, file_get_contents($path)); } }
/** * @Given file :name exists * @Given dir :name exists * @Given file :name exists with content: * @Given I write :name with content: */ public function fileExistsWithContent($name, PyStringNode $string = null) { if (null === $string) { $this->filesystem->createFile($name); } else { $this->filesystem->write($name, $string->__toString(), true); } }
/** * Saves an image. * * @param string $path * @param string $image * * @return string */ public function saveImage($image, $path = null) { if ($path == null) { $path = 'images/' . uniqid() . '.png'; } $this->files->write($path, $image, true); return $path; }
/** * @param mixed $data * @param int $mode * @return int */ public function write($data, $mode = 0) { if ($mode & FILE_APPEND) { $data = $this->read() . $data; } if (!$this->gaufrette->has($this->filePath)) { $this->gaufrette->createFile($this->filePath); } return $this->gaufrette->write($this->filePath, $data, true); }
/** * Store a file content. * * @param UploadContext $context * @param $content * @param array $metadataMap * @return UploadedFile */ public function store(UploadContext $context, $content, array $metadataMap = array()) { $name = $this->namingStrategy->getName($context); $directory = $this->storageStrategy->getDirectory($context, $name); $path = $directory . '/' . $name; $adapter = $this->filesystem->getAdapter(); if ($adapter instanceof MetadataSupporter) { $adapter->setMetadata($path, $this->resolveMetadataMap($context, $metadataMap)); } $this->filesystem->write($path, $content); $file = $this->filesystem->get($path); return new UploadedFile($this, $file); }
/** * {@inheritdoc} */ public function upload(ImageInterface $image) { if (!$image->hasFile()) { return; } if (null !== $image->getPath()) { $this->remove($image->getPath()); } do { $hash = md5(uniqid(mt_rand(), true)); $path = $this->expandPath($hash . '.' . $image->getFile()->guessExtension()); } while ($this->filesystem->has($path)); $image->setPath($path); $this->filesystem->write($image->getPath(), file_get_contents($image->getFile()->getPathname())); }
public function testWriteThrowsAnExceptionIfTheFileAlreadyExistsAndIsNotAllowedToOverwrite() { $adapter = new InMemory(array('myFile' => array())); $fs = new Filesystem($adapter); $this->setExpectedException('InvalidArgumentException'); $fs->write('myFile', 'some text'); }
/** * @param string $fileKey * @param string $fileKeyWithFormat * @param Format $format * * @return */ protected function generateThumbnail($fileKey, $fileKeyWithFormat, Format $format) { // check if has original picture try { $has = $this->fileSystem->has($fileKey); } catch (\OutOfBoundsException $e) { $has = false; } if (!$has) { throw new Exception\ImageDoesNotExistException(); } // create thumbnail try { $blobOriginal = $this->fileSystem->read($fileKey); } catch (FileNotFound $e) { throw new Exception\ImageDoesNotExistException(); } $imagine = new Imagine(); $image = $imagine->load($blobOriginal); $resizedImage = Manipulator::resize($image, $format); $extension = $this->getExtension($fileKey); $blobResizedImage = $resizedImage->get($extension, array('jpeg_quality' => 90, 'png_compression_level' => 9)); $this->fileSystem->write($fileKeyWithFormat, $blobResizedImage, true); return $blobResizedImage; }
function it_duplicates_product_media($filesystem, ProductMediaInterface $source, ProductMediaInterface $target, File $newFile) { $target->setFile(Argument::any())->shouldBeCalled(); $source->getOriginalFilename()->willReturn('akeneo.jpg'); $target->getOriginalFilename()->willReturn('akeneo.jpg'); // upload $target->getFile()->willReturn($newFile); $newFile->getPathname()->willReturn('/tmp/tmp-phpspec'); // write a fake file in tmp $adapter = new LocalAdapter('/tmp'); $fs = new Filesystem($adapter); $fs->write('tmp-phpspec', '', true); $source->getFilename()->willReturn('akeneo.jpg'); $newFile->getFilename()->willReturn('akeneo.jpg'); $filesystem->write('prefix-akeneo.jpg', '', false)->shouldBeCalled(); $target->setOriginalFilename('akeneo.jpg')->shouldBeCalled(); $target->setFilename('prefix-akeneo.jpg')->shouldBeCalled(); $filesystem->has('akeneo.jpg')->willReturn(true); $target->getFilename()->willReturn('akeneo.jpg'); $newFile->getMimeType()->willReturn('jpg'); $target->setMimeType('jpg')->shouldBeCalled(); // update original file name $source->getOriginalFilename()->willReturn('akeneo.jpg'); $target->setOriginalFilename('akeneo.jpg')->shouldBeCalled(); $this->duplicate($source, $target, 'prefix'); }
/** * @test * @group functional */ public function shouldWorkWithHiddenFiles() { $this->filesystem->write('.foo', 'hidden'); $this->assertTrue($this->filesystem->has('.foo')); $this->assertContains('.foo', $this->filesystem->keys()); $this->filesystem->delete('.foo'); $this->assertFalse($this->filesystem->has('.foo')); }
/** * Retrieve information about artist (TopAlbum and track of album) */ public function run() { $result = $this->serviceArtistInfo->setArtist($this->artistName)->getResults(); if (!isset($result->error)) { $artist = new Artist(); $artist->setName($this->artistName)->setInfo($result->artist->bio->summary); if (!is_array($result->artist->tags->tag)) { $tags = array($result->artist->tags->tag); } else { $tags = $result->artist->tags->tag; } foreach ($tags as $tagResult) { $tag = new ArtistTagDocument(); $tag->setName($tagResult->name); $artist->addTag($tag); } $albumResult = $this->serviceArtistAlbum->setArtist($this->artistName)->getResults(); if (!isset($albumResult->error)) { if (!is_array($albumResult->topalbums->album)) { $albums = array($albumResult->topalbums->album); } else { $albums = $albumResult->topalbums->album; } foreach ($albums as $albumResult) { $album = new ArtistAlbumDocument(); $album->setName($albumResult->name)->setMbid($albumResult->mbid); if (is_array($albumResult->image)) { $image = $albumResult->image[count($albumResult->image) - 1]; $var = '#text'; if ('' !== $image->{$var}) { $fileName = substr($image->{$var}, strrpos($image->{$var}, '/')); $this->fileSystem->write($fileName, file_get_contents($image->{$var})); $album->setImage($fileName); } } if ($albumResult->mbid) { $albumInfoResult = $this->serviceAlbumInfo->setArtist($this->artistName)->setMbid($albumResult->mbid)->getResults(); if (!isset($albumInfoResult->error)) { if (!is_array($albumInfoResult->album->tracks->track)) { $tracks = array($albumInfoResult->album->tracks->track); } else { $tracks = $albumInfoResult->album->tracks->track; } foreach ($tracks as $trackResult) { $track = new ArtistTrackDocument(); $track->setName($trackResult->name)->setDuration($trackResult->duration); $album->addTrack($track); } } $artist->addAlbum($album); } } } $this->artistQuery->persist($artist); } }
/** * {@inheritdoc} */ public function upload(MediaInterface $media, $name = null, $test = false) { if (!$media->hasMedia()) { return; } if (null !== $media->getName()) { $this->remove($media->getName()); } do { $hash = md5(uniqid(mt_rand(), true)); if ($test === false) { $name = $hash . '.' . $media->getMedia()->guessExtension(); } } while ($this->filesystem->has($name)); $media->setName($name); if ($test === false) { $this->filesystem->write($media->getName(), file_get_contents($media->getMedia()->getPathname())); } }
public function execute(InputInterface $input, OutputInterface $output) { $file = $input->getArgument('file'); $filesystem = new Filesystem(new Local('/')); $titlerator = new Titlerator(new Transliterator(Settings::LANG_SR), $filesystem->read($file)); $titlerator->fixEncoding(); if ($input->getOption('transliterate')) { $titlerator->transliterate(); } $filesystem->write($file, $titlerator->getText(), true); }
/** * @test * @group functional */ public function shouldFetchKeys() { $this->assertEquals(array(), $this->filesystem->keys()); $this->filesystem->write('foo', 'Some content'); $this->filesystem->write('bar', 'Some content'); $this->filesystem->write('baz', 'Some content'); $actualKeys = $this->filesystem->keys(); $this->assertEquals(3, count($actualKeys)); foreach (array('foo', 'bar', 'baz') as $key) { $this->assertContains($key, $actualKeys); } }
function it_uploads_without_removing(MediaInterface $media, Filesystem $filesystem, UploadedFile $file) { $media->hasMedia()->shouldBeCalled()->willReturn(true); $media->getName()->shouldBeCalled()->willReturn(null); $media->getMedia()->shouldBeCalled()->willReturn($file); $file->guessExtension()->shouldBeCalled()->willReturn('md'); $filesystem->has(Argument::containingString('.md'))->shouldBeCalled()->willReturn(false); $media->setName(Argument::any())->shouldBeCalled()->willReturn($media); $media->getMedia()->shouldBeCalled()->willReturn($file); $file->getPathname()->shouldBeCalled()->willReturn(__DIR__ . '/../../../../../README.md'); $filesystem->write(Argument::any(), file_get_contents(__DIR__ . '/../../../../../README.md'))->shouldBeCalled()->willReturn(Argument::type('int')); $this->upload($media); }
/** * @param string $targetPath * @param array $replacePatterns * @return mixed|void */ public function execute($targetPath = '', array $replacePatterns = array()) { $adapter = new LocalAdapter($targetPath); $filesystem = new Filesystem($adapter); $listKeys = $filesystem->listKeys(); if (count($replacePatterns) > 0) { foreach ($listKeys['keys'] as $file) { if (!@strstr(mime_content_type($targetPath . $file), 'image') && !@strstr($file, '.git') && !@strstr($file, '.svn')) { $filesystem->write($file, $this->replaceContent($replacePatterns, $filesystem->read($file)), TRUE); } } } }
function it_archives_unvalid_items(InvalidItemsCollector $collector, CsvWriter $writer, JobExecution $jobExecution, JobInstance $jobInstance, Filesystem $filesystem) { $collector->getInvalidItems()->willReturn(['items']); $jobExecution->getId()->willReturn('id'); $jobExecution->getJobInstance()->willReturn($jobInstance); $jobInstance->getType()->willReturn('type'); $jobInstance->getAlias()->willReturn('alias'); $filesystem->write('type/alias/id/invalid/invalid_items.csv', '', true)->shouldBeCalled(); $writer->setFilePath('/root/type/alias/id/invalid/invalid_items.csv')->shouldBeCalled(); $writer->initialize()->shouldBeCalled(); $writer->write(['items'])->shouldBeCalled(); $writer->flush()->shouldBeCalled(); $this->archive($jobExecution); }
/** * @param EmailAttachment $emailAttachment * * @return File|null */ protected function copyEmailAttachmentToFileSystem(EmailAttachment $emailAttachment) { $file = new File(); $file->setExtension($emailAttachment->getExtension()); $file->setOriginalFilename($emailAttachment->getFileName()); $file->setMimeType($emailAttachment->getContentType()); $file->setFilename(uniqid() . '.' . $file->getExtension()); $content = ContentDecoder::decode($emailAttachment->getContent()->getContent(), $emailAttachment->getContent()->getContentTransferEncoding()); $this->filesystem->write($file->getFilename(), $content); $f = new ComponentFile($this->getAttachmentFullPath($file->getFilename())); $file->setFile($f); $file->setFileSize($f->getSize()); $file->setUploaded(false); $file->setOwner($this->securityFacadeLink->getService()->getLoggedUser()); return $file; }
function it_create_a_file_when_reader_is_valid(CsvReader $reader, JobExecution $jobExecution, JobInstance $jobInstance, Job $job, ItemStep $step, $filesystem) { $jobExecution->getJobInstance()->willReturn($jobInstance); $jobExecution->getId()->willReturn(12); $jobInstance->getJob()->willReturn($job); $jobInstance->getType()->willReturn('type'); $jobInstance->getAlias()->willReturn('alias'); $job->getSteps()->willReturn([$step]); $step->getReader()->willReturn($reader); $reader->getFilePath()->willReturn('/tmp/tmp'); $adapter = new LocalAdapter('/tmp'); $fs = new Filesystem($adapter); $fs->write('tmp', '', true); $filesystem->write("type/alias/12/input/tmp", "", true)->shouldBeCalled(); $this->archive($jobExecution); }
/** * Push a local image/variation to the remote. * * If it is not hydrated this function will throw an exception * * @param Image $image * @param bool $overwrite * * @return $this * * @throws ImageManagerException * @throws ObjectAlreadyExistsException * @throws \Exception */ public function push(Image $image, $overwrite = true) { if (!$image->isHydrated() && $image instanceof ImageVariation) { // A pull on a variation will check if the variation exists, if not create it $this->pull($image); } if (!$image->isHydrated()) { throw new ImageManagerException(self::ERR_NOT_HYDRATED); } if (!$overwrite && $this->tagExists($image->getKey()) === true) { throw new ObjectAlreadyExistsException(self::ERR_ALREADY_EXISTS); } $adapter = $this->filesystem->getAdapter(); if ($adapter instanceof MetadataSupporter) { $metadata = []; if ($image->getMimeType()) { // Set image ContentType on remote filesystem $metadata['ContentType'] = $image->getMimeType(); } $adapter->setMetadata($image->getKey(), $metadata); } // Retrieve source image metadata $metadata = null; if (!$image instanceof ImageVariation) { $image_manipulation = new ImageInspector(); $metadata = $image_manipulation->getImageMetadata($image); } try { $this->filesystem->write($image->getKey(), $image->getData(), $overwrite); $image->__friendSet('persistent', true); $this->tag($image->getKey(), $metadata); } catch (FileAlreadyExists $e) { $this->tag($image->getKey(), $metadata); throw new ObjectAlreadyExistsException(self::ERR_ALREADY_EXISTS); } return $this; }
/** * Display the list of entities handled by Doctrine and their fields * * @param array $metadata * @param InputInterface $input * @param OutputInterface $output */ private function dumpMetadata(array $metadata, InputInterface $input, OutputInterface $output) { foreach ($metadata as $name => $meta) { $output->writeln(sprintf('<info>%s</info>', $name)); foreach ($meta['fields'] as $fieldName => $columnName) { $output->writeln(sprintf(' <comment>></comment> %s <info>=></info> %s', $fieldName, $columnName)); } } $output->writeln('---------------'); $output->writeln('---- END ----'); $output->writeln('---------------'); $output->writeln(''); if ($input->getOption('filename')) { $directory = dirname($input->getOption('filename')); $filename = basename($input->getOption('filename')); if (empty($directory) || $directory == '.') { $directory = getcwd(); } $adapter = new LocalAdapter($directory, true); $fileSystem = new Filesystem($adapter); $success = $fileSystem->write($filename, json_encode($metadata), true); if ($success) { $output->writeLn(sprintf('<info>File %s/%s successfully created</info>', $directory, $filename)); } else { $output->writeLn(sprintf('<error>File %s/%s could not be created</error>', $directory, $filename)); } } }
/** * {@inheritdoc} */ public function set($id, Response $response) { $this->filesystem->write($id, serialize($response), true); $this->filesystem->write($id . '.etag', $response->getHeader('ETag'), true); }
/** @inheritDoc */ public function putContents($path, $content) { $this->gaufrette->write($this->getGaufrettePath($path), $content, true); }
/** * @param \Gaufrette\Filesystem $filesystem */ function it_sets_size_for_new_file($filesystem) { $filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21); $this->setContent('some content'); $this->getSize()->shouldReturn(21); }
/** * @param Config $config */ public function persist(Config $config) { $this->filesystem->write($this->configFile, json_encode($config), true); }
/** * Write file in filesystem * * @param string $filename Filename * @param string $content File content * @param boolean $overwrite Overwrite file or not */ protected function write($filename, $content, $overwrite = false) { $this->filesystem->write($filename, $content, $overwrite); }
function it_returns_true_for_the_supported_job($writer, $jobExecution, $jobInstance, $job, $step) { $jobExecution->getJobInstance()->willReturn($jobInstance); $jobExecution->getId()->willReturn(12); $jobInstance->getJob()->willReturn($job); $jobInstance->getType()->willReturn('type'); $jobInstance->getAlias()->willReturn('alias'); $job->getSteps()->willReturn([$step]); $step->getWriter()->willReturn($writer); $writer->getWrittenFiles()->willReturn(['path_one']); $writer->getPath()->willReturn('/tmp/tmp'); $adapter = new LocalAdapter('/tmp'); $fs = new Filesystem($adapter); $fs->write('tmp', '', true); $this->supports($jobExecution)->shouldReturn(true); }
function it_creates_a_file_if_writer_is_correct(CsvWriter $writer, JobExecution $jobExecution, JobInstance $jobInstance, Job $job, ItemStep $step, $factory, $filesystem) { $jobExecution->getJobInstance()->willReturn($jobInstance); $jobExecution->getId()->willReturn(12); $jobInstance->getJob()->willReturn($job); $jobInstance->getType()->willReturn('type'); $jobInstance->getAlias()->willReturn('alias'); $job->getSteps()->willReturn([$step]); $step->getWriter()->willReturn($writer); $writer->getWrittenFiles()->willReturn(['/tmp/tmp' => 'tmp', '/tmp/tmp2' => 'tmp2']); $writer->getPath()->willReturn('/tmp/tmp'); $adapter = new LocalAdapter('/tmp'); $fs = new Filesystem($adapter); $fs->write('tmp', '', true); $fs->write('tmp2', '', true); $factory->createZip(Argument::any())->willReturn($filesystem); $filesystem->write('tmp', '', true)->shouldBeCalled(); $filesystem->write('tmp2', '', true)->shouldBeCalled(); $this->archive($jobExecution); }
/** * Steps necessary to store an image * * @param ObjectManager $imageObjectManager Image ObjectManager * @param ImageManager $imageManager ImageManager * @param Filesystem $filesystem Filesystem * @param fileIdentifierTransformer $fileIdentifierTransformer fileIdentifierTransformer * @param ProductInterface $product Product * @param string $imageName Image name * * @return $this Self object */ protected function storeImage(ObjectManager $imageObjectManager, ImageManager $imageManager, Filesystem $filesystem, FileIdentifierTransformerInterface $fileIdentifierTransformer, ProductInterface $product, $imageName) { $imagePath = realpath(dirname(__FILE__) . '/images/' . $imageName); $image = $imageManager->createImage(new File($imagePath)); $image->setPath('products'); $imageObjectManager->persist($image); $imageObjectManager->flush($image); $filesystem->write($fileIdentifierTransformer->transform($image), file_get_contents($imagePath), true); $product->addImage($image); $product->setPrincipalImage($image); return $this; }