/** * Validate the field against the validation rules. * * @uses Transit\Transit * @uses Transit\File * @uses Transit\Validator\ImageValidator * * @param Model $model * @param array $data * @param string $method * @param array $params * @return bool * @throws UnexpectedValueException */ protected function _validate(Model $model, $data, $method, array $params) { foreach ($data as $field => $value) { if ($this->_allowEmpty($model, $field, $value)) { return true; } else { if ($this->_isEmpty($value)) { return false; } } $file = null; // Upload, use temp file if (is_array($value)) { $file = new File($value); // Import, copy file for validation } else { if (preg_match('/^http/i', $value)) { $target = TMP . md5($value); // Already imported from previous validation if (file_exists($target)) { $file = new File($target); // Attempt to copy } else { $transit = new \Transit\Transit($value); $transit->setDirectory(TMP); if ($transit->importFromRemote()) { $file = $transit->getOriginalFile(); $file->rename(basename($target)); } } // Save temp so we can delete later if ($file) { $this->_tempFile = $file; } } } if (!$file) { $this->log(sprintf('Invalid upload or import for validation: %s', json_encode($value)), LOG_DEBUG); return false; } $validator = new ImageValidator(); $validator->setFile($file); return call_user_func_array(array($validator, $method), $params); } return false; }
/** * Validate the field against the validation rules. * * @param Model $model * @param array $data * @param string $method * @param array $params * @return boolean * @throws UnexpectedValueException */ protected function _validate(Model $model, $data, $method, array $params) { foreach ($data as $field => $value) { if ($this->_allowEmpty($model, $field, $value)) { return true; } else { if (empty($value['tmp_name'])) { return false; } } // Extension is special as the tmp_name uses the .tmp extension if ($method === 'ext') { return in_array($this->_ext($value['name']), $params[0]); // Use robust validator } else { $file = null; // Upload, use temp file if (is_array($value)) { $file = new File($value['tmp_name']); // Import, copy file for validation } else { if (preg_match('/^http/', $value)) { $target = TMP . md5($value) . '.' . $this->_ext($value); // Already imported from previous validation if (file_exists($target)) { $file = new File($target); // Attempt to copy } else { if (copy($value, $target)) { $file = new File($target); // Delete just in case } else { @unlink($target); } } // Save temp so we can delete later if ($file) { $this->_tempFile = $file; } } } if (!$file) { throw new UnexpectedValueException('Invalid upload or import for validation'); } $validator = new ImageValidator(); $validator->setFile($file); return call_user_func_array(array($validator, $method), $params); } } return false; }
/** * Test that validation is triggered during upload. * Validation should fail since the file is jpg, not png. */ public function testValidate() { $validator = new ImageValidator(); $validator->addRule('ext', 'Invalid extension', array('png')); $this->object->setValidator($validator); try { $this->object->upload(); $this->assertTrue(false); } catch (Exception $e) { $this->assertTrue(true); } }
<?php require_once 'include.php'; use Transit\Transit; use Transit\Transformer\Image\CropTransformer; use Transit\Transporter\Aws\S3Transporter; use Transit\Validator\ImageValidator; if ($_FILES) { $validator = new ImageValidator(); $validator->addRule('size', 'File size is too large', 2003000); $transit = new Transit($_FILES['file']); $transit->setDirectory(__DIR__ . '/tmp/')->setValidator($validator)->setTransporter(new S3Transporter('access', 'secret', array('bucket' => '', 'region' => '')))->addTransformer(new CropTransformer(array('width' => 100))); try { if ($transit->upload()) { $transit->getOriginalFile()->rename(function ($name) { return md5($name); }); if ($transit->transform()) { debug($transit->getAllFiles()); debug($transit->transport()); } } } catch (Exception $e) { debug($e->getMessage()); } } ?> <!DOCTYPE html> <head> <title>Transit - Upload</title>