예제 #1
0
 /**
  * Test that importFromRemote() downloads and makes a copy of a remote file.
  */
 public function testImportFromRemote()
 {
     $transit = new Transit('http://getcomposer.org/img/logo-composer-transparent.png');
     $transit->setDirectory(TEMP_DIR);
     try {
         if ($transit->importFromRemote(false)) {
             $transit->getOriginalFile()->delete();
             $this->assertTrue(true);
         } else {
             $this->assertTrue(false);
         }
     } catch (Exception $e) {
         $this->assertTrue(false, $e->getMessage());
     }
 }
예제 #2
0
 /**
  * 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 (!empty($value)) {
                 $target = TMP . md5($value);
                 $transit = new Transit($value);
                 $transit->setDirectory(TMP);
                 // Already imported from previous validation
                 if (file_exists($target)) {
                     $file = new File($target);
                     // Local file
                 } else {
                     if (file_exists($value)) {
                         $file = new File($value);
                         // Attempt to copy from remote
                     } else {
                         if (preg_match('/^http/i', $value)) {
                             if ($transit->importFromRemote()) {
                                 $file = $transit->getOriginalFile();
                                 $file->rename(basename($target));
                             }
                             // Or from stream
                         } else {
                             if ($transit->importFromStream()) {
                                 $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;
 }