public static function save_images($file_tmps, $parent_id, $parent_id_field, $related_table, $public_flag = null, $type = 'img') { if (!in_array($type, array('img', 'file'))) { throw new InvalidArgumentException('Parameter type is invalid.'); } $moved_files = array(); $related_table_ids = array(); if (!$file_tmps) { return array($moved_files, $related_table_ids); } $model = Site_Model::get_model_name($related_table); $related_table_obj = $model::forge(); if (!($file_cate = $related_table_obj->get_image_prefix())) { throw new \InvalidArgumentException('Parameter $related_table is invalid.'); } $new_filepath_prefix = Site_Upload::get_filepath_prefix($file_cate, $parent_id); $new_filename_prefix = Site_Upload::convert_filepath2filename($new_filepath_prefix); $is_save_storage = conf('upload.storageType') != 'normal'; if (!$is_save_storage) { $new_file_dir = Site_Upload::get_uploaded_path('raw', 'img', true, false, $new_filepath_prefix); if (!Site_Upload::check_and_make_uploaded_dir($new_file_dir, conf('upload.check_and_make_dir_level'), conf('upload.mkdir_mode'))) { throw newFuelException('Failed to make save dirs.'); } } foreach ($file_tmps as $id => $file_tmp) { $old_file_path = Site_Upload::get_uploaded_file_path($file_tmp->name, 'raw', 'img', true); $moved_files[$file_tmp->id] = array('from' => $old_file_path); if (!$is_save_storage) { $new_file_path = $new_file_dir . $file_tmp->name; Util_file::move($old_file_path, $new_file_path); $moved_files[$file_tmp->id]['to'] = $new_file_path; $moved_files[$file_tmp->id]['filepath_prefix'] = $new_filepath_prefix; } if ($type == 'img') { $moved_files[$file_tmp->id]['from_thumbnail'] = Site_Upload::get_uploaded_file_path($file_tmp->name, 'thumbnail', 'img', true); } $file = Model_File::move_from_file_tmp($file_tmp, $new_filename_prefix, $type); $related_table_obj = $model::forge(); $related_table_obj->{$parent_id_field} = $parent_id; $related_table_obj->file_name = $file->name; $related_table_obj->name = $file_tmp->description; $related_table_obj->shot_at = !empty($file->shot_at) ? $file->shot_at : date('Y-m-d H:i:s'); if (!is_null($public_flag)) { $related_table_obj->public_flag = $public_flag; } $related_table_obj->save(); $related_table_ids[] = $related_table_obj->id; } return array($moved_files, $related_table_ids); }
public function get_file_path() { if ($this->is_nofile) { return $this->type == 'img' ? $this->get_noimage_file_path() : false; } $original_file_path = Site_Upload::get_uploaded_file_path($this->filename, 'raw', $this->type, $this->is_tmp); if ($this->type == 'file' || $this->size == 'raw') { return $original_file_path; } //$target_file_path = Site_Upload::get_uploaded_file_path($this->filename, $this->size, $this->type, $this->is_tmp); $target_file_dir = Site_Upload::get_uploaded_path($this->size, $this->type, $this->is_tmp) . $this->filepath_prefix; $target_file_path = $target_file_dir . $this->file_name; if (!file_exists($target_file_path)) { $this->make_image($original_file_path, $target_file_dir, $this->file_name); } return $target_file_path; }
private static function execute_clean_file($file_type, $is_tmp = false) { if (!self::$absolute_execute && !\Site_Util::check_is_dev_env()) { throw new \FuelException('This task is not work at prod env.'); } $raw_file_dir_path = \Site_Upload::get_uploaded_path('raw', $file_type, $is_tmp); $cache_file_dir_path_key = 'upload.types.' . $file_type; if ($is_tmp) { $cache_file_dir_path_key .= '.tmp'; } $cache_file_dir_path_key .= '.root_path.cache_dir'; $cache_file_dir_path = DOCROOT . conf($cache_file_dir_path_key); if (!file_exists($raw_file_dir_path) && ($cache_file_dir_path && !file_exists($cache_file_dir_path))) { return "File directry '" . $raw_file_dir_path . "' not exists."; } $file_paths = \Util_file::get_file_recursive($raw_file_dir_path); if ($cache_file_dir_path) { $file_paths = array_merge($file_paths, \Util_file::get_file_recursive($cache_file_dir_path)); } if (!$file_paths) { return sprintf("No files at '%s'", $raw_file_dir_path); } $i = 0; foreach ($file_paths as $file_path) { if (self::check_file_exists_record($file_path, $is_tmp)) { continue; } \Util_file::remove($file_path); $i++; } $subject = $file_type; if ($is_tmp) { $subject .= '_tmp'; } return sprintf('%d %s removed.', $i, $subject); }