public static function download($url, $directory, $filename = NULL) { $url = str_replace(' ', '%20', $url); if (!Valid::url($url)) { return FALSE; } $curl = curl_init($url); $file = Upload_Util::combine($directory, uniqid()); $handle = fopen($file, 'w'); $headers = new HTTP_Header(); curl_setopt($curl, CURLOPT_FILE, $handle); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($headers, 'parse_header_string')); if (curl_exec($curl) === FALSE or curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) { fclose($handle); unlink($file); throw new Kohana_Exception('Curl: Download Error: :error, status :status on url :url', array(':url' => $url, ':status' => curl_getinfo($curl, CURLINFO_HTTP_CODE), ':error' => curl_error($curl))); } fclose($handle); if ($filename === NULL) { if (!isset($headers['content-disposition']) or !($filename = Upload_Util::filename_from_content_disposition($headers['content-disposition']))) { $mime_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); $url = urldecode(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL)); $filename = Upload_Util::filename_from_url($url, $mime_type); } } $filename = substr(pathinfo($filename, PATHINFO_FILENAME), 0, 60) . '.' . pathinfo($filename, PATHINFO_EXTENSION); $result_file = Upload_Util::combine($directory, $filename); rename($file, $result_file); return is_file($result_file) ? $filename : FALSE; }
public function test_save_and_delete() { $image = Jam::build('test_image'); $image->file = Upload_Util::combine($this->test_local, 'source', 'logo.gif'); $image->save(); $file = $image->file->file(); $this->assertFileExists($file); $image->delete(); $this->assertFileNotExists($file); }
/** * Copy the source data to a directory * * @param string $directory * @return Upload_Source $this */ public function copy_to($directory) { switch ($this->type()) { case Upload_Source::TYPE_URL: $filename = Upload_Util::download($this->data(), $directory, $this->filename()); $this->filename($filename); break; case Upload_Source::TYPE_UPLOAD: try { $filename = Upload_Source::process_type_upload($this->data(), $directory); $this->filename($filename); } catch (Jam_Exception_Upload $e) { $this->error($e->getMessage()); } break; case Upload_Source::TYPE_STREAM: if (!$this->filename()) { $this->filename(uniqid()); } Upload_Util::stream_copy_to_file($this->data(), Upload_Util::combine($directory, $this->filename())); break; case Upload_Source::TYPE_FILE: if (!Upload_Source::valid_file($this->data())) { throw new Kohana_Exception("File must be in the document root, or must be testing environment"); } if (!$this->filename()) { $this->filename(basename($this->data())); } copy($this->data(), Upload_Util::combine($directory, $this->filename())); break; case Upload_Source::TYPE_TEMP: if (!Upload_Temp::valid($this->data())) { throw new Kohana_Exception("This file does not exist"); } $this->filename(basename($this->data())); break; } $this->_copied = TRUE; return $this; }
protected function location($method, $thumbnail = NULL) { if (!$this->filename()) { return NULL; } try { if ($this->_source) { return $this->temp()->{$method}(Upload_Util::combine($this->temp()->directory(), $thumbnail, $this->filename())); } else { return $this->server()->{$method}($this->full_path($thumbnail)); } } catch (Flex\Storage\Exception_Notsupported $exception) { return NULL; } }
public function url($path, $thumbnail = NULL) { return Upload_Util::combine(Upload_Temp::config('web'), $path, $thumbnail); }
public function test_multiple_rules() { // Logo is 127 x 34 $this->value->source(Upload_Util::combine($this->test_local, 'source', 'logo.gif'))->save_to_temp(); Jam::validator_rule('uploaded', array('only' => array('png'), 'maximum_size' => '1B', 'exact_height' => 20))->validate($this->model, 'file', $this->value); $this->assertHasError($this->model, 'file', 'uploaded_extension'); $this->assertHasError($this->model, 'file', 'uploaded_maximum_size'); $this->assertHasError($this->model, 'file', 'uploaded_exact_height'); }