function it_throws_an_exception_when_the_file_can_not_be_read_on_the_filesystem(FileInterface $file, FilesystemInterface $filesystem)
 {
     $file->getKey()->willReturn('path/to/file.txt');
     $filesystem->has('path/to/file.txt')->willReturn(true);
     $filesystem->readStream('path/to/file.txt')->willReturn(false);
     $this->shouldThrow(new FileTransferException('Unable to fetch the file "path/to/file.txt" from the filesystem.'))->during('fetch', [$file, $filesystem]);
 }
 protected function deleteFile($filePath)
 {
     // If file is already gone somewhere, it is OK for us
     if ($this->filesystem->has($filePath) && !$this->filesystem->delete($filePath)) {
         throw new CommandErrorException('The file cannot be removed: ' . $filePath);
     }
 }
 /**
  * Return string error message if not found to be correct
  *
  * @return string|bool
  */
 public function assertBackups($today)
 {
     $day = $today;
     $sizes = [];
     for ($i = 0; $i < 2; $i++) {
         $expected = 's77_mail_' . $day->format('Y-m-d') . '.tar.gz';
         try {
             $size = $this->filesystem->getSize($expected);
             if ($size === false) {
                 return "Kan bestandsgrootte niet ophalen van '{$expected}'";
             }
             $sizes[] = $size;
             if ($size < 3.4 * 1000 * 1000 * 1000) {
                 $humanSize = $size / 1000 / 1000;
                 return "Email backup lijkt te klein ({$humanSize} MB)";
             }
         } catch (\League\Flysystem\FileNotFoundException $e) {
             return "Kon email backup niet vinden: '{$expected}'";
         }
         $day = $day->sub(new \DateInterval('P1D'));
     }
     if (count(array_unique($sizes)) === 1) {
         return "Laatste twee e-mail backups zijn even groot.";
     }
     return true;
 }
Example #4
0
 private function touchDir(FilesystemInterface $fs, string $entityId, string $collectionUID, string $imageId) : string
 {
     $resultPath = sprintf('%s/%s/%s', $entityId, $collectionUID, $imageId);
     if (!$fs->has($resultPath)) {
         $fs->createDir($resultPath);
     }
     return $resultPath;
 }
 /**
  * {@inheritdoc}
  */
 public function toFile(Workout $workout, string $outputFile) : bool
 {
     $return = $this->filesystem->put($outputFile, $this->toString($workout));
     if ($return !== true) {
         throw new Exception(sprintf('Could not write to %s', $outputFile));
     }
     return true;
 }
 public function testLastBackupsIdentical()
 {
     $meta = $this->getMeta(123123123123.0);
     $this->fs->getSize('s77_mail_2015-04-22.tar.gz')->willReturn($meta);
     $this->fs->getSize('s77_mail_2015-04-21.tar.gz')->willReturn($meta);
     $result = $this->assert->assertBackups(new \DateTime('2015-04-22'));
     $this->assertEquals("Laatste twee e-mail backups zijn even groot.", $result);
 }
Example #7
0
 public function getResource($spiBinaryFileId)
 {
     try {
         return $this->filesystem->readStream($spiBinaryFileId);
     } catch (FlysystemNotFoundException $e) {
         throw new BinaryFileNotFoundException($spiBinaryFileId, $e);
     }
 }
Example #8
0
 /** @inheritdoc */
 public function generate(array $replacements, $autoloader)
 {
     ReflectionEngine::init(new Locator($autoloader));
     foreach ($replacements as $replacement) {
         $fullPath = $this->vendorDir . '/' . $replacement['package'] . '/proxy/' . str_replace('\\', '/', $replacement['originalFullyQualifiedType']) . ".php";
         $this->filesystem->put($fullPath, $this->buildClass($replacement));
     }
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function delete($path)
 {
     try {
         return $this->filesystem->delete($path);
     } catch (FileNotFoundException $exception) {
         return false;
     }
 }
Example #10
0
 /**
  * Delete an item from the storage if it exists.
  *
  * @param string $key
  *
  * @return void
  */
 public function delete($key)
 {
     try {
         $this->flysystem->delete($key);
     } catch (FileNotFoundException $e) {
         //
     }
 }
 /**
  * {@inheritdoc}
  */
 public function find($path)
 {
     if ($this->filesystem->has($path) === false) {
         throw new NotLoadableException(sprintf('Source image "%s" not found.', $path));
     }
     $mimeType = $this->filesystem->getMimetype($path);
     return new Binary($this->filesystem->read($path), $mimeType, $this->extensionGuesser->guess($mimeType));
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public function fromFile($file) : Workout
 {
     $content = $this->filesystem->read($file);
     if ($content === false) {
         throw new UnreadableFileException();
     }
     return $this->fromString($content);
 }
Example #13
0
 protected function adapterType()
 {
     if ($this->filesystem instanceof Filesystem) {
         $reflect = new \ReflectionClass($this->filesystem->getAdapter());
         return $reflect->getShortName();
     }
     return 'Unknown';
 }
Example #14
0
 /**
  * Create response.
  * @param  FilesystemInterface $cache Cache file system.
  * @param  string              $path  Cached file path.
  * @return Response            Response object.
  */
 public function create(FilesystemInterface $cache, $path)
 {
     $stream = $this->streamCallback->__invoke($cache->readStream($path));
     $contentType = $cache->getMimetype($path);
     $contentLength = (string) $cache->getSize($path);
     $cacheControl = 'max-age=31536000, public';
     $expires = date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT';
     return $this->response->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength)->withHeader('Cache-Control', $cacheControl)->withHeader('Expires', $expires);
 }
Example #15
0
 /**
  * Returns a list of Identifier strings
  * Unfortunately the list() method is reserved
  *
  * @return string[]
  */
 public function getList()
 {
     $identifiers = array();
     $fileList = $this->filesystem->listFiles($this->getPath());
     foreach ($fileList as $file) {
         $identifiers[] = $file['filename'];
     }
     return $identifiers;
 }
Example #16
0
 /**
  * @return array
  */
 private function getConnectorsFromFileList()
 {
     $connectors = [];
     $connectorClasses = $this->locator->locate();
     foreach ($connectorClasses as $connectorClass) {
         // @FIXME: Use Dependency Injection Container for connectors that have dependencies
         $connectors[] = new $connectorClass();
     }
     return $connectors;
 }
Example #17
0
 /**
  * @throws FileNotFoundException If fname does not exist in filesystem
  */
 public function __construct(string $fname, FilesystemInterface $fsystem, DecoderInterface $decoder = null)
 {
     if (!$fsystem->has($fname)) {
         throw new FileNotFoundException("Unable to read file {$fname}");
     }
     $this->fname = $fname;
     $this->fsystem = $fsystem;
     $this->decoder = $decoder ?: $this->guessDecoder();
     $this->reset();
 }
 public function getFileHash($path)
 {
     $stream = $this->filesystem->readStream($path);
     if ($stream !== false) {
         $context = hash_init(self::HASH_ALGORITHM);
         hash_update_stream($context, $stream);
         return hash_final($context);
     }
     return false;
 }
Example #19
0
 /**
  * @param ReflectionClass $reflection
  * @param FilesystemInterface $filesystem
  * @param null $name
  */
 public function __construct(ReflectionClass $reflection, FilesystemInterface $filesystem, $name = null)
 {
     $this->reflection = $reflection;
     $this->template = $filesystem->read("/Console/stubs/method.stub");
     $this->name = $name;
     $this->classParameters = $this->getClassParameters();
     $this->uses = $this->getUsages();
     $this->methodParameterNames = $this->getMethodParameters();
     $this->requestParameters = $this->getRequestParameters();
 }
Example #20
0
 /**
  * Set the stream response content.
  * @param  StreamedResponse $response The response object.
  * @return StreamedResponse
  */
 public function setContent(StreamedResponse $response)
 {
     $stream = $this->cache->readStream($this->path);
     $response->setCallback(function () use($stream) {
         rewind($stream);
         fpassthru($stream);
         fclose($stream);
     });
     return $response;
 }
Example #21
0
 /**
  * @param FilesystemInterface $filesystem
  * @param string              $fileName
  *
  * @throws StorageException
  */
 public function __construct(FilesystemInterface $filesystem, $fileName = self::DEFAULT_FILENAME)
 {
     if (!$filesystem->has($fileName)) {
         $filesystem->write($fileName, '');
     }
     $handler = $filesystem->get($fileName);
     if (!$handler->isFile()) {
         throw new StorageException(sprintf('Expected path "%s" to be a file but its a "%s".', $handler->getPath(), $handler->getType()));
     }
     $this->file = $handler;
 }
Example #22
0
 protected function getPaths(FilesystemInterface $filesystem, $skipDirs)
 {
     $paths = [];
     foreach ($filesystem->listContents($this->dir, true) as $path) {
         if ($skipDirs && $path['type'] === 'dir') {
             continue;
         }
         $paths[$path['path']] = $path;
     }
     ksort($paths);
     return $paths;
 }
Example #23
0
 public function handle($term, $extensions = 'jpg,jpeg,gif,png')
 {
     $extensions = explode(",", $extensions);
     $allFiles = $this->filesystem->listContents('', true);
     $files = array();
     foreach ($allFiles as $file) {
         if ($file['type'] == 'file' && ($term == '*' || strpos($file['path'], $term) !== false) && in_array($file['extension'], $extensions)) {
             $files[] = $file['path'];
         }
     }
     return $files;
 }
 /**
  * Create response.
  *
  * @param \League\Flysystem\FilesystemInterface $cache Cache file system.
  * @param string $path Cached file path.
  *
  * @return \Psr\Http\Message\ResponseInterface Response object.
  */
 public function create(FilesystemInterface $cache, $path)
 {
     $stream = new Stream($cache->readStream($path));
     $contentType = $cache->getMimetype($path);
     $contentLength = (string) $cache->getSize($path);
     if ($contentType === false) {
         throw new FilesystemException('Unable to determine the image content type.');
     }
     if ($contentLength === false) {
         throw new FilesystemException('Unable to determine the image content length.');
     }
     return (new Response())->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength);
 }
 /**
  * Should pre-process and transfer an image.
  */
 public function testHandle()
 {
     $filename = '/tmp/' . uniqid();
     file_put_contents($filename, '');
     $this->image->expects($this->atLeastOnce())->method('storageLocation')->willReturn($filename);
     $preProcessed = $this->makeMock(ImagickContract::class);
     $this->imagePreProcessor->expects($this->atLeastOnce())->method('preProcess')->with($this->image)->willReturn(new ImagickCollection([$preProcessed]));
     $this->config->expects($this->atLeastOnce())->method('get')->willReturn('foobar_endpoint');
     $fileContent = 'foo bar';
     $preProcessed->expects($this->atLeastOnce())->method('getImageBlob')->willReturn($fileContent);
     $this->filesystem->expects($this->atLeastOnce())->method('put')->with($this->isType('string'), $fileContent, $this->isType('array'));
     $this->newImageListener->handle($this->event);
 }
 /**
  * @param ServerRequestInterface $request
  *
  * @return mixed|null
  */
 public function data(ServerRequestInterface $request)
 {
     $url = $request->getUri()->getPath();
     $parameters = array_merge($request->getQueryParams(), $request->getParsedBody());
     $file = $this->file($url, $parameters);
     if (!$this->filesystem->has($file)) {
         $file = $this->defaultFile($url);
     }
     if (!$this->filesystem->has($file)) {
         return null;
     }
     return $this->filesystem->read($file);
 }
Example #27
0
 /**
  * Recursively yield files that meet the specification
  *
  * @param SpecificationInterface $specification
  * @param string $path
  * @return Generator
  */
 private function yieldFilesInPath(SpecificationInterface $specification, $path)
 {
     $listContents = $this->filesystem->listContents($path);
     foreach ($listContents as $location) {
         if ($specification->isSatisfiedBy($location)) {
             (yield $location);
         }
         if ($location['type'] == 'dir') {
             foreach ($this->yieldFilesInPath($specification, $location['path']) as $returnedLocation) {
                 (yield $returnedLocation);
             }
         }
     }
 }
Example #28
0
 public function uploadImagePreview(int $themeId, string $path) : string
 {
     $theme = $this->getThemeById($themeId);
     $dir = $theme->getId();
     $name = sprintf('%s.png', GenerateRandomString::gen(self::GENERATE_FILENAME_LENGTH));
     $newPath = sprintf('%s/%s', $dir, $name);
     if ($this->fileSystem->has($dir)) {
         $this->fileSystem->deleteDir($dir);
     }
     $this->fileSystem->write($newPath, file_get_contents($path));
     $theme->setPreview($newPath);
     $this->themeRepository->saveTheme($theme);
     return $theme->getPreview();
 }
 /**
  * {@inheritdoc}
  */
 public function fetch(FileInterface $file, FilesystemInterface $filesystem)
 {
     if (!$filesystem->has($file->getKey())) {
         throw new \LogicException(sprintf('The file "%s" is not present on the filesystem.', $file->getKey()));
     }
     $localPathname = tempnam(sys_get_temp_dir(), 'raw_file_fetcher_');
     if (false === ($stream = $filesystem->readStream($file->getKey()))) {
         throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $file->getKey()));
     }
     if (false === file_put_contents($localPathname, $stream)) {
         throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $file->getKey()));
     }
     return new \SplFileInfo($localPathname);
 }
 /**
  * {@inheritDoc}
  */
 public function load($path)
 {
     try {
         $contents = $this->filesystem->read($path);
     } catch (FileNotFoundException $e) {
         throw new Exception\ImageNotFoundException(sprintf('Source image not found in "%s"', $path));
     }
     $mimeType = $this->filesystem->getMimeType($path);
     if ($mimeType === false) {
         // Mime Type could not be detected
         return $contents;
     }
     return new Binary($contents, $mimeType);
 }