/** * @param bool $human * @return int|string */ public function getSize($human = true) { $humanFilesize = function ($bytes, $decimals = 2) { $sz = 'BKMGTP'; $factor = floor((strlen($bytes) - 1) / 3); return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor]; }; $size = $this->fileInfo->getSize(); if ($human) { $size = $humanFilesize($size); } return $size; }
/** * Create sprite image data holder * @param string $path * @param SplFileInfo $fileInfo */ public function __construct($path, SplFileInfo $fileInfo) { $this->basePath = $path; $this->info = $fileInfo; // Assign fileinfo variables $this->path = $this->info->getPathname(); $this->size = $this->info->getSize(); // Assign image data $info = getimagesize($this->path); if (!is_array($info)) { throw new UnexpectedValueException("The image '{$this->path}' is not a correct image format."); } $this->hash = sha1(file_get_contents($this->getFullPath())); $this->width = (int) $info[0]; $this->height = (int) $info[1]; $this->mime = $info['mime']; $this->type = explode('/', $this->mime)[1]; // Assign css name $ext = $this->info->getExtension(); // Replace path parts with `-` $name = str_replace(['/', '\\', '_'], '-', $this->info->getRelativePathname()); // Remove leading `-` $name = preg_replace('~^-*~', '', $name); // Remove double dashes $name = preg_replace('~-+~', '-', $name); // Remove file extension $name = preg_replace("~\\.{$ext}\$~", '', $name); // Replace dots with - $name = preg_replace('~\\.~', '-', $name); $this->name = $name; }
function it_should_filter_by_size(SplFileInfo $file1, SplFileInfo $file2) { $file1->isFile()->willReturn(true); $file2->isFile()->willReturn(true); $file1->getRealPath()->willReturn($this->tempFile); $file2->getRealPath()->willReturn($this->tempFile); $file1->getSize()->willReturn(8 * 1024); $file2->getSize()->willReturn(16 * 1024); $result = $this->size('>= 4K')->size('<= 10K'); $result->shouldBeAnInstanceOf('GrumPHP\\Collection\\FilesCollection'); $result->count()->shouldBe(1); $files = $result->toArray(); $files[0]->shouldBe($file1); }
/** * addMetadatas * * @param \Symfony\Component\Finder\SplFileInfo $file * @param mixed $object * @param string $scope */ public function addMetadatas($file, $object, $scope) { $user = $this->container->get('security.token_storage')->getToken()->getUser(); $now = new \DateTime('now'); $metadataArray = array('FILESIZE' => $this->getHumanFileSize($file->getSize()), 'AUTHOR' => $user->getLastname() . ' ' . $user->getFirstName(), 'VERSION' => 'v.' . $now->format('YmdHis'), 'DESCRIPTION' => ''); $metadatas = $this->registry->getManager()->getRepository('Erichard\\DmsBundle\\Entity\\DocumentMetadata')->findByScope(array($scope, 'both')); foreach ($metadatas as $metadata) { if (array_key_exists($metadata->getName(), $metadataArray)) { $metadataLnk = $scope == 'document' ? new DocumentMetadataLnk($metadata) : new DocumentNodeMetadataLnk($metadata); $metadataLnk->setValue($metadataArray[$metadata->getName()]); $this->registry->getManager()->persist($metadataLnk); $object->addMetadata($metadataLnk); } } }
/** * Todas las propiedades de un archivo o directorio * * @param SplFileInfo $file Object of SplFileInfo * @param string $path Ruta de la carpeta o archivo * @return array|null Lista de propiedades o null si no es leible */ public function fileInfo($file, $path) { if ($file->isReadable()) { $item = $this->fileDetails; $item["filename"] = $file->getFilename(); $item["filetype"] = $file->getExtension(); $item["lastmodified"] = $file->getMTime(); $item["size"] = $file->getSize(); if ($file->isDir()) { $item["filetype"] = ''; $item["isdir"] = true; $item["urlfolder"] = $path . $item["filename"] . '/'; $item['preview'] = ''; } elseif ($file->isFile()) { $thumb = $this->createThumb($file, $path); if ($thumb) { $item['preview'] = $this->config['url'] . $this->config['source'] . '/_thumbs' . $path . $thumb; } $item['previewfull'] = $this->config['url'] . $this->config['source'] . $path . $item["filename"]; } return $item; } else { return; } }
/** * Checks if a file is empty. * * @param SplFileInfo $file * * @return bool */ private function isFileEmpty(SplFileInfo $file) { return $file->getSize() > 0 ? false : true; }
public function getSize() { return $this->fileInfo->getSize(); }