/**
  * @param Request $request
  * @param string  $filename
  *
  * @throws NotFoundHttpException If media is not found
  *
  * @return Response
  */
 public function showAction(Request $request, $filename)
 {
     if (!$this->filesystem->has($filename)) {
         throw new NotFoundHttpException(sprintf('Media "%s" not found', $filename));
     }
     $response = new Response($content = $this->filesystem->read($filename));
     $mime = $this->filesystem->mimeType($filename);
     if (($filter = $request->query->get('filter')) && null !== $mime && 0 === strpos($mime, 'image')) {
         try {
             $cachePath = $this->cacheManager->resolve($request, $filename, $filter);
             if ($cachePath instanceof Response) {
                 $response = $cachePath;
             } else {
                 $image = $this->imagine->load($content);
                 $response = $this->filterManager->get($request, $filter, $image, $filename);
                 $response = $this->cacheManager->store($response, $cachePath, $filter);
             }
         } catch (\RuntimeException $e) {
             if (0 === strpos($e->getMessage(), 'Filter not defined')) {
                 throw new HttpException(404, sprintf('The filter "%s" cannot be found', $filter), $e);
             }
             throw $e;
         }
     }
     if ($mime) {
         $response->headers->set('Content-Type', $mime);
     }
     return $response;
 }
예제 #2
0
 public function testWriteThrowsAnExceptionIfTheFileAlreadyExistsAndIsNotAllowedToOverwrite()
 {
     $adapter = new InMemory(array('myFile' => array()));
     $fs = new Filesystem($adapter);
     $this->setExpectedException('InvalidArgumentException');
     $fs->write('myFile', 'some text');
 }
예제 #3
0
 /**
  * @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;
 }
예제 #4
0
 function let(Filesystem $filesystem, ImageInterface $image)
 {
     $filesystem->has(Argument::any())->willReturn(false);
     $file = new File(__FILE__, 'img.jpg');
     $image->getFile()->willReturn($file);
     $this->beConstructedWith($filesystem);
 }
 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')]);
 }
예제 #6
0
 private function read(Filesystem $fs)
 {
     $profileFilename = Application::PROFILE_FILENAME;
     if ($fs->has($profileFilename)) {
         $this->processProfileContent($fs->read($profileFilename));
     }
 }
 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');
 }
 /**
  * {@inheritdoc}
  */
 public function getArchive(JobExecution $jobExecution, $key)
 {
     $archives = $this->getArchives($jobExecution);
     if (!isset($archives[$key])) {
         throw new \InvalidArgumentException(sprintf('Key "%s" does not exist', $key));
     }
     return $this->filesystem->createStream($archives[$key]);
 }
 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));
     }
 }
예제 #10
0
 /**
  * delete the given file
  *
  * @param string $file
  */
 public function deleteFile($file)
 {
     if (!$this->filesystem->has($file)) {
         return;
     }
     $this->filesystem->delete($file);
     $this->cacheManager->remove($file);
 }
 /**
  * Test related method
  */
 public function testRemoveAFileIfMediaIsRemoved()
 {
     $this->filesystem->expects($this->any())->method('has')->will($this->returnValue(true));
     $this->filesystem->expects($this->once())->method('delete');
     $media = $this->getMediaMock();
     $media->expects($this->any())->method('isRemoved')->will($this->returnValue(true));
     $media->expects($this->any())->method('getFilename')->will($this->returnValue('foo.jpg'));
     $this->manager->handle($media, '');
 }
예제 #12
0
 /**
  * Given a Doctrine mapped File instance, retrieves its data and fill
  * content local variable with it.
  *
  * @param FileInterface $file File to download
  *
  * @return FileInterface File downloaded
  */
 public function downloadFile(FileInterface $file)
 {
     /**
      * @var string $content
      */
     $content = $this->filesystem->read($this->fileIdentifierTransformer->transform($file));
     $file->setContent($content);
     return $file;
 }
예제 #13
0
 /**
  * @return resource
  */
 private function getPrivateKeyResource()
 {
     try {
         $file = $this->filesystem->get($this->fileName);
     } catch (FileNotFound $e) {
         $file = $this->createKeys();
     }
     $key = openssl_pkey_get_private($file->getContent());
     return $key;
 }
예제 #14
0
 public function __construct(Filesystem $filesystem, $bufferSize, $streamWrapperPrefix, $prefix)
 {
     if (!$filesystem->getAdapter() instanceof StreamFactory) {
         throw new \InvalidArgumentException('The filesystem used as chunk storage must implement StreamFactory');
     }
     $this->filesystem = $filesystem;
     $this->bufferSize = $bufferSize;
     $this->prefix = $prefix;
     $this->streamWrapperPrefix = $streamWrapperPrefix;
 }
예제 #15
0
 /**
  * @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);
 }
 function it_saves_content_of_files_from_gaufrette_at_local_disk(Filesystem $filesystem)
 {
     $filesystem->listKeys('test')->willReturn(['keys' => ['test/aaa/z.txt', 'test/aaa/test2.txt', 'test.txt'], 'dirs' => ['test/aaa']]);
     $filesystem->read('test/aaa/z.txt')->willReturn('Some content');
     $filesystem->read('test/aaa/test2.txt')->willReturn('Other text content');
     $files = $this->getFiles('test');
     $files[0]->shouldBeAnInstanceOf('Cocoders\\FileSource\\File');
     $files[0]->shouldHaveContent('Some content');
     $files[1]->shouldBeAnInstanceOf('Cocoders\\FileSource\\File');
     $files[1]->shouldHaveContent('Other text content');
 }
예제 #17
0
 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);
 }
예제 #18
0
 public function testCache()
 {
     $cacheAdapter = new InMemory(array());
     $this->app['finder.cache.adapter'] = $cacheAdapter;
     $cache = new Filesystem($cacheAdapter);
     $this->assertEmpty($cache->keys());
     // exec without cache
     $this->runCommand('rollback', array('sourcePath' => 'src/'));
     $this->assertEmpty($cache->keys());
     // exec with cache
     $this->runCommand('rollback', array('--cache' => true, 'sourcePath' => 'src/'));
     $this->assertNotEmpty($cache->keys());
 }
예제 #19
0
 /**
  * @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);
             }
         }
     }
 }
예제 #20
0
 /**
  * @param $path
  * @return array|\Cocoders\FileSource\File[]
  */
 public function getFiles($path)
 {
     $listedKeys = $this->filesystem->listKeys($path);
     $files = [];
     foreach ($listedKeys['keys'] as $key) {
         $filePath = $this->tmpPath . '/' . $key;
         $this->createBaseDirectory($filePath);
         if ($this->isNotInRootDir($filePath)) {
             file_put_contents($filePath, $this->filesystem->read($key));
             $files[] = new File($filePath);
         }
     }
     return $files;
 }
 /**
  * @param AttachmentOro $attachmentOro
  *
  * @return AttachmentEntity
  */
 public function oroToEntity(AttachmentOro $attachmentOro)
 {
     $emailAttachmentEntity = new AttachmentEntity();
     $emailAttachmentEntity->setFileName($attachmentOro->getFile()->getFilename());
     $emailAttachmentContent = new EmailAttachmentContent();
     $emailAttachmentContent->setContent(base64_encode($this->filesystem->get($attachmentOro->getFile()->getFilename())->getContent()));
     $emailAttachmentContent->setContentTransferEncoding('base64');
     $emailAttachmentContent->setEmailAttachment($emailAttachmentEntity);
     $emailAttachmentEntity->setContent($emailAttachmentContent);
     $emailAttachmentEntity->setContentType($attachmentOro->getFile()->getMimeType());
     $emailAttachmentEntity->setFile($attachmentOro->getFile());
     $emailAttachmentEntity->setFileName($attachmentOro->getFile()->getOriginalFilename());
     return $emailAttachmentEntity;
 }
 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);
 }
 /**
  * {@inheritDoc}
  */
 public function clearCache($maxAge)
 {
     $delTime = time() - (int) $maxAge;
     $num = 0;
     foreach ($this->temporaryFilesystem->keys() as $key) {
         if (!$this->temporaryFilesystem->getAdapter()->isDirectory($key)) {
             if ($delTime > $this->temporaryFilesystem->mtime($key)) {
                 $this->temporaryFilesystem->delete($key);
                 $num++;
             }
         }
     }
     return $num;
 }
 private function doMove(Institution $institution, Media $media, $sizes)
 {
     // point file system to new path
     $this->fileSystem->rename($institution->getId() . '/' . $media->getName(), $media->getName());
     // do resize
     $this->institutionMediaService->resize($media, $sizes);
 }
 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);
 }
 public function testResize()
 {
     $image = $this->getMock('Imagine\\Image\\ImageInterface');
     $image->expects($this->once())->method('thumbnail')->will($this->returnValue($image));
     $image->expects($this->once())->method('get')->will($this->returnValue(file_get_contents(__DIR__ . '/../fixtures/logo.png')));
     $adapter = $this->getMock('Imagine\\Image\\ImagineInterface');
     $adapter->expects($this->any())->method('load')->will($this->returnValue($image));
     $media = $this->getMock('Sonata\\MediaBundle\\Model\\MediaInterface');
     $media->expects($this->exactly(2))->method('getBox')->will($this->returnValue(new Box(535, 132)));
     $filesystem = new Filesystem(new InMemory());
     $in = $filesystem->get('in', true);
     $in->setContent(file_get_contents(__DIR__ . '/../fixtures/logo.png'));
     $out = $filesystem->get('out', true);
     $resizer = new SimpleResizer($adapter, 'outbound');
     $resizer->resize($media, $in, $out, 'bar', array('height' => null, 'width' => 90, 'quality' => 100));
 }
예제 #27
0
 /**
  * Predicate to know if file exists physically
  *
  * @param ProductMediaInterface $media
  *
  * @return bool
  */
 protected function fileExists(ProductMediaInterface $media)
 {
     if (null === $media->getFilename()) {
         return false;
     }
     return $this->filesystem->has($media->getFilename());
 }
예제 #28
0
 /**
  * @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'));
 }
 /**
  * @test
  * @group functional
  */
 public function shouldWrtieToSameFile()
 {
     $FileObjectA = $this->filesystem->createFile('somefile');
     $FileObjectA->setContent('ABC');
     $FileObjectB = $this->filesystem->createFile('somefile');
     $FileObjectB->setContent('DEF');
     $this->assertEquals('DEF', $FileObjectB->getContent());
     $this->filesystem->delete('somefile');
 }
예제 #30
0
 /**
  * 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);
     }
 }