Exemple #1
0
 public function save($model, $value, $loaded)
 {
     $original = $model->get($this->name, FALSE);
     $config = Kohana::config('torn');
     $force_delete = $model->get($this->name . $config->surfix->delete_old, FALSE);
     if ($force_delete or $this->delete_old_file and $original != $value) {
         if (file_exists($original)) {
             try {
                 unlink($original);
             } catch (Exception $e) {
             }
         }
         if ($original == $value) {
             $value = NULL;
         }
     }
     if (is_string($value) and preg_match('/^[a-z0-9]{32}-[a-z0-9]{32}$/i', $value)) {
         if (file_exists(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $value)) {
             $cache = Cache::instance();
             $cached = $cache->get($value);
             $name = $cached['upload']['name'];
             if (is_callable($this->torn_filename_callback)) {
                 $name = call_user_func($this->torn_filename_callback, $name);
             }
             $i = 1;
             $original = $name;
             while (file_exists($this->path . $name)) {
                 $info = pathinfo($original);
                 $name = $info['filename'] . '-' . $i . '.' . $info['extension'];
                 $i++;
             }
             rename(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $value, $this->path . $name);
             $cache->delete($value);
             return $name;
         }
     }
     $tmp_field = $this->name . $config->surfix->temp;
     if (is_string($value) and array_key_exists($tmp_field, $_POST) and empty($value) and empty($_POST[$tmp_field])) {
         return $model->get($this->name, FALSE);
         // don't save
     }
     return parent::save($model, $value, $loaded);
 }