예제 #1
0
파일: Hasher.php 프로젝트: sndsgd/fs
 /**
  * @param string $dir The directory to hash
  * @throws \InvalidArgumentException If the directory is not readable
  */
 public function __construct(string $dir)
 {
     $this->dir = Fs::dir($dir);
     if (!$this->dir->test(Fs::EXISTS | Fs::READABLE)) {
         throw new \InvalidArgumentException(sprintf("failed to hash directory; %s", $this->dir->getError()));
     }
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function validate(&$value, \sndsgd\form\Validator $validator = null) : bool
 {
     $file = \sndsgd\Fs::file($value);
     $extension = strtolower($file->getExtension());
     if (!in_array($extension, $this->extensions)) {
         return false;
     }
     return true;
 }
예제 #3
0
 public function searchDir(string $dir, bool $recursive = false) : LocatorInterface
 {
     foreach ($this->getIterator($dir, $recursive) as $entity) {
         $entity = \sndsgd\Fs::createFromSplFileInfo($entity);
         $path = $entity->getPath();
         if (isset($this->results[$path]) || $this->filter && !call_user_func($this->filter, $entity)) {
             continue;
         }
         $this->results[$path] = $entity;
     }
     return $this;
 }
예제 #4
0
파일: Temp.php 프로젝트: sndsgd/fs
 /**
  * Create a temp file
  *
  * @param string $prefix A prefix for the filename
  * @return \sndsgd\fs\File
  */
 public static function createFile(string $prefix, int $maxAttempts = 10) : entity\FileEntity
 {
     $tmpdir = static::getDir();
     $prefix = \sndsgd\Fs::sanitizeName($prefix);
     $attempts = 1;
     do {
         if ($attempts > $maxAttempts) {
             throw new \RuntimeException("failed to create temp file; " . "reached max number ({$maxAttempts}) of attempts");
         }
         $rand = \sndsgd\Str::random(10);
         $path = "{$tmpdir}/{$prefix}-{$rand}";
         $attempts++;
     } while (file_exists($path));
     touch($path);
     $file = new entity\FileEntity($path);
     static::registerEntity($file);
     return $file;
 }
예제 #5
0
파일: FileEntity.php 프로젝트: sndsgd/fs
 /**
  * Get the filesize as a formatted string
  *
  * @param int $precision The number of decimal places to return
  * @param string $point The decimal point
  * @param string $sep The thousands separator
  * @return string An empty string if the size could not be determined
  */
 public function getSize(int $precision = 0, string $point = ".", string $sep = ",") : string
 {
     $bytes = $this->getByteSize();
     if ($bytes === -1) {
         return "";
     }
     return \sndsgd\Fs::formatSize($bytes, $precision, $point, $sep);
 }