/** * Get / Set the source. Automatically set the source_type * * @param mixed $source * @return mixed */ public function source($source = NULL) { if ($source !== NULL) { $this->_source = Upload_Source::factory($source); if ($this->_source->type() === Upload_Source::TYPE_TEMP) { $this->temp()->directory(dirname($source)); $this->filename(basename($source)); } return $this; } return $this->_source; }
public function set(Jam_Validated $model, $value, $is_changed) { if (!$is_changed) { return $this->upload_file($model)->filename($value); } if ($value instanceof Upload_File) { $upload_file = $value; } else { $upload_file = $model->{$this->name}; if ($value === NULL) { $upload_file->filename(''); } elseif ($value) { if (Upload_Source::valid($value)) { $upload_file->source($value); } $upload_file->filename($value); } } return $upload_file->path($this->path($model)); }
/** * 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; }
/** * @dataProvider data_process_type_upload */ public function test_process_type_upload($upload_error, $expected_exception) { $data = array('name' => 'file1.txt', 'type' => 'text/plain', 'size' => '4', 'tmp_name' => $this->test_temp . '/test_file', 'error' => $upload_error); $this->setExpectedException('Jam_Exception_Upload', $expected_exception); Upload_Source::process_type_upload($data, $this->test_local); }