/** * Replace physical file * @param Assets_base_file $old_file * @param Assets_base_file $replace_with */ public function replace_file(Assets_base_file $old_file, Assets_base_file $replace_with) { if ($old_file->kind() == 'image') { // we'll need this if replacing images $local_copy = $replace_with->get_local_copy(); } $this->_delete_source_file($old_file->server_path()); $this->_purge_cached_source_file($old_file->server_path()); $this->_delete_generated_thumbs($old_file->file_id()); $this->_move_source_file($replace_with, $replace_with->folder_row(), $old_file->folder_row(), $old_file->filename()); if ($old_file->kind() == 'image') { $this->post_upload_image_actions($old_file->file_id(), $local_copy); } $data = array('width' => (int) $replace_with->width(), 'height' => (int) $replace_with->height(), 'size' => $replace_with->size(), 'date_modified' => $replace_with->date_modified()); $this->EE->db->update('assets_files', $data, array('file_id' => $old_file->file_id())); }
/** * Move a source file. * * @param Assets_base_file $file * @param $previous_folder_row * @param $folder_row * @param $new_file_name * @param bool $overwrite * @return mixed */ protected function _move_source_file(Assets_base_file $file, $previous_folder_row, $folder_row, $new_file_name = '', $overwrite = FALSE) { if (empty($new_file_name)) { $new_file_name = $file->filename(); } $old_path = $this->get_folder_server_path($previous_folder_row->full_path); $new_path = $this->get_folder_server_path($folder_row->full_path); // can we place the file there? if (!$overwrite && ($this->_source_file_exists($new_path . $new_file_name) or empty($this->cache['merge_in_progress']) && $this->EE->assets_lib->get_file_id_by_folder_id_and_name($folder_row->folder_id, $new_file_name))) { return $this->_prompt_result_array($new_file_name); } $previous_settings = $this->get_source_settings($previous_folder_row->source_id); $new_settings = $this->get_source_settings($folder_row->source_id); $this->_copy_file($this->_prepare_request_uri($previous_settings->container, ltrim($old_path, '/') . $file->filename(), $previous_settings), $this->_prepare_request_uri($new_settings->container, ltrim($new_path, '/') . $new_file_name), $new_settings); $this->_delete_source_file($file->server_path(), $this->get_source_settings($previous_folder_row->source_id)); return array('success' => TRUE, 'file_id' => $file->file_id(), 'new_file_name' => $new_file_name); }
/** * Move a source file * * @param Assets_base_file $file * @param $previous_folder_row * @param $folder_row * @param string $new_file_name * @param bool $overwrite * @throws Exception * @return mixed */ protected function _move_source_file(Assets_base_file $file, $previous_folder_row, $folder_row, $new_file_name = '', $overwrite = FALSE) { if (empty($new_file_name)) { $new_file_name = $file->filename(); } $old_path = $this->get_folder_server_path($previous_folder_row->full_path); $new_path = $this->get_folder_server_path($folder_row->full_path); // can we place the file there? if (!$overwrite && ($this->_source_file_exists($new_path . $new_file_name) or empty($this->cache['merge_in_progress']) && $this->EE->assets_lib->get_file_id_by_folder_id_and_name($folder_row->folder_id, $new_file_name))) { return $this->_prompt_result_array($new_file_name); } $old_bucket_data = $this->get_source_settings($previous_folder_row->source_id); $new_bucket_data = $this->get_source_settings($folder_row->source_id); $old_prefix = !empty($old_bucket_data->subfolder) ? rtrim($old_bucket_data->subfolder, '/') . '/' : ''; $this->_s3_set_creds($new_bucket_data->access_key_id, $new_bucket_data->secret_access_key); if ($this->s3->copyObject($old_bucket_data->bucket, $old_prefix . ltrim($old_path, '/') . $file->filename(), $new_bucket_data->bucket, $this->_get_path_prefix() . ltrim($new_path, '/') . $new_file_name, Assets_S3::ACL_PUBLIC_READ)) { $this->_delete_source_file($file->server_path(), $old_bucket_data); return array('success' => TRUE, 'file_id' => $file->file_id(), 'new_file_name' => $new_file_name); } throw new Exception(lang('invalid_path')); }
/** * @param Assets_base_file $file * @param $previous_folder_row * @param $folder_row * @param string $new_file_name * @param bool $overwrite * @return array|mixed */ protected function _move_source_file(Assets_base_file $file, $previous_folder_row, $folder_row, $new_file_name = '', $overwrite = FALSE) { if (empty($new_file_name)) { $new_file_name = $file->filename(); } $new_server_path = $this->get_folder_server_path($folder_row->full_path, $folder_row) . $new_file_name; if (!$overwrite && (file_exists($new_server_path) or empty($this->cache['merge_in_progress']) && $this->EE->assets_lib->get_file_id_by_folder_id_and_name($folder_row->folder_id, $new_file_name))) { return $this->_prompt_result_array($new_file_name); } if (!$overwrite) { // make sure the filename is clean and unique $this->_prep_filename($new_server_path, $file->server_path()); } // attempt to rename the file if (!@rename($file->server_path(), $new_server_path)) { return array('error' => lang('couldnt_save')); } $new_filename = pathinfo($new_server_path, PATHINFO_BASENAME); $is_image = $file->kind() == 'image'; // moved from top level if ($previous_folder_row->parent_id == 0) { // to a different one - UPDATE if ($folder_row->parent_id == 0) { $filedir = $this->get_filedir($folder_row->filedir_id); $this->EE->db->where('upload_location_id', $previous_folder_row->filedir_id)->where('file_name', $file->filename())->update('files', array('site_id' => $filedir->site_id, 'upload_location_id' => $filedir->id, 'rel_path' => $new_server_path, 'file_name' => $new_filename)); } else { $this->EE->db->where('upload_location_id', $previous_folder_row->filedir_id)->where('file_name', $file->filename())->delete('files'); } } else { $source_server_path = $this->get_filedir($folder_row->filedir_id)->server_path; // to a top level one - INSERT // if we can do this without EE complaining if ($folder_row->parent_id == 0 && substr($source_server_path, 0, 3) != '../' && strpos($source_server_path, SYSDIR) === FALSE) { $this->_store_file_data($new_server_path, $folder_row->filedir_id); } } if ($is_image) { $this->_delete_thumbnails($file->server_path(), $previous_folder_row->filedir_id); $this->_create_thumbnails($new_server_path, $folder_row->filedir_id); } return array('success' => TRUE, 'file_id' => $file->file_id(), 'new_file_name' => $new_filename); }