/** * Prepare before using filters * * @return $this */ protected function prepare() { if (empty($this->filters)) { $this->filters[] = $this->manager->getFilter($this->source->getExtension()); } foreach ($this->filters as $filter) { $filter->prepare($this); } return $this; }
/** * Creator * * @param mixed $data Source (automatic determining) or reader config * @param array $args Constructor arguments * * @throws \InvalidArgumentException * @return $this */ public static function objectify($data, $args = array()) { if (is_string($data)) { $source = new File($data); $extension = $source->getExtension(); switch ($extension) { case 'php': case 'php5': return new Php(); break; } } return Objector::objectify($data, $args, get_called_class()); }
/** * Upload resource * * @param array $data File data * @param array $params Path parameters * @param bool $useTemp Use temporary state * * @throws \InvalidArgumentException * @throws \ErrorException * @return Resource|null */ public function upload($data, $params = array(), $useTemp = true) { if (empty($data)) { throw new \InvalidArgumentException("File data to be uploaded is empty."); } $resource = null; switch ($data['error']) { case UPLOAD_ERR_OK: $source = new File($data['name']); $temp = new File($data['tmp_name']); $params = array_merge(array('filename' => $source->getFileName(), 'extension' => $source->getExtension()), $params); $resource = $this->map(static::UPLOAD, $params); $target = $resource->getFile(); $temp->move($target); break; case UPLOAD_ERR_NO_FILE: if ($useTemp) { $resource = $this->map(static::UPLOAD, $params); } else { throw new \ErrorException("File has not been uploaded."); } break; case UPLOAD_ERR_INI_SIZE: throw new \ErrorException(sprintf("Uploaded file size exceeds server limit: %d MB", Php::get('upload_max_filesize'))); break; case UPLOAD_ERR_FORM_SIZE: throw new \ErrorException("Uploaded file size exceeds form limit."); break; case UPLOAD_ERR_PARTIAL: throw new \ErrorException("Uploaded file is only partially completed."); break; case UPLOAD_ERR_NO_TMP_DIR: throw new \ErrorException("Missing temporary directory for uploaded file."); break; case UPLOAD_ERR_CANT_WRITE: throw new \ErrorException("Failed to write uploaded file to disk."); break; case UPLOAD_ERR_EXTENSION: default: throw new \ErrorException("Unknown upload error."); break; } return $resource; }
/** * Generate hashed filename with same extension * * @param string $source Path * @param Encrypter $encrypter Encrypter * * @return File */ public function hashFile($source, Encrypter $encrypter = null) { if (!$source instanceof File) { $source = new File($source); } if ($encrypter === null) { $encrypter = new Encrypter(); } $hash = $encrypter->crypt($source->getAbsolutePath()); $path = $this->getPath(); $ext = $source->getExtension(); $target = new File($path . '/' . $hash . '.' . $ext); return $target; }